diff --git a/src/js_macro_folder.rs b/src/js_macro_folder.rs index fbc4d70..75fbbdf 100644 --- a/src/js_macro_folder.rs +++ b/src/js_macro_folder.rs @@ -23,25 +23,36 @@ impl<'a> JsMacroFolder<'a> { } } - fn create_i18n_fn_call_from_tokens(&mut self, callee_obj: Option>, tokens: Vec) -> CallExpr { + fn create_message_descriptor_from_tokens(&mut self, tokens: Vec) -> Expr { let parsed = MessageBuilder::parse(tokens, false); let mut props: Vec = 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>, tokens: Vec) -> 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()]); } @@ -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); @@ -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 diff --git a/src/macro_utils.rs b/src/macro_utils.rs index 9ffd7f2..174a428 100644 --- a/src/macro_utils.rs +++ b/src/macro_utils.rs @@ -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> { diff --git a/src/tests/js_define_message.rs b/src/tests/js_define_message.rs index 64879de..0b0ddcc 100644 --- a/src/tests/js_define_message.rs +++ b/src/tests/js_define_message.rs @@ -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" }; @@ -76,10 +103,11 @@ 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', @@ -87,11 +115,15 @@ to!( }) "#, r#" - const msg = { - id: "msgId", - values: { - name: name, - }, - }; + const message1 = { + id: "xDAtGP", + }; + + const message2 = { + id: "msgId", + values: { + name: name, + }, + }; "# ); diff --git a/src/tests/js_t.rs b/src/tests/js_t.rs index 4ca8e96..dc22595 100644 --- a/src/tests/js_t.rs +++ b/src/tests/js_t.rs @@ -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!(