Skip to content

Commit

Permalink
fix: expression only choices / select options (#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhou-glean authored Mar 16, 2023
1 parent 4f1d30e commit db6e497
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 4 deletions.
22 changes: 19 additions & 3 deletions packages/macro/src/macroJs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
}),
}),
},
})
})
Expand Down
6 changes: 5 additions & 1 deletion packages/macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions packages/macro/test/js-plural.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions packages/macro/test/js-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions packages/macro/test/jsx-plural.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ const cases: TestCase[] = [
}} />;
`,
},
{
input: `
import { Plural } from '@lingui/macro';
<Plural
value={count}
_0="Zero items"
one={oneText}
other={<a href="/more">A lot of them</a>}
/>;
`,
expected: `
import { Trans } from "@lingui/react";
<Trans
id={"EQvNfC"}
message={
"{count, plural, =0 {Zero items} one {{oneText}} other {<0>A lot of them</0>}}"
}
values={{
count: count,
oneText: oneText,
}}
components={{
0: <a href="/more" />
}}
/>;
`,
},
{
filename: `jsx-plural-select-nested.js`,
},
Expand Down
20 changes: 20 additions & 0 deletions packages/macro/test/jsx-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,25 @@ const cases: TestCase[] = [
/>;
`,
},
{
input: `
import { Select } from '@lingui/macro';
<Select
id="msg.select"
render="strong"
value={user.gender}
_male="He"
_female={\`She\`}
other={otherText}
/>;
`,
expected: `
import { Trans } from "@lingui/react";
<Trans render="strong" id="msg.select" message={"{0, select, male {He} female {She} other {{otherText}}}"} values={{
0: user.gender,
otherText: otherText
}} />;
`,
},
]
export default cases

0 comments on commit db6e497

Please sign in to comment.