Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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: 7 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ impl MessageBuilder {
self.push_msg(&str);
}

MsgToken::Expression(val) => {
let placeholder = self.push_exp(val);
self.push_msg(&format!("{{{placeholder}}}"));
MsgToken::Argument(val) => {
let placeholder = self.push_exp(val.value);
if val.used_utility_name.is_some_and(|n| n == "arg") {
self.push_msg(&placeholder);
} else {
self.push_msg(&format!("{{{placeholder}}}"));
}
}

MsgToken::TagOpening(val) => {
Expand Down
23 changes: 16 additions & 7 deletions src/jsx_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ast_utils::{get_jsx_attr, get_jsx_attr_value_as_string};
use crate::macro_utils::MacroCtx;
use crate::tokens::{CaseOrOffset, ChoiceCase, IcuChoice, MsgToken, TagOpening};
use crate::tokens::{Argument, CaseOrOffset, ChoiceCase, IcuChoice, MsgToken, TagOpening};
use once_cell::sync::Lazy;
use regex::Regex;
use swc_core::common::DUMMY_SP;
Expand Down Expand Up @@ -135,7 +135,10 @@ impl TransJSXVisitor<'_> {
tokens.extend(visitor.tokens)
}

_ => tokens.push(MsgToken::Expression(exp.clone())),
_ => tokens.push(MsgToken::Argument(Argument {
used_utility_name: None,
value: exp.clone(),
})),
}
}

Expand Down Expand Up @@ -228,12 +231,15 @@ impl Visit for TransJSXVisitor<'_> {
Expr::Call(call) => {
if let Some(tokens) = self.ctx.try_tokenize_call_expr_as_choice_cmp(call) {
self.tokens.extend(tokens);
} else if let Some(placeholder) =
self.ctx.try_tokenize_call_expr_as_placeholder_call(call)
} else if let Some(arg_token) =
self.ctx.try_tokenize_call_expr_as_utility_macro_call(call)
{
self.tokens.push(placeholder);
self.tokens.push(arg_token);
} else {
self.tokens.push(MsgToken::Expression(exp.clone()));
self.tokens.push(MsgToken::Argument(Argument {
used_utility_name: None,
value: exp.clone(),
}));
}
}

Expand All @@ -245,7 +251,10 @@ impl Visit for TransJSXVisitor<'_> {
self.tokens.extend(self.ctx.tokenize_tpl(tpl));
}
_ => {
self.tokens.push(MsgToken::Expression(exp.clone()));
self.tokens.push(MsgToken::Argument(Argument {
used_utility_name: None,
value: exp.clone(),
}));
}
}
}
Expand Down
37 changes: 27 additions & 10 deletions src/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl MacroCtx {
self.is_lingui_ident("ph", ident)
}

pub fn is_lingui_argument_expr(&self, ident: &Ident) -> bool {
self.is_lingui_ident("arg", ident)
}

/// is given ident exported from @lingui/macro?
pub fn is_lingui_ident(&self, name: &str, ident: &Ident) -> bool {
self.symbol_to_id_map
Expand Down Expand Up @@ -146,14 +150,17 @@ impl MacroCtx {
tokens.extend(call_tokens);
continue;
}
if let Some(placeholder) = self.try_tokenize_call_expr_as_placeholder_call(call)
if let Some(arg_token) = self.try_tokenize_call_expr_as_utility_macro_call(call)
{
tokens.push(placeholder);
tokens.push(arg_token);
continue;
}
}

tokens.push(MsgToken::Expression(exp.clone()));
tokens.push(MsgToken::Argument(Argument {
used_utility_name: None,
value: exp.clone(),
}));
}
}

Expand Down Expand Up @@ -193,13 +200,20 @@ impl MacroCtx {
None
}

pub fn try_tokenize_call_expr_as_placeholder_call(&self, expr: &CallExpr) -> Option<MsgToken> {
pub fn try_tokenize_call_expr_as_utility_macro_call(
&self,
expr: &CallExpr,
) -> Option<MsgToken> {
if expr.callee.as_expr().is_some_and(|c| {
c.as_ident()
.is_some_and(|i| self.is_lingui_placeholder_expr(i))
c.as_ident().is_some_and(|i| {
self.is_lingui_placeholder_expr(i) || self.is_lingui_argument_expr(i)
})
}) {
if let Some(first) = expr.args.first() {
return Some(MsgToken::Expression(first.expr.clone()));
return Some(MsgToken::Argument(Argument {
used_utility_name: expr.callee.as_expr()?.as_ident()?.sym.clone().into(),
value: first.expr.clone(),
}));
}
}

Expand Down Expand Up @@ -263,9 +277,12 @@ impl MacroCtx {
// todo: panic offset might be only a number, other forms is not supported
}
} else {
let tokens = self
.try_tokenize_expr(&prop.value)
.unwrap_or(vec![MsgToken::Expression(prop.value.clone())]);
let tokens = self.try_tokenize_expr(&prop.value).unwrap_or(vec![
MsgToken::Argument(Argument {
used_utility_name: None,
value: prop.value.clone(),
}),
]);

choices.push(CaseOrOffset::Case(ChoiceCase { tokens, key }));
}
Expand Down
25 changes: 25 additions & 0 deletions src/tests/js_icu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,31 @@ to!(
"#
);

to!(
js_plural_with_arg_macro,
r#"
import { plural, arg } from '@lingui/core/macro';
plural(count, {
"one": `# book on {${arg(today)}, date}`,
other: `# books on {${arg(today)}, date}`
});
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";
$_i18n._(
{
id: "lEIbMo",
message:
"{count, plural, one {# book on {today, date}} other {# books on {today, date}}}",
values: {
count: count,
today: today,
},
}
);
"#
);

to!(
js_should_not_treat_offset_in_select,
r#"
Expand Down
21 changes: 21 additions & 0 deletions src/tests/js_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,27 @@ to!(
"#
);

to!(
js_variables_with_arg_macro_is_not_wrapped_in_curly_brackets,
r#"
import { t, arg } from '@lingui/core/macro';
t`Number {${arg(num)}, number, myNumberStyle}`;
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";
$_i18n._(
/*i18n*/
{
id: "6HvXd1",
message: "Number {num, number, myNumberStyle}",
values: {
num: num,
},
}
);
"#
);

to!(
js_newlines_are_preserved,
r#"
Expand Down
8 changes: 7 additions & 1 deletion src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use swc_core::ecma::atoms::Atom;

pub enum MsgToken {
String(String),
Expression(Box<Expr>),
Argument(Argument),
TagOpening(TagOpening),
TagClosing,
IcuChoice(IcuChoice),
Expand All @@ -21,6 +21,12 @@ pub struct IcuChoice {
pub cases: Vec<CaseOrOffset>,
}

pub struct Argument {
/// ph | arg
pub used_utility_name: Option<Atom>,
pub value: Box<Expr>,
}

pub enum CaseOrOffset {
Case(ChoiceCase),
Offset(String),
Expand Down
Loading