Skip to content

Commit 7d56f32

Browse files
author
Sergio
committed
fix: import date-number from i18n instance
1 parent 083d2b8 commit 7d56f32

7 files changed

+75
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import { React } from "react";
2-
import { t, date } from "@lingui/macro";
2+
import { t, date } from "@lingui/macro";
3+
import { Trans, NumberFormat } from "@lingui/react";
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { React } from "react";
2-
import { date } from "@lingui/core";
3-
import { t } from "@lingui/macro";
2+
import { i18n } from "@lingui/core";
3+
import { t, Trans } from "@lingui/macro";

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import { NumberFormat, DateFormat, Trans, withI18n } from "@lingui/react"
33
import { plural } from "@lingui/macro"
44

5-
const App = ({ i18n }) => {
5+
const App = () => {
66
return (
77
<div>
88
<div>

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React from "react";
2-
import { number, date } from "@lingui/core";
2+
import { i18n } from "@lingui/core";
33
import { withI18n } from "@lingui/react";
44
import { plural, Trans } from "@lingui/macro";
55

6-
const App = ({ i18n }) => {
6+
const App = () => {
77
return (
88
<div>
99
<div>
10-
{number(1_000_000, { currency: "EUR" })}
10+
{i18n.number(1_000_000, { currency: "EUR" })}
1111
</div>
1212
<div>
13-
{date(new Date(), { hour12: true })}
13+
{i18n.date(new Date(), { hour12: true })}
1414
</div>
1515
<Trans>Component to replace</Trans>
1616
{/* TODO: if there isn't any with children we should keep lingui/react */}

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import { DateFormat, NumberFormat } from "@lingui/react";
33

44
const GLOBAL_VALUE = new Date();
5+
56
const App = () => {
67
return (
78
<div>
@@ -13,6 +14,26 @@ const App = () => {
1314
style: "currency",
1415
maximumFractionDigits: 2
1516
}} />
17+
{true ? (
18+
<NumberFormat value={10} format={{
19+
style: "currency",
20+
maximumFractionDigits: 2
21+
}} />
22+
) : false}
1623
</div>
1724
);
18-
}
25+
}
26+
27+
const formatfValue = (value) => {
28+
return (
29+
<NumberFormat value={10} format={{ style: "currency", maximumFractionDigits: 2 }} />
30+
)
31+
};
32+
33+
const formatAssetValue = (value) => {
34+
if (value !== null) {
35+
const formatValue = <NumberFormat value={value} format={{style: "percent", minimumFractionDigits: 2 }} />;
36+
return formatValue;
37+
}
38+
return "-";
39+
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
import React from "react";
2-
import { number, date } from "@lingui/core";
2+
import { i18n } from "@lingui/core";
33

44
const GLOBAL_VALUE = new Date();
5+
56
const App = () => {
67
return (
78
<div>
8-
{date(new Date(), { hour12: true })}
9-
{date(new Date())}
10-
{date(GLOBAL_VALUE)}
11-
{date("10/01/2015")}
12-
{number(10, {
9+
{i18n.date(new Date(), { hour12: true })}
10+
{i18n.date(new Date())}
11+
{i18n.date(GLOBAL_VALUE)}
12+
{i18n.date("10/01/2015")}
13+
{i18n.number(10, {
1314
style: "currency",
1415
maximumFractionDigits: 2
1516
})}
17+
{true ? (
18+
i18n.number(10, {
19+
style: "currency",
20+
maximumFractionDigits: 2
21+
})
22+
) : false}
1623
</div>
1724
);
1825
}
26+
27+
const formatfValue = (value) => {
28+
return (i18n.number(10, { style: "currency", maximumFractionDigits: 2 }));
29+
};
30+
31+
const formatAssetValue = (value) => {
32+
if (value !== null) {
33+
const formatValue = i18n.number(value, {style: "percent", minimumFractionDigits: 2 });
34+
return formatValue;
35+
}
36+
return "-";
37+
};

transforms/v2-to-v3.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ function changeJsxToCoreDeprecatedFuncs(root, j: JSCodeshift) {
2525
[
2626
{
2727
component: "DateFormat",
28-
macro: "date"
28+
macro: "i18n.date"
2929
},
3030
{
3131
component: "NumberFormat",
32-
macro: "number"
32+
macro: "i18n.number"
3333
}
3434
].forEach(mapper => {
3535
root
@@ -60,7 +60,17 @@ function changeJsxToCoreDeprecatedFuncs(root, j: JSCodeshift) {
6060
}
6161

6262
// if someone uses the components inside ternaries we can't add {number()}, must be just number()
63-
return path.parentPath.value.type === "ConditionalExpression" ? ast : j.jsxExpressionContainer(ast);
63+
if (path.parentPath.value.type === "ConditionalExpression" || path.parentPath.value.type === "VariableDeclarator") {
64+
return ast
65+
}
66+
67+
// if is a direct return, just add parenthesis
68+
if (path.parentPath.value.type === "ReturnStatement") {
69+
return j.parenthesizedExpression(ast);
70+
}
71+
72+
// if not, just add {}
73+
return j.jsxExpressionContainer(ast);
6474
});
6575
})
6676

@@ -80,8 +90,8 @@ function changeReactImportToNewImports(root: Collection , j: JSCodeshift) {
8090
});
8191

8292
migrateTo(root, linguiReactImports, j, "Trans", "Trans", "@lingui/macro");
83-
migrateTo(root, linguiReactImports, j, "NumberFormat", "number", "@lingui/core");
84-
migrateTo(root, linguiReactImports, j, "DateFormat", "date", "@lingui/core");
93+
migrateTo(root, linguiReactImports, j, "NumberFormat", "i18n", "@lingui/core");
94+
migrateTo(root, linguiReactImports, j, "DateFormat", "i18n", "@lingui/core");
8595
}
8696

8797
/**
@@ -96,8 +106,8 @@ function changeFromMacroToCore(root: Collection , j: JSCodeshift) {
96106
});
97107

98108
migrateTo(root, linguiMacroImports, j, "number", "number", "@lingui/core");
99-
migrateTo(root, linguiMacroImports, j, "date", "date", "@lingui/core");
100-
migrateTo(root, linguiMacroImports, j, "NumberFormat", "number", "@lingui/core");
109+
migrateTo(root, linguiMacroImports, j, "date", "i18n", "@lingui/core");
110+
migrateTo(root, linguiMacroImports, j, "NumberFormat", "i18n", "@lingui/core");
101111
}
102112

103113
/**
@@ -120,8 +130,10 @@ function migrateTo(root, linguiReactImports, j, lookupImport, newLookupImport, n
120130
value: newPackageName
121131
}
122132
});
133+
123134
linguiReactImports.forEach((path) => {
124135
const node = path.value;
136+
if (!node) return;
125137
const transImportIndex = node.specifiers.findIndex((el) => el.imported.name === lookupImport);
126138

127139
if (transImportIndex !== -1) {

0 commit comments

Comments
 (0)