typescript/ast/scanner: scan() never advances past `#` not followed by an identifier start (7.1.0-beta)

Đang mở
#65,075 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

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

Hướng nghiên cứu

Start with the createScanner entry point in typescript/ast/scanner and run the provided reproduction on the three inputs. Trace the # scanning behavior when no identifier start follows, then check existing scanner tests for where to add regression coverage. Done means each scan advances or returns EndOfFile, and the inputs produce tokens that do not repeat at the same position.

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

Mô tả

🔎 Search Terms

createScanner, scan, PrivateIdentifier, #, infinite loop, scanner does not advance, typescript/ast/scanner

🕗 Version & Regression Information
  • Reproduces in [email protected] (typescript/ast/scanner). The JS scanner is used through the new API entry points.
  • Does not reproduce in [email protected] (ts.createScanner).
  • Node 22.22.3, Linux x64.
⏯ Playground Link

No response

💻 Code
// node repro.mjs
import { createScanner } from "typescript/ast/scanner";
import { SyntaxKind } from "typescript/ast";

for (const text of ["a # b", "# ", "#1"]) {
  const scanner = createScanner(/*skipTrivia*/ true, /*languageVariant*/ undefined, text);
  const tokens = [];
  for (let i = 0; i < 6; i++) {
    const kind = scanner.scan();
    tokens.push(`${SyntaxKind[kind]}@${scanner.getTokenStart()}-${scanner.getTokenEnd()}`);
    if (kind === SyntaxKind.EndOfFile) break;
  }
  console.log(JSON.stringify(text), tokens.join(", "));
}
🙁 Actual behavior

When # is not followed by an identifier start, scan() returns PrivateIdentifier with an empty range and never advances, so it never reaches EndOfFile:

"a # b" Identifier@0-1, PrivateIdentifier@2-2, PrivateIdentifier@2-2, PrivateIdentifier@2-2, PrivateIdentifier@2-2, PrivateIdentifier@2-2
"# "    PrivateIdentifier@0-0, PrivateIdentifier@0-0, ...
"#1"    PrivateIdentifier@0-0, PrivateIdentifier@0-0, ...

Any loop of the form while (scanner.scan() !== SyntaxKind.EndOfFile) hangs on such input.

🙂 Expected behavior

The scanner consumes the # and moves on, as [email protected] does:

"a # b" Identifier@0, PrivateIdentifier@2, Identifier@4, EndOfFileToken@5
"# "    PrivateIdentifier@0, EndOfFileToken@2
"#1"    PrivateIdentifier@0, NumericLiteral@1, EndOfFileToken@2

(5.9 also reports an "Invalid character" error for the lone #.) At minimum, every scan() call should advance the position or return EndOfFile.

Additional information about the issue

Input like this is common in tooling that scans arbitrary text, such as comments, markdown-ish strings, and partial edits.

Ngôn ngữ chính
Go
Star
112k
Fork
16k
Merge trung bình
3 ngày 4 giờ
Pull request đã merge (30 ngày)
145

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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.

Issue khác của microsoft/TypeScript

Tất cả issue của microsoft/TypeScript

Issue tương tự

Thêm issue về Go

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.