Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(semantic): transform checker check symbol references #5088

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ impl<'s> PostTransformChecker<'s> {
if self.remap_scope_ids(scope_ids).is_mismatch() {
self.errors.push_mismatch("Symbol scope ID mismatch", symbol_ids, scope_ids);
}

// NB: Skip checking declarations match - transformer does not set `AstNodeId`s

// Check resolved references match
let reference_ids = self.get_pair(symbol_ids, |data, symbol_id| {
data.symbols.resolved_references[symbol_id].clone()
});
if self.remap_reference_ids_sets(&reference_ids).is_mismatch() {
self.errors.push_mismatch(
"Symbol reference IDs mismatch",
symbol_ids,
reference_ids,
);
}
}
}

Expand Down Expand Up @@ -552,6 +566,27 @@ impl<'s> PostTransformChecker<'s> {

Pair::new(after_transform, rebuilt)
}

/// Remap pair of arrays of `ReferenceId`s.
/// Map `after_transform` IDs to `rebuilt` IDs.
/// Sort both sets.
fn remap_reference_ids_sets(
&self,
reference_ids: &Pair<Vec<ReferenceId>>,
) -> Pair<Vec<Option<ReferenceId>>> {
let mut after_transform = reference_ids
.after_transform
.iter()
.map(|&reference_id| self.reference_ids_map.get(reference_id))
.collect::<Vec<_>>();
let mut rebuilt =
reference_ids.rebuilt.iter().copied().map(Option::Some).collect::<Vec<_>>();

after_transform.sort_unstable();
rebuilt.sort_unstable();

Pair::new(after_transform, rebuilt)
}
}

/// Collection of `ScopeId`s, `SymbolId`s and `ReferenceId`s from an AST.
Expand Down
63 changes: 62 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: 1786/2101 (85.01%)
Positive Passed: 1784/2101 (84.91%)
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 @@ -196,6 +196,11 @@ semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts
semantic error: Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1)]
rebuilt : SymbolId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/input.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1)]
Expand All @@ -210,11 +215,23 @@ tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assign/TSTyp
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["M"]
rebuilt : ScopeId(1): []
Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(3)]
rebuilt : SymbolId(0): [ReferenceId(0)]
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(4), ReferenceId(6)]
rebuilt : SymbolId(1): [ReferenceId(1)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(1): ["M"]
rebuilt : ScopeId(1): []
Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(3)]
rebuilt : SymbolId(0): [ReferenceId(0)]
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(4), ReferenceId(6)]
rebuilt : SymbolId(1): [ReferenceId(1)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/abstract/input.ts
semantic error: Scope children mismatch:
Expand Down Expand Up @@ -1480,18 +1497,27 @@ rebuilt : SymbolId(1): SymbolFlags(BlockScopedVariable)
Symbol span mismatch:
after transform: SymbolId(1): Span { start: 16, end: 23 }
rebuilt : SymbolId(1): Span { start: 104, end: 124 }
Symbol reference IDs mismatch:
after transform: SymbolId(1): [ReferenceId(0)]
rebuilt : SymbolId(1): []
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(BlockScopedVariable | Import)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable)
Symbol span mismatch:
after transform: SymbolId(2): Span { start: 49, end: 50 }
rebuilt : SymbolId(2): Span { start: 156, end: 160 }
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(2)]
rebuilt : SymbolId(2): []
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(BlockScopedVariable | Import)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable)
Symbol span mismatch:
after transform: SymbolId(3): Span { start: 81, end: 82 }
rebuilt : SymbolId(3): Span { start: 170, end: 174 }
Symbol reference IDs mismatch:
after transform: SymbolId(3): [ReferenceId(3)]
rebuilt : SymbolId(3): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-type-let/input.ts
semantic error: Symbol flags mismatch:
Expand All @@ -1500,18 +1526,27 @@ rebuilt : SymbolId(1): SymbolFlags(BlockScopedVariable)
Symbol span mismatch:
after transform: SymbolId(1): Span { start: 21, end: 28 }
rebuilt : SymbolId(1): Span { start: 119, end: 139 }
Symbol reference IDs mismatch:
after transform: SymbolId(1): [ReferenceId(0)]
rebuilt : SymbolId(1): []
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(BlockScopedVariable | TypeImport)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable)
Symbol span mismatch:
after transform: SymbolId(2): Span { start: 59, end: 60 }
rebuilt : SymbolId(2): Span { start: 171, end: 175 }
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(2)]
rebuilt : SymbolId(2): []
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(BlockScopedVariable | TypeImport)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable)
Symbol span mismatch:
after transform: SymbolId(3): Span { start: 96, end: 97 }
rebuilt : SymbolId(3): Span { start: 185, end: 189 }
Symbol reference IDs mismatch:
after transform: SymbolId(3): [ReferenceId(3)]
rebuilt : SymbolId(3): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-type-type/input.ts
semantic error: Bindings mismatch:
Expand All @@ -1528,18 +1563,27 @@ rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)
Symbol span mismatch:
after transform: SymbolId(1): Span { start: 21, end: 28 }
rebuilt : SymbolId(1): Span { start: 119, end: 139 }
Symbol reference IDs mismatch:
after transform: SymbolId(1): [ReferenceId(0)]
rebuilt : SymbolId(1): []
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable | TypeImport)
rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable)
Symbol span mismatch:
after transform: SymbolId(2): Span { start: 59, end: 60 }
rebuilt : SymbolId(2): Span { start: 171, end: 175 }
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(2)]
rebuilt : SymbolId(2): []
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable | TypeImport)
rebuilt : SymbolId(3): SymbolFlags(FunctionScopedVariable)
Symbol span mismatch:
after transform: SymbolId(3): Span { start: 96, end: 97 }
rebuilt : SymbolId(3): Span { start: 185, end: 189 }
Symbol reference IDs mismatch:
after transform: SymbolId(3): [ReferenceId(3)]
rebuilt : SymbolId(3): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-import-var/input.ts
semantic error: Symbol flags mismatch:
Expand All @@ -1548,18 +1592,27 @@ rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)
Symbol span mismatch:
after transform: SymbolId(1): Span { start: 16, end: 23 }
rebuilt : SymbolId(1): Span { start: 104, end: 124 }
Symbol reference IDs mismatch:
after transform: SymbolId(1): [ReferenceId(0)]
rebuilt : SymbolId(1): []
Symbol flags mismatch:
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable | Import)
rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable)
Symbol span mismatch:
after transform: SymbolId(2): Span { start: 49, end: 50 }
rebuilt : SymbolId(2): Span { start: 156, end: 160 }
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(2)]
rebuilt : SymbolId(2): []
Symbol flags mismatch:
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable | Import)
rebuilt : SymbolId(3): SymbolFlags(FunctionScopedVariable)
Symbol span mismatch:
after transform: SymbolId(3): Span { start: 81, end: 82 }
rebuilt : SymbolId(3): Span { start: 170, end: 174 }
Symbol reference IDs mismatch:
after transform: SymbolId(3): [ReferenceId(3)]
rebuilt : SymbolId(3): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-different-module/input.ts
semantic error: Bindings mismatch:
Expand All @@ -1576,6 +1629,9 @@ rebuilt : ScopeId(0): ["foo", "fooBar"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3)]
rebuilt : ScopeId(0): []
Symbol reference IDs mismatch:
after transform: SymbolId(1): [ReferenceId(1), ReferenceId(4)]
rebuilt : SymbolId(0): [ReferenceId(0)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-in-nested-module/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -1712,6 +1768,11 @@ semantic error: Bindings mismatch:
after transform: ScopeId(1): ["T", "input"]
rebuilt : ScopeId(1): ["input"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/tsx/brace-is-block/input.tsx
semantic error: Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(2), ReferenceId(3)]
rebuilt : SymbolId(2): [ReferenceId(2)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/tsx/type-arguments/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["A"]
Expand Down
42 changes: 42 additions & 0 deletions tasks/coverage/semantic_misc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@ tasks/coverage/misc/pass/oxc-3948-1.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["BrowserWorkingCopyBackupTracker", "CancellationToken", "DisposableStore", "EditorPart", "EditorService", "IEditorGroupsService", "IEditorService", "IFilesConfigurationService", "IInstantiationService", "ILifecycleService", "ILogService", "IUntitledTextResourceEditorInput", "IWorkingCopyBackup", "IWorkingCopyBackupService", "IWorkingCopyEditorHandler", "IWorkingCopyEditorService", "IWorkingCopyService", "InMemoryTestWorkingCopyBackupService", "LifecyclePhase", "Schemas", "TestServiceAccessor", "TestWorkingCopy", "URI", "UntitledTextEditorInput", "VSBuffer", "assert", "bufferToReadable", "createEditorPart", "ensureNoDisposablesAreLeakedInTestSuite", "isWindows", "registerTestResourceEditor", "timeout", "toResource", "toTypedWorkingCopyId", "toUntypedWorkingCopyId", "workbenchInstantiationService", "workbenchTeardown"]
rebuilt : ScopeId(0): ["BrowserWorkingCopyBackupTracker", "DisposableStore", "EditorService", "IEditorGroupsService", "IEditorService", "IFilesConfigurationService", "ILifecycleService", "ILogService", "IWorkingCopyBackupService", "IWorkingCopyEditorService", "IWorkingCopyService", "InMemoryTestWorkingCopyBackupService", "LifecyclePhase", "Schemas", "TestServiceAccessor", "TestWorkingCopy", "URI", "UntitledTextEditorInput", "VSBuffer", "assert", "bufferToReadable", "createEditorPart", "ensureNoDisposablesAreLeakedInTestSuite", "isWindows", "registerTestResourceEditor", "timeout", "toResource", "toTypedWorkingCopyId", "toUntypedWorkingCopyId", "workbenchInstantiationService", "workbenchTeardown"]
Symbol reference IDs mismatch:
after transform: SymbolId(1): [ReferenceId(109), ReferenceId(117), ReferenceId(156), ReferenceId(158), ReferenceId(160), ReferenceId(162)]
rebuilt : SymbolId(1): [ReferenceId(127), ReferenceId(129), ReferenceId(131), ReferenceId(133)]
Symbol reference IDs mismatch:
after transform: SymbolId(2): [ReferenceId(23), ReferenceId(24), ReferenceId(67), ReferenceId(184)]
rebuilt : SymbolId(2): [ReferenceId(16), ReferenceId(49), ReferenceId(154)]
Symbol reference IDs mismatch:
after transform: SymbolId(4): [ReferenceId(25), ReferenceId(26), ReferenceId(57), ReferenceId(176)]
rebuilt : SymbolId(3): [ReferenceId(17), ReferenceId(40), ReferenceId(147)]
Symbol reference IDs mismatch:
after transform: SymbolId(5): [ReferenceId(61), ReferenceId(64), ReferenceId(178), ReferenceId(181)]
rebuilt : SymbolId(4): [ReferenceId(46), ReferenceId(151)]
Symbol reference IDs mismatch:
after transform: SymbolId(7): [ReferenceId(11), ReferenceId(12), ReferenceId(51), ReferenceId(170)]
rebuilt : SymbolId(5): [ReferenceId(10), ReferenceId(34), ReferenceId(141)]
Symbol reference IDs mismatch:
after transform: SymbolId(10): [ReferenceId(13), ReferenceId(14)]
rebuilt : SymbolId(8): [ReferenceId(11)]
Symbol reference IDs mismatch:
after transform: SymbolId(11): [ReferenceId(15), ReferenceId(16)]
rebuilt : SymbolId(9): [ReferenceId(12)]
Symbol reference IDs mismatch:
after transform: SymbolId(13): [ReferenceId(19), ReferenceId(20)]
rebuilt : SymbolId(10): [ReferenceId(14)]
Symbol reference IDs mismatch:
after transform: SymbolId(14): [ReferenceId(17), ReferenceId(18)]
rebuilt : SymbolId(11): [ReferenceId(13)]
Symbol reference IDs mismatch:
after transform: SymbolId(17): [ReferenceId(38), ReferenceId(87)]
rebuilt : SymbolId(13): [ReferenceId(27)]
Symbol reference IDs mismatch:
after transform: SymbolId(19): [ReferenceId(43), ReferenceId(46), ReferenceId(165)]
rebuilt : SymbolId(15): [ReferenceId(29), ReferenceId(136)]
Symbol reference IDs mismatch:
after transform: SymbolId(21): [ReferenceId(1), ReferenceId(40), ReferenceId(71), ReferenceId(155), ReferenceId(188)]
rebuilt : SymbolId(17): [ReferenceId(53), ReferenceId(158)]
Symbol reference IDs mismatch:
after transform: SymbolId(32): [ReferenceId(21), ReferenceId(22)]
rebuilt : SymbolId(26): [ReferenceId(15)]
Symbol reference IDs mismatch:
after transform: SymbolId(39): [ReferenceId(42), ReferenceId(74), ReferenceId(154), ReferenceId(215)]
rebuilt : SymbolId(33): [ReferenceId(56), ReferenceId(185)]

tasks/coverage/misc/pass/oxc-4449.ts
semantic error: Scope flags mismatch:
Expand Down
Loading