From 14c4d3cb3741a214ca4e080aa818ecb3a4be7a9a Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Thu, 29 Feb 2024 19:22:22 +1100 Subject: [PATCH] Workaround swc double line break bug --- valuescript_compiler/src/expression_compiler.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/valuescript_compiler/src/expression_compiler.rs b/valuescript_compiler/src/expression_compiler.rs index 9df6ee1..12b2d02 100644 --- a/valuescript_compiler/src/expression_compiler.rs +++ b/valuescript_compiler/src/expression_compiler.rs @@ -1370,7 +1370,10 @@ impl<'a, 'fnc> ExpressionCompiler<'a, 'fnc> { for child in jsx_children { let mut compiled_child = match child { swc_ecma_ast::JSXElementChild::JSXText(text) => { - Value::String(text.value.to_string()).to_ce() + // For some reason using text.value.to_string() duplicates the line breaks. I can only + // guess this is an swc bug. + let actual_text = get_span_text(text.span, &self.fnc.mc.source); + Value::String(actual_text).to_ce() } swc_ecma_ast::JSXElementChild::JSXExprContainer(jsx_expr_container) => { match &jsx_expr_container.expr { @@ -1879,3 +1882,15 @@ pub fn value_from_literal(lit: &swc_ecma_ast::Lit) -> Result return Err("JSXText literals"), }) } + +fn get_span_text(span: swc_common::Span, source: &str) -> String { + let swc_common::BytePos(start) = span.lo; + let swc_common::BytePos(end) = span.hi; + + let chars = source + .chars() + .skip(start as usize) + .take((end - start) as usize); + + chars.collect::() +}