Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Convert t tags with ids to new object syntax #7

Merged
merged 2 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion transforms/__testfixtures__/v2-to-v3/macroWrap.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { i18n } from "@lingui/core";
import { t, plural, select, selectOrdinal } from "@lingui/macro";

i18n._(t`Some input`)
i18n._(t('my.id')`Some input`)
i18n._(
plural({
value: 1,
Expand All @@ -23,4 +24,4 @@ i18n._(
other: 'SORT_#_RESULT_BY:',
zero: 'SORT_#_RESULTS_BY:'
})
)
)
6 changes: 5 additions & 1 deletion transforms/__testfixtures__/v2-to-v3/macroWrap.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { i18n } from "@lingui/core";
import { t, plural, select, selectOrdinal } from "@lingui/macro";

t`Some input`
t({
id: "my.id",
message: `Some input`
})
plural(1, {
one: 'SORT_#_RESULT_BY:',
other: 'SORT_#_RESULT_BY:',
Expand All @@ -16,4 +20,4 @@ selectOrdinal({
one: 'SORT_#_RESULT_BY:',
other: 'SORT_#_RESULT_BY:',
zero: 'SORT_#_RESULTS_BY:'
})
})
8 changes: 8 additions & 0 deletions transforms/__testfixtures__/v2-to-v3/t.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { i18n } from "@lingui/core";
import { t, plural, select, selectOrdinal } from "@lingui/macro";

t`Some input`
t('my.id')`Some input`
const value = 1
t('my.id')`Some value (${value})`
t('my.id')`Some complex value (${value.toFixed(2)})`
17 changes: 17 additions & 0 deletions transforms/__testfixtures__/v2-to-v3/t.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { i18n } from "@lingui/core";
import { t, plural, select, selectOrdinal } from "@lingui/macro";

t`Some input`
t({
id: "my.id",
message: `Some input`
})
const value = 1
t({
id: "my.id",
message: `Some value (${value})`
})
t({
id: "my.id",
message: `Some complex value (${value.toFixed(2)})`
})
13 changes: 11 additions & 2 deletions transforms/__tests__/v2-to-v3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Deprecated methods inside @lingui/react moved to @core and @macro tran
});


describe("i18n._(t`name`) is not required, use intead simply t`name`", () => {
describe("i18n._(t`name`) is not required, use instead simply t`name`", () => {
defineTest(
__dirname,
"v2-to-v3",
Expand Down Expand Up @@ -62,4 +62,13 @@ describe("Complete example with all the past actions", () => {
null,
"v2-to-v3/complete",
);
});
});

describe("t props changed from an argument, to an object", () => {
defineTest(
__dirname,
"v2-to-v3",
null,
"v2-to-v3/t",
);
});
30 changes: 29 additions & 1 deletion transforms/v2-to-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const transform: Transform = (fileInfo, api, options) => {
changeJsxToCoreDeprecatedFuncs(root, j)
changeFromMacroToCore(root, j)
pluralPropsChanges(root, j)
tWithIdPropsChanges(root, j)

return root.toSource(options.printOptions);
};
Expand Down Expand Up @@ -235,4 +236,31 @@ function removeMacroWrap(root, j: JSCodeshift) {
const { node } = nodePath;
return node.arguments;
})
}
}

/**
* t arguments changed. Id needs to be passed as a part of an object.
* t('id')'Message') => t({ id: 'id', message: `Message` })
* No attempts are made to convert template literal into a regular string; this
* is a project-specific codestyle rule that should be fixed with Prettier or
* eslint.
*/
function tWithIdPropsChanges(root, j: JSCodeshift) {
return root
.find(j.TaggedTemplateExpression, {
tag: {
callee: {
name: "t"
}
}
})
.replaceWith((nodePath) => {
const id = nodePath.node.tag.arguments[0].value;
return j.callExpression(nodePath.node.tag.callee, [
j.objectExpression([
j.property("init", j.identifier("id"), j.literal(id)),
j.property("init", j.identifier("message"), nodePath.node.quasi)
])
]);
})
}