Skip to content

Commit

Permalink
Fix decorator memoiser binding kind (#16331)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 12, 2024
1 parent 5ca9b9a commit d386707
Show file tree
Hide file tree
Showing 535 changed files with 1,040 additions and 790 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,28 @@ function replaceClassWithVar(
id: t.Identifier;
path: NodePath<t.ClassDeclaration | t.ClassExpression>;
} {
const id = path.node.id;
const scope = path.scope;
if (path.type === "ClassDeclaration") {
const id = path.node.id;
const className = id.name;
const varId = path.scope.generateUidIdentifierBasedOnNode(id);
const varId = scope.generateUidIdentifierBasedOnNode(id);
const classId = t.identifier(className);

path.scope.rename(className, varId.name);
scope.rename(className, varId.name);

path.get("id").replaceWith(classId);

return { id: t.cloneNode(varId), path };
} else {
let varId: t.Identifier;

if (path.node.id) {
className = path.node.id.name;
varId = path.scope.parent.generateDeclaredUidIdentifier(className);
path.scope.rename(className, varId.name);
if (id) {
className = id.name;
varId = generateLetUidIdentifier(scope.parent, className);
scope.rename(className, varId.name);
} else {
varId = path.scope.parent.generateDeclaredUidIdentifier(
varId = generateLetUidIdentifier(
scope.parent,
typeof className === "string" ? className : "decorated_class",
);
}
Expand Down Expand Up @@ -1005,7 +1007,7 @@ function transformClass(
hint: string,
assignments: t.AssignmentExpression[],
) => {
const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint);
const localEvaluatedId = generateLetUidIdentifier(scopeParent, hint);
assignments.push(t.assignmentExpression("=", localEvaluatedId, expression));
return t.cloneNode(localEvaluatedId);
};
Expand Down Expand Up @@ -1048,11 +1050,15 @@ function transformClass(
/* fallthrough */
default:
if (element.node.static) {
staticInitLocal ??=
scopeParent.generateDeclaredUidIdentifier("initStatic");
staticInitLocal ??= generateLetUidIdentifier(
scopeParent,
"initStatic",
);
} else {
protoInitLocal ??=
scopeParent.generateDeclaredUidIdentifier("initProto");
protoInitLocal ??= generateLetUidIdentifier(
scopeParent,
"initProto",
);
}
break;
}
Expand Down Expand Up @@ -1142,8 +1148,7 @@ function transformClass(
} else if (scopeParent.isStatic(expression.object)) {
object = t.cloneNode(expression.object);
} else {
decoratorReceiverId ??=
scopeParent.generateDeclaredUidIdentifier("obj");
decoratorReceiverId ??= generateLetUidIdentifier(scopeParent, "obj");
object = t.assignmentExpression(
"=",
t.cloneNode(decoratorReceiverId),
Expand All @@ -1170,7 +1175,7 @@ function transformClass(
let classDecorations: t.Expression[] = [];
let classDecorationsId: t.Identifier;
if (classDecorators) {
classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass");
classInitLocal = generateLetUidIdentifier(scopeParent, "initClass");
needsDeclaraionForClassBinding = path.isClassDeclaration();
({ id: classIdLocal, path } = replaceClassWithVar(path, className));

Expand Down Expand Up @@ -1346,8 +1351,10 @@ function transformClass(
}

const newId = generateClassPrivateUid();
const newFieldInitId =
element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`);
const newFieldInitId = generateLetUidIdentifier(
scopeParent,
`init_${name}`,
);
const newValue = t.callExpression(
t.cloneNode(newFieldInitId),
params,
Expand All @@ -1359,12 +1366,8 @@ function transformClass(
if (isPrivate) {
privateMethods = extractProxyAccessorsFor(newId, version);

const getId = newPath.scope.parent.generateDeclaredUidIdentifier(
`get_${name}`,
);
const setId = newPath.scope.parent.generateDeclaredUidIdentifier(
`set_${name}`,
);
const getId = generateLetUidIdentifier(scopeParent, `get_${name}`);
const setId = generateLetUidIdentifier(scopeParent, `set_${name}`);

addCallAccessorsFor(version, newPath, key, getId, setId, isStatic);

Expand All @@ -1385,9 +1388,7 @@ function transformClass(
locals = [newFieldInitId];
}
} else if (kind === FIELD) {
const initId = element.scope.parent.generateDeclaredUidIdentifier(
`init_${name}`,
);
const initId = generateLetUidIdentifier(scopeParent, `init_${name}`);
const valuePath = (
element as NodePath<t.ClassProperty | t.ClassPrivateProperty>
).get("value");
Expand All @@ -1406,9 +1407,7 @@ function transformClass(
privateMethods = extractProxyAccessorsFor(key, version);
}
} else if (isPrivate) {
const callId = element.scope.parent.generateDeclaredUidIdentifier(
`call_${name}`,
);
const callId = generateLetUidIdentifier(scopeParent, `call_${name}`);
locals = [callId];

const replaceSupers = new ReplaceSupers({
Expand Down Expand Up @@ -1508,7 +1507,8 @@ function transformClass(

if (hasDecorators && version === "2023-11") {
if (kind === FIELD || kind === ACCESSOR) {
const initExtraId = scopeParent.generateDeclaredUidIdentifier(
const initExtraId = generateLetUidIdentifier(
scopeParent,
`init_extra_${name}`,
);
locals.push(initExtraId);
Expand Down Expand Up @@ -2140,6 +2140,12 @@ function isDecoratedAnonymousClassExpression(path: NodePath) {
);
}

function generateLetUidIdentifier(scope: Scope, name: string) {
const id = scope.generateUidIdentifier(name);
scope.push({ id, kind: "let" });
return t.cloneNode(id);
}

export default function (
{ assertVersion, assumption }: PluginAPI,
{ loose }: Options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _computedKey;
var _initStatic, _init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _init_computedKey7, _Foo;
var _Foo;
let _initStatic, _init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _computedKey, _init_computedKey7;
const logs = [];
const dec = (value, context) => {
logs.push(context.name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
var _Foo;
let _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b;
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _Foo_brand = /*#__PURE__*/new WeakSet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initProto, _init_a, _init_b, _init_computedKey, _Foo;
var _Foo;
let _initProto, _init_a, _init_b, _init_computedKey;
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _B = /*#__PURE__*/new WeakMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initStatic, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
var _Foo;
let _initStatic, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b;
const dec = () => {};
class Foo {}
_Foo = Foo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initStatic, _init_a, _init_b, _init_computedKey, _Foo;
var _Foo;
let _initStatic, _init_a, _init_b, _init_computedKey;
const dec = () => {};
class Foo {
static get a() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
let _computedKey;
var _initStatic, _init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _init_computedKey7;
let _initStatic, _init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _computedKey, _init_computedKey7;
const logs = [];
const dec = (value, context) => {
logs.push(context.name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b;
let _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b;
const dec = () => {};
class Foo {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initProto, _init_a, _init_b, _init_computedKey;
let _initProto, _init_a, _init_b, _init_computedKey;
const dec = () => {};
class Foo {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initStatic, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b;
let _initStatic, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b;
const dec = () => {};
class Foo {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initStatic, _init_a, _init_b, _init_computedKey;
let _initStatic, _init_a, _init_b, _init_computedKey;
const dec = () => {};
class Foo {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass;
let _initClass;
const dec = () => {};
let _Foo;
class Foo extends Bar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initProto, _call_x;
let _initProto, _call_x;
const dec = () => {};
class Foo extends Bar {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initProto, _call_x;
let _initProto, _call_x;
const dec = () => {};
class Foo extends Bar {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _A2, _C2, _D2, _ref, _G2, _ref2, _H2, _K2;
var _initClass, _A, _Class, _A3, _initClass2, _C, _Class2, _C3, _initClass3, _D, _Class3, _D3, _initClass4, _decorated_class, _Class4, _Class5, _initClass5, _G, _Class6, _G3, _initClass6, _decorated_class2, _Class7, _Class8, _initClass7, _H, _Class9, _H3, _initClass8, _K, _Class10, _K3;
var _Class, _A3, _Class2, _C3, _Class3, _D3, _Class4, _Class5, _Class6, _G3, _Class7, _Class8, _Class9, _H3, _Class10, _K3;
let _initClass, _A, _A2, _initClass2, _C, _C2, _initClass3, _D, _D2, _initClass4, _decorated_class, _ref, _initClass5, _G, _G2, _initClass6, _decorated_class2, _ref2, _initClass7, _H, _H2, _initClass8, _K, _K2;
const dec = () => {};
const A = (new (_A2 = (_A3 = class A {}, [_A, _initClass] = babelHelpers.applyDecs(_A3, [], [dec]), _A3), (_Class = class extends babelHelpers.identity {
constructor() {
Expand Down Expand Up @@ -41,8 +41,8 @@ const J = (new (_K2 = (_K3 = class K extends L {}, [_K, _initClass8] = babelHelp
}
}, babelHelpers.defineProperty(_Class10, _K2, void 0), _Class10))(), _K);
function classFactory() {
let _ref3;
var _initClass9, _decorated_class3, _Class11, _Class12;
var _Class11, _Class12;
let _initClass9, _decorated_class3, _ref3;
return new (_ref3 = (_Class12 = class _ref3 {}, [_decorated_class3, _initClass9] = babelHelpers.applyDecs(_Class12, [], [dec]), _Class12), (_Class11 = class extends babelHelpers.identity {
constructor() {
super(_decorated_class3), (() => {})(), _initClass9();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initClass, _A, _A2, _initClass2, _C, _C2, _initClass3, _D, _D2, _initClass4, _decorated_class, _Class, _initClass5, _G, _G2, _initClass6, _decorated_class2, _Class2, _initClass7, _H, _H2, _initClass8, _K, _K2;
var _A2, _C2, _D2, _Class, _G2, _Class2, _H2, _K2;
let _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K;
const dec = () => {};
const A = ((_A2 = class A {}, [_A, _initClass] = babelHelpers.applyDecs(_A2, [], [dec]), _initClass()), _A);
const B = ((_C2 = class C {}, [_C, _initClass2] = babelHelpers.applyDecs(_C2, [], [dec]), _initClass2()), _C);
Expand All @@ -8,6 +9,7 @@ const F = [((_G2 = class G {}, [_G, _initClass5] = babelHelpers.applyDecs(_G2, [
const H = ((_H2 = class H extends I {}, [_H, _initClass7] = babelHelpers.applyDecs(_H2, [], [dec]), _initClass7()), _H);
const J = ((_K2 = class K extends L {}, [_K, _initClass8] = babelHelpers.applyDecs(_K2, [], [dec]), _initClass8()), _K);
function classFactory() {
var _initClass9, _decorated_class3, _Class3;
var _Class3;
let _initClass9, _decorated_class3;
return (_Class3 = class {}, [_decorated_class3, _initClass9] = babelHelpers.applyDecs(_Class3, [], [dec]), _initClass9()), _decorated_class3;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initClass, _Bar2, _initClass2, _Foo2;
var _Bar2, _Foo2;
let _initClass, _initClass2;
const dec1 = () => {};
const dec2 = () => {};
let _Bar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _Foo2, _Bar2;
var _initClass, _Class, _Foo3, _initClass2, _Class2, _Bar3;
var _Class, _Foo3, _Class2, _Bar3;
let _initClass, _Foo2, _initClass2, _Bar2;
const dec = () => {};
let _Foo;
new (_Foo2 = (_Foo3 = class Foo {}, [_Foo, _initClass] = babelHelpers.applyDecs(_Foo3, [], [dec]), _Foo3), (_Class = class extends babelHelpers.identity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _Foo2;
var _initClass, _Class, _x, _A, _Class_brand, _B, _Foo3;
var _Class, _x, _A, _Class_brand, _B, _Foo3;
let _initClass, _Foo2;
const dec = () => {};
let hasX, hasA, hasM;
let _Foo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _Foo2;
var _initClass, _Class, _Foo3;
var _Class, _Foo3;
let _initClass, _Foo2;
const dec = () => {};
let _Foo;
new (_Foo2 = (_Foo3 = class Foo {}, [_Foo, _initClass] = babelHelpers.applyDecs(_Foo3, [], [dec]), _Foo3), (_Class = class extends babelHelpers.identity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _initClass, _Bar, _Bar2;
var _Bar2;
let _initClass, _Bar;
const dec = () => {};
const Foo = ((_Bar2 = class Bar {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _Foo2;
var _initClass, _Class, _Foo3;
var _Class, _Foo3;
let _initClass, _Foo2;
const dec = () => {};
let _Foo;
new (_Foo2 = (_Foo3 = class Foo {}, [_Foo, _initClass] = babelHelpers.applyDecs(_Foo3, [], [dec]), _Foo3), (_Class = class extends babelHelpers.identity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K;
let _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K;
const dec = () => {};
const A = (new class extends babelHelpers.identity {
static [class A {
Expand Down Expand Up @@ -80,7 +80,7 @@ const J = (new class extends babelHelpers.identity {
}
}(), _K);
function classFactory() {
var _initClass9, _decorated_class3;
let _initClass9, _decorated_class3;
return new class extends babelHelpers.identity {
static [class {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K;
let _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K;
const dec = () => {};
const A = (class A {
static {
Expand Down Expand Up @@ -64,7 +64,7 @@ const J = (class K extends L {
}
}, _K);
function classFactory() {
var _initClass9, _decorated_class3;
let _initClass9, _decorated_class3;
return class {
static {
[_decorated_class3, _initClass9] = babelHelpers.applyDecs(this, [], [dec]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass, _classDecs, _initClass2, _classDecs2;
let _initClass, _classDecs, _initClass2, _classDecs2;
const dec = () => {};
_classDecs = [dec1];
let _Bar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass, _initClass2;
let _initClass, _initClass2;
const dec = () => {};
let _Foo;
new class extends babelHelpers.identity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass;
let _initClass;
const dec = () => {};
let hasX, hasA, hasM;
let _Foo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass;
let _initClass;
const dec = () => {};
let _Foo;
new class extends babelHelpers.identity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass, _Bar;
let _initClass, _Bar;
const dec = () => {};
const Foo = (class Bar {
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _initClass;
let _initClass;
const dec = () => {};
let _Foo;
new class extends babelHelpers.identity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let _computedKey, _computedKey2;
var _initProto, _Foo;
var _Foo;
let _initProto, _computedKey, _computedKey2;
const dec = () => {};
_computedKey = babelHelpers.toPropertyKey(getKey());
_computedKey2 = babelHelpers.toPropertyKey(getKey());
Expand Down

0 comments on commit d386707

Please sign in to comment.