Skip to content

feat(js): one typo hid every other error, and none came with a frame - #216

Open
jamesconwaydev wants to merge 2 commits into
js-perffrom
js-diag
Open

jamesconwaydev wants to merge 2 commits into
js-perffrom
js-diag

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Fixes #166.

Problem. A syntax error ended the parse. A file with three mistakes showed one, and fixing it showed the next. The error itself was a code and a byte range: for if (ready { nothing pointed back at the ( left open, for a duplicate label nothing pointed at the first, and there was no way to turn it into the lines a person reads. The codes were stable in practice but listed nowhere.

Change.

  • parsec-base: Diagnostic::render(src, &LineIndex, &RenderOptions) prints a code frame. A gutter of line numbers, the lines the error and its labels are on (a line between two is shown, a longer gap is ...), ^ under the error and - under each label, the rightmost label's message inline and the others hanging below. Tabs expand to tab_width, East Asian wide characters and most emoji count two columns, combining marks none, and control and bidi characters print as U+FFFD so a frame can't reorder or drive the terminal. A line longer than max_width (100), like a bundle's, is cut to a window around the marks with …. No new dependencies.
  • parsec-js: parse_recovering goes on after an error. A statement that fails is skipped past the next ;, to the } that closes its block, or to a statement keyword at the start of a line, with braces and template substitutions it opened counted, so an error inside an object or a template doesn't stop at its own }. Every error comes back; the first is the one parse gives. too-deep and step-budget still stop it.
  • Labels where a second place matters: this ( is still open (and [, {) on a missing closer, and the first of two labels, defaults, constructors, bindings, exports, import attributes and private names; continue L points at a label that isn't on a loop, "use strict" with complex parameters at the directive, a legacy octal escape at the "use strict" after it, and a line-broken throw at the throw.
  • parsec_js::diagnostics documents every code: what's wrong and the ECMAScript 2025 section, linked. A unit test reads the sources and fails if a code is missing from the table or the table lists one nothing gives.

Trade-off. Recovery doesn't count ( and [: one left open by a typo would otherwise hide every statement after it. The cost is a follow-on error now and then, like ) after ({};).

Tests.

  • test262_code_frames renders every error for 53 test262 negatives, one or two per kind, and compares them with tests/snapshots/diagnostics.txt. I read each frame; PARSEC_BLESS=1 rewrites the file.
  • recovery_reports_each_statement, recovery_resyncs_at_a_statement_keyword_on_a_new_line, recovery_skips_braces_templates_and_regexps_whole, recovery_inside_functions_and_blocks: each fails without recovery, which reports only the first error.
  • recovery_stops_at_limits: a depth or step limit still ends the parse.
  • recovery_changes_nothing_for_good_programs: a sample of 2,000+ test262 files that should parse give the same ESTree, comments and warnings either way.
  • labels_point_back: one case per label.
  • render::tests in parsec-base: carets, paths and warnings, an empty span at the end, labels on other lines and the ... gap, hanging labels, tabs, wide and combining characters, control and bidi characters, windowed long lines, multi-line spans, CR, CRLF and U+2028 lines, and spans past the end.
  • The parse fuzz target now compares parse with parse_recovering and renders every error: 266,773 runs in 3 minutes, nothing found.

Parse speed on the js bench, before and after: typescript.js 280 and 279 MB/s, all sources 289 and 287 MB/s, inside the run-to-run noise (oxc moved 363 to 359 in the same runs).

Passed locally: cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --all --check, RUSTDOCFLAGS=-D warnings cargo doc, scripts/check-headers.sh.

A syntax error ended the parse, so a file with three mistakes showed
one, and the diagnostic was a code and a byte range: nothing pointed
at the `(` left open or the first of two declarations, and nothing
turned it into lines a person reads.

- parsec-base: `Diagnostic::render` prints a code frame: a gutter of
  line numbers, the lines the error and its labels are on, carets and
  dashes under them, tabs expanded, wide characters two columns,
  control and bidi characters replaced, and a long line cut to a
  window around the marks.
- parsec-js: `parse_recovering` skips a statement that fails to the
  next `;`, to the `}` closing its block, or to a statement keyword on
  a new line, and reports every error. The first is the one `parse`
  gives; limits still stop it. `parse` itself only checks a flag per
  statement.
- Labels on the second place an error involves: the bracket still
  open, and the first of two labels, defaults, constructors,
  bindings, exports, attributes and private names.
- `parsec_js::diagnostics` lists every code with its meaning and the
  ECMAScript 2025 section, and a test fails if a code is missing.
- tests/diagnostics.rs compares the frames for 53 test262 negatives
  with tests/snapshots/diagnostics.txt, and checks that recovery
  leaves good programs' trees alone. The parse fuzz target compares
  `parse` with `parse_recovering`.
Git on Windows checks the snapshot out with CRLF line ends, and the
frames are compared with LF ones. Read it with CRLF turned to LF.

This branch has not been deployed

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

Labels

javascript The JavaScript parser, parsec-js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant