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: Support ordered-imports option #1402

Merged
merged 4 commits into from Feb 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 112 additions & 9 deletions src/converters/lintConfigs/rules/ruleConverters/ordered-imports.ts
@@ -1,21 +1,124 @@
import { RuleConverter } from "../ruleConverter";

const unsupportedTSLintOptions = [
"import-sources-order",
"module-source-path",
"named-imports-order",
];

export const convertOrderedImports: RuleConverter = (tslintRule) => {
const notices = unsupportedTSLintOptions
.filter((option) => tslintRule.ruleArguments.includes(option))
.map((option) => `Option "${option}" is not supported by ESLint.`);
const argument = { ...tslintRule.ruleArguments[0] };
const notices: string[] = [];

const patternOptions = {
nocomment: true,
dot: true,
};

const importOrderRule = {
alphabetize: {
caseInsensitive: true,
order: "asc",
},
"newlines-between": "ignore",
groups: [
["builtin", "external", "internal", "unknown", "object", "type"],
"parent",
["sibling", "index"],
],
distinctGroup: false,
pathGroupsExcludedImportTypes: [],
pathGroups: [
{
pattern: "./",
patternOptions,
group: "sibling",
position: "before",
},

{
pattern: ".",
patternOptions,
group: "sibling",
position: "before",
},
{
pattern: "..",
patternOptions,
group: "parent",
position: "before",
},
{
pattern: "../",
patternOptions,
group: "parent",
position: "before",
},
],
};

switch (argument["import-sources-order"]) {
case "case-insensitive":
case "case-insensitive-legacy":
importOrderRule.alphabetize.caseInsensitive = true;
importOrderRule.alphabetize.order = "asc";
break;
case "lowercase-first":
importOrderRule.alphabetize.caseInsensitive = false;
importOrderRule.alphabetize.order = "asc";
importOrderRule.pathGroups = importOrderRule.pathGroups.concat([
{
pattern: "[a-z]*",
patternOptions,
group: "external",
position: "before",
},
{
pattern: "../[a-z]*",
patternOptions,
group: "parent",
position: "before",
},
{
pattern: "./[a-z]*",
patternOptions,
group: "sibling",
position: "before",
},
]);
break;
case "lowercase-last":
importOrderRule.alphabetize.caseInsensitive = false;
importOrderRule.alphabetize.order = "asc";
break;
case "any":
importOrderRule.alphabetize.caseInsensitive = false;
importOrderRule.alphabetize.order = "ignore";
break;
}

if (argument["grouped-imports"] === true) {
importOrderRule["newlines-between"] = "always";
}

if ("groups" in argument) {
notices.push(
"Option 'groups' is too bespoke to be converted to ESLint plugin 'eslint-plugin-import'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂 I love this

);
}

if ("named-imports-order" in argument) {
notices.push(
"Option 'named-imports-order' is not supported by ESLint plugin 'eslint-plugin-import'",
);
}

if (argument["module-source-path"] === "basename") {
notices.push(
"Option 'module-source-path' with a value of 'basename' is not supported by ESLint plugin 'eslint-plugin-import'. The behavior will fallback to 'full'",
);
}

return {
plugins: ["eslint-plugin-import"],
rules: [
{
...(notices.length !== 0 && { notices }),
ruleArguments: [importOrderRule],
ruleName: "import/order",
},
],
Expand Down