Skip to content

Commit

Permalink
Update macro to wrap defaults in brackets when necessary (#1472)
Browse files Browse the repository at this point in the history
* Update macro to wrap defaults in curly brackets

* Fixup, add test
  • Loading branch information
heyimalex authored Mar 25, 2022
1 parent 8e1e852 commit a7af333
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
17 changes: 14 additions & 3 deletions icu.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,20 @@ function buildTransElement(

// add generated Trans attributes
if (!attributeExistsAlready('defaults', finalAttributes))
finalAttributes.push(
t.jSXAttribute(t.jSXIdentifier('defaults'), t.StringLiteral(extracted.defaults)),
);
if (extracted.defaults.includes(`"`)) {
// wrap defaults that contain double quotes in brackets
finalAttributes.push(
t.jSXAttribute(
t.jSXIdentifier('defaults'),
t.jSXExpressionContainer(t.StringLiteral(extracted.defaults)),
),
);
} else {
finalAttributes.push(
t.jSXAttribute(t.jSXIdentifier('defaults'), t.StringLiteral(extracted.defaults)),
);
}

if (!attributeExistsAlready('components', finalAttributes))
finalAttributes.push(
t.jSXAttribute(t.jSXIdentifier('components'), t.jSXExpressionContainer(extracted.components)),
Expand Down
15 changes: 15 additions & 0 deletions test/__snapshots__/icu.macro.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,18 @@ const x = <Trans i18nKey=\\"key\\" defaults=\\"<0>exciting!</0>{count, plural, =
}} />;
"
`;
exports[`macros 22. macros: 22. macros 1`] = `
"
import { Trans } from \\"../icu.macro\\";
const x = <Trans>Welcome, &quot;{ name }&quot;!</Trans>
↓ ↓ ↓ ↓ ↓ ↓
import { Trans } from \\"react-i18next\\";
const x = <Trans defaults={\\"Welcome, \\\\\\"{name}\\\\\\"!\\"} components={[]} values={{
name
}} />;
"
`;
5 changes: 5 additions & 0 deletions test/icu.macro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ pluginTester({
</Trans>
);
`,
`
import { Trans } from "../icu.macro";
const x = <Trans>Welcome, &quot;{ name }&quot;!</Trans>
`,
{
code: `
import React from "react"
Expand Down

0 comments on commit a7af333

Please sign in to comment.