Unverified Commit 562fac78 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(next-swc): Fix react compiler usefulness detector (15.3) (#79480)

### What?

Fix the usefulness detector for the React Compiler

### Why?

JSX was also a target of the React Compiler
parent 06097fd7
use swc_core::ecma::{ use swc_core::ecma::{
ast::{Callee, Expr, FnDecl, FnExpr, Pat, Program, ReturnStmt, VarDeclarator}, ast::{Callee, Expr, FnDecl, FnExpr, Pat, Program, ReturnStmt, Stmt, VarDeclarator},
visit::{Visit, VisitWith}, visit::{Visit, VisitWith},
}; };
...@@ -34,6 +34,25 @@ impl Visit for Finder { ...@@ -34,6 +34,25 @@ impl Visit for Finder {
node.visit_children_with(self); node.visit_children_with(self);
} }
fn visit_expr(&mut self, node: &Expr) {
if self.found {
return;
}
if matches!(
node,
Expr::JSXMember(..)
| Expr::JSXNamespacedName(..)
| Expr::JSXEmpty(..)
| Expr::JSXElement(..)
| Expr::JSXFragment(..)
) {
self.found = true;
return;
}
node.visit_children_with(self);
}
fn visit_fn_decl(&mut self, node: &FnDecl) { fn visit_fn_decl(&mut self, node: &FnDecl) {
let old = self.is_interested; let old = self.is_interested;
self.is_interested = node.ident.sym.starts_with("use") self.is_interested = node.ident.sym.starts_with("use")
...@@ -47,7 +66,7 @@ impl Visit for Finder { ...@@ -47,7 +66,7 @@ impl Visit for Finder {
fn visit_fn_expr(&mut self, node: &FnExpr) { fn visit_fn_expr(&mut self, node: &FnExpr) {
let old = self.is_interested; let old = self.is_interested;
self.is_interested = node.ident.as_ref().is_some_and(|ident| { self.is_interested |= node.ident.as_ref().is_some_and(|ident| {
ident.sym.starts_with("use") || ident.sym.starts_with(|c: char| c.is_ascii_uppercase()) ident.sym.starts_with("use") || ident.sym.starts_with(|c: char| c.is_ascii_uppercase())
}); });
...@@ -69,6 +88,13 @@ impl Visit for Finder { ...@@ -69,6 +88,13 @@ impl Visit for Finder {
node.visit_children_with(self); node.visit_children_with(self);
} }
fn visit_stmt(&mut self, node: &Stmt) {
if self.found {
return;
}
node.visit_children_with(self);
}
fn visit_var_declarator(&mut self, node: &VarDeclarator) { fn visit_var_declarator(&mut self, node: &VarDeclarator) {
let old = self.is_interested; let old = self.is_interested;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment