diff --git a/crates/oxc_semantic/src/checker/javascript.rs b/crates/oxc_semantic/src/checker/javascript.rs index da81f41923c9c..7df5ff04a2fda 100644 --- a/crates/oxc_semantic/src/checker/javascript.rs +++ b/crates/oxc_semantic/src/checker/javascript.rs @@ -649,7 +649,18 @@ fn label_redeclaration(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic { pub fn check_labeled_statement(ctx: &SemanticBuilder) { ctx.label_builder.labels.iter().for_each(|labels| { let mut defined = FxHashMap::default(); + //only need to care about the monotone increasing depth of the array + let mut increase_depth = vec![]; for labeled in labels { + increase_depth.push(labeled); + // have to traverse because HashMap can only delete one by one + while increase_depth.len() > 2 + && increase_depth.iter().rev().nth(1).unwrap().depth + >= increase_depth.iter().next_back().unwrap().depth + { + defined.remove(increase_depth[increase_depth.len() - 2].name); + increase_depth.remove(increase_depth.len() - 2); + } if let Some(span) = defined.get(labeled.name) { ctx.error(label_redeclaration(labeled.name, *span, labeled.span)); } else { diff --git a/crates/oxc_semantic/src/label.rs b/crates/oxc_semantic/src/label.rs index fb74da22f6ab3..822a2b6a80b9d 100644 --- a/crates/oxc_semantic/src/label.rs +++ b/crates/oxc_semantic/src/label.rs @@ -11,7 +11,7 @@ pub struct Label<'a> { pub span: Span, used: bool, /// depth is the number of nested labeled statements - depth: usize, + pub depth: usize, /// is accessible means that the label is accessible from the current position is_accessible: bool, /// is_inside_function_or_static_block means that the label is inside a function or static block diff --git a/tasks/coverage/codegen_misc.snap b/tasks/coverage/codegen_misc.snap index 6f1ab4f301cad..f560e60a4b440 100644 --- a/tasks/coverage/codegen_misc.snap +++ b/tasks/coverage/codegen_misc.snap @@ -1,3 +1,3 @@ codegen_misc Summary: -AST Parsed : 25/25 (100.00%) -Positive Passed: 25/25 (100.00%) +AST Parsed : 26/26 (100.00%) +Positive Passed: 26/26 (100.00%) diff --git a/tasks/coverage/misc/fail/oxc-5036.js b/tasks/coverage/misc/fail/oxc-5036.js new file mode 100644 index 0000000000000..483d9ad196bc9 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-5036.js @@ -0,0 +1,3 @@ +Test: { + Test: console.log('Test'); +} \ No newline at end of file diff --git a/tasks/coverage/misc/pass/oxc-5036.js b/tasks/coverage/misc/pass/oxc-5036.js new file mode 100644 index 0000000000000..a2ca864a89ef0 --- /dev/null +++ b/tasks/coverage/misc/pass/oxc-5036.js @@ -0,0 +1,41 @@ +Parent: { + Child1: { + Child2: console.log("Child1"); + Child2: console.log("Child2"); + } + Child1: { + Child2: console.log("Child3"); + Child2: console.log("Child4"); + } + Child2: { + Child1: console.log("Child5"); + Child1: console.log("Child6"); + } +} + +Parent: { + Child1: { + Child2: console.log("Child7"); + Child2: console.log("Child8"); + } +} + +Parent: { + Child1: { + Child12: { + Child3: { + Child4: { + Child5: console.log("Child9"); + } + } + } + } + Child1: console.log("Child10"); + Child2: console.log("Child11"); + Child1: { + Child2: { + Child3: console.log("Child12"); + } + } + Child3: console.log("Child13"); +} diff --git a/tasks/coverage/parser_misc.snap b/tasks/coverage/parser_misc.snap index 13d3adfb1e70a..cd01bf1e4823c 100644 --- a/tasks/coverage/parser_misc.snap +++ b/tasks/coverage/parser_misc.snap @@ -1,7 +1,7 @@ parser_misc Summary: -AST Parsed : 25/25 (100.00%) -Positive Passed: 25/25 (100.00%) -Negative Passed: 14/14 (100.00%) +AST Parsed : 26/26 (100.00%) +Positive Passed: 26/26 (100.00%) +Negative Passed: 15/15 (100.00%) × Unexpected token ╭─[misc/fail/oxc-169.js:2:1] @@ -219,6 +219,17 @@ Negative Passed: 14/14 (100.00%) · ───────────── ╰──── + × Label `Test` has already been declared + ╭─[misc/fail/oxc-5036.js:1:1] + 1 │ Test: { + · ──┬─ + · ╰── `Test` has already been declared here + 2 │ Test: console.log('Test'); + · ──┬─ + · ╰── It can not be redeclared here + 3 │ } + ╰──── + × The keyword 'let' is reserved ╭─[misc/fail/oxc.js:1:1] 1 │ let.a = 1; diff --git a/tasks/coverage/semantic_misc.snap b/tasks/coverage/semantic_misc.snap index bf90e26b52f92..ba72a88e04984 100644 --- a/tasks/coverage/semantic_misc.snap +++ b/tasks/coverage/semantic_misc.snap @@ -1,6 +1,6 @@ semantic_misc Summary: -AST Parsed : 25/25 (100.00%) -Positive Passed: 16/25 (64.00%) +AST Parsed : 26/26 (100.00%) +Positive Passed: 17/26 (65.38%) tasks/coverage/misc/pass/oxc-1288.ts semantic error: Bindings mismatch: after transform: ScopeId(0): ["from"] diff --git a/tasks/coverage/transformer_misc.snap b/tasks/coverage/transformer_misc.snap index 30570bb25636a..f605ae970bcb9 100644 --- a/tasks/coverage/transformer_misc.snap +++ b/tasks/coverage/transformer_misc.snap @@ -1,3 +1,3 @@ transformer_misc Summary: -AST Parsed : 25/25 (100.00%) -Positive Passed: 25/25 (100.00%) +AST Parsed : 26/26 (100.00%) +Positive Passed: 26/26 (100.00%)