`__setFunctionName` breaks custom static `name` on classes
#62,854 opened on 2025幎12æ8æ¥
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 3d 5h) (30d ã§ 8 merged PRs)
説æ
ð Search Terms
__setFunctionName class static name ES decorators static name __esDecorate static name
ð Version & Regression Information
5.0.4-5.9.3
⯠Playground Link
ð» Code
const noop = (Self: any, ctx: ClassDecoratorContext) => {};
const rand = () => 4;
@noop export default class {
static get name() { return 2434; }
}
(@noop class {
static get name() { return 2434; }
});
(@noop class __A {
static get name() { return 2434; }
});
@noop
class __B {
static get name() { return 2434; }
}
var __C = @noop class {
static get name() { return 2434; }
};
var __D = {
[rand()]: @noop class {
static get name() { return 2434; }
}
};
// Unconditionally throws at runtime:
@noop class __E {
static #a = 2434;
static get name() { return this.#a; }
}
// Undecorated classes are broken as well:
var __F = class {
static get name() { return 2434; }
// targetâ€es2021 => '__F'
// targetâ¥es2022 => 2434
static {}
}
ð Actual behavior
Static name is overridden by __setFunctionName().
ð Expected behavior
Static name should work as defined in the class code.
Additional information about the issue
Generated code for the ES decorators breaks custom static name property on the decorated class. The only syntax that works is plain static name; field declaration; other kinds (methods, get/set/accessor) are broken.
The issue reproduces iff the compiler decides to inject the static { __setFunctionName(this, ...) } block, which unconditionally overrides the static name descriptor on the class object.
The issue also affects undecorated classes (when targeting older environments; see the examples).
Iâm not sure what exactly triggers the
__setFunctionName()block injection. Sometimes itâs injected even if the class has a correct unambiguous automatic name. Sometimes it isnât injected, even if the class becomes incorrectly named (e.g., when targetinges3/es5):// tests/cases/compiler/blockScopedVariablesUseBeforeDef.ts function foo8() { let y = class { a = x; }; let x; // @ts-ignore console.log(y.name); } // targetâ€es5 => 'class_###' // targetâ¥es2015 => 'y' foo8();But either way, it shadows non-field static
namedeclarations.
Possible Fix
At runtime, check that the descriptor of this.name matches the automatic name descriptor shape (i.e., value:string, writableâ¡enumerableâ¡false, configurable=true). The runtime check is necessary to correctly handle static name declared with a computed key.
Technically, the necessary and sufficient condition is even simplerâjust check for writableâ¡false before __setFunctionName to robustly prevent overriding of non-field static name declarations:
- At this point, the descriptors are completely defined by the syntax:
- No
static {}blocks evaluated. - No class decorators evaluated.
- No class element initializers evaluated.
- No
- So, the static
nameis one of:- Automatic, always defined. If thereâs a static
namefield, itâs to be reconfigured at the time of the actual field initialization. - Method.
get/setoraccessor.
- Automatic, always defined. If thereâs a static
- Methods have
writableâ¡true. get/set/accessordeclarations havewritableâ¡undefined.
This matches the correct behavior:
- With a field
static namedeclaration, itâs the automaticnameup to the time of the actualstatic nameinitialization. The field initialization successfully overrides the automatic descriptor. - For non-fields, the descriptor matches the declaration from the very beginning of the class. If the
nameis reconfigured here, the actual declaration doesnât revert the override.
Adjust createClassNamedEvaluationHelperBlock() and isClassNamedEvaluationHelperBlock() in namedEvaluation.ts to produce a code like this:
static {
Object.getOwnPropertyDescriptor(this, "name").writable === false &&
__setFunctionName(this, ...);
}
NB: With
targetâ€es5anduseDefineForClassFieldsâ¡false, the compiler uses plainC.name=...assignment to set the staticname, which has no effect (doesnât reconfigure the staticnamedescriptor), so the injected blockâif injectedâwill call__setFunctionName()unconditionally. But this is already a compile-time errorStatic property 'name' conflicts with built-in property 'Function.name' of constructor function 'A'. (2699)so it doesnât matter here.