| 1 | import type { File } from "@babel/core"; |
| 2 | import environmentVisitor from "@babel/helper-environment-visitor"; |
| 3 | import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions"; |
| 4 | import type { HandlerState } from "@babel/helper-member-expression-to-functions"; |
| 5 | import optimiseCall from "@babel/helper-optimise-call-expression"; |
| 6 | import template from "@babel/template"; |
| 7 | import traverse from "@babel/traverse"; |
| 8 | import type { NodePath, Scope } from "@babel/traverse"; |
| 9 | import { |
| 10 | assignmentExpression, |
| 11 | booleanLiteral, |
| 12 | callExpression, |
| 13 | cloneNode, |
| 14 | identifier, |
| 15 | memberExpression, |
| 16 | sequenceExpression, |
| 17 | stringLiteral, |
| 18 | thisExpression, |
| 19 | } from "@babel/types"; |
| 20 | import type * as t from "@babel/types"; |
| 21 | |
| 22 | // TODO (Babel 8): Don't export this. |
| 23 | export { |
| 24 | default as environmentVisitor, |
| 25 | skipAllButComputedKey, |
| 26 | } from "@babel/helper-environment-visitor"; |
| 27 | |
| 28 | type ThisRef = |
| 29 | | { |
| 30 | memo: t.AssignmentExpression; |
| 31 | this: t.Identifier; |
| 32 | } |
| 33 | | { this: t.ThisExpression }; |
| 34 | /** |
| 35 | * Creates an expression which result is the proto of objectRef. |
| 36 | * |
| 37 | * @example <caption>isStatic === true</caption> |
| 38 | * |
| 39 | * helpers.getPrototypeOf(CLASS) |
| 40 | * |
| 41 | * @example <caption>isStatic === false</caption> |
| 42 | * |
| 43 | * helpers.getPrototypeOf(CLASS.prototype) |
| 44 | */ |
| 45 | function getPrototypeOfExpression( |
| 46 | objectRef: t.Identifier, |
| 47 | isStatic: boolean, |
| 48 | file: File, |
| 49 | isPrivateMethod: boolean, |
| 50 | ) { |
| 51 | objectRef = cloneNode(objectRef); |
| 52 | const targetRef = |
| 53 | isStatic || isPrivateMethod |
| 54 | ? objectRef |
| 55 | : memberExpression(objectRef, identifier("prototype")); |
| 56 | |
| 57 | return callExpression(file.addHelper("getPrototypeOf"), [targetRef]); |
| 58 | } |
| 59 | |
| 60 | const visitor = traverse.visitors.merge< |
| 61 | HandlerState<ReplaceState> & ReplaceState |
| 62 | >([ |
| 63 | environmentVisitor, |
| 64 | { |
| 65 | Super(path, state) { |
| 66 | const { node, parentPath } = path; |
| 67 | if (!parentPath.isMemberExpression({ object: node })) return; |
| 68 | state.handle(parentPath); |
| 69 | }, |
| 70 | }, |
| 71 | ]); |
| 72 | |
| 73 | const unshadowSuperBindingVisitor = traverse.visitors.merge<{ |
| 74 | refName: string; |
| 75 | }>([ |
| 76 | environmentVisitor, |
| 77 | { |
| 78 | Scopable(path, { refName }) { |
| 79 | // https://github.com/Zzzen/babel/pull/1#pullrequestreview-564833183 |
| 80 | const binding = path.scope.getOwnBinding(refName); |
| 81 | if (binding && binding.identifier.name === refName) { |
| 82 | path.scope.rename(refName); |
| 83 | } |
| 84 | }, |
| 85 | }, |
| 86 | ]); |
| 87 | |
| 88 | type SharedState = { |
| 89 | file: File; |
| 90 | scope: Scope; |
| 91 | isDerivedConstructor: boolean; |
| 92 | isStatic: boolean; |
| 93 | isPrivateMethod: boolean; |
| 94 | getObjectRef: () => t.Identifier; |
| 95 | getSuperRef: () => t.Identifier; |
| 96 | // we dont need boundGet here, but memberExpressionToFunctions handler needs it. |
| 97 | boundGet: HandlerState["get"]; |
| 98 | }; |
| 99 | |
| 100 | type Handler = HandlerState<SharedState> & SharedState; |
| 101 | type SuperMember = NodePath< |
| 102 | | t.MemberExpression & { |
| 103 | object: t.Super; |
| 104 | property: Exclude<t.MemberExpression["property"], t.PrivateName>; |
| 105 | } |
| 106 | >; |
| 107 | |
| 108 | interface SpecHandler |
| 109 | extends Pick< |
| 110 | Handler, |
| 111 | | "memoise" |
| 112 | | "get" |
| 113 | | "set" |
| 114 | | "destructureSet" |
| 115 | | "call" |
| 116 | | "optionalCall" |
| 117 | | "delete" |
| 118 | > { |
| 119 | _get( |
| 120 | this: Handler & SpecHandler, |
| 121 | superMember: SuperMember, |
| 122 | thisRefs: ThisRef, |
| 123 | ): t.CallExpression; |
| 124 | _getThisRefs(): ThisRef; |
| 125 | prop(this: Handler & SpecHandler, superMember: SuperMember): t.Expression; |
| 126 | } |
| 127 | |
| 128 | const specHandlers: SpecHandler = { |
| 129 | memoise( |
| 130 | this: Handler & SpecHandler, |
| 131 | superMember: SuperMember, |
| 132 | count: number, |
| 133 | ) { |
| 134 | const { scope, node } = superMember; |
| 135 | const { computed, property } = node; |
| 136 | if (!computed) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | const memo = scope.maybeGenerateMemoised(property); |
| 141 | if (!memo) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | this.memoiser.set(property, memo, count); |
| 146 | }, |
| 147 | |
| 148 | prop(this: Handler & SpecHandler, superMember: SuperMember) { |
| 149 | const { computed, property } = superMember.node; |
| 150 | if (this.memoiser.has(property)) { |
| 151 | return cloneNode(this.memoiser.get(property)); |
| 152 | } |
| 153 | |
| 154 | if (computed) { |
| 155 | return cloneNode(property); |
| 156 | } |
| 157 | |
| 158 | return stringLiteral((property as t.Identifier).name); |
| 159 | }, |
| 160 | |
| 161 | get(this: Handler & SpecHandler, superMember: SuperMember) { |
| 162 | return this._get(superMember, this._getThisRefs()); |
| 163 | }, |
| 164 | |
| 165 | _get( |
| 166 | this: Handler & SpecHandler, |
| 167 | superMember: SuperMember, |
| 168 | thisRefs: ThisRef, |
| 169 | ) { |
| 170 | const proto = getPrototypeOfExpression( |
| 171 | this.getObjectRef(), |
| 172 | this.isStatic, |
| 173 | this.file, |
| 174 | this.isPrivateMethod, |
| 175 | ); |
| 176 | return callExpression(this.file.addHelper("get"), [ |
| 177 | // @ts-expect-error memo does not exist when this.isDerivedConstructor is false |
| 178 | thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, |
| 179 | this.prop(superMember), |
| 180 | thisRefs.this, |
| 181 | ]); |
| 182 | }, |
| 183 | |
| 184 | _getThisRefs(this: Handler & SpecHandler): ThisRef { |
| 185 | if (!this.isDerivedConstructor) { |
| 186 | return { this: thisExpression() }; |
| 187 | } |
| 188 | const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper"); |
| 189 | return { |
| 190 | memo: assignmentExpression("=", thisRef, thisExpression()), |
| 191 | this: cloneNode(thisRef), |
| 192 | }; |
| 193 | }, |
| 194 | |
| 195 | set( |
| 196 | this: Handler & SpecHandler, |
| 197 | superMember: SuperMember, |
| 198 | value: t.Expression, |
| 199 | ) { |
| 200 | const thisRefs = this._getThisRefs(); |
| 201 | const proto = getPrototypeOfExpression( |
| 202 | this.getObjectRef(), |
| 203 | this.isStatic, |
| 204 | this.file, |
| 205 | this.isPrivateMethod, |
| 206 | ); |
| 207 | return callExpression(this.file.addHelper("set"), [ |
| 208 | // @ts-expect-error memo does not exist when this.isDerivedConstructor is false |
| 209 | thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, |
| 210 | this.prop(superMember), |
| 211 | value, |
| 212 | thisRefs.this, |
| 213 | booleanLiteral(superMember.isInStrictMode()), |
| 214 | ]); |
| 215 | }, |
| 216 | |
| 217 | destructureSet(this: Handler & SpecHandler, superMember: SuperMember) { |
| 218 | throw superMember.buildCodeFrameError( |
| 219 | `Destructuring to a super field is not supported yet.`, |
| 220 | ); |
| 221 | }, |
| 222 | |
| 223 | call( |
| 224 | this: Handler & SpecHandler, |
| 225 | superMember: SuperMember, |
| 226 | args: t.CallExpression["arguments"], |
| 227 | ) { |
| 228 | const thisRefs = this._getThisRefs(); |
| 229 | return optimiseCall( |
| 230 | this._get(superMember, thisRefs), |
| 231 | cloneNode(thisRefs.this), |
| 232 | args, |
| 233 | false, |
| 234 | ); |
| 235 | }, |
| 236 | |
| 237 | optionalCall( |
| 238 | this: Handler & SpecHandler, |
| 239 | superMember: SuperMember, |
| 240 | args: t.CallExpression["arguments"], |
| 241 | ) { |
| 242 | const thisRefs = this._getThisRefs(); |
| 243 | return optimiseCall( |
| 244 | this._get(superMember, thisRefs), |
| 245 | cloneNode(thisRefs.this), |
| 246 | args, |
| 247 | true, |
| 248 | ); |
| 249 | }, |
| 250 | |
| 251 | delete(this: Handler & SpecHandler, superMember: SuperMember) { |
| 252 | if (superMember.node.computed) { |
| 253 | return sequenceExpression([ |
| 254 | callExpression(this.file.addHelper("toPropertyKey"), [ |
| 255 | cloneNode(superMember.node.property), |
| 256 | ]), |
| 257 | template.expression.ast` |
| 258 | function () { throw new ReferenceError("'delete super[expr]' is invalid"); }() |
| 259 | `, |
| 260 | ]); |
| 261 | } else { |
| 262 | return template.expression.ast` |
| 263 | function () { throw new ReferenceError("'delete super.prop' is invalid"); }() |
| 264 | `; |
| 265 | } |
| 266 | }, |
| 267 | }; |
| 268 | |
| 269 | const looseHandlers = { |
| 270 | ...specHandlers, |
| 271 | |
| 272 | prop(this: Handler & typeof specHandlers, superMember: SuperMember) { |
| 273 | const { property } = superMember.node; |
| 274 | if (this.memoiser.has(property)) { |
| 275 | return cloneNode(this.memoiser.get(property)); |
| 276 | } |
| 277 | |
| 278 | return cloneNode(property); |
| 279 | }, |
| 280 | |
| 281 | get(this: Handler & typeof specHandlers, superMember: SuperMember) { |
| 282 | const { isStatic, getSuperRef } = this; |
| 283 | const { computed } = superMember.node; |
| 284 | const prop = this.prop(superMember); |
| 285 | |
| 286 | let object; |
| 287 | if (isStatic) { |
| 288 | object = |
| 289 | getSuperRef() ?? |
| 290 | memberExpression(identifier("Function"), identifier("prototype")); |
| 291 | } else { |
| 292 | object = memberExpression( |
| 293 | getSuperRef() ?? identifier("Object"), |
| 294 | identifier("prototype"), |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | return memberExpression(object, prop, computed); |
| 299 | }, |
| 300 | |
| 301 | set( |
| 302 | this: Handler & typeof specHandlers, |
| 303 | superMember: SuperMember, |
| 304 | value: t.Expression, |
| 305 | ) { |
| 306 | const { computed } = superMember.node; |
| 307 | const prop = this.prop(superMember); |
| 308 | |
| 309 | return assignmentExpression( |
| 310 | "=", |
| 311 | memberExpression(thisExpression(), prop, computed), |
| 312 | value, |
| 313 | ); |
| 314 | }, |
| 315 | |
| 316 | destructureSet( |
| 317 | this: Handler & typeof specHandlers, |
| 318 | superMember: SuperMember, |
| 319 | ) { |
| 320 | const { computed } = superMember.node; |
| 321 | const prop = this.prop(superMember); |
| 322 | |
| 323 | return memberExpression(thisExpression(), prop, computed); |
| 324 | }, |
| 325 | |
| 326 | call( |
| 327 | this: Handler & typeof specHandlers, |
| 328 | superMember: SuperMember, |
| 329 | args: t.CallExpression["arguments"], |
| 330 | ) { |
| 331 | return optimiseCall(this.get(superMember), thisExpression(), args, false); |
| 332 | }, |
| 333 | |
| 334 | optionalCall( |
| 335 | this: Handler & typeof specHandlers, |
| 336 | superMember: SuperMember, |
| 337 | args: t.CallExpression["arguments"], |
| 338 | ) { |
| 339 | return optimiseCall(this.get(superMember), thisExpression(), args, true); |
| 340 | }, |
| 341 | }; |
| 342 | |
| 343 | type ReplaceSupersOptionsBase = { |
| 344 | methodPath: NodePath< |
| 345 | | t.ClassMethod |
| 346 | | t.ClassProperty |
| 347 | | t.ObjectMethod |
| 348 | | t.ClassPrivateMethod |
| 349 | | t.ClassPrivateProperty |
| 350 | | t.StaticBlock |
| 351 | >; |
| 352 | constantSuper?: boolean; |
| 353 | file: File; |
| 354 | // objectRef might have been shadowed in child scopes, |
| 355 | // in that case, we need to rename related variables. |
| 356 | refToPreserve?: t.Identifier; |
| 357 | }; |
| 358 | |
| 359 | type ReplaceSupersOptions = ReplaceSupersOptionsBase & |
| 360 | ( |
| 361 | | { objectRef?: undefined; getObjectRef: () => t.Node } |
| 362 | | { objectRef: t.Node; getObjectRef?: undefined } |
| 363 | ) & |
| 364 | ( |
| 365 | | { superRef?: undefined; getSuperRef: () => t.Node } |
| 366 | | { superRef: t.Node; getSuperRef?: undefined } |
| 367 | ); |
| 368 | |
| 369 | interface ReplaceState { |
| 370 | file: File; |
| 371 | scope: Scope; |
| 372 | isDerivedConstructor: boolean; |
| 373 | isStatic: boolean; |
| 374 | isPrivateMethod: boolean; |
| 375 | getObjectRef: ReplaceSupers["getObjectRef"]; |
| 376 | getSuperRef: ReplaceSupers["getSuperRef"]; |
| 377 | } |
| 378 | |
| 379 | export default class ReplaceSupers { |
| 380 | constructor(opts: ReplaceSupersOptions) { |
| 381 | const path = opts.methodPath; |
| 382 | |
| 383 | this.methodPath = path; |
| 384 | this.isDerivedConstructor = |
| 385 | path.isClassMethod({ kind: "constructor" }) && !!opts.superRef; |
| 386 | this.isStatic = |
| 387 | path.isObjectMethod() || |
| 388 | // @ts-expect-error static is not in ClassPrivateMethod |
| 389 | path.node.static || |
| 390 | path.isStaticBlock?.(); |
| 391 | this.isPrivateMethod = path.isPrivate() && path.isMethod(); |
| 392 | |
| 393 | this.file = opts.file; |
| 394 | this.constantSuper = process.env.BABEL_8_BREAKING |
| 395 | ? opts.constantSuper |
| 396 | : // Fallback to isLoose for backward compatibility |
| 397 | opts.constantSuper ?? (opts as any).isLoose; |
| 398 | this.opts = opts; |
| 399 | } |
| 400 | |
| 401 | declare file: File; |
| 402 | declare isDerivedConstructor: boolean; |
| 403 | declare constantSuper: boolean; |
| 404 | declare isPrivateMethod: boolean; |
| 405 | declare isStatic: boolean; |
| 406 | declare methodPath: NodePath; |
| 407 | declare opts: ReplaceSupersOptions; |
| 408 | |
| 409 | getObjectRef() { |
| 410 | return cloneNode(this.opts.objectRef || this.opts.getObjectRef()); |
| 411 | } |
| 412 | |
| 413 | getSuperRef() { |
| 414 | if (this.opts.superRef) return cloneNode(this.opts.superRef); |
| 415 | if (this.opts.getSuperRef) { |
| 416 | return cloneNode(this.opts.getSuperRef()); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | replace() { |
| 421 | // https://github.com/babel/babel/issues/11994 |
| 422 | if (this.opts.refToPreserve) { |
| 423 | this.methodPath.traverse(unshadowSuperBindingVisitor, { |
| 424 | refName: this.opts.refToPreserve.name, |
| 425 | }); |
| 426 | } |
| 427 | |
| 428 | const handler = this.constantSuper ? looseHandlers : specHandlers; |
| 429 | |
| 430 | memberExpressionToFunctions<ReplaceState>(this.methodPath, visitor, { |
| 431 | file: this.file, |
| 432 | scope: this.methodPath.scope, |
| 433 | isDerivedConstructor: this.isDerivedConstructor, |
| 434 | isStatic: this.isStatic, |
| 435 | isPrivateMethod: this.isPrivateMethod, |
| 436 | getObjectRef: this.getObjectRef.bind(this), |
| 437 | getSuperRef: this.getSuperRef.bind(this), |
| 438 | // we dont need boundGet here, but memberExpressionToFunctions handler needs it. |
| 439 | boundGet: handler.get, |
| 440 | ...handler, |
| 441 | }); |
| 442 | } |
| 443 | } |