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 @@ -1110,3 +1110,10 @@ pub fn expect_switch_clause(span: Span) -> OxcDiagnostic {
.with_label(span.label("`case` or `default` clause expected here"))
.with_help("If this is intended to be the condition for the switch statement, add `case` before it.")
}

#[cold]
pub fn unexpected_optional_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Optional declaration is not allowed here")
.with_label(span)
.with_help("Remove the `?`")
}
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a> ParserImpl<'a> {
None
};
let optional = if self.at(Kind::Question) {
self.error(diagnostics::unexpected_token(self.cur_token().span()));
self.error(diagnostics::unexpected_optional_declaration(self.cur_token().span()));
self.bump_any();
true
} else {
Expand Down
21 changes: 14 additions & 7 deletions tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3273,51 +3273,57 @@ Negative Passed: 120/120 (100.00%)
· ─────
╰────

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-2253.ts:1:8]
1 │ const a? = "A"
· ─
2 │ const [b]? = ["B"]
╰────
help: Remove the `?`

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-2253.ts:2:10]
1 │ const a? = "A"
2 │ const [b]? = ["B"]
· ─
3 │ const { c }? = { c: "C" }
╰────
help: Remove the `?`

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-2253.ts:3:12]
2 │ const [b]? = ["B"]
3 │ const { c }? = { c: "C" }
· ─
4 │
╰────
help: Remove the `?`

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-2253.ts:5:13]
4 │
5 │ const d ? = "A"
· ─
6 │ const [e, f] ? = ["B"]
╰────
help: Remove the `?`

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-2253.ts:6:19]
5 │ const d ? = "A"
6 │ const [e, f] ? = ["B"]
· ─
7 │ const { g, h } ? = { c: "C" }
╰────
help: Remove the `?`

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-2253.ts:7:22]
6 │ const [e, f] ? = ["B"]
7 │ const { g, h } ? = { c: "C" }
· ─
╰────
help: Remove the `?`

× Empty parenthesized expression
╭─[misc/fail/oxc-232.js:1:5]
Expand Down Expand Up @@ -3494,12 +3500,13 @@ Negative Passed: 120/120 (100.00%)
· ───────
╰────

× Unexpected token
× Optional declaration is not allowed here
╭─[misc/fail/oxc-5955-1.ts:1:8]
1 │ const x?: number = 1;
· ─
2 │
╰────
help: Remove the `?`

× Unexpected token
╭─[misc/fail/oxc-5955-2.ts:3:8]
Expand Down
Loading