Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,10 @@ pub fn unexpected_optional_declaration(span: Span) -> OxcDiagnostic {
.with_label(span)
.with_help("Remove the `?`")
}

#[cold]
pub fn identifier_expected_after_question_dot(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Identifier expected after '?.'")
.with_label(span)
.with_help("Add an identifier after '?.'")
Copy link
Contributor

Choose a reason for hiding this comment

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

should this also consider the optional call expression case?

foo?.()

}
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ impl<'a> ParserImpl<'a> {

if let Some(span) = question_dot_span {
// We parsed `?.` but then failed to parse anything, so report a missing identifier here.
let error = diagnostics::unexpected_token(span);
let error = diagnostics::identifier_expected_after_question_dot(span);
return self.fatal_error(error);
}

Expand Down
3 changes: 2 additions & 1 deletion tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14698,11 +14698,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: Try inserting a semicolon here

× Unexpected token
× Identifier expected after '?.'
╭─[babel/packages/babel-parser/test/fixtures/typescript/type-arguments/call-optional-chain-invalid/input.ts:1:2]
1 │ f?.<string>[1];
· ──
╰────
help: Add an identifier after '?.'

× TS(1099): Type argument list cannot be empty.
╭─[babel/packages/babel-parser/test/fixtures/typescript/type-arguments/empty-function/input.ts:1:4]
Expand Down
12 changes: 8 additions & 4 deletions tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3532,32 +3532,36 @@ Negative Passed: 120/120 (100.00%)
5 │ }
╰────

× Unexpected token
× Identifier expected after '?.'
╭─[misc/fail/oxc-9497.js:2:8]
1 │ let repro = {};
2 │ repro.f?.
· ──
╰────
help: Add an identifier after '?.'

× Unexpected token
× Identifier expected after '?.'
╭─[misc/fail/oxc-9525-1.js:1:2]
1 │ x?.;
· ──
╰────
help: Add an identifier after '?.'

× Unexpected token
× Identifier expected after '?.'
╭─[misc/fail/oxc-9525-2.js:1:3]
1 │ [x?.];
· ──
╰────
help: Add an identifier after '?.'

× Unexpected token
× Identifier expected after '?.'
╭─[misc/fail/oxc-9525-3.js:2:4]
1 │ () => {
2 │ x?.
· ──
3 │ }
╰────
help: Add an identifier after '?.'

× The keyword 'let' is reserved
╭─[misc/fail/oxc.js:3:1]
Expand Down
Loading