Skip to content

panic: strings: negative Repeat count with --pretty; TS8010 span for an empty type annotation in JS ends before it starts #65078

Open

Description

🔎 Search Terms

panic, negative Repeat count, writeCodeSnippet, jsErrorAtRange, TS8010, checkJs, pretty

🕗 Version & Regression Information

Crashes in 7.0.2 and in 7.1.0-dev.20261031.1 (main at 6161772). TypeScript 6.0.3 does not crash and reports a different column (below).

💻 Code

// a.js
var R: ;
// tsconfig.json
{ "compilerOptions": { "noEmit": true, "allowJs": true, "checkJs": true }, "files": ["a.js"] }

🙁 Actual behavior

tsc -p tsconfig.json --pretty true prints the TS8010 header and source line, then crashes while drawing the underline:

a.js:1:8 - error TS8010: Type annotations can only be used in TypeScript files.

1 var R: ;
         panic: strings: negative Repeat count

goroutine 1 [running]:
strings.Repeat(...)
github.com/microsoft/TypeScript/tsc/internal/diagnosticwriter.writeCodeSnippet(..., 0x7, 0xffffffffffffffff, ...)
	tsc/internal/diagnosticwriter/diagnosticwriter.go:343
github.com/microsoft/TypeScript/tsc/internal/diagnosticwriter.FormatDiagnosticWithColorAndContext(...)
	tsc/internal/diagnosticwriter/diagnosticwriter.go:251

With --pretty false there is no crash, but TS8010 is at column 8:

a.js(1,8): error TS8010: Type annotations can only be used in TypeScript files.
a.js(1,8): error TS1110: Type expected.

🙂 Expected behavior

The same as TypeScript 6.0.3, which reports TS8010 at column 7 with an empty span and prints normally with --pretty:

a.js(1,7): error TS8010: Type annotations can only be used in TypeScript files.
a.js(1,8): error TS1110: Type expected.

Additional information about the issue

jsErrorAtRange (parser/parser.go:6745) builds the range as NewTextRange(scanner.SkipTrivia(p.sourceText, loc.Pos()), loc.End()). The type node here is missing (empty at index 6). Skipping the space moves the start to index 7, past the end, so the range has length −1. That gives the column-8 report, and writeCodeSnippet then calls strings.Repeat with a negative count. TypeScript 6.0.3 reports this through createDiagnosticForNode, whose getErrorSpanForNode doesn't skip trivia for missing nodes.

Found by differential fuzzing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions