Skip to content

Commit 162bf10

Browse files
author
Sergio
committed
feat: added i18n transform and plural
1 parent 38c330e commit 162bf10

File tree

8 files changed

+93
-34
lines changed

8 files changed

+93
-34
lines changed

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

-26
This file was deleted.

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from "react";
2+
import { I18nProvider } from "@lingui/react"
3+
4+
export const App = () => {
5+
return (
6+
<I18nProvider defaultRender="p">
7+
<div>hola</div>
8+
</I18nProvider>
9+
);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from "react";
2+
import { I18nProvider } from "@lingui/react"
3+
4+
export const App = () => {
5+
return (
6+
<I18nProvider defaultComponent="p">
7+
<div>hola</div>
8+
</I18nProvider>
9+
);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { plural } from "@lingui/macro"
2+
3+
const value = "value";
4+
plural({ value: "hola", one: "a", other: "b" });
5+
plural({ value, one: "a", other: "b" });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { plural } from "@lingui/macro"
2+
3+
const value = "value";
4+
plural("hola", {
5+
one: "a",
6+
other: "b"
7+
});
8+
plural(value, {
9+
one: "a",
10+
other: "b"
11+
});

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { defineTest } from "jscodeshift/dist/testUtils";
22

3-
describe("v2-to-v3", () => {
3+
describe("plural transform", () => {
44
defineTest(
55
__dirname,
66
"v2-to-v3",
77
null,
8-
"v2-to-v3/basic",
8+
"v2-to-v3/plural",
9+
);
10+
});
11+
12+
describe("i18nProvider transform", () => {
13+
defineTest(
14+
__dirname,
15+
"v2-to-v3",
16+
null,
17+
"v2-to-v3/i18nProvider",
918
);
1019
});

transforms/v2-to-v3.ts

+46-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const transform: Transform = (fileInfo, api) => {
44
const j = api.jscodeshift;
55
const root = j(fileInfo.source);
66

7-
changeToCoreDeprecatedFuncs(root)
8-
changeReactImportToMacro(root)
9-
pluralPropsChanges(root)
10-
replaceDeprecatedMethodsToMacros(root)
7+
renameDefaultRenderToDefaultComponent(root, j)
8+
pluralPropsChanges(root, j)
9+
// changeReactImportToMacro(root, j)
10+
// changeToCoreDeprecatedFuncs(root)
11+
// replaceDeprecatedMethodsToMacros(root)
12+
// removeMacroWrap(root)
1113

1214
return root.toSource();
1315
};
@@ -24,20 +26,58 @@ function changeToCoreDeprecatedFuncs(root) {
2426
/**
2527
* Change import of components to macro from react package
2628
*/
27-
function changeReactImportToMacro(root) {
29+
function changeReactImportToMacro(root, j) {
2830
}
2931

3032
/**
3133
* plural parameters changed:
3234
* - plural({ value, one: "# book", other: "# books" })
3335
* + plural(value, { one: "# book", other: "# books" })
3436
*/
35-
function pluralPropsChanges(root) {
37+
function pluralPropsChanges(root, j) {
38+
return root.find(j.CallExpression, {
39+
callee: {
40+
name: "plural"
41+
}
42+
}).forEach(element => {
43+
if (element.node.arguments.length === 1) {
44+
element.node.arguments[0].properties.map((node, index) => {
45+
if (node.key.name === "value") {
46+
element.node.arguments[0].properties.splice(index, 1)
47+
element.node.arguments.unshift(node.value)
48+
}
49+
});
50+
}
51+
})
3652
}
3753

3854
/**
3955
* i18n.t(), i18n.plural(), i18n.select() and i18n.selectOrdinal()
4056
* methods are removed and replaced with macros.
4157
*/
4258
function replaceDeprecatedMethodsToMacros(root) {
59+
}
60+
61+
/**
62+
* Rename I18nProvider.defaultRender prop to I18nProvider.defaultComponent
63+
*/
64+
function renameDefaultRenderToDefaultComponent(root, j) {
65+
return root.find(j.JSXElement, {
66+
openingElement: { name: { name: "I18nProvider" } }
67+
}).forEach(path => {
68+
const Node = path.value;
69+
70+
Node.openingElement.attributes
71+
.filter(obj => obj.name.name === "defaultRender")
72+
.forEach(item => {
73+
item.name.name = "defaultComponent";
74+
});
75+
})
76+
}
77+
78+
/**
79+
* Macros don't need to be wrapped inside i18n._: i18n._(t'Message') => t'Message'
80+
*/
81+
function removeMacroWrap(root) {
82+
4383
}

0 commit comments

Comments
 (0)