-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from lingui/improved-codemod
fix: import date-number from i18n instance
- Loading branch information
Showing
10 changed files
with
209 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import { React } from "react"; | ||
import { t, date } from "@lingui/macro"; | ||
import { t, date } from "@lingui/macro"; | ||
import { Trans, NumberFormat, Plural } from "@lingui/react"; |
4 changes: 2 additions & 2 deletions
4
transforms/__testfixtures__/v2-to-v3/changeMacrosToCore.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, Plural, Trans } from "@lingui/macro"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 53 additions & 6 deletions
59
transforms/__testfixtures__/v2-to-v3/jsxTransformMacros.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,65 @@ | ||
import React from "react"; | ||
import { number, date } from "@lingui/core"; | ||
import { i18n } from "@lingui/core"; | ||
import { Plural, Select, SelectOrdinal } from "@lingui/macro"; | ||
|
||
const GLOBAL_VALUE = new Date(); | ||
|
||
const App = () => { | ||
const count = 1; | ||
return ( | ||
<div> | ||
{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} | ||
<Plural | ||
id="string" | ||
value={100} | ||
offset="number | string" | ||
zero="ReactNode" | ||
one="ReactNode" | ||
two="ReactNode" | ||
few="ReactNode" | ||
many="ReactNode" | ||
other="ReactNode" | ||
_1="_1" | ||
_2="_2" | ||
/> | ||
<SelectOrdinal | ||
value={count} | ||
one="#st" | ||
two="#nd" | ||
few="#rd" | ||
other="#th" | ||
/> | ||
<Select | ||
value={count} | ||
male="His book" | ||
female="Her book" | ||
other="Their book" | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
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 "-"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="hola" _1="hola" /> -> 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); | ||
}); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters