Skip to content

Commit 94278a7

Browse files
authored
fix: handle spread props for inner components in Trans (#1869) (#1877)
1 parent a5287b5 commit 94278a7

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

icu.macro.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ function jsxElementToDeclaration(jsxElement, t) {
373373
: t.stringLiteral(propName);
374374

375375
propsProperties.push(t.objectProperty(propKey, propValue));
376+
} else if (t.isJSXSpreadAttribute(attr)) {
377+
// Handle spread attributes like {...spreadProps}
378+
propsProperties.push(t.spreadElement(attr.argument));
376379
}
377380
});
378381

test/__snapshots__/icu.macro.spec.js.snap

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,3 +1204,64 @@ const UsageTracker = ({
12041204
export default UsageTracker;
12051205
"
12061206
`;
1207+
1208+
exports[`macros > 36. macros > 36. macros 1`] = `
1209+
"
1210+
1211+
import { Trans } from '../../../icu.macro'
1212+
1213+
const spreadProps = { className: 'test', id: 'bold-text' }
1214+
const x = <Trans><b {...spreadProps}>test</b></Trans>
1215+
1216+
1217+
↓ ↓ ↓ ↓ ↓ ↓
1218+
1219+
import { IcuTrans } from 'react-i18next';
1220+
const spreadProps = {
1221+
className: 'test',
1222+
id: 'bold-text',
1223+
};
1224+
const x = (
1225+
<IcuTrans
1226+
defaultTranslation="<0>test</0>"
1227+
content={[
1228+
{
1229+
type: 'b',
1230+
props: {
1231+
...spreadProps,
1232+
},
1233+
},
1234+
]}
1235+
values={{}}
1236+
/>
1237+
);
1238+
"
1239+
`;
1240+
1241+
exports[`macros > 37. macros > 37. macros 1`] = `
1242+
"
1243+
1244+
import { Trans } from '../../../icu.macro'
1245+
1246+
const x = <Trans><b aria-hidden>test</b></Trans>
1247+
1248+
1249+
↓ ↓ ↓ ↓ ↓ ↓
1250+
1251+
import { IcuTrans } from 'react-i18next';
1252+
const x = (
1253+
<IcuTrans
1254+
defaultTranslation="<0>test</0>"
1255+
content={[
1256+
{
1257+
type: 'b',
1258+
props: {
1259+
'aria-hidden': true,
1260+
},
1261+
},
1262+
]}
1263+
values={{}}
1264+
/>
1265+
);
1266+
"
1267+
`;

test/icu.macro.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,16 @@ pluginTester({
495495
snapshot: false,
496496
error: /Must pass a variable, not an expression to "number``" in "[^"]+" on line 8/,
497497
},
498+
`
499+
import { Trans } from '../../../icu.macro'
500+
501+
const spreadProps = { className: 'test', id: 'bold-text' }
502+
const x = <Trans><b {...spreadProps}>test</b></Trans>
503+
`,
504+
`
505+
import { Trans } from '../../../icu.macro'
506+
507+
const x = <Trans><b aria-hidden>test</b></Trans>
508+
`,
498509
],
499510
});

0 commit comments

Comments
 (0)