diff --git a/.changeset/tangy-dancers-jam.md b/.changeset/tangy-dancers-jam.md new file mode 100644 index 0000000000..7956abc56b --- /dev/null +++ b/.changeset/tangy-dancers-jam.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/react": patch +--- + +Fix error like `Unterminated string constant` when using multi-line JSX StringLiteral. diff --git a/packages/react/transform/src/swc_plugin_snapshot/mod.rs b/packages/react/transform/src/swc_plugin_snapshot/mod.rs index ab1847e4c8..86f80ae751 100644 --- a/packages/react/transform/src/swc_plugin_snapshot/mod.rs +++ b/packages/react/transform/src/swc_plugin_snapshot/mod.rs @@ -560,6 +560,20 @@ where .for_each(|attr_or_spread| match attr_or_spread { JSXAttrOrSpread::SpreadElement(_) => todo!(), JSXAttrOrSpread::JSXAttr(JSXAttr { name, value, .. }) => { + if let Some(JSXAttrValue::Lit(lit)) = value { + match lit { + Lit::Str(s) => { + let transformed_value = transform_jsx_attr_str(&s.value); + *value = Some(JSXAttrValue::Lit(Lit::Str(Str { + span: s.span, + raw: None, + value: transformed_value.into(), + }))); + } + _ => {} + } + } + match name { JSXAttrName::Ident(ident_name) => { let attr_name = AttrName::from(>::into(ident_name.clone())); @@ -786,18 +800,6 @@ where match value { None => {} Some(JSXAttrValue::Lit(value)) => { - let value = match &value { - Lit::Str(s) => { - let value = transform_jsx_attr_str(&s.value); - - Lit::Str(Str { - span: s.span, - raw: None, - value: value.into(), - }) - } - _ => value.clone(), - }; let stmt = quote!( r#"__SetClasses($element, $value)"# as Stmt, element: Expr = el.clone(), @@ -2911,6 +2913,23 @@ mod tests { + "# ); diff --git a/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_snapshot/mod.rs/should_excape_newline_character.js b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_snapshot/mod.rs/should_excape_newline_character.js index 36ef632e88..3a073d793e 100644 --- a/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_snapshot/mod.rs/should_excape_newline_character.js +++ b/packages/react/transform/tests/__swc_snapshots__/src/swc_plugin_snapshot/mod.rs/should_excape_newline_character.js @@ -23,6 +23,12 @@ const __snapshot_da39a_test_1 = ReactLynx.createSnapshot("__snapshot_da39a_test_ const el7 = __CreateView(pageId); __SetClasses(el7, "123\\t456"); __AppendElement(el, el7); + const el8 = __CreateElement("svg", pageId); + __SetAttribute(el8, "content", ' '); + __SetInlineStyles(el8, "width:24rpx;height:24rpx;opacity:0.4"); + __SetID(el8, "x y"); + __AddDataset(el8, "attr", "x y"); + __AppendElement(el, el8); return [ el, el1, @@ -31,7 +37,18 @@ const __snapshot_da39a_test_1 = ReactLynx.createSnapshot("__snapshot_da39a_test_ el4, el5, el6, - el7 + el7, + el8 ]; -}, null, null, undefined, globDynamicComponentEntry); -<__snapshot_da39a_test_1/>; +}, [ + function(ctx) { + if (ctx.__elements) { + __SetAttribute(ctx.__elements[8], '__lynx_timing_flag', ctx.__values[0].__ltf); + } + } +], null, undefined, globDynamicComponentEntry); +<__snapshot_da39a_test_1 values={[ + { + __ltf: " aaaaa " + } +]}/>;