Unverified Commit b141f3b2 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix cases for the `optimize_server_react` transform (#59390)

Closes #59310, see attached test cases.

Closes NEXT-1830
parent d5291fad
...@@ -129,76 +129,67 @@ impl Fold for OptimizeServerReact { ...@@ -129,76 +129,67 @@ impl Fold for OptimizeServerReact {
} }
} }
expr expr.fold_children_with(self)
} }
// const [state, setState] = useState(x); // const [state, setState] = useState(x);
// const [state, setState] = React.useState(x); // const [state, setState] = React.useState(x);
fn fold_var_declarators(&mut self, d: Vec<VarDeclarator>) -> Vec<VarDeclarator> { fn fold_var_declarator(&mut self, decl: VarDeclarator) -> VarDeclarator {
if !self.optimize_use_state { if !self.optimize_use_state {
return d; return decl;
} }
let mut new_d = vec![]; if let Pat::Array(array_pat) = &decl.name {
for decl in d { if array_pat.elems.len() == 2 {
if let Pat::Array(array_pat) = &decl.name { if let Some(box Expr::Call(call)) = &decl.init {
if array_pat.elems.len() == 2 { if let Callee::Expr(box Expr::Ident(f)) = &call.callee {
if let (Some(array_pat_1), Some(array_pat_2)) = if let Some(use_state_ident) = &self.use_state_ident {
(&array_pat.elems[0], &array_pat.elems[1]) if &f.to_id() == use_state_ident && call.args.len() == 1 {
{ // We do the optimization only if the arg is a literal or a
if let Some(box Expr::Call(call)) = &decl.init { // type that we can
if let Callee::Expr(box Expr::Ident(f)) = &call.callee { // be sure is not a function (e.g. {} or [] lit).
if let Some(use_state_ident) = &self.use_state_ident { // This is because useState allows a function as the
if &f.to_id() == use_state_ident && call.args.len() == 1 { // initialiser.
// We do the optimization only if the arg is a literal or a match &call.args[0].expr {
// type that we can box Expr::Lit(_) | box Expr::Object(_) | box Expr::Array(_) => {
// be sure is not a function (e.g. {} or [] lit). // const [state, setState] = [x, () => {}];
// This is because useState allows a function as the return VarDeclarator {
// initialiser. definite: false,
match &call.args[0].expr { name: decl.name.clone(),
box Expr::Lit(_) init: Some(Box::new(Expr::Array(ArrayLit {
| box Expr::Object(_) elems: vec![
| box Expr::Array(_) => { Some(call.args[0].expr.clone().into()),
// const state = x, setState = () => {}; Some(
new_d.push(VarDeclarator { Expr::Arrow(ArrowExpr {
definite: false, span: DUMMY_SP,
name: array_pat_1.clone(), body: Box::new(BlockStmtOrExpr::Expr(
init: Some(call.args[0].expr.clone()), Box::new(Expr::Lit(Lit::Null(
span: DUMMY_SP, Null { span: DUMMY_SP },
}); ))),
new_d.push(VarDeclarator { )),
definite: false, is_async: false,
name: array_pat_2.clone(), is_generator: false,
init: Some(Box::new(Expr::Arrow(ArrowExpr { params: vec![],
body: Box::new(BlockStmtOrExpr::Expr( return_type: None,
Box::new(Expr::Lit(Lit::Null(Null { type_params: None,
span: DUMMY_SP, })
}))), .into(),
)), ),
params: vec![], ],
is_async: false, span: DUMMY_SP,
is_generator: false, }))),
span: DUMMY_SP, span: DUMMY_SP,
type_params: None, };
return_type: None,
}))),
span: DUMMY_SP,
});
continue;
}
_ => {}
}
} }
_ => {}
} }
} }
} }
} }
} }
} }
new_d.push(decl.fold_with(self));
} }
new_d decl.fold_children_with(self)
} }
} }
...@@ -7,8 +7,14 @@ import { FilterItem } from './item'; ...@@ -7,8 +7,14 @@ import { FilterItem } from './item';
export default function FilterItemDropdown({ list }) { export default function FilterItemDropdown({ list }) {
const pathname = usePathname(); const pathname = usePathname();
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const active = '', setActive = ()=>null; const [active, setActive] = [
const openSelect = false, setOpenSelect = ()=>null; '',
()=>null
];
const [openSelect, setOpenSelect] = [
false,
()=>null
];
const ref = useRef(null); const ref = useRef(null);
null; null;
null; null;
......
import { useState } from 'react'; import { useState } from 'react';
export default function App({ x }) { export default function App({ x }) {
const state = 0, setState = ()=>null; const [state, setState] = [
0,
()=>null
];
const [state2, setState2] = useState(()=>0); const [state2, setState2] = useState(()=>0);
const [state3, setState3] = useState(x); const [state3, setState3] = useState(x);
const s = useState(0); const s = useState(0);
const [state4] = useState(0); const [state4] = useState(0);
const { a } = { const [{ a }, setState5] = [
a: 0 {
}, setState5 = ()=>null; a: 0
},
()=>null
];
return <div> return <div>
<h1>Hello World</h1> <h1>Hello World</h1>
......
import { useState, useEffect, useLayoutEffect } from 'react'
import React from 'react'
const Component = ({ children, fallback }) => {
const [mounted, setMounted] = useState(false)
useEffect(() => setMounted(true), [])
if (!mounted) {
return fallback ?? /* @__PURE__ */ jsx(Fragment, {})
}
return children
}
export { Component }
import { useState, useEffect, useLayoutEffect } from 'react';
import React from 'react';
const Component = ({ children, fallback })=>{
const [mounted, setMounted] = [
false,
()=>null
];
null;
if (!mounted) {
return fallback ?? /* @__PURE__ */ jsx(Fragment, {});
}
return children;
};
export { Component };
import { useEffect, useLayoutEffect, useMemo } from 'react'
import * as React from 'react'
export default function App() {
useEffect(() => {
console.log('Hello World')
}, [])
useLayoutEffect(() => {
function foo() {}
return () => {}
}, [1, 2, App])
useLayoutEffect(() => {}, [runSideEffect()])
useEffect(() => {}, [1, runSideEffect(), 2])
useEffect(() => {}, getArray())
const a = useMemo(() => {
return 1
}, [])
React.useEffect(() => {
console.log('Hello World')
})
return (
<div>
<h1>Hello World</h1>
</div>
)
}
import { useEffect, useLayoutEffect, useMemo } from 'react';
import * as React from 'react';
export default function App() {
null;
null;
useLayoutEffect(()=>{}, [
runSideEffect()
]);
useEffect(()=>{}, [
1,
runSideEffect(),
2
]);
useEffect(()=>{}, getArray());
const a = useMemo(()=>{
return 1;
}, []);
React.useEffect(()=>{
console.log('Hello World');
});
return <div>
<h1>Hello World</h1>
</div>;
}
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