Skip to content

Commit

Permalink
feat: Support ordered-imports option (#1402)
Browse files Browse the repository at this point in the history
* feat: Support `ordered-imports` option

* wip: wip

* test: Final test fixup
  • Loading branch information
hyperupcall authored Feb 21, 2023
1 parent db62cac commit 2675a01
Show file tree
Hide file tree
Showing 2 changed files with 1,208 additions and 20 deletions.
121 changes: 112 additions & 9 deletions src/converters/lintConfigs/rules/ruleConverters/ordered-imports.ts
Original file line number Diff line number Diff line change
@@ -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'",
);
}

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
Loading

0 comments on commit 2675a01

Please sign in to comment.