Skip to content

Commit

Permalink
Reuse computed key memoiser (#16159)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Dec 11, 2023
1 parent 4c78c0d commit 93dd407
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
46 changes: 31 additions & 15 deletions packages/babel-helper-create-class-features-plugin/src/misc.ts
Original file line number Diff line number Diff line change
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);
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
Original file line number Diff line number Diff line change
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

0 comments on commit 93dd407

Please sign in to comment.