Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiline JSX string conversion to string #167

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,29 @@ fn get_attr_name(jsx_attr: &JSXAttr, is_component: bool) -> String {
}
}

fn normalize_lit_str(lit: &Lit) -> Lit {
match lit {
Lit::Str(lit_str) => {
let value = lit_str.value.to_string();
marvinhagemeister marked this conversation as resolved.
Show resolved Hide resolved
let mut replaced = "".to_string();

for (i, line) in value.lines().enumerate() {
if i > 0 {
replaced.push(' ');
}
replaced.push_str(line.trim_start());
}

Lit::Str(Str {
span: lit_str.span,
value: replaced.into(),
raw: None,
})
}
_ => lit.clone(),
}
}

/// Convert a JSXMemberExpr to MemberExpr. We offload this to a
/// function because conversion is recursive.
fn jsx_member_expr_to_normal(jsx_member_expr: &JSXMemberExpr) -> MemberExpr {
Expand Down Expand Up @@ -515,7 +538,10 @@ impl JsxPrecompile {

if attr_name == "key" {
key_value = match attr_value {
JSXAttrValue::Lit(lit) => Some(Expr::Lit(lit.clone())),
JSXAttrValue::Lit(lit) => {
let normalized_lit = normalize_lit_str(lit);
Some(Expr::Lit(normalized_lit))
}
JSXAttrValue::JSXExprContainer(jsx_expr_container) => {
match &jsx_expr_container.expr {
// This is treated as a syntax error in attributes
Expand All @@ -537,10 +563,12 @@ impl JsxPrecompile {
// Case: <Foo class={null}>
match attr_value {
JSXAttrValue::Lit(lit) => {
let normalized_lit = normalize_lit_str(lit);

props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: prop_name,
value: Box::new(Expr::Lit(lit.clone())),
value: Box::new(Expr::Lit(normalized_lit)),
},
))));
}
Expand Down Expand Up @@ -1771,6 +1799,24 @@ _jsxssr($$_tpl_1, _jsx(Foo, {
);
}

#[test]
fn multi_jsx_string_line_to_jsx_call_test() {
test_transform(
JsxPrecompile::default(),
r#"const a = <Foo
key="Register a module with the third party
registry."
description="Register a module with the third party
registry."
/>
"#,
r#"import { jsx as _jsx } from "react/jsx-runtime";
const a = _jsx(Foo, {
description: "Register a module with the third party registry."
}, "Register a module with the third party registry.");"#,
);
}

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