Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve super.x output #16374

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -9,5 +9,5 @@ class A extends B {
_A = A;
function _foo() {
let _A2;
babelHelpers.get(babelHelpers.getPrototypeOf(_A.prototype), "x", this);
babelHelpers.superPropertyGetCall(_A.prototype, "x", this);
}
286 changes: 211 additions & 75 deletions packages/babel-helper-replace-supers/src/index.ts
Expand Up @@ -7,7 +7,6 @@ import { traverse, template, types as t } from "@babel/core";
import type { NodePath, Scope } from "@babel/traverse";
const {
assignmentExpression,
booleanLiteral,
callExpression,
cloneNode,
identifier,
Expand All @@ -26,37 +25,6 @@ if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
exports.skipAllButComputedKey = ns.skipAllButComputedKey;
}

type ThisRef = {
needAccessFirst?: boolean;
this: t.ThisExpression;
};

/**
* Creates an expression which result is the proto of objectRef.
*
* @example <caption>isStatic === true</caption>
*
* helpers.getPrototypeOf(CLASS)
*
* @example <caption>isStatic === false</caption>
*
* helpers.getPrototypeOf(CLASS.prototype)
*/
function getPrototypeOfExpression(
objectRef: t.Identifier,
isStatic: boolean,
file: File,
isPrivateMethod: boolean,
) {
objectRef = cloneNode(objectRef);
const targetRef =
isStatic || isPrivateMethod
? objectRef
: memberExpression(objectRef, identifier("prototype"));

return callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
}

const visitor = traverse.visitors.merge<
HandlerState<ReplaceState> & ReplaceState
>([
Expand Down Expand Up @@ -116,12 +84,17 @@ interface SpecHandler
| "optionalCall"
| "delete"
> {
_get(
_get?(
this: Handler & SpecHandler,
superMember: SuperMember,
thisRefs: ThisRef,
): t.CallExpression;
_getThisRefs(): ThisRef;
_call?(
this: Handler & SpecHandler,
superMember: SuperMember,
args: t.CallExpression["arguments"],
optional: boolean,
): t.CallExpression | t.OptionalCallExpression;
_getPrototypeOfExpression(this: Handler & SpecHandler): t.CallExpression;
prop(this: Handler & SpecHandler, superMember: SuperMember): t.Expression;
}

Expand Down Expand Up @@ -158,57 +131,222 @@ const specHandlers: SpecHandler = {
return stringLiteral((property as t.Identifier).name);
},

/**
* Creates an expression which result is the proto of objectRef.
*
* @example <caption>isStatic === true</caption>
*
* helpers.getPrototypeOf(CLASS)
*
* @example <caption>isStatic === false</caption>
*
* helpers.getPrototypeOf(CLASS.prototype)
*/
_getPrototypeOfExpression(this: Handler & SpecHandler) {
const objectRef = cloneNode(this.getObjectRef());
const targetRef =
this.isStatic || this.isPrivateMethod
? objectRef
: memberExpression(objectRef, identifier("prototype"));

return callExpression(this.file.addHelper("getPrototypeOf"), [targetRef]);
},

get(this: Handler & SpecHandler, superMember: SuperMember) {
return this._get(superMember, this._getThisRefs());
const objectRef = cloneNode(this.getObjectRef());
return callExpression(this.file.addHelper("superPropertyGetCall"), [
this.isDerivedConstructor
? sequenceExpression([thisExpression(), objectRef])
: objectRef,
this.prop(superMember),
thisExpression(),
...(this.isStatic || this.isPrivateMethod ? [] : [t.numericLiteral(1)]),
]);
},

_call(
this: Handler & SpecHandler,
superMember: SuperMember,
args: t.CallExpression["arguments"],
optional: boolean,
): t.CallExpression | t.OptionalCallExpression {
const objectRef = cloneNode(this.getObjectRef());
let argsNode: t.ArrayExpression | t.Identifier;
if (
args.length === 1 &&
t.isSpreadElement(args[0]) &&
(t.isIdentifier(args[0].argument) ||
t.isArrayExpression(args[0].argument))
) {
argsNode = args[0].argument;
} else {
argsNode = t.arrayExpression(args as t.Expression[]);
}
const buildArgs = [
this.file.addHelper("superPropertyGetCall"),
[
this.isDerivedConstructor
? sequenceExpression([thisExpression(), objectRef])
: objectRef,
this.prop(superMember),
thisExpression(),
t.numericLiteral(this.isStatic || this.isPrivateMethod ? 0 : 1),
argsNode,
],
] as Parameters<typeof t.callExpression>;
if (optional) {
return t.optionalCallExpression(
buildArgs[0] as t.Expression,
buildArgs[1],
true,
);
}
return callExpression(...buildArgs);
},

_get(
set(
this: Handler & SpecHandler,
superMember: SuperMember,
thisRefs: ThisRef,
value: t.Expression,
) {
const proto = getPrototypeOfExpression(
this.getObjectRef(),
this.isStatic,
this.file,
this.isPrivateMethod,
const objectRef = cloneNode(this.getObjectRef());

return callExpression(this.file.addHelper("superPropertySet"), [
this.isDerivedConstructor
? sequenceExpression([thisExpression(), objectRef])
: objectRef,
this.prop(superMember),
value,
thisExpression(),
t.numericLiteral(superMember.isInStrictMode() ? 1 : 0),
...(this.isStatic || this.isPrivateMethod ? [] : [t.numericLiteral(1)]),
]);
},

destructureSet(this: Handler & SpecHandler, superMember: SuperMember) {
throw superMember.buildCodeFrameError(
`Destructuring to a super field is not supported yet.`,
);
},

call(
this: Handler & SpecHandler,
superMember: SuperMember,
args: t.CallExpression["arguments"],
) {
return this._call(superMember, args, false);
},

optionalCall(
this: Handler & SpecHandler,
superMember: SuperMember,
args: t.CallExpression["arguments"],
) {
return this._call(superMember, args, true);
},

delete(this: Handler & SpecHandler, superMember: SuperMember) {
if (superMember.node.computed) {
return sequenceExpression([
callExpression(this.file.addHelper("toPropertyKey"), [
cloneNode(superMember.node.property),
]),
template.expression.ast`
function () { throw new ReferenceError("'delete super[expr]' is invalid"); }()
`,
]);
} else {
return template.expression.ast`
function () { throw new ReferenceError("'delete super.prop' is invalid"); }()
`;
}
},
};

const specHandlers_old: SpecHandler = {
memoise(
this: Handler & SpecHandler,
superMember: SuperMember,
count: number,
) {
const { scope, node } = superMember;
const { computed, property } = node;
if (!computed) {
return;
}

const memo = scope.maybeGenerateMemoised(property);
if (!memo) {
return;
}

this.memoiser.set(property, memo, count);
},

prop(this: Handler & SpecHandler, superMember: SuperMember) {
const { computed, property } = superMember.node;
if (this.memoiser.has(property)) {
return cloneNode(this.memoiser.get(property));
}

if (computed) {
return cloneNode(property);
}

return stringLiteral((property as t.Identifier).name);
},

/**
* Creates an expression which result is the proto of objectRef.
*
* @example <caption>isStatic === true</caption>
*
* helpers.getPrototypeOf(CLASS)
*
* @example <caption>isStatic === false</caption>
*
* helpers.getPrototypeOf(CLASS.prototype)
*/
_getPrototypeOfExpression(this: Handler & SpecHandler) {
const objectRef = cloneNode(this.getObjectRef());
const targetRef =
this.isStatic || this.isPrivateMethod
? objectRef
: memberExpression(objectRef, identifier("prototype"));

return callExpression(this.file.addHelper("getPrototypeOf"), [targetRef]);
},

get(this: Handler & SpecHandler, superMember: SuperMember) {
return this._get(superMember);
},

_get(this: Handler & SpecHandler, superMember: SuperMember) {
const proto = this._getPrototypeOfExpression();
return callExpression(this.file.addHelper("get"), [
thisRefs.needAccessFirst
? sequenceExpression([thisRefs.this, proto])
this.isDerivedConstructor
? sequenceExpression([thisExpression(), proto])
: proto,
this.prop(superMember),
thisRefs.this,
thisExpression(),
]);
},

_getThisRefs(this: Handler & SpecHandler): ThisRef {
return {
needAccessFirst: this.isDerivedConstructor,
this: thisExpression(),
};
},

set(
this: Handler & SpecHandler,
superMember: SuperMember,
value: t.Expression,
) {
const thisRefs = this._getThisRefs();
const proto = getPrototypeOfExpression(
this.getObjectRef(),
this.isStatic,
this.file,
this.isPrivateMethod,
);
const proto = this._getPrototypeOfExpression();

return callExpression(this.file.addHelper("set"), [
thisRefs.needAccessFirst
? sequenceExpression([thisRefs.this, proto])
this.isDerivedConstructor
? sequenceExpression([thisExpression(), proto])
: proto,
this.prop(superMember),
value,
thisRefs.this,
booleanLiteral(superMember.isInStrictMode()),
thisExpression(),
t.booleanLiteral(superMember.isInStrictMode()),
]);
},

Expand All @@ -223,24 +361,17 @@ const specHandlers: SpecHandler = {
superMember: SuperMember,
args: t.CallExpression["arguments"],
) {
const thisRefs = this._getThisRefs();
return optimiseCall(
this._get(superMember, thisRefs),
cloneNode(thisRefs.this),
args,
false,
);
return optimiseCall(this._get(superMember), thisExpression(), args, false);
},

optionalCall(
this: Handler & SpecHandler,
superMember: SuperMember,
args: t.CallExpression["arguments"],
) {
const thisRefs = this._getThisRefs();
return optimiseCall(
this._get(superMember, thisRefs),
cloneNode(thisRefs.this),
this._get(superMember),
cloneNode(thisExpression()),
args,
true,
);
Expand Down Expand Up @@ -424,7 +555,12 @@ export default class ReplaceSupers {
});
}

const handler = this.constantSuper ? looseHandlers : specHandlers;
const handler = this.constantSuper
? looseHandlers
: process.env.BABEL_8_BREAKING ||
this.file.availableHelper("superPropertySet")
? specHandlers
: specHandlers_old;

// todo: this should have been handled by the environmentVisitor,
// consider add visitSelf support for the path.traverse
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-helpers/src/helpers-generated.ts
Expand Up @@ -163,6 +163,16 @@ export default Object.freeze({
"7.23.6",
'export default function setFunctionName(e,t,n){"symbol"==typeof t&&(t=(t=t.description)?"["+t+"]":"");try{Object.defineProperty(e,"name",{configurable:!0,value:n?n+" "+t:t})}catch(e){}return e}',
),
// size: 192, gzip size: 151
superPropertyGetCall: helper(
"7.24.4",
'import get from"get";import getPrototypeOf from"getPrototypeOf";export default function _superPropertyGetCall(t,e,o,r,p){var f=get(getPrototypeOf(r?t.prototype:t),e,o);return p?f.apply(o,p):f}',
),
// size: 171, gzip size: 133
superPropertySet: helper(
"7.24.4",
'import set from"set";import getPrototypeOf from"getPrototypeOf";export default function _superPropertySet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}',
),
// size: 285, gzip size: 210
toPrimitive: helper(
"7.1.5",
Expand Down