Skip to content
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
7 changes: 6 additions & 1 deletion icu.macro.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createMacro } = require('babel-plugin-macros');

Check warning on line 1 in icu.macro.js

View workflow job for this annotation

GitHub Actions / Check code quality (lint and format)

'babel-plugin-macros' should be listed in the project's dependencies, not devDependencies

// copy to:
// https://astexplorer.net/#/gist/642aebbb9e449e959f4ad8907b4adf3a/4a65742e2a3e926eb55eaa3d657d1472b9ac7970
Expand Down Expand Up @@ -364,7 +364,12 @@
propValue = t.nullLiteral();
}

propsProperties.push(t.objectProperty(t.identifier(propName), propValue));
// Use string literal for keys that aren't valid identifiers (e.g., contain hyphens)
const propKey = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propName)
? t.identifier(propName)
: t.stringLiteral(propName);

propsProperties.push(t.objectProperty(propKey, propValue));
}
});

Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/icu.macro.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,59 @@ const x = (
);
"
`;

exports[`macros > 23. macros > 23. macros 1`] = `
"

import { Trans } from "../../../icu.macro";

const x = <Trans data-cy="test">Welcome, { name }!</Trans>


↓ ↓ ↓ ↓ ↓ ↓

import { IcuTrans } from 'react-i18next';
const x = (
<IcuTrans
data-cy="test"
defaultTranslation="Welcome, {name}!"
content={[]}
values={{
name,
}}
/>
);
"
`;

exports[`macros > 24. macros > 24. macros 1`] = `
"

import { Trans } from "../../../icu.macro";

const x = <Trans data-cy="test" data-testid="trans-component">Welcome, <strong data-cy="name">{ name }</strong>!</Trans>


↓ ↓ ↓ ↓ ↓ ↓

import { IcuTrans } from 'react-i18next';
const x = (
<IcuTrans
data-cy="test"
data-testid="trans-component"
defaultTranslation="Welcome, <0>{name}</0>!"
content={[
{
type: 'strong',
props: {
'data-cy': 'name',
},
},
]}
values={{
name,
}}
/>
);
"
`;
10 changes: 10 additions & 0 deletions test/icu.macro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ pluginTester({

const x = <Trans>Welcome, &quot;{ name }&quot;!</Trans>
`,
`
import { Trans } from "../../../icu.macro";

const x = <Trans data-cy="test">Welcome, { name }!</Trans>
`,
`
import { Trans } from "../../../icu.macro";

const x = <Trans data-cy="test" data-testid="trans-component">Welcome, <strong data-cy="name">{ name }</strong>!</Trans>
`,
{
code: `
import React from "react"
Expand Down