Skip to content

Commit

Permalink
fix(semantic): transform checker compare scope flags (#5052)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 22, 2024
1 parent 0ba6f50 commit 67d1a96
Show file tree
Hide file tree
Showing 5 changed files with 689 additions and 15 deletions.
45 changes: 30 additions & 15 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn check_semantic_after_transform(
rebuilt: data_rebuilt,
errors: Errors::default(),
};
checker.check_bindings();
checker.check_scopes();
checker.check_symbols();
checker.check_references();

Expand Down Expand Up @@ -180,16 +180,16 @@ impl Errors {
}

impl<'s> PostTransformChecker<'s> {
fn check_bindings(&mut self) {
fn check_scopes(&mut self) {
if self.after_transform.ids.scope_ids.len() != self.rebuilt.ids.scope_ids.len() {
self.errors.push("Scopes mismatch after transform");
return;
}

// Check whether bindings are the same for scopes in the same visitation order.
let PostTransformChecker { after_transform, rebuilt, .. } = self;
for (&scope_id_after_transform, &scope_id_rebuilt) in
self.after_transform.ids.scope_ids.iter().zip(self.rebuilt.ids.scope_ids.iter())
after_transform.ids.scope_ids.iter().zip(rebuilt.ids.scope_ids.iter())
{
// Check bindings are the same
fn get_sorted_bindings(scopes: &ScopeTree, scope_id: ScopeId) -> Vec<CompactStr> {
let mut bindings =
scopes.get_bindings(scope_id).keys().cloned().collect::<Vec<_>>();
Expand All @@ -201,12 +201,10 @@ impl<'s> PostTransformChecker<'s> {
match (scope_id_after_transform, scope_id_rebuilt) {
(None, None) => continue,
(Some(scope_id_after_transform), Some(scope_id_rebuilt)) => {
let bindings_after_transform = get_sorted_bindings(
self.after_transform.scopes,
scope_id_after_transform,
);
let bindings_after_transform =
get_sorted_bindings(after_transform.scopes, scope_id_after_transform);
let bindings_rebuilt =
get_sorted_bindings(self.rebuilt.scopes, scope_id_rebuilt);
get_sorted_bindings(rebuilt.scopes, scope_id_rebuilt);
if bindings_after_transform == bindings_rebuilt {
continue;
}
Expand All @@ -216,18 +214,16 @@ impl<'s> PostTransformChecker<'s> {
)
}
(Some(scope_id_after_transform), None) => {
let bindings_after_transform = get_sorted_bindings(
self.after_transform.scopes,
scope_id_after_transform,
);
let bindings_after_transform =
get_sorted_bindings(after_transform.scopes, scope_id_after_transform);
(
format!("{scope_id_after_transform:?}: {bindings_after_transform:?}"),
"No scope".to_string(),
)
}
(None, Some(scope_id_rebuilt)) => {
let bindings_rebuilt =
get_sorted_bindings(self.rebuilt.scopes, scope_id_rebuilt);
get_sorted_bindings(rebuilt.scopes, scope_id_rebuilt);
(
"No scope".to_string(),
format!("{scope_id_rebuilt:?}: {bindings_rebuilt:?}"),
Expand All @@ -242,6 +238,25 @@ after transform: {result_after_transform}
rebuilt : {result_rebuilt}
"
));

let (Some(scope_id_after_transform), Some(scope_id_rebuilt)) =
(scope_id_after_transform, scope_id_rebuilt)
else {
continue;
};

// Check flags match
let flags_after_transform = after_transform.scopes.get_flags(scope_id_after_transform);
let flags_rebuilt = rebuilt.scopes.get_flags(scope_id_rebuilt);
if flags_after_transform != flags_rebuilt {
self.errors.push(format!(
"
Scope flags mismatch:
after transform: {scope_id_after_transform:?}: {flags_after_transform:?}
rebuilt : {scope_id_rebuilt:?}: {flags_rebuilt:?}
"
));
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions tasks/coverage/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -387,26 +387,41 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["A", "B", "E"]
rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-reserved-words/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["E", "const", "default"]
rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-strings/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["E", "bar", "foo"]
rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-trailing-comma/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["A", "E"]
rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["A", "E"]
rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/estree-compat/shorthand-ambient-module/input.ts
semantic error: Bindings mismatch:
Expand Down
12 changes: 12 additions & 0 deletions tasks/coverage/semantic_misc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,27 @@ tasks/coverage/misc/pass/oxc-4449.ts
semantic error: Bindings mismatch:
after transform: ScopeId(5): ["E", "baz"]
rebuilt : ScopeId(5): ["E"]
Scope flags mismatch:
after transform: ScopeId(5): ScopeFlags(StrictMode)
rebuilt : ScopeId(5): ScopeFlags(StrictMode | Function)
Bindings mismatch:
after transform: ScopeId(6): ["F", "baz"]
rebuilt : ScopeId(6): ["F"]
Scope flags mismatch:
after transform: ScopeId(6): ScopeFlags(StrictMode)
rebuilt : ScopeId(6): ScopeFlags(StrictMode | Function)
Bindings mismatch:
after transform: ScopeId(7): ["G", "baz"]
rebuilt : ScopeId(7): ["G"]
Scope flags mismatch:
after transform: ScopeId(7): ScopeFlags(StrictMode)
rebuilt : ScopeId(7): ScopeFlags(StrictMode | Function)
Bindings mismatch:
after transform: ScopeId(8): ["H", "baz"]
rebuilt : ScopeId(8): ["H"]
Scope flags mismatch:
after transform: ScopeId(8): ScopeFlags(StrictMode)
rebuilt : ScopeId(8): ScopeFlags(StrictMode | Function)

tasks/coverage/misc/pass/swc-7187.ts
semantic error: Bindings mismatch:
Expand Down
Loading

0 comments on commit 67d1a96

Please sign in to comment.