microsoft / microsoft/TypeScript

ES `ClassDecoratorContext.name` has incorrect value with static `name`

オープン
#62,870 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: Decorators Help Wanted
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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("")
```

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

transformers/esDecorators.ts の transformClassLike から始め、クラスデコレーターコンテキスト用に classNameReference がどのように構築されるかを調べます。リンクされた playground で問題を再現し、static name getter を持つクラスが getter の結果ではなく、実際に宣言された名前("A" など)を ctx.name に渡すことを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
55/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。