diff --git a/src/ast_utils.rs b/src/ast_utils.rs index 41840e3..ee98402 100644 --- a/src/ast_utils.rs +++ b/src/ast_utils.rs @@ -52,7 +52,7 @@ pub fn get_expr_as_string(val: &Box) -> Option { // `Hello` Expr::Tpl(Tpl {quasis, ..}) => { if quasis.len() == 1 { - return Some(quasis.get(0).unwrap().raw.to_string()); + return Some(get_template_string_cooked_string(quasis.get(0).unwrap())); } else { None } } @@ -60,6 +60,14 @@ pub fn get_expr_as_string(val: &Box) -> Option { } } +pub fn get_template_string_cooked_string(element: &TplElement) -> String { + if let Some(cooked) = &element.cooked { + cooked.to_string() + } else { + element.raw.to_string() + } +} + pub fn pick_jsx_attrs(mut attrs: Vec, names: HashSet<&str>) -> Vec { attrs.retain(|attr| { if let JSXAttrOrSpread::JSXAttr(attr) = attr { diff --git a/src/macro_utils.rs b/src/macro_utils.rs index 0a495c8..7f80c9b 100644 --- a/src/macro_utils.rs +++ b/src/macro_utils.rs @@ -110,7 +110,7 @@ impl MacroCtx { let mut tokens: Vec = Vec::with_capacity(tpl.quasis.len()); for (i, tpl_element) in tpl.quasis.iter().enumerate() { - tokens.push(MsgToken::String(tpl_element.raw.to_string())); + tokens.push(MsgToken::String(get_template_string_cooked_string(tpl_element))); if let Some(exp) = tpl.exprs.get(i) { if let Expr::Call(call) = exp.as_ref() { diff --git a/src/tests/js_t.rs b/src/tests/js_t.rs index dc22595..7a7f28c 100644 --- a/src/tests/js_t.rs +++ b/src/tests/js_t.rs @@ -162,6 +162,21 @@ to!( "# ); +to!( + js_backslash_newlines_are_preserved, + r#" + import { t } from '@lingui/macro'; + t`Multiline\nstring`; + "#, + r#" + import { i18n } from "@lingui/core"; + i18n._({ + id: "EfogM+", + message: "Multiline\nstring" + }); + "# +); + to!( js_support_message_descriptor_in_t_fn, r#"