Skip to content

Commit db839ae

Browse files
committed
feat(parser): improve diagnostic for unexpected optional declarations (#16131)
Currently we just emit "unexpected token", but we actually have a good amount of context here to provide better help + error message.
1 parent 85c3a10 commit db839ae

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

crates/oxc_parser/src/diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,3 +1110,10 @@ pub fn expect_switch_clause(span: Span) -> OxcDiagnostic {
11101110
.with_label(span.label("`case` or `default` clause expected here"))
11111111
.with_help("If this is intended to be the condition for the switch statement, add `case` before it.")
11121112
}
1113+
1114+
#[cold]
1115+
pub fn unexpected_optional_declaration(span: Span) -> OxcDiagnostic {
1116+
OxcDiagnostic::error("Optional declaration is not allowed here")
1117+
.with_label(span)
1118+
.with_help("Remove the `?`")
1119+
}

crates/oxc_parser/src/js/declaration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a> ParserImpl<'a> {
118118
None
119119
};
120120
let optional = if self.at(Kind::Question) {
121-
self.error(diagnostics::unexpected_token(self.cur_token().span()));
121+
self.error(diagnostics::unexpected_optional_declaration(self.cur_token().span()));
122122
self.bump_any();
123123
true
124124
} else {

tasks/coverage/snapshots/parser_misc.snap

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,51 +3273,57 @@ Negative Passed: 120/120 (100.00%)
32733273
· ─────
32743274
╰────
32753275
3276-
× Unexpected token
3276+
× Optional declaration is not allowed here
32773277
╭─[misc/fail/oxc-2253.ts:1:8]
32783278
1const a? = "A"
32793279
· ─
32803280
2const [b]? = ["B"]
32813281
╰────
3282+
help: Remove the `?`
32823283
3283-
× Unexpected token
3284+
× Optional declaration is not allowed here
32843285
╭─[misc/fail/oxc-2253.ts:2:10]
32853286
1const a? = "A"
32863287
2const [b]? = ["B"]
32873288
· ─
32883289
3const { c }? = { c: "C" }
32893290
╰────
3291+
help: Remove the `?`
32903292
3291-
× Unexpected token
3293+
× Optional declaration is not allowed here
32923294
╭─[misc/fail/oxc-2253.ts:3:12]
32933295
2const [b]? = ["B"]
32943296
3const { c }? = { c: "C" }
32953297
· ─
32963298
4
32973299
╰────
3300+
help: Remove the `?`
32983301
3299-
× Unexpected token
3302+
× Optional declaration is not allowed here
33003303
╭─[misc/fail/oxc-2253.ts:5:13]
33013304
4
33023305
5const d ? = "A"
33033306
· ─
33043307
6const [e, f] ? = ["B"]
33053308
╰────
3309+
help: Remove the `?`
33063310
3307-
× Unexpected token
3311+
× Optional declaration is not allowed here
33083312
╭─[misc/fail/oxc-2253.ts:6:19]
33093313
5const d ? = "A"
33103314
6const [e, f] ? = ["B"]
33113315
· ─
33123316
7const { g, h } ? = { c: "C" }
33133317
╰────
3318+
help: Remove the `?`
33143319
3315-
× Unexpected token
3320+
× Optional declaration is not allowed here
33163321
╭─[misc/fail/oxc-2253.ts:7:22]
33173322
6const [e, f] ? = ["B"]
33183323
7const { g, h } ? = { c: "C" }
33193324
· ─
33203325
╰────
3326+
help: Remove the `?`
33213327
33223328
× Empty parenthesized expression
33233329
╭─[misc/fail/oxc-232.js:1:5]
@@ -3494,12 +3500,13 @@ Negative Passed: 120/120 (100.00%)
34943500
· ───────
34953501
╰────
34963502
3497-
× Unexpected token
3503+
× Optional declaration is not allowed here
34983504
╭─[misc/fail/oxc-5955-1.ts:1:8]
34993505
1const x?: number = 1;
35003506
· ─
35013507
2
35023508
╰────
3509+
help: Remove the `?`
35033510
35043511
× Unexpected token
35053512
╭─[misc/fail/oxc-5955-2.ts:3:8]

0 commit comments

Comments
 (0)