From a584ce951e1ace558c0a5e4f914b9363b683ad17 Mon Sep 17 00:00:00 2001 From: Jack Works <5390719+Jack-Works@users.noreply.github.com> Date: Wed, 13 Nov 2024 00:27:32 +0800 Subject: [PATCH] resolve review --- src/ast_utils.rs | 10 +++------- src/macro_utils.rs | 2 +- src/tests/jsx.rs | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/ast_utils.rs b/src/ast_utils.rs index ee98402..7efe17c 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(get_template_string_cooked_string(quasis.get(0).unwrap())); + return Some(get_tpl_cooked_or_raw_string(quasis.get(0).unwrap())); } else { None } } @@ -60,12 +60,8 @@ 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 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, names: HashSet<&str>) -> Vec { diff --git a/src/macro_utils.rs b/src/macro_utils.rs index 7f80c9b..f1c57c7 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(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() { diff --git a/src/tests/jsx.rs b/src/tests/jsx.rs index 2d60c2b..2d5eaa3 100644 --- a/src/tests/jsx.rs +++ b/src/tests/jsx.rs @@ -309,6 +309,22 @@ to!( "# ); + +// TODO: failed case +// to!( +// keep_forced_newlines_in_string, +// r#" +// import { Trans } from '@lingui/macro'; +// {`Multiline\nstring`}; +// {"Multiline\nstring"}; +// "#, +// r#" +// import { Trans } from "@lingui/react"; +// ; +// ; +// "# +// ); + to!( use_js_macro_in_jsx_attrs, r#" @@ -404,4 +420,3 @@ to!( // ; // `, // }, -