Repository navigation
test(parser): regression tests for <!-- in a regex after ( - #28181
ashokram-si wants to merge 10 commits into
Conversation
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
Merging this PR will not alter performance
Comparing Footnotes
|
leaysgur
left a comment
There was a problem hiding this comment.
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
|
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 |
…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.
…n used to parse it
<!-- in a regex after ( as an HTML comment<!-- in a regex after (
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
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/miscfixtures named after the issue (the parser's testing guide; the earlier unit test inoxc_parseris gone):misc/pass/oxc-28158.ts: the issue's file, a module throughexport {}, with(/<!-- x -->/),(/<!-- x -->/).exec('x')andasync (/<!-- 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/failcase (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.tsreports "HTML comments are not allowed in modules" three times andoxc-28158.mjsparses cleanly (as the issue says,.mjswas fine);.tsstill 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
*_miscsnapshots: three more files, all passing. The head, f95cafc, adds theasyncline tooxc-28158.js; the three fixtures pass all six tools again there.cargo test -p oxc_parser,cargo fmt --checkandcargo clippy -p oxc_parser --all-targets -- -D warningsare 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.