Skip to content

Fix reparseTopLevelAwait diagnostic slicing bug#3937

Merged
DanielRosenwasser merged 3 commits into
mainfrom
copilot/fix-await-literal-object-parsing
May 18, 2026
Merged

Fix reparseTopLevelAwait diagnostic slicing bug#3937
DanielRosenwasser merged 3 commits into
mainfrom
copilot/fix-await-literal-object-parsing

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 17, 2026

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 from diagnosticStart instead of forward:

// Bug: searches [0, diagnosticStart) — always returns -1
core.FindIndex(savedParseDiagnostics[:diagnosticStart], ...)

// Fix: searches [diagnosticStart, end) — correctly finds range boundary
core.FindIndex(savedParseDiagnostics[diagnosticStart:], ...)

This is a mistranslation of TypeScript's findIndex(array, predicate, startIndex), which searches from startIndex. The -1 result 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 search
  • testdata/tests/cases/compiler/awaitObjectLiteral.ts: New test case
  • Baseline updates: reachabilityChecksNoCrash1 loses 2 spurious errors (convergence improvement)

Copilot Checklist

I successfully ran these commands at the end of my session, and they completed without error:

  • npx hereby build
  • npx hereby test
  • npx hereby lint
  • npx hereby format

Copilot AI and others added 2 commits May 17, 2026 08:07
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>
Copilot AI changed the title [WIP] Fix tsgo parsing of literal object after await Fix reparseTopLevelAwait diagnostic slicing bug May 17, 2026
Copilot AI requested a review from jakebailey May 17, 2026 08:21
@jakebailey jakebailey marked this pull request as ready for review May 17, 2026 08:23
Copilot AI review requested due to automatic review settings May 17, 2026 08:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reparseTopLevelAwait diagnostic range search.
  • New compiler test awaitObjectLiteral.ts exercising top-level await of an object literal in module context.
  • Baseline updates: reachabilityChecksNoCrash1 loses 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.

@DanielRosenwasser DanielRosenwasser added this pull request to the merge queue May 18, 2026
Merged via the queue into main with commit ce98adb May 18, 2026
25 checks passed
@DanielRosenwasser DanielRosenwasser deleted the copilot/fix-await-literal-object-parsing branch May 18, 2026 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsgo wrongly parses literal object after await

4 participants