Skip to content

Commit 93d41da

Browse files
authored
Change closing elements name (#64)
1 parent 6c7735c commit 93d41da

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/app.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ impl VisitMut for AppTransformer {
104104
}
105105
}
106106

107-
fn visit_mut_jsx_opening_element(&mut self, elem: &mut JSXOpeningElement) {
107+
fn visit_mut_jsx_element(&mut self, elem: &mut JSXElement) {
108108
elem.visit_mut_children_with(self);
109109

110110
let mut found = false;
111111

112112
// find and remove data-superjson directive
113-
elem.attrs.retain(|attr_or_spread| {
113+
elem.opening.attrs.retain(|attr_or_spread| {
114114
if let JSXAttrOrSpread::JSXAttr(JSXAttr {
115115
name: JSXAttrName::Ident(id),
116116
..
@@ -127,6 +127,7 @@ impl VisitMut for AppTransformer {
127127
if found {
128128
// attrs -> obj props
129129
let list: Vec<PropOrSpread> = elem
130+
.opening
130131
.attrs
131132
.take()
132133
.into_iter()
@@ -169,7 +170,7 @@ impl VisitMut for AppTransformer {
169170
.collect();
170171

171172
// replace attrs
172-
elem.attrs = vec![
173+
elem.opening.attrs = vec![
173174
JSXAttr {
174175
name: Ident::new(DESERIALIZER_PROPS_ATTR.into(), DUMMY_SP).into(),
175176
span: DUMMY_SP,
@@ -201,7 +202,7 @@ impl VisitMut for AppTransformer {
201202
span: DUMMY_SP,
202203
value: Some(
203204
JSXExprContainer {
204-
expr: Box::new(elem.name.as_expr()).into(),
205+
expr: Box::new(elem.opening.name.as_expr()).into(),
205206
span: DUMMY_SP,
206207
}
207208
.into(),
@@ -211,7 +212,12 @@ impl VisitMut for AppTransformer {
211212
];
212213

213214
// change element name
214-
elem.name = Ident::new(DESERIALIZER_COMPONENT.into(), DUMMY_SP).into();
215+
elem.opening.name = Ident::new(DESERIALIZER_COMPONENT.into(), DUMMY_SP).into();
216+
217+
if let Some(closing) = &mut elem.closing {
218+
closing.name = Ident::new(DESERIALIZER_COMPONENT.into(), DUMMY_SP).into();
219+
}
220+
215221
self.transformed = true;
216222
}
217223
}

tests/fixture/app/children/code.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import ClientComponent from "./ClientComponent";
2+
3+
export default function Page() {
4+
const rest = {};
5+
const date = new Date();
6+
7+
return (
8+
<ClientComponent date={date} {...rest} data-superjson>
9+
<p>children</p>
10+
</ClientComponent>
11+
);
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { serialize } from "next-superjson-plugin/tools";
2+
import SuperJSONComponent from "next-superjson-plugin/client";
3+
import ClientComponent from "./ClientComponent";
4+
export default function Page() {
5+
const rest = {};
6+
const date = new Date();
7+
return <SuperJSONComponent props={serialize({
8+
date: date,
9+
...rest
10+
})} component={ClientComponent}>
11+
12+
<p >children</p>
13+
14+
</SuperJSONComponent>;
15+
}

0 commit comments

Comments
 (0)