Skip to content

Commit

Permalink
fix(semantic): transform checker check symbol flags (#5074)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 22, 2024
1 parent 78f135d commit 1b6b27a
Show file tree
Hide file tree
Showing 6 changed files with 1,053 additions and 35 deletions.
25 changes: 7 additions & 18 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,24 +422,6 @@ impl<'s> PostTransformChecker<'s> {
}

fn check_symbols(&mut self) {
// Check whether symbols are valid
for symbol_id in self.rebuilt.ids.symbol_ids.iter().copied() {
if self.rebuilt.symbols.get_flags(symbol_id).is_empty() {
let name = self.rebuilt.symbols.get_name(symbol_id);
self.errors
.push(format!("Expect non-empty SymbolFlags for BindingIdentifier({name})"));
if !self
.rebuilt
.scopes
.has_binding(self.rebuilt.symbols.get_scope_id(symbol_id), name)
{
self.errors.push(format!(
"Cannot find BindingIdentifier({name}) in the Scope corresponding to the Symbol"
));
}
}
}

if self.get_static_pair(|data| data.ids.symbol_ids.len()).is_mismatch() {
self.errors.push("Symbols mismatch after transform");
return;
Expand All @@ -455,11 +437,18 @@ impl<'s> PostTransformChecker<'s> {
.zip(self.rebuilt.ids.symbol_ids.iter().copied())
.map(Pair::from_tuple)
{
// Check names match
let symbol_names =
self.get_pair(symbol_ids, |data, symbol_id| data.symbols.names[symbol_id].clone());
if symbol_names.is_mismatch() {
self.errors.push_mismatch("Symbol mismatch", symbol_ids, symbol_names);
}

// Check flags match
let flags = self.get_pair(symbol_ids, |data, symbol_id| data.symbols.flags[symbol_id]);
if flags.is_mismatch() {
self.errors.push_mismatch("Symbol flags mismatch", symbol_ids, flags);
}
}
}

Expand Down
136 changes: 135 additions & 1 deletion tasks/coverage/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 12619ffe

semantic_babel Summary:
AST Parsed : 2101/2101 (100.00%)
Positive Passed: 1790/2101 (85.20%)
Positive Passed: 1786/2101 (85.01%)
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)]
Expand Down Expand Up @@ -510,6 +510,9 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/const/i
semantic error: Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(ConstEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/declare/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -539,11 +542,17 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/export/
semantic error: Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(Export | RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Export)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/export-const/input.ts
semantic error: Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(Export | ConstEnum)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Export)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/input.ts
semantic error: Bindings mismatch:
Expand All @@ -560,6 +569,9 @@ rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-reserved-words/input.ts
semantic error: Bindings mismatch:
Expand All @@ -568,6 +580,9 @@ rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-strings/input.ts
semantic error: Bindings mismatch:
Expand All @@ -576,6 +591,9 @@ rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-trailing-comma/input.ts
semantic error: Bindings mismatch:
Expand All @@ -584,6 +602,9 @@ rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/input.ts
semantic error: Bindings mismatch:
Expand All @@ -592,6 +613,9 @@ rebuilt : ScopeId(1): ["E"]
Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/estree-compat/shorthand-ambient-module/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -643,6 +667,9 @@ rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)]
Scope flags mismatch:
after transform: ScopeId(3): ScopeFlags(StrictMode)
rebuilt : ScopeId(3): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(Export | RegularEnum)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable | Export)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/nested-same-name/input.ts
semantic error: Semantic Collector failed after transform
Expand Down Expand Up @@ -1201,6 +1228,9 @@ rebuilt : ScopeId(0): [ScopeId(1)]
Scope flags mismatch:
after transform: ScopeId(2): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(1): SymbolFlags(Export | RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Export)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/regression/issue-7742/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -1270,6 +1300,9 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/enum-b
semantic error: Scope flags mismatch:
after transform: ScopeId(2): ScopeFlags(StrictMode)
rebuilt : ScopeId(2): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/input.ts
semantic error: Scope children mismatch:
Expand All @@ -1285,11 +1318,17 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export
semantic error: Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(Export | RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable | Export)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/input.ts
semantic error: Scope flags mismatch:
after transform: ScopeId(1): ScopeFlags(StrictMode)
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(Export | RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable | Export)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/export-func-in-declare-module/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -1372,11 +1411,17 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redecl
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(Class | Interface)
rebuilt : SymbolId(0): SymbolFlags(Class)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-const-type/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | ConstVariable | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | ConstVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/input.ts
semantic error: Scope flags mismatch:
Expand All @@ -1385,6 +1430,9 @@ rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Scope flags mismatch:
after transform: ScopeId(2): ScopeFlags(StrictMode)
rebuilt : ScopeId(2): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(ConstEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/input.ts
semantic error: Scope flags mismatch:
Expand All @@ -1393,16 +1441,25 @@ rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
Scope flags mismatch:
after transform: ScopeId(2): ScopeFlags(StrictMode)
rebuilt : ScopeId(2): ScopeFlags(StrictMode | Function)
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(RegularEnum)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | Function | Interface)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | Function | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-ambient-class/input.ts
semantic error: Bindings mismatch:
Expand All @@ -1416,6 +1473,28 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redecl
semantic error: Semantic Collector failed after transform
Missing SymbolId: a

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-let/input.ts
semantic error: Symbol flags mismatch:
after transform: SymbolId(1): SymbolFlags(BlockScopedVariable | Import)
rebuilt : SymbolId(1): SymbolFlags(BlockScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(BlockScopedVariable | Import)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(BlockScopedVariable | Import)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-type-let/input.ts
semantic error: Symbol flags mismatch:
after transform: SymbolId(1): SymbolFlags(BlockScopedVariable | TypeImport)
rebuilt : SymbolId(1): SymbolFlags(BlockScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(BlockScopedVariable | TypeImport)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(BlockScopedVariable | TypeImport)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-type-type/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["Global", "a"]
Expand All @@ -1424,6 +1503,28 @@ Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-type-var/input.ts
semantic error: Symbol flags mismatch:
after transform: SymbolId(1): SymbolFlags(FunctionScopedVariable | TypeImport)
rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable | TypeImport)
rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable | TypeImport)
rebuilt : SymbolId(3): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-var/input.ts
semantic error: Symbol flags mismatch:
after transform: SymbolId(1): SymbolFlags(FunctionScopedVariable | Import)
rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable | Import)
rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable)
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable | Import)
rebuilt : SymbolId(3): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-different-module/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["T", "test", "test/submodule"]
Expand Down Expand Up @@ -1452,11 +1553,17 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redecl
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(Class | Interface)
rebuilt : SymbolId(0): SymbolFlags(Class)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | Function | Interface)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/input.ts
semantic error: Bindings mismatch:
Expand All @@ -1470,46 +1577,73 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redecl
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | Interface)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(FunctionScopedVariable | Interface)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | Interface)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-type/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
rebuilt : ScopeId(0): [ScopeId(1)]
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | Function | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(BlockScopedVariable | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(BlockScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(FunctionScopedVariable | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(FunctionScopedVariable | Interface)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-type/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []
Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(FunctionScopedVariable | TypeAlias)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/input.ts
semantic error: Bindings mismatch:
Expand Down
Loading

0 comments on commit 1b6b27a

Please sign in to comment.