Skip to content

Commit

Permalink
fix(semantic): incorrect semantic check for label has same name (#5041)
Browse files Browse the repository at this point in the history
fix: #5036

---------

Co-authored-by: Boshen <boshenc@gmail.com>
  • Loading branch information
heygsc and Boshen authored Aug 23, 2024
1 parent d5de97d commit ad2be97
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 10 deletions.
11 changes: 11 additions & 0 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/codegen_misc.snap
Original file line number Diff line number Diff line change
@@ -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%)
3 changes: 3 additions & 0 deletions tasks/coverage/misc/fail/oxc-5036.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test: {
Test: console.log('Test');
}
41 changes: 41 additions & 0 deletions tasks/coverage/misc/pass/oxc-5036.js
Original file line number Diff line number Diff line change
@@ -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");
}
17 changes: 14 additions & 3 deletions tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -219,6 +219,17 @@ Negative Passed: 14/14 (100.00%)
· ─────────────
╰────

× Label `Test` has already been declared
╭─[misc/fail/oxc-5036.js:1:1]
1Test: {
· ──┬─
· ╰── `Test` has already been declared here
2Test: console.log('Test');
· ──┬─
· ╰── It can not be redeclared here
3 │ }
╰────

× The keyword 'let' is reserved
╭─[misc/fail/oxc.js:1:1]
1let.a = 1;
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/semantic_misc.snap
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/transformer_misc.snap
Original file line number Diff line number Diff line change
@@ -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%)

0 comments on commit ad2be97

Please sign in to comment.