|
1 | | -import type { unit } from "@let/eff"; |
| 1 | +import { unit } from "@let/eff"; |
2 | 2 | import { P, match } from "ts-pattern"; |
3 | | -import { defineRule } from "tsl"; |
| 3 | +import { type AST, defineRule } from "tsl"; |
4 | 4 | import ts from "typescript"; |
5 | 5 |
|
6 | 6 | export const messages = { |
7 | | - noDuplicateImports: (p: { source: string }) => |
8 | | - `Duplicate import from module ${p.source}. Combine into a single import statement.`, |
| 7 | + noDuplicateImports: (p: { source: string }) => `Duplicate import from module ${p.source}.`, |
9 | 8 | } as const; |
10 | 9 |
|
| 10 | +type ImportKind = 0 | 1 | 2; // 0: import, 1: import type, 2: import defer |
| 11 | + |
| 12 | +interface ImportInfo { |
| 13 | + node: AST.ImportDeclaration; |
| 14 | + kind: ImportKind; |
| 15 | + defaultImport: string | unit; |
| 16 | + namedImports: string[]; |
| 17 | + namespaceImport: string | unit; |
| 18 | + source: string; |
| 19 | +} |
| 20 | + |
11 | 21 | /** |
12 | | - * Rule to disallow duplicate imports from the same module. Combine multiple import statements from the same module into a single statement. |
| 22 | + * Rule to detect and merge duplicate `import from` statements from the same module. |
13 | 23 | * |
14 | 24 | * @todo Add autofix to merge duplicate imports automatically. |
15 | 25 | * |
@@ -43,33 +53,96 @@ export const messages = { |
43 | 53 | export const noDuplicateImports = defineRule(() => { |
44 | 54 | return { |
45 | 55 | name: "module/no-duplicate-imports", |
46 | | - createData() { |
47 | | - return [ |
48 | | - new Set<string>(), // for import |
49 | | - new Set<string>(), // for import type |
50 | | - new Set<string>(), // for import defer |
51 | | - ] as const; |
| 56 | + createData(): { imports: [ImportInfo[], ImportInfo[], ImportInfo[]] } { |
| 57 | + return { imports: [[], [], []] }; |
52 | 58 | }, |
53 | 59 | visitor: { |
54 | 60 | ImportDeclaration(ctx, node) { |
55 | 61 | if (node.importClause == null) return; // skip side-effect imports |
| 62 | + const importKind = getImportKind(node); |
56 | 63 | const importSource = node.moduleSpecifier.getText(); |
57 | | - const seen = ctx.data[ |
58 | | - match<ts.ImportPhaseModifierSyntaxKind | unit, 0 | 1 | 2>(node.importClause.phaseModifier) |
59 | | - .with(P.nullish, () => 0) |
60 | | - .with(ts.SyntaxKind.TypeKeyword, () => 1) |
61 | | - .with(ts.SyntaxKind.DeferKeyword, () => 2) |
62 | | - .otherwise(() => 0) |
63 | | - ]; |
64 | | - if (seen.has(importSource)) { |
| 64 | + const importInfo = { |
| 65 | + node, |
| 66 | + source: importSource, |
| 67 | + kind: importKind, |
| 68 | + ...decodeImportClause(node.importClause), |
| 69 | + } as const satisfies ImportInfo; |
| 70 | + const existingImports = ctx.data.imports[importKind]; |
| 71 | + const duplicateImport = existingImports.find((imp) => imp.source === importInfo.source); |
| 72 | + if (duplicateImport != null) { |
65 | 73 | ctx.report({ |
66 | 74 | node, |
67 | | - message: messages.noDuplicateImports({ source: importSource }), |
| 75 | + message: messages.noDuplicateImports({ source: importInfo.source }), |
| 76 | + suggestions: [ |
| 77 | + { |
| 78 | + message: "Merge duplicate imports", |
| 79 | + changes: [ |
| 80 | + { |
| 81 | + node, |
| 82 | + newText: "", |
| 83 | + }, |
| 84 | + { |
| 85 | + node: duplicateImport.node, |
| 86 | + newText: buildMergedImport(duplicateImport, importInfo), |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + ], |
68 | 91 | }); |
69 | 92 | return; |
70 | 93 | } |
71 | | - seen.add(importSource); |
| 94 | + existingImports.push(importInfo); |
72 | 95 | }, |
73 | 96 | }, |
74 | 97 | }; |
75 | 98 | }); |
| 99 | + |
| 100 | +function getImportKind(node: AST.ImportDeclaration): ImportKind { |
| 101 | + return match<ts.ImportPhaseModifierSyntaxKind | unit, ImportKind>(node.importClause?.phaseModifier) |
| 102 | + .with(P.nullish, () => 0) |
| 103 | + .with(ts.SyntaxKind.TypeKeyword, () => 1) |
| 104 | + .with(ts.SyntaxKind.DeferKeyword, () => 2) |
| 105 | + .otherwise(() => 0); |
| 106 | +} |
| 107 | + |
| 108 | +function decodeImportClause(node: AST.ImportClause) { |
| 109 | + const { name, namedBindings } = node; |
| 110 | + return { |
| 111 | + defaultImport: name?.getText(), |
| 112 | + namedImports: namedBindings != null |
| 113 | + && ts.isNamedImports(namedBindings) |
| 114 | + ? namedBindings.elements.map((el) => el.getText()) |
| 115 | + : [], |
| 116 | + namespaceImport: namedBindings != null |
| 117 | + && ts.isNamespaceImport(namedBindings) |
| 118 | + ? namedBindings.name.getText() |
| 119 | + : unit, |
| 120 | + } as const; |
| 121 | +} |
| 122 | + |
| 123 | +function buildMergedImport(a: ImportInfo, b: ImportInfo): string { |
| 124 | + const parts: string[] = []; |
| 125 | + // Default import |
| 126 | + if (a.defaultImport != null) { |
| 127 | + parts.push(a.defaultImport); |
| 128 | + } else if (b.defaultImport != null) { |
| 129 | + parts.push(b.defaultImport); |
| 130 | + } |
| 131 | + // Namespace import |
| 132 | + if (a.namespaceImport != null) { |
| 133 | + parts.push(`* as ${a.namespaceImport}`); |
| 134 | + } else if (b.namespaceImport != null) { |
| 135 | + parts.push(`* as ${b.namespaceImport}`); |
| 136 | + } |
| 137 | + // Named imports |
| 138 | + const namedImports = Array.from(new Set([...a.namedImports, ...b.namedImports])); |
| 139 | + if (namedImports.length > 0) { |
| 140 | + parts.push(`{ ${namedImports.join(", ")} }`); |
| 141 | + } |
| 142 | + const importKindPrefix = match<ImportKind, string>(a.kind) |
| 143 | + .with(0, () => "import") |
| 144 | + .with(1, () => "import type") |
| 145 | + .with(2, () => "import defer") |
| 146 | + .exhaustive(); |
| 147 | + return `${importKindPrefix} ${parts.join(", ")} from ${a.source};`; |
| 148 | +} |
0 commit comments