From f2f5e5aaf58085e19bf89b1b7f7ab651a973f246 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 11 Sep 2024 09:02:28 +0000 Subject: [PATCH] fix(transformer/react): missing scope_id for function in fast refresh plugin (#5693) Fix semantic error in the fast react plugin --- crates/oxc_transformer/src/react/refresh.rs | 9 ++++++--- tasks/transform_conformance/oxc.snap.md | 17 ++--------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/crates/oxc_transformer/src/react/refresh.rs b/crates/oxc_transformer/src/react/refresh.rs index 546dbf42fefea..ad84a62584d38 100644 --- a/crates/oxc_transformer/src/react/refresh.rs +++ b/crates/oxc_transformer/src/react/refresh.rs @@ -3,7 +3,7 @@ use std::{cell::Cell, iter::once}; use base64::prelude::{Engine, BASE64_STANDARD}; use oxc_allocator::CloneIn; use oxc_ast::{ast::*, match_expression, AstBuilder}; -use oxc_semantic::{Reference, ReferenceFlags, ScopeId, SymbolFlags, SymbolId}; +use oxc_semantic::{Reference, ReferenceFlags, ScopeFlags, ScopeId, SymbolFlags, SymbolId}; use oxc_span::{Atom, GetSpan, SPAN}; use oxc_syntax::operator::AssignmentOperator; use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; @@ -666,7 +666,7 @@ impl<'a> ReactRefresh<'a> { Some(self.ctx.ast.expression_array(SPAN, custom_hooks_in_scope, None)), )), ); - let fn_expr = self.ctx.ast.expression_function( + let function = self.ctx.ast.function( FunctionType::FunctionExpression, SPAN, None, @@ -679,7 +679,10 @@ impl<'a> ReactRefresh<'a> { Option::::None, Some(function_body), ); - arguments.push(self.ctx.ast.argument_expression(fn_expr)); + let scope_id = ctx.create_child_scope_of_current(ScopeFlags::Function); + function.scope_id.set(Some(scope_id)); + arguments + .push(self.ctx.ast.argument_expression(ctx.ast.expression_from_function(function))); } let symbol_id = diff --git a/tasks/transform_conformance/oxc.snap.md b/tasks/transform_conformance/oxc.snap.md index fb4db3cd2accb..e662a8a4b8df4 100644 --- a/tasks/transform_conformance/oxc.snap.md +++ b/tasks/transform_conformance/oxc.snap.md @@ -1,6 +1,6 @@ commit: 3bcfee23 -Passed: 38/52 +Passed: 40/52 # All Passed: * babel-plugin-transform-nullish-coalescing-operator @@ -167,7 +167,7 @@ rebuilt : SymbolId(2): [] x Output mismatch -# babel-plugin-transform-react-jsx (24/30) +# babel-plugin-transform-react-jsx (26/30) * refresh/does-not-get-tripped-by-iifes/input.jsx Bindings mismatch: after transform: ScopeId(0): [] @@ -182,19 +182,6 @@ rebuilt : SymbolId(0): ScopeId(0) * refresh/does-not-transform-it-because-it-is-not-used-in-the-AST/input.jsx x Output mismatch -* refresh/generates-valid-signature-for-exotic-ways-to-call-hooks/input.jsx -Missing ScopeId -Scope children mismatch: -after transform: ScopeId(0): [ScopeId(1)] -rebuilt : ScopeId(0): [ScopeId(1), ScopeId(3)] - -* refresh/includes-custom-hooks-into-the-signatures/input.jsx -Missing ScopeId -Missing ScopeId -Scope children mismatch: -after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(4)] -rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(5), ScopeId(6)] - * refresh/supports-typescript-namespace-syntax/input.tsx x Output mismatch