From 7d56f3219c36693186467851c4572545e38d431e Mon Sep 17 00:00:00 2001 From: Sergio Date: Tue, 27 Oct 2020 18:20:58 +0100 Subject: [PATCH 1/3] fix: import date-number from i18n instance --- .../v2-to-v3/changeMacrosToCore.input.js | 3 +- .../v2-to-v3/changeMacrosToCore.output.js | 4 +-- .../v2-to-v3/complete.input.js | 2 +- .../v2-to-v3/complete.output.js | 8 ++--- .../v2-to-v3/jsxTransformMacros.input.js | 23 +++++++++++++- .../v2-to-v3/jsxTransformMacros.output.js | 31 +++++++++++++++---- transforms/v2-to-v3.ts | 26 +++++++++++----- 7 files changed, 75 insertions(+), 22 deletions(-) diff --git a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js index 40277f2..493b054 100644 --- a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js +++ b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js @@ -1,2 +1,3 @@ import { React } from "react"; -import { t, date } from "@lingui/macro"; \ No newline at end of file +import { t, date } from "@lingui/macro"; +import { Trans, NumberFormat } from "@lingui/react"; \ No newline at end of file diff --git a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js index 135a1e5..99a70c9 100644 --- a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js +++ b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js @@ -1,3 +1,3 @@ import { React } from "react"; -import { date } from "@lingui/core"; -import { t } from "@lingui/macro"; +import { i18n } from "@lingui/core"; +import { t, Trans } from "@lingui/macro"; \ No newline at end of file diff --git a/transforms/__testfixtures__/v2-to-v3/complete.input.js b/transforms/__testfixtures__/v2-to-v3/complete.input.js index b148361..c1ee9cd 100644 --- a/transforms/__testfixtures__/v2-to-v3/complete.input.js +++ b/transforms/__testfixtures__/v2-to-v3/complete.input.js @@ -2,7 +2,7 @@ import React from "react"; import { NumberFormat, DateFormat, Trans, withI18n } from "@lingui/react" import { plural } from "@lingui/macro" -const App = ({ i18n }) => { +const App = () => { return (
diff --git a/transforms/__testfixtures__/v2-to-v3/complete.output.js b/transforms/__testfixtures__/v2-to-v3/complete.output.js index 8f9ed2e..5a3daef 100644 --- a/transforms/__testfixtures__/v2-to-v3/complete.output.js +++ b/transforms/__testfixtures__/v2-to-v3/complete.output.js @@ -1,16 +1,16 @@ import React from "react"; -import { number, date } from "@lingui/core"; +import { i18n } from "@lingui/core"; import { withI18n } from "@lingui/react"; import { plural, Trans } from "@lingui/macro"; -const App = ({ i18n }) => { +const App = () => { return (
- {number(1_000_000, { currency: "EUR" })} + {i18n.number(1_000_000, { currency: "EUR" })}
- {date(new Date(), { hour12: true })} + {i18n.date(new Date(), { hour12: true })}
Component to replace {/* TODO: if there isn't any with children we should keep lingui/react */} diff --git a/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js b/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js index 7942726..e3d3c4a 100644 --- a/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js +++ b/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js @@ -2,6 +2,7 @@ import React from "react"; import { DateFormat, NumberFormat } from "@lingui/react"; const GLOBAL_VALUE = new Date(); + const App = () => { return (
@@ -13,6 +14,26 @@ const App = () => { style: "currency", maximumFractionDigits: 2 }} /> + {true ? ( + + ) : false}
); -} \ No newline at end of file +} + +const formatfValue = (value) => { + return ( + + ) +}; + +const formatAssetValue = (value) => { + if (value !== null) { + const formatValue = ; + return formatValue; + } + return "-"; +}; \ No newline at end of file diff --git a/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.output.js b/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.output.js index 74ff83c..7d32b43 100644 --- a/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.output.js +++ b/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.output.js @@ -1,18 +1,37 @@ import React from "react"; -import { number, date } from "@lingui/core"; +import { i18n } from "@lingui/core"; const GLOBAL_VALUE = new Date(); + const App = () => { return (
- {date(new Date(), { hour12: true })} - {date(new Date())} - {date(GLOBAL_VALUE)} - {date("10/01/2015")} - {number(10, { + {i18n.date(new Date(), { hour12: true })} + {i18n.date(new Date())} + {i18n.date(GLOBAL_VALUE)} + {i18n.date("10/01/2015")} + {i18n.number(10, { style: "currency", maximumFractionDigits: 2 })} + {true ? ( + i18n.number(10, { + style: "currency", + maximumFractionDigits: 2 + }) + ) : false}
); } + +const formatfValue = (value) => { + return (i18n.number(10, { style: "currency", maximumFractionDigits: 2 })); +}; + +const formatAssetValue = (value) => { + if (value !== null) { + const formatValue = i18n.number(value, {style: "percent", minimumFractionDigits: 2 }); + return formatValue; + } + return "-"; +}; \ No newline at end of file diff --git a/transforms/v2-to-v3.ts b/transforms/v2-to-v3.ts index cb2a0e0..77303ac 100644 --- a/transforms/v2-to-v3.ts +++ b/transforms/v2-to-v3.ts @@ -25,11 +25,11 @@ function changeJsxToCoreDeprecatedFuncs(root, j: JSCodeshift) { [ { component: "DateFormat", - macro: "date" + macro: "i18n.date" }, { component: "NumberFormat", - macro: "number" + macro: "i18n.number" } ].forEach(mapper => { root @@ -60,7 +60,17 @@ function changeJsxToCoreDeprecatedFuncs(root, j: JSCodeshift) { } // if someone uses the components inside ternaries we can't add {number()}, must be just number() - return path.parentPath.value.type === "ConditionalExpression" ? ast : j.jsxExpressionContainer(ast); + if (path.parentPath.value.type === "ConditionalExpression" || path.parentPath.value.type === "VariableDeclarator") { + return ast + } + + // if is a direct return, just add parenthesis + if (path.parentPath.value.type === "ReturnStatement") { + return j.parenthesizedExpression(ast); + } + + // if not, just add {} + return j.jsxExpressionContainer(ast); }); }) @@ -80,8 +90,8 @@ function changeReactImportToNewImports(root: Collection , j: JSCodeshift) { }); migrateTo(root, linguiReactImports, j, "Trans", "Trans", "@lingui/macro"); - migrateTo(root, linguiReactImports, j, "NumberFormat", "number", "@lingui/core"); - migrateTo(root, linguiReactImports, j, "DateFormat", "date", "@lingui/core"); + migrateTo(root, linguiReactImports, j, "NumberFormat", "i18n", "@lingui/core"); + migrateTo(root, linguiReactImports, j, "DateFormat", "i18n", "@lingui/core"); } /** @@ -96,8 +106,8 @@ function changeFromMacroToCore(root: Collection , j: JSCodeshift) { }); migrateTo(root, linguiMacroImports, j, "number", "number", "@lingui/core"); - migrateTo(root, linguiMacroImports, j, "date", "date", "@lingui/core"); - migrateTo(root, linguiMacroImports, j, "NumberFormat", "number", "@lingui/core"); + migrateTo(root, linguiMacroImports, j, "date", "i18n", "@lingui/core"); + migrateTo(root, linguiMacroImports, j, "NumberFormat", "i18n", "@lingui/core"); } /** @@ -120,8 +130,10 @@ function migrateTo(root, linguiReactImports, j, lookupImport, newLookupImport, n value: newPackageName } }); + linguiReactImports.forEach((path) => { const node = path.value; + if (!node) return; const transImportIndex = node.specifiers.findIndex((el) => el.imported.name === lookupImport); if (transImportIndex !== -1) { From 18309883b5842bbefffb94b915cda40acc4cc6d0 Mon Sep 17 00:00:00 2001 From: Sergio Date: Wed, 28 Oct 2020 11:15:16 +0100 Subject: [PATCH 2/3] fix: pending react-components to macro migrated --- .../v2-to-v3/changeMacrosToCore.input.js | 2 +- .../v2-to-v3/changeMacrosToCore.output.js | 2 +- .../v2-to-v3/jsxTransformMacros.input.js | 29 +++++++- .../v2-to-v3/jsxTransformMacros.output.js | 28 ++++++++ transforms/legacy.ts | 70 +++++++++++++++++++ transforms/v2-to-v3.ts | 4 ++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 transforms/legacy.ts diff --git a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js index 493b054..ad5dc2f 100644 --- a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js +++ b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js @@ -1,3 +1,3 @@ import { React } from "react"; import { t, date } from "@lingui/macro"; -import { Trans, NumberFormat } from "@lingui/react"; \ No newline at end of file +import { Trans, NumberFormat, Plural } from "@lingui/react"; \ No newline at end of file diff --git a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js index 99a70c9..c545424 100644 --- a/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js +++ b/transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js @@ -1,3 +1,3 @@ import { React } from "react"; import { i18n } from "@lingui/core"; -import { t, Trans } from "@lingui/macro"; \ No newline at end of file +import { t, Plural, Trans } from "@lingui/macro"; \ No newline at end of file diff --git a/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js b/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js index e3d3c4a..f7ed0eb 100644 --- a/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js +++ b/transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.input.js @@ -1,9 +1,10 @@ import React from "react"; -import { DateFormat, NumberFormat } from "@lingui/react"; +import { DateFormat, NumberFormat, Plural, SelectOrdinal, Select } from "@lingui/react"; const GLOBAL_VALUE = new Date(); const App = () => { + const count = 1; return (
@@ -20,6 +21,32 @@ const App = () => { maximumFractionDigits: 2 }} /> ) : false} + + +
); } diff --git a/transforms/legacy.ts b/transforms/legacy.ts new file mode 100644 index 0000000..b3e726f --- /dev/null +++ b/transforms/legacy.ts @@ -0,0 +1,70 @@ +/** This file has some functions that aren't used by could be used in the future */ + +/** Change JSX Elements to simple functions + * -> jsx(value, { _1: "hola" }) + */ +function changeJsxPluralToMacro(root, j) { + [ + { + component: 'Plural', + macro: 'plural', + }, + { + component: 'Select', + macro: 'select', + }, + { + component: 'SelectOrdinal', + macro: 'selectOrdinal', + }, + ].forEach((mapper) => { + root + .find(j.JSXElement, { + openingElement: { name: { name: mapper.component } } + }) + .replaceWith((path) => { + const Node = path.value; + + const valueProp = Node.openingElement.attributes.filter( + (obj) => obj.name.name === "value" + )[0]; + const propsToObject = j.objectExpression( + Node.openingElement.attributes + .filter(el => el.name.name !== "value") + .map( + (obj) => j.property( + "init", + j.identifier(obj.name.name), + j.literal(obj.value.value) + ) + ) + ) + + let ast = null; + // format options are not required so + if (!propsToObject.properties.length) { + ast = j.callExpression(j.identifier(mapper.macro), [ + valueProp.value.expression, + ]); + } else { + ast = j.callExpression(j.identifier(mapper.macro), [ + valueProp.value.expression, + propsToObject + ]); + } + + // if someone uses the components inside ternaries we can't add {number()}, must be just number() + if (path.parentPath.value.type === "ConditionalExpression" || path.parentPath.value.type === "VariableDeclarator") { + return ast + } + + // if is a direct return, just add parenthesis + if (path.parentPath.value.type === "ReturnStatement") { + return j.parenthesizedExpression(ast); + } + + // if not, just add {} + return j.jsxExpressionContainer(ast); + }); + }) +} \ No newline at end of file diff --git a/transforms/v2-to-v3.ts b/transforms/v2-to-v3.ts index 77303ac..547175c 100644 --- a/transforms/v2-to-v3.ts +++ b/transforms/v2-to-v3.ts @@ -89,7 +89,11 @@ function changeReactImportToNewImports(root: Collection , j: JSCodeshift) { } }); + migrateTo(root, linguiReactImports, j, "Plural", "Plural", "@lingui/macro"); + migrateTo(root, linguiReactImports, j, "Select", "Select", "@lingui/macro"); + migrateTo(root, linguiReactImports, j, "SelectOrdinal", "SelectOrdinal", "@lingui/macro"); migrateTo(root, linguiReactImports, j, "Trans", "Trans", "@lingui/macro"); + migrateTo(root, linguiReactImports, j, "NumberFormat", "i18n", "@lingui/core"); migrateTo(root, linguiReactImports, j, "DateFormat", "i18n", "@lingui/core"); } From 0f21a903697b1489c3eb7de4c660eb7e846a2196 Mon Sep 17 00:00:00 2001 From: Sergio Date: Wed, 28 Oct 2020 13:04:12 +0100 Subject: [PATCH 3/3] chore: log codemod version inside command --- bin/cli.ts | 5 +++-- tsconfig.json | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/cli.ts b/bin/cli.ts index 10e13a6..96a757a 100755 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -5,6 +5,7 @@ import path from 'path'; import execa from 'execa'; import chalk from 'chalk'; import isGitClean from 'is-git-clean'; +import pkg from "../package.json"; const transformerDirectory = path.join(__dirname, '../', 'transforms'); const jscodeshiftExecutable = require.resolve('.bin/jscodeshift'); @@ -151,7 +152,7 @@ function expandFilePathsIfNeeded(filesBeforeExpansion) { function run() { const cli = meow( { - description: 'Codemods for @lingui APIs.', + description: `Codemods for @lingui APIs. Version: ${pkg.version}`, help: ` Usage $ npx lingui-codemod <...options> @@ -166,7 +167,7 @@ function run() { ` }, { - boolean: ['force', 'dry', 'print', 'remove-unused', 'help'], + boolean: ['force', 'dry', 'print', 'remove-unused-imports', 'help'], string: ['_'], alias: { h: 'help' diff --git a/tsconfig.json b/tsconfig.json index 1db15b6..571df81 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "module": "commonjs", "target": "es6", "esModuleInterop": true, + "resolveJsonModule": true, "jsx": "preserve" }, "include": [