From 05fff16d559e9913e0465702f4682a24e20cc199 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 22 Aug 2024 02:34:44 +0000 Subject: [PATCH] fix(semantic): transform checker compare binding symbol IDs (#5057) --- .../src/post_transform_checker.rs | 32 +++++++++++++++++-- tasks/coverage/semantic_babel.snap | 12 ++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/oxc_semantic/src/post_transform_checker.rs b/crates/oxc_semantic/src/post_transform_checker.rs index 130ee5e43a84c..59e6e82c67b80 100644 --- a/crates/oxc_semantic/src/post_transform_checker.rs +++ b/crates/oxc_semantic/src/post_transform_checker.rs @@ -328,9 +328,35 @@ impl<'s> PostTransformChecker<'s> { (None, None) => continue, (Some(scope_id_after_transform), Some(scope_id_rebuilt)) => { let scope_ids = Pair::new(scope_id_after_transform, scope_id_rebuilt); - let bindings = self.get_pair(scope_ids, get_sorted_bindings); - if bindings.is_mismatch() { - self.errors.push_mismatch("Bindings mismatch", scope_ids, bindings); + let binding_names = self.get_pair(scope_ids, get_sorted_bindings); + if binding_names.is_mismatch() { + self.errors.push_mismatch("Bindings mismatch", scope_ids, binding_names); + } else { + let symbol_ids = self.get_pair(scope_ids, |data, scope_id| { + data.scopes.get_bindings(scope_id).values().copied().collect::>() + }); + + let mut symbol_ids_after_transform = symbol_ids + .after_transform + .iter() + .map(|symbol_id| self.symbol_ids_map.get(symbol_id).copied()) + .collect::>(); + symbol_ids_after_transform.sort_unstable(); + let mut symbol_ids_rebuilt = symbol_ids + .rebuilt + .iter() + .copied() + .map(Option::Some) + .collect::>(); + symbol_ids_rebuilt.sort_unstable(); + + if symbol_ids_after_transform != symbol_ids_rebuilt { + self.errors.push_mismatch( + "Binding symbols mismatch", + scope_ids, + symbol_ids, + ); + } } scope_ids } diff --git a/tasks/coverage/semantic_babel.snap b/tasks/coverage/semantic_babel.snap index e4df10282ee75..b8865bfdd7d7c 100644 --- a/tasks/coverage/semantic_babel.snap +++ b/tasks/coverage/semantic_babel.snap @@ -2,7 +2,7 @@ commit: 12619ffe semantic_babel Summary: AST Parsed : 2101/2101 (100.00%) -Positive Passed: 1792/2101 (85.29%) +Positive Passed: 1790/2101 (85.20%) tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.3-function-in-if-body/input.js semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1), ScopeId(2)] @@ -27,6 +27,11 @@ semantic error: Scope children mismatch: after transform: ScopeId(3): [ScopeId(4)] rebuilt : ScopeId(3): [ScopeId(4)] +tasks/coverage/babel/packages/babel-parser/test/fixtures/core/uncategorised/230/input.js +semantic error: Binding symbols mismatch: +after transform: ScopeId(0): [SymbolId(0)] +rebuilt : ScopeId(0): [SymbolId(0)] + tasks/coverage/babel/packages/babel-parser/test/fixtures/core/uncategorised/327/input.js semantic error: A 'return' statement can only be used within a function body. @@ -133,6 +138,11 @@ semantic error: Bindings mismatch: after transform: ScopeId(0): ["nil"] rebuilt : ScopeId(0): [] +tasks/coverage/babel/packages/babel-parser/test/fixtures/esprima/statement-if/migrated_0002/input.js +semantic error: Binding symbols mismatch: +after transform: ScopeId(0): [SymbolId(0)] +rebuilt : ScopeId(0): [SymbolId(0)] + tasks/coverage/babel/packages/babel-parser/test/fixtures/esprima/statement-if/migrated_0003/input.js semantic error: Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)]