|
1 | 1 | import type { Rule } from 'eslint' |
2 | 2 | import { enforceConfig, jsonSchema } from './config.js' |
3 | | -import { inspect } from 'node:util' |
4 | | - |
5 | | -/** |
6 | | - * Groups an array of elements by keys extracted using a specified function. |
7 | | - * |
8 | | - * @param array - The array of elements to be grouped. |
9 | | - * @param keyExtractor - A function that takes an element and returns the key to group by. |
10 | | - * @returns An object with keys being the unique values returned by the keyExtractor and values being arrays of corresponding elements. |
11 | | - */ |
12 | | -function groupBy<T, R extends keyof any>(array: T[], keyExtractor: (item: T) => R) { |
13 | | - const initial: Record<R, T[]> = {} as Record<R, T[]> |
14 | | - |
15 | | - return array.reduce((result, current) => { |
16 | | - const key = keyExtractor(current) |
17 | | - |
18 | | - if (!result[key]) { |
19 | | - result[key] = [] |
20 | | - } |
21 | | - |
22 | | - result[key].push(current) |
23 | | - return result |
24 | | - }, initial) |
25 | | -} |
26 | 3 |
|
27 | 4 | const plugin: Rule.RuleModule = { |
28 | 5 | meta: { |
@@ -53,35 +30,13 @@ const plugin: Rule.RuleModule = { |
53 | 30 | } |
54 | 31 |
|
55 | 32 | const identifiers = node.specifiers.filter(s => s.type === 'ImportSpecifier') |
56 | | - const identifiersPerLine = groupBy(identifiers, i => { |
57 | | - if (!i.loc) { |
58 | | - throw new Error() |
59 | | - } |
60 | | - |
61 | | - return i.loc.start.line |
62 | | - }) |
63 | | - console.log(inspect(Object.entries(identifiersPerLine).map(([line, specs]) => [ |
64 | | - line, |
65 | | - specs.map(s => ({ name: s.imported.name, loc: s.imported.loc, range: s.imported.range })), |
66 | | - ]), false, null)) |
67 | | - |
68 | 33 | const identifierNames = identifiers |
69 | 34 | .sort((a, b) => a.imported.name.localeCompare(b.imported.name)) |
70 | 35 | .map(s => `\t${s.imported.name}`) |
71 | 36 | const replaced = ['import {', identifierNames.join(',\n'), `} from ${node.source.raw}`].join('\n') |
72 | 37 |
|
73 | 38 | const length = node.loc.end.column - node.loc.start.column |
74 | | - if (identifiers.length > config.maxItems) { |
75 | | - context.report({ |
76 | | - node, |
77 | | - messageId: 'mustSplitMany', |
78 | | - data: { |
79 | | - maxItems: config.maxItems.toString(), |
80 | | - }, |
81 | | - fix: fixer => fixer.replaceText(node, replaced), |
82 | | - }) |
83 | | - } |
84 | | - else if (length > config.maxLineLength) { |
| 39 | + if (length > config.maxLineLength) { |
85 | 40 | context.report({ |
86 | 41 | node, |
87 | 42 | messageId: 'mustSplitLong', |
|
0 commit comments