Skip to content

Commit

Permalink
fix: duplicate template index (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Oct 25, 2023
1 parent 29359d5 commit 5014fc8
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ impl JsxPrecompile {
// These are now safe to be serialized
// Case: <div foo="1" />
self.next_index += 1;
let index = self.next_index;
let mut static_strs: Vec<String> = vec![];
let mut dynamic_exprs: Vec<Expr> = vec![];
self.serialize_jsx_element_to_string_vec(
Expand All @@ -876,7 +877,7 @@ impl JsxPrecompile {
&mut dynamic_exprs,
);

self.gen_template(self.next_index, static_strs, dynamic_exprs)
self.gen_template(index, static_strs, dynamic_exprs)
} else {
// Case: <div {...props} />
Expr::Call(self.serialize_jsx_to_call_expr(el))
Expand Down Expand Up @@ -1039,6 +1040,7 @@ impl VisitMut for JsxPrecompile {
}
_ => {
self.next_index += 1;
let index = self.next_index;
let mut strings: Vec<String> = vec![];
let mut dynamic_exprs: Vec<Expr> = vec![];

Expand All @@ -1049,7 +1051,7 @@ impl VisitMut for JsxPrecompile {
&mut strings,
&mut dynamic_exprs,
);
*expr = self.gen_template(self.next_index, strings, dynamic_exprs)
*expr = self.gen_template(index, strings, dynamic_exprs)
}
}
}
Expand Down Expand Up @@ -1750,6 +1752,25 @@ const a = _jsxDEV(Foo, {
);
}

#[test]
fn template_index_test() {
test_transform(
JsxPrecompile::default(),
r#"<div><Foo><span /></Foo></div>;"#,
r#"import { jsx as _jsx, jsxssr as _jsxssr } from "react/jsx-runtime";
const $$_tpl_2 = [
"<span></span>"
];
const $$_tpl_1 = [
"<div>",
"</div>"
];
_jsxssr($$_tpl_1, _jsx(Foo, {
children: _jsxssr($$_tpl_2)
}));"#,
);
}

#[track_caller]
fn test_transform(
transform: impl VisitMut,
Expand Down

0 comments on commit 5014fc8

Please sign in to comment.