microsoft / microsoft/TypeScript

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

Đang mở
#62,870 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
Bug Domain: Decorators Help Wanted
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔎 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("")
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu trong transformers/esDecorators.ts tại transformClassLike và kiểm tra cách classNameReference được xây dựng cho ngữ cảnh decorator của class. Tái hiện vấn đề với playground được liên kết, sau đó xác minh rằng một class có static name getter cung cấp tên được khai báo thực tế của nó, chẳng hạn như "A", cho ctx.name thay vì kết quả của getter.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.