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,23 +129,18 @@ impl Fold for OptimizeServerReact { ...@@ -129,23 +129,18 @@ 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![];
for decl in d {
if let Pat::Array(array_pat) = &decl.name { if let Pat::Array(array_pat) = &decl.name {
if array_pat.elems.len() == 2 { if array_pat.elems.len() == 2 {
if let (Some(array_pat_1), Some(array_pat_2)) =
(&array_pat.elems[0], &array_pat.elems[1])
{
if let Some(box Expr::Call(call)) = &decl.init { if let Some(box Expr::Call(call)) = &decl.init {
if let Callee::Expr(box Expr::Ident(f)) = &call.callee { if let Callee::Expr(box Expr::Ident(f)) = &call.callee {
if let Some(use_state_ident) = &self.use_state_ident { if let Some(use_state_ident) = &self.use_state_ident {
...@@ -156,35 +151,35 @@ impl Fold for OptimizeServerReact { ...@@ -156,35 +151,35 @@ impl Fold for OptimizeServerReact {
// This is because useState allows a function as the // This is because useState allows a function as the
// initialiser. // initialiser.
match &call.args[0].expr { match &call.args[0].expr {
box Expr::Lit(_) box Expr::Lit(_) | box Expr::Object(_) | box Expr::Array(_) => {
| box Expr::Object(_) // const [state, setState] = [x, () => {}];
| box Expr::Array(_) => { return VarDeclarator {
// const state = x, setState = () => {};
new_d.push(VarDeclarator {
definite: false, definite: false,
name: array_pat_1.clone(), name: decl.name.clone(),
init: Some(call.args[0].expr.clone()), init: Some(Box::new(Expr::Array(ArrayLit {
elems: vec![
Some(call.args[0].expr.clone().into()),
Some(
Expr::Arrow(ArrowExpr {
span: DUMMY_SP, span: DUMMY_SP,
});
new_d.push(VarDeclarator {
definite: false,
name: array_pat_2.clone(),
init: Some(Box::new(Expr::Arrow(ArrowExpr {
body: Box::new(BlockStmtOrExpr::Expr( body: Box::new(BlockStmtOrExpr::Expr(
Box::new(Expr::Lit(Lit::Null(Null { Box::new(Expr::Lit(Lit::Null(
span: DUMMY_SP, Null { span: DUMMY_SP },
}))), ))),
)), )),
params: vec![],
is_async: false, is_async: false,
is_generator: false, is_generator: false,
span: DUMMY_SP, params: vec![],
type_params: None,
return_type: None, return_type: None,
type_params: None,
})
.into(),
),
],
span: DUMMY_SP,
}))), }))),
span: DUMMY_SP, span: DUMMY_SP,
}); };
continue;
} }
_ => {} _ => {}
} }
...@@ -194,11 +189,7 @@ impl Fold for OptimizeServerReact { ...@@ -194,11 +189,7 @@ impl Fold for OptimizeServerReact {
} }
} }
} }
}
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 a: 0
}, setState5 = ()=>null; },
()=>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