Skip to content

Commit 6cc4ec1

Browse files
committed
refactor: simplify
1 parent 58c97dd commit 6cc4ec1

File tree

1 file changed

+1
-46
lines changed
  • packages/eslint-plugin-import-newlines/lib/rules/enforce

1 file changed

+1
-46
lines changed

packages/eslint-plugin-import-newlines/lib/rules/enforce/index.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
import type { Rule } from 'eslint'
22
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-
}
263

274
const plugin: Rule.RuleModule = {
285
meta: {
@@ -53,35 +30,13 @@ const plugin: Rule.RuleModule = {
5330
}
5431

5532
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-
6833
const identifierNames = identifiers
6934
.sort((a, b) => a.imported.name.localeCompare(b.imported.name))
7035
.map(s => `\t${s.imported.name}`)
7136
const replaced = ['import {', identifierNames.join(',\n'), `} from ${node.source.raw}`].join('\n')
7237

7338
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) {
8540
context.report({
8641
node,
8742
messageId: 'mustSplitLong',

0 commit comments

Comments
 (0)