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

support tpl literal in defineMessage #36

Merged
merged 2 commits into from
Mar 13, 2023
Merged
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
35 changes: 28 additions & 7 deletions src/js_macro_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,36 @@ impl<'a> JsMacroFolder<'a> {
}
}

fn create_i18n_fn_call_from_tokens(&mut self, callee_obj: Option<Box<Expr>>, tokens: Vec<MsgToken>) -> CallExpr {
fn create_message_descriptor_from_tokens(&mut self, tokens: Vec<MsgToken>) -> Expr {
let parsed = MessageBuilder::parse(tokens, false);

let mut props: Vec<PropOrSpread> = vec![
create_key_value_prop("id", generate_message_id(&parsed.message_str, "").into()),
create_key_value_prop("message", parsed.message),

];

if !self.ctx.options.strip_non_essential_fields {
props.push(
create_key_value_prop("message", parsed.message),
);
}

if let Some(v) = parsed.values {
props.push(
create_key_value_prop("values", v),
)
}

let message_descriptor = Box::new(Expr::Object(ObjectLit {
let message_descriptor = Expr::Object(ObjectLit {
span: DUMMY_SP,
props,
}));
});

return message_descriptor;
}

fn create_i18n_fn_call_from_tokens(&mut self, callee_obj: Option<Box<Expr>>, tokens: Vec<MsgToken>) -> CallExpr {
let message_descriptor = Box::new(self.create_message_descriptor_from_tokens(tokens));
return self.create_i18n_fn_call(callee_obj, vec![message_descriptor.as_arg()]);
}

Expand Down Expand Up @@ -127,6 +138,7 @@ impl<'a> JsMacroFolder<'a> {

impl<'a> Fold for JsMacroFolder<'a> {
fn fold_expr(&mut self, expr: Expr) -> Expr {
// t`Message`
if let Expr::TaggedTpl(tagged_tpl) = &expr {
let (is_t, callee) = self.ctx.is_lingui_t_call_expr(&tagged_tpl.tag);

Expand All @@ -138,10 +150,19 @@ impl<'a> Fold for JsMacroFolder<'a> {
}
}

// defineMessage`Message`
if let Expr::TaggedTpl(tagged_tpl) = &expr {
if let Expr::Ident(ident) = tagged_tpl.tag.as_ref() {
if self.ctx.is_define_message_ident(&ident) {
let tokens = self.ctx.tokenize_tpl(&tagged_tpl.tpl);
return self.create_message_descriptor_from_tokens(tokens);
}
}
}

// defineMessage({message: "Message"})
if let Expr::Call(call) = &expr {
if let Some(_) = match_callee_name(&call, |n| self.ctx.is_lingui_ident(
"defineMessage", n
)) {
if let Some(_) = match_callee_name(&call, |n| self.ctx.is_define_message_ident(n)) {
if call.args.len() == 1 {
let descriptor = self.update_msg_descriptor_props(
call.args.clone().into_iter().next().unwrap().expr
Expand Down
5 changes: 5 additions & 0 deletions src/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl MacroCtx {
false
}

pub fn is_define_message_ident(&self, ident: &Ident) -> bool {
return self.is_lingui_ident("defineMessage", &ident)
|| self.is_lingui_ident("msg", &ident)
}

/// given import {plural as i18nPlural} from "@lingui/macro";
/// get_ident_export_name("i18nPlural") would return `plural`
pub fn get_ident_export_name(&self, ident: &Ident) -> Option<&JsWord> {
Expand Down
54 changes: 43 additions & 11 deletions src/tests/js_define_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,41 @@ use crate::{to};
to!(
should_transform_define_message,
r#"
import { defineMessage, plural, arg } from '@lingui/macro';
const message = defineMessage({
import { defineMessage, msg } from '@lingui/macro';
const message1 = defineMessage({
comment: "Description",
message: "Message"
})
const message2 = msg({
comment: "Description",
message: "Message"
})
"#,
r#"
const message = {
const message1 = {
id: "xDAtGP",
message: "Message"
};
const message2 = {
id: "xDAtGP",
message: "Message"
};
"#
);

to!(
define_message_should_support_template_literal,
r#"
import { defineMessage, msg } from '@lingui/macro';
const message1 = defineMessage`Message`;
const message2 = msg`Message`
"#,
r#"
const message1 = {
id: "xDAtGP",
message: "Message"
};
const message2 = {
id: "xDAtGP",
message: "Message"
};
Expand Down Expand Up @@ -76,22 +103,27 @@ to!(

to!(
production,
should_kept_only_essential_props,
should_kept_only_essential_props,
r#"
import { defineMessage } from '@lingui/macro'
const msg = defineMessage({
const message1 = defineMessage`Message`;
const message2 = defineMessage({
message: `Hello ${name}`,
id: 'msgId',
comment: 'description for translators',
context: 'My Context',
})
"#,
r#"
const msg = {
id: "msgId",
values: {
name: name,
},
};
const message1 = {
id: "xDAtGP",
};

const message2 = {
id: "msgId",
values: {
name: name,
},
};
"#
);
25 changes: 15 additions & 10 deletions src/tests/js_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,27 @@ to!(
js_should_kept_only_essential_props,
r#"
import { t } from '@lingui/macro'
const msg = t({
const msg1 = t`Message`
const msg2 = t({
message: `Hello ${name}`,
id: 'msgId',
comment: 'description for translators',
context: 'My Context',
})
"#,
r#"
import { i18n } from "@lingui/core";
const msg = i18n._({
id: "msgId",
values: {
name: name,
},
});
"#
r#"
import { i18n } from "@lingui/core";
const msg1 = i18n._({
id: "xDAtGP"
});

const msg2 = i18n._({
id: "msgId",
values: {
name: name,
},
});
"#
);

to!(
Expand Down