diff --git a/packages/eslint-plugin-import-newlines/lib/rules/enforce/index.ts b/packages/eslint-plugin-import-newlines/lib/rules/enforce/index.ts index 88dacb8..0eba49b 100644 --- a/packages/eslint-plugin-import-newlines/lib/rules/enforce/index.ts +++ b/packages/eslint-plugin-import-newlines/lib/rules/enforce/index.ts @@ -1,28 +1,5 @@ import type { Rule } from 'eslint' import { enforceConfig, jsonSchema } from './config.js' -import { inspect } from 'node:util' - -/** - * Groups an array of elements by keys extracted using a specified function. - * - * @param array - The array of elements to be grouped. - * @param keyExtractor - A function that takes an element and returns the key to group by. - * @returns An object with keys being the unique values returned by the keyExtractor and values being arrays of corresponding elements. - */ -function groupBy(array: T[], keyExtractor: (item: T) => R) { - const initial: Record = {} as Record - - return array.reduce((result, current) => { - const key = keyExtractor(current) - - if (!result[key]) { - result[key] = [] - } - - result[key].push(current) - return result - }, initial) -} const plugin: Rule.RuleModule = { meta: { @@ -53,35 +30,13 @@ const plugin: Rule.RuleModule = { } const identifiers = node.specifiers.filter(s => s.type === 'ImportSpecifier') - const identifiersPerLine = groupBy(identifiers, i => { - if (!i.loc) { - throw new Error() - } - - return i.loc.start.line - }) - console.log(inspect(Object.entries(identifiersPerLine).map(([line, specs]) => [ - line, - specs.map(s => ({ name: s.imported.name, loc: s.imported.loc, range: s.imported.range })), - ]), false, null)) - const identifierNames = identifiers .sort((a, b) => a.imported.name.localeCompare(b.imported.name)) .map(s => `\t${s.imported.name}`) const replaced = ['import {', identifierNames.join(',\n'), `} from ${node.source.raw}`].join('\n') const length = node.loc.end.column - node.loc.start.column - if (identifiers.length > config.maxItems) { - context.report({ - node, - messageId: 'mustSplitMany', - data: { - maxItems: config.maxItems.toString(), - }, - fix: fixer => fixer.replaceText(node, replaced), - }) - } - else if (length > config.maxLineLength) { + if (length > config.maxLineLength) { context.report({ node, messageId: 'mustSplitLong',