microsoft / microsoft/TypeScript

[Bug] PrivateIdentifier nodes are omitted from 2020 semantic classifications

Đang mở Phù hợp với người mới
#64,322 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

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ả

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
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.
Image Image
🙂 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):

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
+    isPrivateIdentifier,
     isPropertyAccessExpression,
-        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.

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

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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong src/services/classifier2020.ts tại collectTokens và xem lại ánh xạ declaration-kind hiện có cho các nút định danh. Thêm bài kiểm thử hồi quy fourslash liên quan, bao quát các khai báo và tham chiếu đến trường và phương thức private, sau đó xác minh rằng các nút nhận được các phân loại property hoặc method và declaration modifiers.

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, vscode
Lĩnh vực
compilers, developer-experience
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
88/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.