Skip to content

Commit

Permalink
fix: backslash
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Nov 10, 2024
1 parent d02b493 commit 8546f90
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,22 @@ pub fn get_expr_as_string(val: &Box<Expr>) -> Option<String> {
// `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 }
}

_ => None
}
}

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<JSXAttrOrSpread>, names: HashSet<&str>) -> Vec<JSXAttrOrSpread> {
attrs.retain(|attr| {
if let JSXAttrOrSpread::JSXAttr(attr) = attr {
Expand Down
2 changes: 1 addition & 1 deletion src/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl MacroCtx {
let mut tokens: Vec<MsgToken> = 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() {
Expand Down
15 changes: 15 additions & 0 deletions src/tests/js_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down

0 comments on commit 8546f90

Please sign in to comment.