Fix reparseTopLevelAwait diagnostic slicing bug#3937
Conversation
Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/e349c6c9-f922-4939-b9e7-b51399e84279 Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
The diagnostic slicing in reparseTopLevelAwait used savedParseDiagnostics[:diagnosticStart] when it should have used savedParseDiagnostics[diagnosticStart:] to find the end of the diagnostic range. This caused initial parse diagnostics to leak through even after a successful reparse with await context. Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/e349c6c9-f922-4939-b9e7-b51399e84279 Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a mistranslation bug in reparseTopLevelAwait where the diagnostic-range end search was slicing the diagnostics list in the wrong direction ([:diagnosticStart] instead of [diagnosticStart:]). The bug caused initial-parse diagnostics covering the awaited region to leak into the reparsed output (since FindIndex on the prefix slice always returned -1, so the entire tail was appended), producing spurious parse errors for code like await { bar: 42 } at the top level of a module. With the fix, diagnosticEnd is correctly interpreted as a relative offset, matching the existing slice math savedParseDiagnostics[diagnosticStart : diagnosticStart+diagnosticEnd].
Changes:
- One-line fix to slice direction in
reparseTopLevelAwaitdiagnostic range search. - New compiler test
awaitObjectLiteral.tsexercising top-levelawaitof an object literal in module context. - Baseline updates:
reachabilityChecksNoCrash1loses 2 spurious errors (convergence toward TS reference).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/parser/parser.go | Fix slice direction ([diagnosticStart:]) so FindIndex searches forward from the start of the range, returning the correct relative end offset. |
| testdata/tests/cases/compiler/awaitObjectLiteral.ts | New minimal regression test covering top-level await { ... } across two module files. |
| testdata/baselines/reference/compiler/awaitObjectLiteral.js | New emit baseline for the new test. |
| testdata/baselines/reference/compiler/awaitObjectLiteral.symbols | New symbols baseline for the new test. |
| testdata/baselines/reference/compiler/awaitObjectLiteral.types | New types baseline confirming await { bar: 42 } is typed as { bar: number; }. |
| testdata/baselines/reference/submodule/compiler/reachabilityChecksNoCrash1.errors.txt | Removes 2 spurious diagnostics (TS1135, TS1128) and updates error count from 37 to 35. |
| testdata/baselines/reference/submoduleAccepted/compiler/reachabilityChecksNoCrash1.errors.txt.diff | Shrinks the accepted diff vs. the TS reference, reflecting convergence. |
await { bar: 42 }in a module context produced spurious parse errors because initial-parse diagnostics leaked through the reparse.Analysis
In
reparseTopLevelAwait, the diagnostic range-end search sliced backward fromdiagnosticStartinstead of forward:This is a mistranslation of TypeScript's
findIndex(array, predicate, startIndex), which searches fromstartIndex. The-1result caused the entire tail of initial-parse diagnostics to be copied, including diagnostics for the range being reparsed with await context.Changes
internal/parser/parser.go: Fix slice direction in diagnostic range searchtestdata/tests/cases/compiler/awaitObjectLiteral.ts: New test casereachabilityChecksNoCrash1loses 2 spurious errors (convergence improvement)Copilot Checklist
I successfully ran these commands at the end of my session, and they completed without error: