Skip to content

test(parser): regression tests for <!-- in a regex after ( - #28181

Open
ashokram-si wants to merge 10 commits into
oxc-project:mainfrom
ashokram-si:fix/regex-html-comment-28158
Open

ashokram-si wants to merge 10 commits into
oxc-project:mainfrom
ashokram-si:fix/regex-html-comment-28158

Conversation

@ashokram-si

@ashokram-si ashokram-si commented Oct 26, 2026 •

Copy link
Copy Markdown

Refs #28158

The bug is gone from main since #29557 ("skip arrow speculation for array and object literals"): among other things it stops reading ahead after ( when the next token cannot start a parameter, so the lookahead no longer lexes past (/ into the regex. Bisected with the issue's file as a module: "HTML comments are not allowed in modules" on 85c0269 (its parent), none on 2c6ee72. #29603 later replaced that speculation with a cover grammar. The fix here is dropped. What's left are the regression tests, so a later change to the parenthesized-expression or lexer path can't bring the bug back unseen. (An earlier version of this description credited #29603; it was #29557.)

Tests, as tasks/coverage/misc fixtures named after the issue (the parser's testing guide; the earlier unit test in oxc_parser is gone):

  • misc/pass/oxc-28158.ts: the issue's file, a module through export {}, with (/<!-- x -->/), (/<!-- x -->/).exec('x') and async (/<!-- x -->/).
  • misc/pass/oxc-28158.mjs: the same three in a module.
  • misc/pass/oxc-28158.js: a script (no import or export) that starts with a real <!-- line comment, then the regex cases. The formatter panicked on the regex case in a script on the main this PR was first opened against.

An HTML-like comment in a module is already a misc/fail case (html-comment-in-module.mjs, html-comment-with-esm.js), so there's no new one for it.

Run on older mains: on f32a8d8 (the main this PR was opened against) oxc-28158.ts reports "HTML comments are not allowed in modules" three times and oxc-28158.mjs parses cleanly (as the issue says, .mjs was fine); .ts still reports it on 85c0269 and parses cleanly on 2c6ee72 (#29557); on f43a92f, just before #29603, all three pass the parser, formatter and codegen coverage runs.

With current main merged (832ed712c), the parser, lexer, semantic, codegen, formatter and transformer coverage runs with the Test262, Babel and TypeScript suites at their pinned commits change only the *_misc snapshots: three more files, all passing. The head, f95cafc, adds the async line to oxc-28158.js; the three fixtures pass all six tools again there. cargo test -p oxc_parser, cargo fmt --check and cargo clippy -p oxc_parser --all-targets -- -D warnings are clean.

No Example or playground link: this PR only adds tests, and the behavior it checks is already on main.

AI disclosure: written with Claude Code (model: Claude Opus 5.5, claude-opus-5-5; the reasoning effort level was not recorded); I reviewed the change and ran the tests.

Arrow function lookahead at `(` read the token after the second one
before checking whether the second could start a parameter. For
`(/<!-- x -->/)` it lexed `/` as a punctuator and then `<!--` as an
HTML-like comment. Rewinding does not undo the deferred module error,
so unambiguous files with module syntax reported "HTML comments are not
allowed in modules", and scripts kept a stray comment that made the
formatter panic.

Return `Tristate::False` before reading the third token when the second
token is not an identifier, `this` or a modifier, as TypeScript does.

Fixes oxc-project#28158
@ashokram-si
ashokram-si requested a review from Boshen as a code owner October 26, 2026 12:59
@github-actions github-actions Bot added the A-parser Area - JS/TS parsing and syntax errors, oxc_parser label Oct 26, 2026
@codspeed

codspeed Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 87 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing ashokram-si:fix/regex-html-comment-28158 (54f9061) with main (028a267)

Open in CodSpeed

Footnotes

  1. 9 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@leaysgur leaysgur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems good to me.

TS(RS) also has the same check here: https://github.com/oxc-project/tsrs/blob/677622d08ef3c12f370b9342623a41d156687c0c/crates/tsrs_parser/src/parser_3.rs#L233-L238

Lines are slightly different, but it doesn't seem to cause any actual harm. Perhaps it might be worth aligning?

…t-28158

# Conflicts:
#	crates/oxc_parser/src/js/arrow.rs
#	tasks/coverage/snapshots/codegen_misc.snap
#	tasks/coverage/snapshots/formatter_misc.snap
#	tasks/coverage/snapshots/lexer_misc.snap
#	tasks/coverage/snapshots/parser_misc.snap
#	tasks/coverage/snapshots/semantic_misc.snap
#	tasks/coverage/snapshots/transformer_misc.snap
#	tasks/track_memory_allocations/allocs_parser.yaml
@ashokram-si

Copy link
Copy Markdown
Author

Thanks for the pointer. I tried aligning with tsrs, but #28412 (merged today) now reads these tokens straight from the lexer without a checkpoint. Following tsrs's order would need a lookahead again for the token after a modifier, which works against that change. So I kept the single early return before next_token() in the new is_parenthesized_arrow_function_expression_after_lparen: it returns the same answer as tsrs for every token (anything that isn't an identifier, this or a modifier is False either way) and only skips lexing one token. Merged main; the misc coverage snapshots are +2 and allocs_parser.yaml is one allocation lower, as before.

…t-28158

# Conflicts:
#	tasks/coverage/snapshots/codegen_misc.snap
#	tasks/coverage/snapshots/formatter_misc.snap
#	tasks/coverage/snapshots/lexer_misc.snap
#	tasks/coverage/snapshots/parser_misc.snap
#	tasks/coverage/snapshots/semantic_misc.snap
#	tasks/coverage/snapshots/transformer_misc.snap
#	tasks/track_memory_allocations/allocs_parser.yaml
…t-28158

# Conflicts:
#	tasks/coverage/snapshots/codegen_misc.snap
#	tasks/coverage/snapshots/formatter_misc.snap
#	tasks/coverage/snapshots/lexer_misc.snap
#	tasks/coverage/snapshots/parser_misc.snap
#	tasks/coverage/snapshots/semantic_misc.snap
#	tasks/coverage/snapshots/transformer_misc.snap
#	tasks/track_memory_allocations/allocs_parser.yaml
…t-28158

# Conflicts:
#	tasks/coverage/snapshots/parser_misc.snap
#	tasks/track_memory_allocations/allocs_parser.yaml
…t-28158

# Conflicts:
#	tasks/track_memory_allocations/allocs_parser.yaml
oxc-project#29603 parses `(…)` once with a cover grammar, which removed the
speculative lookahead in `is_parenthesized_arrow_function_expression_worker`
that this branch patched, and fixed oxc-project#28158 with it. The branch's change to
arrow.rs and its allocation count are dropped (main's files are taken); the
parser test and the two misc fixtures stay as regression tests, and the
`*_misc` coverage snapshots count them.
@ashokram-si ashokram-si changed the title fix(parser): don't treat <!-- in a regex after ( as an HTML comment test(parser): regression tests for <!-- in a regex after ( Nov 17, 2026
AGENTS.md and crates/oxc_parser/AGENTS.md now ask for parser
regressions as tasks/coverage/misc fixtures named after the issue, not
Rust unit tests. The unit test is removed; oxc-28158.ts (the issue's
file, unambiguous and a module through `export`), oxc-28158.mjs (a
module) and oxc-28158.js (a script with an HTML-like comment of its own)
cover the module, unambiguous-to-module and script goals. The error for
an HTML-like comment in a module is already covered by
misc/fail/html-comment-in-module.mjs and html-comment-with-esm.js.
…t-28158

# Conflicts:
#	tasks/coverage/snapshots/codegen_misc.snap
#	tasks/coverage/snapshots/formatter_misc.snap
#	tasks/coverage/snapshots/lexer_misc.snap
#	tasks/coverage/snapshots/parser_misc.snap
#	tasks/coverage/snapshots/semantic_misc.snap
#	tasks/coverage/snapshots/transformer_misc.snap

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

A-parser Area - JS/TS parsing and syntax errors, oxc_parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants