Skip to content

FIX: Add support for import aliases to i18n-import-location #113

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

Merged
merged 3 commits into from
Apr 15, 2025
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
19 changes: 14 additions & 5 deletions lint-configs/eslint-rules/i18n-import-location.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
message:
"Import from 'i18n' is not allowed. Use 'discourse-i18n' instead.",
fix(fixer) {
return fixer.replaceText(node.source, "'discourse-i18n'");
return fixer.replaceText(node.source, `"discourse-i18n"`);
},
});
}
Expand All @@ -35,13 +35,22 @@ export default {
fix(fixer) {
const canSafelyReplace =
node.specifiers.length === 1 &&
node.specifiers[0].type === "ImportDefaultSpecifier" &&
node.specifiers[0].local.name === "i18n";
node.specifiers[0].type === "ImportDefaultSpecifier";

if (!canSafelyReplace) {
return;
}

const localName = node.specifiers[0].local.name;
let sourceName = node.source.value.match(/([^/]+)$/)[0];
let importString;

if (localName === sourceName) {
importString = `${localName}`;
} else {
importString = `${sourceName} as ${localName}`;
}

const existingImport = context
.getSourceCode()
.ast.body.find(
Expand All @@ -54,13 +63,13 @@ export default {
return [
fixer.remove(node),
fixImport(fixer, existingImport, {
namedImportsToAdd: ["i18n"],
namedImportsToAdd: [importString],
}),
];
} else {
return fixer.replaceText(
node,
`import { i18n } from 'discourse-i18n';`
`import { ${importString} } from "discourse-i18n";`
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion lint-configs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@discourse/lint-configs",
"version": "2.13.1",
"version": "2.13.2",
"description": "Shareable lint configs for Discourse core, plugins, and themes",
"author": "Discourse",
"license": "MIT",
Expand Down
20 changes: 17 additions & 3 deletions test/eslint-rules/i18n-import-location.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ruleTester.run("i18n-import-location", rule, {
"Import from 'i18n' is not allowed. Use 'discourse-i18n' instead.",
},
],
output: "import { i18n } from 'discourse-i18n';",
output: `import { i18n } from "discourse-i18n";`,
},
{
code: "import i18n from 'discourse-common/helpers/i18n';",
Expand All @@ -28,7 +28,7 @@ ruleTester.run("i18n-import-location", rule, {
"Import from 'discourse-common/helpers/i18n' is not allowed. Use 'discourse-i18n' instead.",
},
],
output: "import { i18n } from 'discourse-i18n';",
output: `import { i18n } from "discourse-i18n";`,
},
{
code: "import i18n from 'discourse/helpers/i18n';",
Expand All @@ -38,7 +38,7 @@ ruleTester.run("i18n-import-location", rule, {
"Import from 'discourse/helpers/i18n' is not allowed. Use 'discourse-i18n' instead.",
},
],
output: "import { i18n } from 'discourse-i18n';",
output: `import { i18n } from "discourse-i18n";`,
},
{
code: "import i18n0 from 'discourse/helpers/i18n';",
Expand All @@ -48,6 +48,20 @@ ruleTester.run("i18n-import-location", rule, {
"Import from 'discourse/helpers/i18n' is not allowed. Use 'discourse-i18n' instead.",
},
],
output: `import { i18n as i18n0 } from "discourse-i18n";`,
},
{
code: `
import i18n0 from 'discourse/helpers/i18n';
import I18n from 'discourse-i18n';
`.replace(/^\s*|\s*$/gm, ""),
errors: [
{
message:
"Import from 'discourse/helpers/i18n' is not allowed. Use 'discourse-i18n' instead.",
},
],
output: `\nimport I18n, { i18n as i18n0 } from "discourse-i18n";`,
},
],
});