From b38b20ba558d1ac105d74350a9efbc88c84590b5 Mon Sep 17 00:00:00 2001 From: Cameron Clark Date: Tue, 4 Mar 2025 10:10:53 +0000 Subject: [PATCH] fix(parser): fix false positive parsing optional member expr --- crates/oxc_parser/src/js/expression.rs | 12 +++++++++--- tasks/coverage/misc/fail/oxc-9525-1.js | 1 + tasks/coverage/misc/fail/oxc-9525-2.js | 1 + tasks/coverage/misc/fail/oxc-9525-3.js | 3 +++ tasks/coverage/snapshots/parser_misc.snap | 24 +++++++++++++++++++++-- 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 tasks/coverage/misc/fail/oxc-9525-1.js create mode 100644 tasks/coverage/misc/fail/oxc-9525-2.js create mode 100644 tasks/coverage/misc/fail/oxc-9525-3.js diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 219407ad6ae77..6ba5aac386551 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -711,10 +711,16 @@ impl<'a> ParserImpl<'a> { kind if kind.is_identifier_name() => { self.parse_static_member_expression(lhs_span, lhs, true)? } - Kind::Eof => { - return Err(diagnostics::unexpected_end(self.cur_token().span())); + Kind::Bang + | Kind::LAngle + | Kind::LParen + | Kind::NoSubstitutionTemplate + | Kind::ShiftLeft + | Kind::TemplateHead + | Kind::LBrack => break, + _ => { + return Err(diagnostics::unexpected_token(self.cur_token().span())); } - _ => break, } } // computed member expression is not allowed in decorator diff --git a/tasks/coverage/misc/fail/oxc-9525-1.js b/tasks/coverage/misc/fail/oxc-9525-1.js new file mode 100644 index 0000000000000..25846d2638db9 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-9525-1.js @@ -0,0 +1 @@ +x?.; diff --git a/tasks/coverage/misc/fail/oxc-9525-2.js b/tasks/coverage/misc/fail/oxc-9525-2.js new file mode 100644 index 0000000000000..95051a008188e --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-9525-2.js @@ -0,0 +1 @@ +[x?.]; diff --git a/tasks/coverage/misc/fail/oxc-9525-3.js b/tasks/coverage/misc/fail/oxc-9525-3.js new file mode 100644 index 0000000000000..7c0ab49dedca4 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-9525-3.js @@ -0,0 +1,3 @@ +() => { + x?. +} \ No newline at end of file diff --git a/tasks/coverage/snapshots/parser_misc.snap b/tasks/coverage/snapshots/parser_misc.snap index 4665609f86f1c..99d13ac62439b 100644 --- a/tasks/coverage/snapshots/parser_misc.snap +++ b/tasks/coverage/snapshots/parser_misc.snap @@ -1,7 +1,7 @@ parser_misc Summary: AST Parsed : 32/32 (100.00%) Positive Passed: 32/32 (100.00%) -Negative Passed: 27/27 (100.00%) +Negative Passed: 30/30 (100.00%) × Unexpected token ╭─[misc/fail/oxc-169.js:2:1] @@ -272,13 +272,33 @@ Negative Passed: 27/27 (100.00%) 5 │ } ╰──── - × Unexpected end of file + × Unexpected token ╭─[misc/fail/oxc-9497.js:2:8] 1 │ let repro = {}; 2 │ repro.f?. · ── ╰──── + × Unexpected token + ╭─[misc/fail/oxc-9525-1.js:1:2] + 1 │ x?.; + · ── + ╰──── + + × Unexpected token + ╭─[misc/fail/oxc-9525-2.js:1:3] + 1 │ [x?.]; + · ── + ╰──── + + × Unexpected token + ╭─[misc/fail/oxc-9525-3.js:2:4] + 1 │ () => { + 2 │ x?. + · ── + 3 │ } + ╰──── + × The keyword 'let' is reserved ╭─[misc/fail/oxc.js:3:1] 2 │