Workaround swc double line break bug

This commit is contained in:
Andrew Morris
2024-02-29 19:22:22 +11:00
parent 4cb49dc81c
commit 14c4d3cb37

View File

@@ -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<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>()
}