microsoft / microsoft/TypeScript
ES `ClassDecoratorContext.name` has incorrect value with static `name`
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
ClassDecoratorContext static name
### 🕗 Version & Regression Information
5.0.4-5.9.3
### ⏯ Playground Link
https://tsplay.dev/N7vAow
### 💻 Code
```ts
const decorate = (_: unknown, ctx: ClassDecoratorContext) => {
console.log('decorate(%o)', ctx.name);
};
@decorate
class A {
static get name() {
return 2434;
}
}
```
### 🙁 Actual behavior
`ctx.name` is `2434` (from `A.name`)
### 🙂 Expected behavior
`ctx.name` is `'A'` (actual class name)
### Additional information about the issue
The wrong value comes from the context initializer in the emitted code:
```ts
__esDecorate(
null,
(_classDescriptor = { value: _classThis }),
_classDecorators,
{ kind: 'class', name: _classThis.name, metadata: _metadata },
null,
_classExtraInitializers,
);
```
The context `name` is initialized with `_classThis.name` (which triggers the getter) instead of the actual class name value (literal `"A"` in this case).
## Possible Fix
Currently, the `_classThis.name` fragment comes from `const classNameReference` in `transformClassLike` in `transformers/esDecorators.ts`:
```ts
const classNameReference = factory.createPropertyAccessExpression(renamedClassThis, "name");
```
It should be adjusted to get an actual class name (either literal or computed), possibly like this:
```ts
const classNameReference = node.name ? factory.createStringLiteralFromNode(node.name) :
node.emitNode?.assignedName ?? factory.createStringLiteral("")
```
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 transformers/esDecorators.ts 中的 transformClassLike 开始,检查 classNameReference 是如何为类装饰器上下文构建的。使用链接的 playground 重现该问题,然后验证带有静态 name getter 的类会向 ctx.name 提供其实际声明的名称(例如 "A"),而不是 getter 的结果。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 55/100