Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backslash in tagged template #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
Jack-Works marked this conversation as resolved.
Show resolved Hide resolved

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