Skip to content

Commit

Permalink
Workaround swc double line break bug
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Feb 29, 2024
1 parent 4cb49dc commit 14c4d3c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion valuescript_compiler/src/expression_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1879,3 +1882,15 @@ pub fn value_from_literal(lit: &swc_ecma_ast::Lit) -> Result<Value, &'static str
JSXText(_) => 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::<String>()
}

0 comments on commit 14c4d3c

Please sign in to comment.