microsoft / microsoft/TypeScript

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

Aperta
#62,870 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Decorators Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in transformers/esDecorators.ts, in transformClassLike, e verifica come viene costruito classNameReference per il contesto del decoratore di classe. Riproduci il problema con il playground collegato, quindi verifica che una classe con un getter statico di name fornisca a ctx.name il suo nome effettivamente dichiarato, ad esempio "A", invece del risultato del getter.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.