Skip to content

Commit dab3838

Browse files
authored
feat: Convert t tags with ids to new object syntax (#7)
1 parent fc3e706 commit dab3838

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

transforms/__testfixtures__/v2-to-v3/macroWrap.input.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { i18n } from "@lingui/core";
22
import { t, plural, select, selectOrdinal } from "@lingui/macro";
33

44
i18n._(t`Some input`)
5+
i18n._(t('my.id')`Some input`)
56
i18n._(
67
plural({
78
value: 1,
@@ -23,4 +24,4 @@ i18n._(
2324
other: 'SORT_#_RESULT_BY:',
2425
zero: 'SORT_#_RESULTS_BY:'
2526
})
26-
)
27+
)

transforms/__testfixtures__/v2-to-v3/macroWrap.output.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { i18n } from "@lingui/core";
22
import { t, plural, select, selectOrdinal } from "@lingui/macro";
33

44
t`Some input`
5+
t({
6+
id: "my.id",
7+
message: `Some input`
8+
})
59
plural(1, {
610
one: 'SORT_#_RESULT_BY:',
711
other: 'SORT_#_RESULT_BY:',
@@ -16,4 +20,4 @@ selectOrdinal({
1620
one: 'SORT_#_RESULT_BY:',
1721
other: 'SORT_#_RESULT_BY:',
1822
zero: 'SORT_#_RESULTS_BY:'
19-
})
23+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { i18n } from "@lingui/core";
2+
import { t, plural, select, selectOrdinal } from "@lingui/macro";
3+
4+
t`Some input`
5+
t('my.id')`Some input`
6+
const value = 1
7+
t('my.id')`Some value (${value})`
8+
t('my.id')`Some complex value (${value.toFixed(2)})`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { i18n } from "@lingui/core";
2+
import { t, plural, select, selectOrdinal } from "@lingui/macro";
3+
4+
t`Some input`
5+
t({
6+
id: "my.id",
7+
message: `Some input`
8+
})
9+
const value = 1
10+
t({
11+
id: "my.id",
12+
message: `Some value (${value})`
13+
})
14+
t({
15+
id: "my.id",
16+
message: `Some complex value (${value.toFixed(2)})`
17+
})

transforms/__tests__/v2-to-v3.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("Deprecated methods inside @lingui/react moved to @core and @macro tran
2828
});
2929

3030

31-
describe("i18n._(t`name`) is not required, use intead simply t`name`", () => {
31+
describe("i18n._(t`name`) is not required, use instead simply t`name`", () => {
3232
defineTest(
3333
__dirname,
3434
"v2-to-v3",
@@ -62,4 +62,13 @@ describe("Complete example with all the past actions", () => {
6262
null,
6363
"v2-to-v3/complete",
6464
);
65-
});
65+
});
66+
67+
describe("t props changed from an argument, to an object", () => {
68+
defineTest(
69+
__dirname,
70+
"v2-to-v3",
71+
null,
72+
"v2-to-v3/t",
73+
);
74+
});

transforms/v2-to-v3.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const transform: Transform = (fileInfo, api, options) => {
1010
changeJsxToCoreDeprecatedFuncs(root, j)
1111
changeFromMacroToCore(root, j)
1212
pluralPropsChanges(root, j)
13+
tWithIdPropsChanges(root, j)
1314

1415
return root.toSource(options.printOptions);
1516
};
@@ -235,4 +236,31 @@ function removeMacroWrap(root, j: JSCodeshift) {
235236
const { node } = nodePath;
236237
return node.arguments;
237238
})
238-
}
239+
}
240+
241+
/**
242+
* t arguments changed. Id needs to be passed as a part of an object.
243+
* t('id')'Message') => t({ id: 'id', message: `Message` })
244+
* No attempts are made to convert template literal into a regular string; this
245+
* is a project-specific codestyle rule that should be fixed with Prettier or
246+
* eslint.
247+
*/
248+
function tWithIdPropsChanges(root, j: JSCodeshift) {
249+
return root
250+
.find(j.TaggedTemplateExpression, {
251+
tag: {
252+
callee: {
253+
name: "t"
254+
}
255+
}
256+
})
257+
.replaceWith((nodePath) => {
258+
const id = nodePath.node.tag.arguments[0].value;
259+
return j.callExpression(nodePath.node.tag.callee, [
260+
j.objectExpression([
261+
j.property("init", j.identifier("id"), j.literal(id)),
262+
j.property("init", j.identifier("message"), nodePath.node.quasi)
263+
])
264+
]);
265+
})
266+
}

0 commit comments

Comments
 (0)