Skip to content

fix(codemode): report parse errors with a location and a hint - #59834

Merged
rekram1-node merged 2 commits into
devfrom
codemode-parse-errors
Oct 28, 2026
Merged

rekram1-node merged 2 commits into
devfrom
codemode-parse-errors

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #50577

Problem

When Code Mode can't parse a program, the model gets acorn's raw message: no location, acorn's own (line:col) format with a 0-based column, and for imports a hint about an internal parser option. Every other Code Mode error reads like TypeError: ... (line 1, col 15).

Change

executeProgram now catches acorn's SyntaxError and returns a ParseError with a 1-based location and the same (line L, col C) suffix as other errors. Imports and exports get a dedicated message. Everything else keeps acorn's text and adds a short note on what code has to look like. Anything else thrown while parsing (like a stack overflow on deeply nested input) goes down the same path as before.

What the model sees (copied from a run through core's execute tool):

Program Before After
const = 1 Unexpected token (1:6) SyntaxError: Unexpected token. Code must be plain JavaScript written as a program body. (line 1, col 7)
import x from "y" 'import' and 'export' may appear only with 'sourceType: module' (1:0) SyntaxError: Imports and exports aren't available. Call tools through `tools` and use the built-in globals. (line 1, col 1)
const x: number = 1 Unexpected token (1:7) SyntaxError: Unexpected token. Code must be plain JavaScript written as a program body. (line 1, col 8)

The structured diagnostic also carries location: { line, column } now. formatLocation takes a location instead of a node so both paths share it. No public types changed.

Out of scope

Unsupported syntax that parses fine (classes, getters and setters, and so on) already reports UnsupportedSyntax with its own hint. I left that alone.

Verification

From packages/codemode, after bun run script/sync-test262.ts ~/development/test262:

  • bun run typecheck: no type errors
  • bun run typecheck:tsc: clean
  • bun test: 14795 pass, 1678 skip, 0 fail (14791 pass and the same skips before the change; test262 negative parse tests still pass)

From packages/core:

  • bun run script/test.ts test/codemode test/tool-execute.test.ts: 30 pass, 0 fail

From the repo root:

  • bun run check: passed

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good. On the base, CodeMode.execute returned acorn's raw messages (Unexpected token (1:6), 'import' and 'export' may appear only with 'sourceType: module' (1:0)) with no location. With this change the same programs report SyntaxError: … (line L, col C) with a 1-based location, imports and exports get the dedicated message, and non-acorn failures while parsing (deep nesting) still report the nesting-depth error. In packages/codemode, bun test and tsc --noEmit pass, as do core's codemode/execute tests.

Public API: unchanged. Only the diagnostic message text changes, plus the already-typed optional location field.

No concerns.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new commit is fine. It removes the TypeScript clause from the general parse-error hint and updates the matching tests. I ran the reproduction from #50577 with a simulated model on the base and on this head. The base returns acorn's raw messages. With the PR, every error has a 1-based (line L, col C) location and the shorter hint, and imports get the dedicated message. errors.test.ts and tsc --noEmit pass.

Public API: unchanged.

No new concerns.

Before

After

@rekram1-node
rekram1-node merged commit c96d200 into dev Oct 28, 2026
15 checks passed
@rekram1-node
rekram1-node deleted the codemode-parse-errors branch October 28, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

codemode: parse errors surface raw Acorn messages with no location or hint

1 participant