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 IDs #5078

Merged
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
20 changes: 17 additions & 3 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,8 @@ impl<'s> PostTransformChecker<'s> {
self.get_pair(scope_ids, |data, scope_id| data.scopes.get_parent_id(scope_id));
let is_match = match parent_ids.into_parts() {
(Some(parent_id_after_transform), Some(parent_id_rebuilt)) => {
let parent_id_after_transform =
self.scope_ids_map.get(&parent_id_after_transform);
parent_id_after_transform == Some(&parent_id_rebuilt)
let parent_ids = Pair::new(parent_id_after_transform, parent_id_rebuilt);
self.remap_scope_ids(parent_ids).is_match()
}
(None, None) => true,
_ => false,
Expand Down Expand Up @@ -455,6 +454,13 @@ impl<'s> PostTransformChecker<'s> {
if spans.is_mismatch() {
self.errors.push_mismatch("Symbol span mismatch", symbol_ids, spans);
}

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

Expand Down Expand Up @@ -512,6 +518,14 @@ impl<'s> PostTransformChecker<'s> {
fn get_static_pair<R, F: Fn(&SemanticData) -> R>(&self, getter: F) -> Pair<R> {
Pair::new(getter(&self.after_transform), getter(&self.rebuilt))
}

/// Map `after_transform` scope ID to `rebuilt` scope ID
fn remap_scope_ids(&self, scope_ids: Pair<ScopeId>) -> Pair<Option<ScopeId>> {
Pair::new(
self.scope_ids_map.get(&scope_ids.after_transform).copied(),
Some(scope_ids.rebuilt),
)
}
}

/// Collection of `ScopeId`s, `SymbolId`s and `ReferenceId`s from an AST.
Expand Down
40 changes: 40 additions & 0 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,10 @@ preset-env: unknown field `shippedProposals`, expected `targets` or `bugfixes`
| rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable |
| CatchVariable)

x Symbol scope ID mismatch:
| after transform: SymbolId(0): ScopeId(0)
| rebuilt : SymbolId(0): ScopeId(3)


* optional-catch-bindings/try-catch-finally-no-binding/input.js
x Bindings mismatch:
Expand Down Expand Up @@ -1358,6 +1362,10 @@ preset-env: unknown field `shippedProposals`, expected `targets` or `bugfixes`
| rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable |
| CatchVariable)

x Symbol scope ID mismatch:
| after transform: SymbolId(0): ScopeId(0)
| rebuilt : SymbolId(0): ScopeId(3)



# babel-plugin-transform-exponentiation-operator (3/4)
Expand All @@ -1383,11 +1391,19 @@ preset-env: unknown field `shippedProposals`, expected `targets` or `bugfixes`
| after transform: ScopeId(6): ["_this2"]
| rebuilt : ScopeId(6): []

x Symbol scope ID mismatch:
| after transform: SymbolId(6): ScopeId(6)
| rebuilt : SymbolId(1): ScopeId(1)

x Symbol flags mismatch:
| after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable |
| ArrowFunction)
| rebuilt : SymbolId(3): SymbolFlags(FunctionScopedVariable)

x Symbol scope ID mismatch:
| after transform: SymbolId(5): ScopeId(5)
| rebuilt : SymbolId(6): ScopeId(4)


* assumption-newableArrowFunctions-false/naming/input.js
x Symbol flags mismatch:
Expand Down Expand Up @@ -4978,6 +4994,14 @@ transform-typescript: unknown field `optimizeConstEnums`, expected one of `jsxPr
| after transform: ScopeId(4): ["_this2"]
| rebuilt : ScopeId(4): []

x Symbol scope ID mismatch:
| after transform: SymbolId(2): ScopeId(2)
| rebuilt : SymbolId(1): ScopeId(1)

x Symbol scope ID mismatch:
| after transform: SymbolId(3): ScopeId(4)
| rebuilt : SymbolId(3): ScopeId(3)


* react/should-disallow-valueless-key/input.js
! Please provide an explicit key value. Using "key" as a shorthand for
Expand Down Expand Up @@ -5026,6 +5050,14 @@ transform-typescript: unknown field `optimizeConstEnums`, expected one of `jsxPr
| after transform: ScopeId(4): ["_this2"]
| rebuilt : ScopeId(4): []

x Symbol scope ID mismatch:
| after transform: SymbolId(3): ScopeId(2)
| rebuilt : SymbolId(2): ScopeId(1)

x Symbol scope ID mismatch:
| after transform: SymbolId(4): ScopeId(4)
| rebuilt : SymbolId(4): ScopeId(3)


* react-automatic/does-not-add-source-self-automatic/input.mjs
transform-react-jsx: unknown field `autoImport`, expected one of `runtime`, `development`, `throwIfNamespace`, `pure`, `importSource`, `pragma`, `pragmaFrag`, `useBuiltIns`, `useSpread`, `refresh`
Expand Down Expand Up @@ -5079,6 +5111,14 @@ transform-react-jsx: unknown field `autoImport`, expected one of `runtime`, `dev
| after transform: ScopeId(3): ["_this2"]
| rebuilt : ScopeId(3): []

x Symbol scope ID mismatch:
| after transform: SymbolId(1): ScopeId(1)
| rebuilt : SymbolId(0): ScopeId(0)

x Symbol scope ID mismatch:
| after transform: SymbolId(2): ScopeId(3)
| rebuilt : SymbolId(2): ScopeId(2)



# babel-plugin-transform-react-jsx-development (7/10)
Expand Down
8 changes: 8 additions & 0 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Passed: 8/35
| rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable |
| CatchVariable)

x Symbol scope ID mismatch:
| after transform: SymbolId(1): ScopeId(0)
| rebuilt : SymbolId(1): ScopeId(3)



# babel-plugin-transform-typescript (2/7)
Expand Down Expand Up @@ -221,6 +225,10 @@ Passed: 8/35
| after transform: ScopeId(1): ["_s"]
| rebuilt : ScopeId(1): []

x Symbol scope ID mismatch:
| after transform: SymbolId(1): ScopeId(1)
| rebuilt : SymbolId(0): ScopeId(0)

x Reference mismatch:
| after transform: ReferenceId(3): Some("_s")
| rebuilt : ReferenceId(1): None
Expand Down