Skip to content

Commit

Permalink
fix(semantic): fix error message for duplicated label
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 22, 2024
1 parent efbdced commit bb92b29
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
9 changes: 8 additions & 1 deletion crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,20 @@ pub fn check_continue_statement<'a>(
}
}

fn label_redeclaration(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Label `{x0}` has already been declared")).with_labels([
span1.label(format!("`{x0}` has already been declared here")),
span2.label("It can not be redeclared here"),
])
}

#[allow(clippy::option_if_let_else)]
pub fn check_labeled_statement(ctx: &SemanticBuilder) {
ctx.label_builder.labels.iter().for_each(|labels| {
let mut defined = FxHashMap::default();
for labeled in labels {
if let Some(span) = defined.get(labeled.name) {
ctx.error(redeclaration(labeled.name, *span, labeled.span));
ctx.error(label_redeclaration(labeled.name, *span, labeled.span));
} else {
defined.insert(labeled.name, labeled.span);
}
Expand Down
6 changes: 3 additions & 3 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: A `continue` statement can only be used within an enclosing `for`, `while` or `do while`

× Identifier `x` has already been declared
× Label `x` has already been declared
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/465/input.js:1:1]
1 │ x: while (true) { x: while (true) { } }
· ┬ ┬
Expand Down Expand Up @@ -9131,7 +9131,7 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: A `continue` statement can only be used within an enclosing `for`, `while` or `do while`

× Identifier `x` has already been declared
× Label `x` has already been declared
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0182/input.js:1:1]
1 │ x: while (true) { x: while (true) { } }
· ┬ ┬
Expand Down Expand Up @@ -9540,7 +9540,7 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ───────
╰────

× Identifier `__proto__` has already been declared
× Label `__proto__` has already been declared
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0248/input.js:1:1]
1 │ __proto__: __proto__: 42;
· ────┬──── ────┬────
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/parser_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21564,7 +21564,7 @@ Negative Passed: 4220/4220 (100.00%)
· ╰── It cannot be redeclared here
╰────

× Identifier `label` has already been declared
× Label `label` has already been declared
╭─[test262/test/language/module-code/early-dup-lables.js:16:1]
15 │
16 │ label: {
Expand Down Expand Up @@ -30302,7 +30302,7 @@ Negative Passed: 4220/4220 (100.00%)
31 │ }
╰────

× Identifier `x` has already been declared
× Label `x` has already been declared
╭─[test262/test/language/statements/class/static-init-invalid-label-dup.js:21:5]
20 │ static {
21 │ x: x: 0;
Expand Down
8 changes: 4 additions & 4 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7269,7 +7269,7 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro
20 │ return 0;
╰────

× Identifier `target` has already been declared
× Label `target` has already been declared
╭─[typescript/tests/cases/compiler/duplicateLabel1.ts:1:1]
1 │ target:
· ───┬──
Expand All @@ -7280,7 +7280,7 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro
3 │ while (true) {
╰────

× Identifier `target` has already been declared
× Label `target` has already been declared
╭─[typescript/tests/cases/compiler/duplicateLabel2.ts:1:1]
1 │ target:
· ───┬──
Expand Down Expand Up @@ -20674,7 +20674,7 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro
3 │ }
╰────

× Identifier `target` has already been declared
× Label `target` has already been declared
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Statements/LabeledStatements/parser_duplicateLabel1.ts:1:1]
1 │ target:
· ───┬──
Expand All @@ -20685,7 +20685,7 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro
3 │ while (true) {
╰────

× Identifier `target` has already been declared
× Label `target` has already been declared
╭─[typescript/tests/cases/conformance/parser/ecmascript5/Statements/LabeledStatements/parser_duplicateLabel2.ts:1:1]
1 │ target:
· ───┬──
Expand Down

0 comments on commit bb92b29

Please sign in to comment.