|
1 | 1 | import { RuleConverter } from "../ruleConverter";
|
2 | 2 |
|
3 |
| -const unsupportedTSLintOptions = [ |
4 |
| - "import-sources-order", |
5 |
| - "module-source-path", |
6 |
| - "named-imports-order", |
7 |
| -]; |
8 |
| - |
9 | 3 | export const convertOrderedImports: RuleConverter = (tslintRule) => {
|
10 |
| - const notices = unsupportedTSLintOptions |
11 |
| - .filter((option) => tslintRule.ruleArguments.includes(option)) |
12 |
| - .map((option) => `Option "${option}" is not supported by ESLint.`); |
| 4 | + const argument = { ...tslintRule.ruleArguments[0] }; |
| 5 | + const notices: string[] = []; |
| 6 | + |
| 7 | + const patternOptions = { |
| 8 | + nocomment: true, |
| 9 | + dot: true, |
| 10 | + }; |
| 11 | + |
| 12 | + const importOrderRule = { |
| 13 | + alphabetize: { |
| 14 | + caseInsensitive: true, |
| 15 | + order: "asc", |
| 16 | + }, |
| 17 | + "newlines-between": "ignore", |
| 18 | + groups: [ |
| 19 | + ["builtin", "external", "internal", "unknown", "object", "type"], |
| 20 | + "parent", |
| 21 | + ["sibling", "index"], |
| 22 | + ], |
| 23 | + distinctGroup: false, |
| 24 | + pathGroupsExcludedImportTypes: [], |
| 25 | + pathGroups: [ |
| 26 | + { |
| 27 | + pattern: "./", |
| 28 | + patternOptions, |
| 29 | + group: "sibling", |
| 30 | + position: "before", |
| 31 | + }, |
| 32 | + |
| 33 | + { |
| 34 | + pattern: ".", |
| 35 | + patternOptions, |
| 36 | + group: "sibling", |
| 37 | + position: "before", |
| 38 | + }, |
| 39 | + { |
| 40 | + pattern: "..", |
| 41 | + patternOptions, |
| 42 | + group: "parent", |
| 43 | + position: "before", |
| 44 | + }, |
| 45 | + { |
| 46 | + pattern: "../", |
| 47 | + patternOptions, |
| 48 | + group: "parent", |
| 49 | + position: "before", |
| 50 | + }, |
| 51 | + ], |
| 52 | + }; |
| 53 | + |
| 54 | + switch (argument["import-sources-order"]) { |
| 55 | + case "case-insensitive": |
| 56 | + case "case-insensitive-legacy": |
| 57 | + importOrderRule.alphabetize.caseInsensitive = true; |
| 58 | + importOrderRule.alphabetize.order = "asc"; |
| 59 | + break; |
| 60 | + case "lowercase-first": |
| 61 | + importOrderRule.alphabetize.caseInsensitive = false; |
| 62 | + importOrderRule.alphabetize.order = "asc"; |
| 63 | + importOrderRule.pathGroups = importOrderRule.pathGroups.concat([ |
| 64 | + { |
| 65 | + pattern: "[a-z]*", |
| 66 | + patternOptions, |
| 67 | + group: "external", |
| 68 | + position: "before", |
| 69 | + }, |
| 70 | + { |
| 71 | + pattern: "../[a-z]*", |
| 72 | + patternOptions, |
| 73 | + group: "parent", |
| 74 | + position: "before", |
| 75 | + }, |
| 76 | + { |
| 77 | + pattern: "./[a-z]*", |
| 78 | + patternOptions, |
| 79 | + group: "sibling", |
| 80 | + position: "before", |
| 81 | + }, |
| 82 | + ]); |
| 83 | + break; |
| 84 | + case "lowercase-last": |
| 85 | + importOrderRule.alphabetize.caseInsensitive = false; |
| 86 | + importOrderRule.alphabetize.order = "asc"; |
| 87 | + break; |
| 88 | + case "any": |
| 89 | + importOrderRule.alphabetize.caseInsensitive = false; |
| 90 | + importOrderRule.alphabetize.order = "ignore"; |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + if (argument["grouped-imports"] === true) { |
| 95 | + importOrderRule["newlines-between"] = "always"; |
| 96 | + } |
| 97 | + |
| 98 | + if ("groups" in argument) { |
| 99 | + notices.push( |
| 100 | + "Option 'groups' is too bespoke to be converted to ESLint plugin 'eslint-plugin-import'", |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + if ("named-imports-order" in argument) { |
| 105 | + notices.push( |
| 106 | + "Option 'named-imports-order' is not supported by ESLint plugin 'eslint-plugin-import'", |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + if (argument["module-source-path"] === "basename") { |
| 111 | + notices.push( |
| 112 | + "Option 'module-source-path' with a value of 'basename' is not supported by ESLint plugin 'eslint-plugin-import'. The behavior will fallback to 'full'", |
| 113 | + ); |
| 114 | + } |
13 | 115 |
|
14 | 116 | return {
|
15 | 117 | plugins: ["eslint-plugin-import"],
|
16 | 118 | rules: [
|
17 | 119 | {
|
18 | 120 | ...(notices.length !== 0 && { notices }),
|
| 121 | + ruleArguments: [importOrderRule], |
19 | 122 | ruleName: "import/order",
|
20 | 123 | },
|
21 | 124 | ],
|
|
0 commit comments