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
5 changes: 4 additions & 1 deletion crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,10 @@ pub fn check_switch_statement<'a>(stmt: &SwitchStatement<'a>, ctx: &SemanticBuil
for case in &stmt.cases {
if case.test.is_none() {
if let Some(previous_span) = previous_default {
ctx.error(diagnostics::redeclaration("default", previous_span, case.span));
ctx.error(diagnostics::switch_stmt_cannot_have_multiple_default_case(
previous_span,
case.span,
));
break;
}
previous_default.replace(case.span);
Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_semantic/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,15 @@ pub fn ts_export_assignment_cannot_be_used_with_other_exports(span: Span) -> Oxc
.with_label(span)
.with_help("If you want to use `export =`, remove other `export`s and put all of them to the right hand value of `export =`. If you want to use `export`s, remove `export =` statement.")
}

#[cold]
pub fn switch_stmt_cannot_have_multiple_default_case(
first_default: Span,
other_default: Span,
) -> OxcDiagnostic {
ts_error("1113", "A 'default' clause cannot appear more than once in a 'switch' statement.")
.with_labels(vec![
first_default.label("First 'default' clause is here."),
other_default.label("Another 'default' clause cannot appear here."),
])
}
12 changes: 6 additions & 6 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1716,12 +1716,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ─
╰────

× Identifier `default` has already been declared
× TS(1113): A 'default' clause cannot appear more than once in a 'switch' statement.
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/427/input.js:1:14]
1 │ switch (c) { default: default: }
· ────┬─── ────┬───
· │ ╰── It can not be redeclared here
· ╰── `default` has already been declared here
· │ ╰── Another 'default' clause cannot appear here.
· ╰── First 'default' clause is here.
╰────

× Unexpected token
Expand Down Expand Up @@ -11147,12 +11147,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: Wrap this declaration in a block statement

× Identifier `default` has already been declared
× TS(1113): A 'default' clause cannot appear more than once in a 'switch' statement.
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0143/input.js:1:14]
1 │ switch (c) { default: default: }
· ────┬─── ────┬───
· │ ╰── It can not be redeclared here
· ╰── `default` has already been declared here
· │ ╰── Another 'default' clause cannot appear here.
· ╰── First 'default' clause is here.
╰────

× Unexpected token
Expand Down
6 changes: 3 additions & 3 deletions tasks/coverage/snapshots/parser_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38454,17 +38454,17 @@ Negative Passed: 4581/4581 (100.00%)
22 │ }
╰────

× Identifier `default` has already been declared
× TS(1113): A 'default' clause cannot appear more than once in a 'switch' statement.
╭─[test262/test/language/statements/switch/S12.11_A2_T1.js:22:5]
21 │ result += 2;
22 │ ╭─▶ default:
23 │ │ result += 32;
24 │ ├─▶ break;
· ╰──── `default` has already been declared here
· ╰──── First 'default' clause is here.
25 │ ╭─▶ default:
26 │ │ result += 32;
27 │ ├─▶ break;
· ╰──── It can not be redeclared here
· ╰──── Another 'default' clause cannot appear here.
28 │ }
╰────

Expand Down
18 changes: 9 additions & 9 deletions tasks/coverage/snapshots/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11474,39 +11474,39 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
28 │ default: default:
╰────

× Identifier `default` has already been declared
× TS(1113): A 'default' clause cannot appear more than once in a 'switch' statement.
╭─[typescript/tests/cases/compiler/switchStatementsWithMultipleDefaults.ts:6:5]
5 │ case 2:
6 │ ╭─▶ default: // No issues.
7 │ ├─▶ break;
· ╰──── `default` has already been declared here
· ╰──── First 'default' clause is here.
8 │ default: // Error; second 'default' clause.
· ────┬───
· ╰── It can not be redeclared here
· ╰── Another 'default' clause cannot appear here.
9 │ default: // Error; third 'default' clause.
╰────

× Identifier `default` has already been declared
× TS(1113): A 'default' clause cannot appear more than once in a 'switch' statement.
╭─[typescript/tests/cases/compiler/switchStatementsWithMultipleDefaults.ts:19:13]
18 │ switch (x * x) {
19 │ default: // No issues.
· ────┬───
· ╰── `default` has already been declared here
· ╰── First 'default' clause is here.
20 │ ╭─▶ default: // Error; second 'default' clause.
21 │ ├─▶ break;
· ╰──── It can not be redeclared here
· ╰──── Another 'default' clause cannot appear here.
22 │ case 10000:
╰────

× Identifier `default` has already been declared
× TS(1113): A 'default' clause cannot appear more than once in a 'switch' statement.
╭─[typescript/tests/cases/compiler/switchStatementsWithMultipleDefaults1.ts:6:9]
5 │ case 2:
6 │ ╭─▶ default: // No issues.
7 │ ├─▶ break;
· ╰──── `default` has already been declared here
· ╰──── First 'default' clause is here.
8 │ default: // Error; second 'default' clause.
· ────┬───
· ╰── It can not be redeclared here
· ╰── Another 'default' clause cannot appear here.
9 │ default: // Error; third 'default' clause.
╰────

Expand Down
Loading