Skip to content

Commit

Permalink
resolve review
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Nov 12, 2024
1 parent 8546f90 commit a584ce9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,16 @@ pub fn get_expr_as_string(val: &Box<Expr>) -> Option<String> {
// `Hello`
Expr::Tpl(Tpl {quasis, ..}) => {
if quasis.len() == 1 {
return Some(get_template_string_cooked_string(quasis.get(0).unwrap()));
return Some(get_tpl_cooked_or_raw_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 get_tpl_cooked_or_raw_string(tpl: &TplElement) -> String {
tpl.cooked.as_ref().unwrap_or(&tpl.raw).to_string()
}

pub fn pick_jsx_attrs(mut attrs: Vec<JSXAttrOrSpread>, names: HashSet<&str>) -> Vec<JSXAttrOrSpread> {
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(get_template_string_cooked_string(tpl_element)));
tokens.push(MsgToken::String(get_tpl_cooked_or_raw_string(tpl_element)));

if let Some(exp) = tpl.exprs.get(i) {
if let Expr::Call(call) = exp.as_ref() {
Expand Down
17 changes: 16 additions & 1 deletion src/tests/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ to!(
"#
);


// TODO: failed case
// to!(
// keep_forced_newlines_in_string,
// r#"
// import { Trans } from '@lingui/macro';
// <Trans>{`Multiline\nstring`}</Trans>;
// <Trans>{"Multiline\nstring"}</Trans>;
// "#,
// r#"
// import { Trans } from "@lingui/react";
// <Trans message={"Multiline\nstring"} id={"EfogM+"}/>;
// <Trans message={"Multiline\nstring"} id={"EfogM+"}/>;
// "#
// );

to!(
use_js_macro_in_jsx_attrs,
r#"
Expand Down Expand Up @@ -404,4 +420,3 @@ to!(
// <Trans id="msg.hello" />;
// `,
// },

0 comments on commit a584ce9

Please sign in to comment.