microsoft / microsoft/TypeScript
[Bug] PrivateIdentifier nodes are omitted from 2020 semantic classifications
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
# Bug Report
### 🔎 Search Terms
semantic highlighting, semantic token, PrivateIdentifier, private field, private method, encodedSemanticClassifications
### 🕗 Version & Regression Information
- TypeScript 6.0.3 (bundled with VS Code)
- Also present in the v5.9 classifier implementation
- This is not a theme-specific issue
### 💻 Code
```ts
class Foo {
field = 1;
#privateField = 1;
method() {}
#privateMethod() {}
test() {
this.field;
this.#privateField;
this.method();
this.#privateMethod();
}
}
```
### 🙁 Actual behavior
In VS Code's **Developer: Inspect Editor Tokens and Scopes**:
- `field` and `method` receive semantic token types.
- `#privateField` and `#privateMethod` receive no semantic token and fall back to TextMate scopes.
- As a result, `editor.semanticTokenColorCustomizations` rules for `property` and `method` do not apply to ECMAScript private members.
### 🙂 Expected behavior
- `#privateField` should be classified as `property`.
- `#privateMethod` should be classified as `method`.
- Declarations should also receive the `declaration` modifier.
### Root cause
VS Code requests `encodedSemanticClassifications-full` with format `"2020"`.
In `src/services/classifier2020.ts`, `collectTokens` only enters the semantic-classification path for `isIdentifier(node)`:
```ts
if (isIdentifier(node) && ...) {
let symbol = typeChecker.getSymbolAtLocation(node);
// ...
}
```
ECMAScript private names are separate `SyntaxKind.PrivateIdentifier` nodes, so they are skipped before `getSymbolAtLocation` is called.
Relevant source:
https://github.com/microsoft/TypeScript/blob/050880ce59e30b356b686bd3144efe24f875ebc8/src/services/classifier2020.ts#L121-L203
### Proposed fix
```diff
+ isPrivateIdentifier,
isPropertyAccessExpression,
```
```diff
- if (isIdentifier(node) && ...) {
+ if ((isIdentifier(node) || isPrivateIdentifier(node)) && ...) {
```
The existing declaration-kind mapping already classifies private fields and methods correctly once these nodes reach the symbol-classification path.
I tested the equivalent change against TypeScript 6.0.3:
- `#privateField` declaration/reference → `property`
- `#privateMethod` declaration/reference → `member` internally, mapped by VS Code to `method`
- declaration occurrences receive the `declaration` modifier
- added fourslash regression test passes
### Related issue
https://github.com/microsoft/TypeScript/issues/44483 requests new `private`/`protected` semantic modifiers. This report is different: ECMAScript `PrivateIdentifier` nodes currently receive no semantic classification at all.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in src/services/classifier2020.ts, in collectTokens, e rivedi la mappatura esistente di declaration-kind per i nodi identificatore. Aggiungi il test di regressione fourslash pertinente che copra le dichiarazioni e i riferimenti a campi e metodi private, quindi verifica che i nodi ricevano classificazioni property o method e declaration modifiers.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript, vscode
- Ambito
- compilers, developer-experience
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Attiva
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 88/100