Skip to content

Commit 600baef

Browse files
authored
Keep getInitialProps while cheking its usage (#44)
1 parent e859d2b commit 600baef

File tree

5 files changed

+66
-27
lines changed

5 files changed

+66
-27
lines changed

src/lib.rs

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct NextSuperJsonTransformer {
6868

6969
has_init_props: bool,
7070
use_init_props: bool,
71+
keep_init_props: bool,
7172

7273
has_multiple_props: bool,
7374
}
@@ -92,6 +93,7 @@ pub fn transform(config: Config) -> impl VisitMut {
9293

9394
has_init_props: false,
9495
use_init_props: false,
96+
keep_init_props: false,
9597

9698
has_multiple_props: false,
9799
}
@@ -512,7 +514,9 @@ impl VisitMut for NextSuperJsonTransformer {
512514
if &*id.sym == INITIAL_PROPS {
513515
if let Some(expr) = &mut p.value {
514516
self.use_init_props = true;
515-
p.value = Some(expr.take().wrap_init_props(self.excluded_expr()));
517+
if !self.keep_init_props {
518+
p.value = Some(expr.take().wrap_init_props(self.excluded_expr()));
519+
}
516520
}
517521
}
518522
}
@@ -521,27 +525,29 @@ impl VisitMut for NextSuperJsonTransformer {
521525
if let PropName::Ident(id) = &m.key {
522526
if &*id.sym == INITIAL_PROPS {
523527
self.use_init_props = true;
524-
*member = ClassMember::ClassProp(ClassProp {
525-
accessibility: m.accessibility.take(),
526-
declare: false,
527-
decorators: vec![],
528-
definite: false,
529-
is_abstract: m.is_abstract,
530-
is_optional: m.is_optional,
531-
is_override: m.is_override,
532-
is_static: m.is_static,
533-
key: m.key.take(),
534-
readonly: false,
535-
span: DUMMY_SP,
536-
type_ann: None,
537-
value: Some(
538-
Box::new(Expr::Fn(FnExpr {
539-
function: m.function.take(),
540-
ident: None,
541-
}))
542-
.wrap_init_props(self.excluded_expr()),
543-
),
544-
});
528+
if !self.keep_init_props {
529+
*member = ClassMember::ClassProp(ClassProp {
530+
accessibility: m.accessibility.take(),
531+
declare: false,
532+
decorators: vec![],
533+
definite: false,
534+
is_abstract: m.is_abstract,
535+
is_optional: m.is_optional,
536+
is_override: m.is_override,
537+
is_static: m.is_static,
538+
key: m.key.take(),
539+
readonly: false,
540+
span: DUMMY_SP,
541+
type_ann: None,
542+
value: Some(
543+
Box::new(Expr::Fn(FnExpr {
544+
function: m.function.take(),
545+
ident: None,
546+
}))
547+
.wrap_init_props(self.excluded_expr()),
548+
),
549+
});
550+
}
545551
}
546552
}
547553
}
@@ -559,7 +565,9 @@ impl VisitMut for NextSuperJsonTransformer {
559565
}
560566

561567
if self.has_init_props {
562-
a.right = a.right.take().wrap_init_props(self.excluded_expr());
568+
if !self.keep_init_props {
569+
a.right = a.right.take().wrap_init_props(self.excluded_expr());
570+
}
563571
self.use_init_props = true;
564572
self.has_init_props = false;
565573
}
@@ -677,11 +685,16 @@ impl NextSuperJsonTransformer {
677685
return;
678686
}
679687

688+
self.keep_init_props = first.is_some();
689+
680690
// check initial props
681-
if first.is_none() {
682-
items
683-
.iter_mut()
684-
.for_each(|item| item.visit_mut_children_with(self));
691+
items
692+
.iter_mut()
693+
.for_each(|item| item.visit_mut_children_with(self));
694+
695+
if first.is_some() && self.use_init_props {
696+
self.has_multiple_props = true;
697+
return;
685698
}
686699

687700
self.props.export.orig = first;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function Page() {
2+
return <div>Page</div>;
3+
}
4+
5+
Page.getInitialProps = () => {
6+
return {};
7+
}
8+
9+
export const getStaticProps = () => {
10+
return {
11+
props: {},
12+
};
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function Page() {
2+
return <div>Page</div>;
3+
}
4+
5+
Page.getInitialProps = () => {
6+
return {};
7+
}
8+
9+
export const getStaticProps = () => {
10+
return {
11+
props: {},
12+
};
13+
}

0 commit comments

Comments
 (0)