diff --git a/packages/macro/src/macroJs.test.ts b/packages/macro/src/macroJs.test.ts index 7af7b01b2..3f746f123 100644 --- a/packages/macro/src/macroJs.test.ts +++ b/packages/macro/src/macroJs.test.ts @@ -267,10 +267,11 @@ describe("js macro", () => { const exp = parseExpression( `plural(count, { one: select(gender, { - male: "he", + male: hePronoun, female: "she", other: "they" - }) + }), + other: otherText })` ) const tokens = macro.tokenizeChoiceComponent(exp as CallExpression) @@ -293,12 +294,27 @@ describe("js macro", () => { }), format: "select", options: { - male: "he", + male: expect.objectContaining({ + type: "arg", + name: "hePronoun", + value: expect.objectContaining({ + name: "hePronoun", + type: "Identifier", + }), + }), female: "she", other: "they", }, }, ], + other: expect.objectContaining({ + type: "arg", + name: "otherText", + value: expect.objectContaining({ + name: "otherText", + type: "Identifier", + }), + }), }, }) }) diff --git a/packages/macro/src/macroJs.ts b/packages/macro/src/macroJs.ts index 0198eb0ca..ffebb0ea8 100644 --- a/packages/macro/src/macroJs.ts +++ b/packages/macro/src/macroJs.ts @@ -352,8 +352,12 @@ export default class MacroJs { value = this.tokenizeTemplateLiteral(attrValue) } else if (this.types.isCallExpression(attrValue)) { value = this.tokenizeNode(attrValue) + } else if (this.types.isStringLiteral(attrValue)) { + value = attrValue.value + } else if (this.types.isExpression(attrValue)) { + value = this.tokenizeExpression(attrValue) } else { - value = (attrValue as StringLiteral).value + value = (attrValue as unknown as StringLiteral).value } token.options[name] = value } diff --git a/packages/macro/test/js-plural.ts b/packages/macro/test/js-plural.ts index ca7283f79..bf2f3db6c 100644 --- a/packages/macro/test/js-plural.ts +++ b/packages/macro/test/js-plural.ts @@ -74,5 +74,31 @@ const cases: TestCase[] = [ ); `, }, + { + name: "Macro with expression only choice", + input: ` + import { plural } from '@lingui/macro' + plural(users.length, { + offset: 1, + 0: "No books", + 1: "1 book", + other: someOtherExp + }); + `, + expected: ` + import { i18n } from "@lingui/core"; + i18n._( + /*i18n*/ + { + id: "0mcXIe", + message: "{0, plural, offset:1 =0 {No books} =1 {1 book} other {{someOtherExp}}}", + values: { + 0: users.length, + someOtherExp: someOtherExp, + }, + } + ); + `, + }, ] export default cases diff --git a/packages/macro/test/js-select.ts b/packages/macro/test/js-select.ts index cf1b3436e..0e5477754 100644 --- a/packages/macro/test/js-select.ts +++ b/packages/macro/test/js-select.ts @@ -29,5 +29,34 @@ const cases: TestCase[] = [ ); `, }, + { + name: "Nested macros with pure expressions option", + input: ` + import { select, plural } from '@lingui/macro' + select(gender, { + "male": plural(numOfGuests, { + one: "He invites one guest", + other: "He invites # guests" + }), + female: \`She is \${gender}\`, + other: someOtherExp + }); + `, + expected: ` + import { i18n } from "@lingui/core"; + i18n._( + /*i18n*/ + { + id: "j9PNNm", + message: "{gender, select, male {{numOfGuests, plural, one {He invites one guest} other {He invites # guests}}} female {She is {gender}} other {{someOtherExp}}}", + values: { + gender: gender, + numOfGuests: numOfGuests, + someOtherExp: someOtherExp, + }, + } + ); + `, + }, ] export default cases diff --git a/packages/macro/test/jsx-plural.ts b/packages/macro/test/jsx-plural.ts index 0f774c95f..17a035894 100644 --- a/packages/macro/test/jsx-plural.ts +++ b/packages/macro/test/jsx-plural.ts @@ -202,6 +202,33 @@ const cases: TestCase[] = [ }} />; `, }, + { + input: ` + import { Plural } from '@lingui/macro'; + A lot of them} + />; + `, + expected: ` + import { Trans } from "@lingui/react"; + A lot of them}}" + } + values={{ + count: count, + oneText: oneText, + }} + components={{ + 0: + }} + />; + `, + }, { filename: `jsx-plural-select-nested.js`, }, diff --git a/packages/macro/test/jsx-select.ts b/packages/macro/test/jsx-select.ts index bfdc29f31..5fa938b9a 100644 --- a/packages/macro/test/jsx-select.ts +++ b/packages/macro/test/jsx-select.ts @@ -77,5 +77,25 @@ const cases: TestCase[] = [ />; `, }, + { + input: ` + import { Select } from '@lingui/macro'; +