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

Reuse computed key memoiser #16159

Merged
merged 1 commit into from Dec 11, 2023
Merged
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
46 changes: 31 additions & 15 deletions packages/babel-helper-create-class-features-plugin/src/misc.ts
Expand Up @@ -142,21 +142,37 @@ export function extractComputedKeys(
// Make sure computed property names are only evaluated once (upon class definition)
// and in the right order in combination with static properties
if (!computedKey.isConstantExpression()) {
const ident = path.scope.generateUidIdentifierBasedOnNode(
computedNode.key,
);
// Declaring in the same block scope
// Ref: https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92
path.scope.push({
id: ident,
kind: "let",
});
declarations.push(
t.expressionStatement(
t.assignmentExpression("=", t.cloneNode(ident), computedNode.key),
),
);
computedNode.key = t.cloneNode(ident);
const scope = path.scope;
const isUidReference =
t.isIdentifier(computedKey.node) && scope.hasUid(computedKey.node.name);
const isMemoiseAssignment =
computedKey.isAssignmentExpression({ operator: "=" }) &&
t.isIdentifier(computedKey.node.left) &&
scope.hasUid(computedKey.node.left.name);
Comment on lines +146 to +151
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't understand why hasUid is used, can you explain it? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The hasUid checks whether the identifier is generated from scope.generateUidIdentifier, if it is, it is very likely generated by official Babel plugins, intended to be a memoiser. If not, it is from user code and we should handle it more conservatively.

if (isUidReference) {
continue;
} else if (isMemoiseAssignment) {
declarations.push(t.expressionStatement(t.cloneNode(computedNode.key)));
computedNode.key = t.cloneNode(
(computedNode.key as t.AssignmentExpression).left as t.Identifier,
);
} else {
const ident = path.scope.generateUidIdentifierBasedOnNode(
computedNode.key,
);
// Declaring in the same block scope
// Ref: https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92
scope.push({
id: ident,
kind: "let",
});
declarations.push(
t.expressionStatement(
t.assignmentExpression("=", t.cloneNode(ident), computedNode.key),
),
);
computedNode.key = t.cloneNode(ident);
}
}
}

Expand Down
Expand Up @@ -12,13 +12,12 @@ let Outer = /*#__PURE__*/function (_Hello) {
babelHelpers.inherits(Outer, _Hello);
var _super = babelHelpers.createSuper(Outer);
function Outer() {
let _this2;
var _this;
babelHelpers.classCallCheck(this, Outer);
_this2 = _this = _super.call(this);
_this = _super.call(this);
let Inner = /*#__PURE__*/babelHelpers.createClass(function Inner() {
babelHelpers.classCallCheck(this, Inner);
babelHelpers.defineProperty(this, _this2, "hello");
babelHelpers.defineProperty(this, _this, "hello");
});
return babelHelpers.possibleConstructorReturn(_this, new Inner());
}
Expand Down