Skip to content

Commit

Permalink
simplify logic and add more thorough tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pveyes committed Dec 22, 2021
1 parent b470525 commit 67a8d45
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
14 changes: 3 additions & 11 deletions packages/next-swc/crates/core/src/next_ssg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,13 @@ struct Analyzer<'a> {
state: &'a mut State,
in_lhs_of_var: bool,
in_data_fn: bool,
in_render_fn: bool,
}

impl Analyzer<'_> {
fn add_ref(&mut self, id: Id) {
tracing::trace!("add_ref({}{:?}, data = {})", id.0, id.1, self.in_data_fn);
if self.in_data_fn {
self.state.refs_from_data_fn.insert(id);
} else if self.in_render_fn {
self.state.refs_from_other.insert(id);
} else {
if self.state.cur_declaring.contains(&id) {
return;
Expand Down Expand Up @@ -153,15 +150,12 @@ impl Fold for Analyzer<'_> {
}
_ => {}
}
let jsx = jsx.fold_children_with(self);
jsx

jsx.fold_children_with(self)
}

fn fold_export_default_decl(&mut self, export: ExportDefaultDecl) -> ExportDefaultDecl {
self.in_render_fn = true;
let export = export.fold_children_with(self);
self.in_render_fn = false;
export
export.fold_children_with(self)
}

fn fold_fn_decl(&mut self, f: FnDecl) -> FnDecl {
Expand Down Expand Up @@ -333,7 +327,6 @@ impl NextSsg {
state: &mut self.state,
in_lhs_of_var: false,
in_data_fn: true,
in_render_fn: false,
};

let n = n.fold_with(&mut v);
Expand Down Expand Up @@ -398,7 +391,6 @@ impl Fold for NextSsg {
state: &mut self.state,
in_lhs_of_var: false,
in_data_fn: false,
in_render_fn: false,
};
m = m.fold_with(&mut v);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import Component from '../'
import { useState, useEffect } from 'react'
import { Root, Children, AttributeValue, AttributeJSX, ValueInRender, ValueInEffect, UnusedInRender } from '../'

export default function Test() {
return <Component />
const [x, setX] = useState(ValueInRender.value)
useEffect(() => {
setX(ValueInEffect.value)
}, [])

return (
<Root x={x}>
<div>
<Children attr={AttributeValue} jsx={<AttributeJSX />} />
</div>
</Root>
)
}

export async function getStaticProps() {
return { props: { name: Component.displayName } }
return {
props: {
// simulate Component usage inside getStaticProps
used: [
// these import references should not be removed
Root.value,
Children.value,
AttributeValue.value,
AttributeJSX.value,
ValueInRender.value,
ValueInEffect.value,
// this import reference should be removed
UnusedInRender.value,
],
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import Component from '../'
export var __N_SSG = true
import { useState, useEffect } from "react";
import { Root, Children, AttributeValue, AttributeJSX, ValueInRender, ValueInEffect } from "../";
export var __N_SSG = true;
export default function Test() {
return __jsx(Component, null)
const [x, setX] = useState(ValueInRender.value);
useEffect(() => {
setX(ValueInEffect.value);
}, [])
return __jsx(Root, {
x: x
}, __jsx('div', null, __jsx(Children, {
attr: AttributeValue,
jsx: __jsx(AttributeJSX, null)
})));
}

0 comments on commit 67a8d45

Please sign in to comment.