Skip to content

Commit

Permalink
emit setFunctionName call for undecorated class with accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 13, 2024
1 parent ab03576 commit f45d9b3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
Expand Up @@ -184,6 +184,18 @@ function generateClassProperty(
}
}

function assignIdForAnonymousClass(
path: NodePath<t.Class>,
className: string | t.Identifier | t.StringLiteral | undefined,
) {
if (!path.node.id) {
path.node.id =
typeof className === "string"
? t.identifier(className)
: path.scope.generateUidIdentifier("Class");
}
}

function addProxyAccessorsFor(
className: t.Identifier,
element: NodePath<ClassDecoratableElement>,
Expand Down Expand Up @@ -1031,6 +1043,8 @@ function transformClass(
let protoInitLocal: t.Identifier;
let staticInitLocal: t.Identifier;
const classIdName = path.node.id?.name;
// Whether to generate a setFunctionName call to preserve the class name
const setClassName = typeof className === "object" ? className : undefined;
// Check if the decorator does not reference function-specific
// context or the given identifier name or contains yield or await expression.
// `true` means "maybe" and `false` means "no".
Expand Down Expand Up @@ -1141,9 +1155,7 @@ function transformClass(
setterKey = t.cloneNode(key);
}

if (!path.node.id) {
path.node.id = path.scope.generateUidIdentifier("Class");
}
assignIdForAnonymousClass(path, className);

addProxyAccessorsFor(
path.node.id,
Expand All @@ -1163,6 +1175,16 @@ function transformClass(
}

if (!classDecorators && !hasElementDecorators) {
if (!path.node.id && typeof className === "string") {
path.node.id = t.identifier(className);
}
if (setClassName) {
path.node.body.body.unshift(
createStaticBlockFromExpressions([
createSetFunctionNameCall(state, setClassName),
]),
);
}
// If nothing is decorated and no assignments inserted, return
return;
}
Expand Down Expand Up @@ -1292,9 +1314,7 @@ function transformClass(
}
}
} else {
if (!path.node.id) {
path.node.id = path.scope.generateUidIdentifier("Class");
}
assignIdForAnonymousClass(path, className);
classIdLocal = t.cloneNode(path.node.id);
}

Expand Down Expand Up @@ -1458,9 +1478,7 @@ function transformClass(

locals = [newFieldInitId, getId, setId];
} else {
if (!path.node.id) {
path.node.id = path.scope.generateUidIdentifier("Class");
}
assignIdForAnonymousClass(path, className);
addProxyAccessorsFor(
path.node.id,
newPath,
Expand Down Expand Up @@ -1965,7 +1983,7 @@ function transformClass(
classDecorationsId ?? t.arrayExpression(classDecorations),
t.numericLiteral(classDecorationsFlag),
needsInstancePrivateBrandCheck ? lastInstancePrivateName : null,
typeof className === "object" ? className : undefined,
setClassName,
t.cloneNode(superClass),
state,
version,
Expand Down
Expand Up @@ -15,19 +15,19 @@ class Foo {
Foo.#B = v;
}
}
Foo = class _Class {
Foo = class Foo {
static #A;
static get #a() {
return _Class.#A;
return Foo.#A;
}
static set #a(v) {
_Class.#A = v;
Foo.#A = v;
}
static #B = 123;
static get #b() {
return _Class.#B;
return Foo.#B;
}
static set #b(v) {
_Class.#B = v;
Foo.#B = v;
}
};
Expand Up @@ -22,26 +22,26 @@ class Foo {
Foo.#C = v;
}
}
Foo = class _Class {
Foo = class Foo {
static #A;
static get a() {
return _Class.#A;
return Foo.#A;
}
static set a(v) {
_Class.#A = v;
Foo.#A = v;
}
static #B = 123;
static get b() {
return _Class.#B;
return Foo.#B;
}
static set b(v) {
_Class.#B = v;
Foo.#B = v;
}
static #C = 456;
static get ['c']() {
return _Class.#C;
return Foo.#C;
}
static set ['c'](v) {
_Class.#C = v;
Foo.#C = v;
}
};
Expand Up @@ -155,6 +155,34 @@ const noop = () => {}
expect(logs).toEqual(["computing f", "computing symbol", "A0", "1", "2", "3", "4", "5", "6", "7", "8", "[9]", "#_10"]);
}

{
const logs = [];
const dec = () => {
return {
init(v) {
logs.push(v.name);
return v;
}
}
};
const f = () => { logs.push("computing f"); return 8. }

class C {
@dec static accessor A0 = class { static accessor q; };
@dec static accessor "1" = class { accessor q; };
@dec static accessor 2 = class extends class {} { static accessor p; };
@dec static accessor 3n = class extends class {} { accessor #q; };
@dec static accessor ["4"] = class { static accessor p; };
@dec static accessor [5] = class { static accessor #p; };
@dec static accessor [6n] = class { accessor p; };
@dec static accessor [f()] = class { @dec static accessor 7 = class { static accessor p }; };
@dec static accessor [{ [Symbol.toPrimitive]: () => (logs.push("computing symbol"), Symbol(9)) }] = class { static accessor p; };
@dec static accessor #_10 = class { static accessor #p; };
}

expect(logs).toEqual(["computing f", "computing symbol", "A0", "1", "2", "3", "4", "5", "6", "7", "8", "[9]", "#_10"]);
}

{
const logs = [];
const dec = decFactory(logs);
Expand Down

0 comments on commit f45d9b3

Please sign in to comment.