Skip to content

Commit

Permalink
fix(transformer/async-to-generator): correct the SymbolFlags of fun…
Browse files Browse the repository at this point in the history
…ction id in module (#7470)
  • Loading branch information
Dunqing committed Nov 26, 2024
1 parent 37842c1 commit 58a125f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 300 deletions.
30 changes: 23 additions & 7 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
(scope_id, wrapper_scope_id)
};

let bound_ident = Self::create_bound_identifier(id.as_ref(), wrapper_scope_id, ctx);
let bound_ident = Self::create_bound_identifier(
id.as_ref(),
wrapper_scope_id,
SymbolFlags::FunctionScopedVariable,
ctx,
);

let caller_function = {
let scope_id = ctx.create_child_scope(wrapper_scope_id, ScopeFlags::Function);
Expand Down Expand Up @@ -359,9 +364,18 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let params =
Self::create_placeholder_params(&wrapper_function.params, wrapper_scope_id, ctx);
let params = mem::replace(&mut wrapper_function.params, params);

// TODO: Needs a better way to handle the function SymbolFlags in different ModuleKind.
let flags = if self.ctx.source_type.is_module() && ctx.current_scope_flags().is_top() {
SymbolFlags::BlockScopedVariable | SymbolFlags::Function
} else {
SymbolFlags::FunctionScopedVariable
};

let bound_ident = Self::create_bound_identifier(
wrapper_function.id.as_ref(),
ctx.current_scope_id(),
flags,
ctx,
);

Expand Down Expand Up @@ -442,7 +456,12 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
// to change the parent scope of the generator function to the wrapper function.
ctx.scopes_mut().change_parent_id(generator_function_id, Some(wrapper_scope_id));

let bound_ident = Self::create_bound_identifier(None, wrapper_scope_id, ctx);
let bound_ident = Self::create_bound_identifier(
None,
wrapper_scope_id,
SymbolFlags::FunctionScopedVariable,
ctx,
);

let caller_function = {
let scope_id = ctx.create_child_scope(wrapper_scope_id, ScopeFlags::Function);
Expand Down Expand Up @@ -739,13 +758,10 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
fn create_bound_identifier(
id: Option<&BindingIdentifier<'a>>,
scope_id: ScopeId,
flags: SymbolFlags,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
ctx.generate_uid(
id.as_ref().map_or_else(|| "ref", |id| id.name.as_str()),
scope_id,
SymbolFlags::FunctionScopedVariable,
)
ctx.generate_uid(id.as_ref().map_or_else(|| "ref", |id| id.name.as_str()), scope_id, flags)
}

/// Check whether the given [`Ancestor`] is a class method-like node.
Expand Down
20 changes: 1 addition & 19 deletions tasks/coverage/snapshots/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 54a8389f

semantic_babel Summary:
AST Parsed : 2218/2218 (100.00%)
Positive Passed: 1846/2218 (83.23%)
Positive Passed: 1849/2218 (83.36%)
tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.3-function-in-if-body/input.js
semantic error: Symbol scope ID mismatch for "f":
after transform: SymbolId(0): ScopeId(4294967294)
Expand All @@ -26,24 +26,6 @@ semantic error: A 'return' statement can only be used within a function body.
tasks/coverage/babel/packages/babel-parser/test/fixtures/core/uncategorised/328/input.js
semantic error: A 'return' statement can only be used within a function body.

tasks/coverage/babel/packages/babel-parser/test/fixtures/es2015/uncategorised/385/input.js
semantic error: Symbol flags mismatch for "_foo":
after transform: SymbolId(1): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/es2017/async-functions/25/input.js
semantic error: Symbol flags mismatch for "_foo":
after transform: SymbolId(1): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/es2017/async-functions/export/input.js
semantic error: Symbol flags mismatch for "_foo":
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)
Symbol flags mismatch for "_bar":
after transform: SymbolId(4): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(4): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/class-private-properties/await-in-private-property-in-params-of-async-arrow/input.js
semantic error: Expected a semicolon or an implicit semicolon after a statement, but found none

Expand Down
47 changes: 1 addition & 46 deletions tasks/coverage/snapshots/semantic_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: fd979d85

semantic_test262 Summary:
AST Parsed : 44026/44026 (100.00%)
Positive Passed: 43486/44026 (98.77%)
Positive Passed: 43495/44026 (98.79%)
tasks/coverage/test262/test/annexB/language/function-code/if-decl-else-decl-a-func-block-scoping.js
semantic error: Symbol scope ID mismatch for "f":
after transform: SymbolId(3): ScopeId(4294967294)
Expand Down Expand Up @@ -3220,41 +3220,6 @@ Symbol scope ID mismatch for "x":
after transform: SymbolId(4): ScopeId(7)
rebuilt : SymbolId(5): ScopeId(4)

tasks/coverage/test262/test/language/expressions/dynamic-import/catch/nested-async-function-await-eval-script-code-target.js
semantic error: Symbol flags mismatch for "_f":
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/dynamic-import/catch/nested-async-function-eval-script-code-target.js
semantic error: Symbol flags mismatch for "_f":
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/dynamic-import/catch/nested-async-function-return-await-eval-script-code-target.js
semantic error: Symbol flags mismatch for "_f":
after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/dynamic-import/catch/nested-async-gen-await-eval-script-code-target.js
semantic error: Symbol flags mismatch for "_f":
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/dynamic-import/catch/nested-async-gen-return-await-eval-script-code-target.js
semantic error: Symbol flags mismatch for "_f":
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/dynamic-import/imported-self-update.js
semantic error: Symbol flags mismatch for "_fn":
after transform: SymbolId(3): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(3): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/dynamic-import/namespace/await-ns-get-nested-namespace-dflt-indirect.js
semantic error: Symbol flags mismatch for "_fn":
after transform: SymbolId(4): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/expressions/object/dstr/async-gen-meth-ary-ptrn-elem-ary-empty-init.js
semantic error: Scope children mismatch:
after transform: ScopeId(5): [ScopeId(2)]
Expand Down Expand Up @@ -3812,16 +3777,6 @@ Symbol scope ID mismatch for "_obj3$a":
after transform: SymbolId(7): ScopeId(8)
rebuilt : SymbolId(1): ScopeId(0)

tasks/coverage/test262/test/language/module-code/export-default-asyncfunction-declaration-binding.js
semantic error: Symbol flags mismatch for "_A":
after transform: SymbolId(1): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/module-code/export-default-asyncgenerator-declaration-binding.js
semantic error: Symbol flags mismatch for "_AG":
after transform: SymbolId(1): SymbolFlags(FunctionScopedVariable)
rebuilt : SymbolId(2): SymbolFlags(BlockScopedVariable | Function)

tasks/coverage/test262/test/language/module-code/top-level-await/syntax/for-await-await-expr-func-expression.js
semantic error: Scope children mismatch:
after transform: ScopeId(14): [ScopeId(1)]
Expand Down
Loading

0 comments on commit 58a125f

Please sign in to comment.