diff --git a/docs/content/rules/sort-named-exports.mdx b/docs/content/rules/sort-named-exports.mdx index 52798335..2505d410 100644 --- a/docs/content/rules/sort-named-exports.mdx +++ b/docs/content/rules/sort-named-exports.mdx @@ -135,6 +135,43 @@ Allows you to group named exports by their kind, determining whether value expor - `values-first` — Group all value exports before type exports. - `types-first` — Group all type exports before value exports. +### partitionByComment + +default: `false` + +Allows you to use comments to separate the members of named exports into logical groups. This can help in organizing and maintaining large enums by creating partitions within the enum based on comments. + +- `true` — All comments will be treated as delimiters, creating partitions. +- `false` — Comments will not be used as delimiters. +- `string` — A glob pattern to specify which comments should act as delimiters. +- `string[]` — A list of glob patterns to specify which comments should act as delimiters. + +### partitionByNewLine + +default: `false` + +When `true`, the rule will not sort the members of named exports if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order. + +```ts +export { + // Group 1 + Drone, + Keyboard, + Mouse, + Smartphone, + + // Group 2 + Laptop, + Monitor, + Smartwatch, + Tablet, + + // Group 3 + Headphones, + Router, +} from './devices' +``` + ## Usage , @@ -56,6 +61,29 @@ export default createEslintRule({ enum: ['mixed', 'values-first', 'types-first'], type: 'string', }, + partitionByComment: { + description: + 'Allows you to use comments to separate the named exports members into logical groups.', + anyOf: [ + { + type: 'array', + items: { + type: 'string', + }, + }, + { + type: 'boolean', + }, + { + type: 'string', + }, + ], + }, + partitionByNewLine: { + description: + 'Allows to use spaces to separate the nodes into logical groups.', + type: 'boolean', + }, }, additionalProperties: false, }, @@ -70,6 +98,8 @@ export default createEslintRule({ type: 'alphabetical', order: 'asc', ignoreCase: true, + partitionByNewLine: false, + partitionByComment: false, groupKind: 'mixed', }, ], @@ -82,25 +112,45 @@ export default createEslintRule({ type: 'alphabetical', groupKind: 'mixed', ignoreCase: true, + partitionByNewLine: false, + partitionByComment: false, order: 'asc', } as const) let sourceCode = getSourceCode(context) + let partitionComment = options.partitionByComment - let nodes: SortingNode[] = node.specifiers.map(specifier => { + let formattedMembers: SortingNode[][] = [[]] + for (let specifier of node.specifiers) { let group: undefined | 'value' | 'type' if (specifier.exportKind === 'type') { group = 'type' } else { group = 'value' } - return { + + let lastSortingNode = formattedMembers.at(-1)?.at(-1) + let sortingNode: SortingNode = { size: rangeToDiff(specifier.range), name: specifier.local.name, node: specifier, group, } - }) + if ( + (partitionComment && + hasPartitionComment( + partitionComment, + getCommentsBefore(specifier, sourceCode), + )) || + (options.partitionByNewLine && + lastSortingNode && + getLinesBetween(sourceCode, lastSortingNode, sortingNode)) + ) { + formattedMembers.push([]) + } + + formattedMembers.at(-1)!.push(sortingNode) + } let shouldGroupByKind = options.groupKind !== 'mixed' let groupKindOrder = @@ -108,33 +158,38 @@ export default createEslintRule({ ? ['value', 'type'] : ['type', 'value'] - pairwise(nodes, (left, right) => { - let leftNum = getGroupNumber(groupKindOrder, left) - let rightNum = getGroupNumber(groupKindOrder, right) + for (let nodes of formattedMembers) { + pairwise(nodes, (left, right) => { + let leftNum = getGroupNumber(groupKindOrder, left) + let rightNum = getGroupNumber(groupKindOrder, right) - if ( - (shouldGroupByKind && leftNum > rightNum) || - ((!shouldGroupByKind || leftNum === rightNum) && - isPositive(compare(left, right, options))) - ) { - let sortedNodes = shouldGroupByKind - ? groupKindOrder - .map(group => nodes.filter(n => n.group === group)) - .map(groupedNodes => sortNodes(groupedNodes, options)) - .flat() - : sortNodes(nodes, options) + if ( + (shouldGroupByKind && leftNum > rightNum) || + ((!shouldGroupByKind || leftNum === rightNum) && + isPositive(compare(left, right, options))) + ) { + let sortedNodes = shouldGroupByKind + ? groupKindOrder + .map(group => nodes.filter(n => n.group === group)) + .map(groupedNodes => sortNodes(groupedNodes, options)) + .flat() + : sortNodes(nodes, options) - context.report({ - messageId: 'unexpectedNamedExportsOrder', - data: { - left: left.name, - right: right.name, - }, - node: right.node, - fix: fixer => makeFixes(fixer, nodes, sortedNodes, sourceCode), - }) - } - }) + context.report({ + messageId: 'unexpectedNamedExportsOrder', + data: { + left: left.name, + right: right.name, + }, + node: right.node, + fix: fixer => + makeFixes(fixer, nodes, sortedNodes, sourceCode, { + partitionComment, + }), + }) + } + }) + } } }, }), diff --git a/test/sort-named-exports.test.ts b/test/sort-named-exports.test.ts index ef985053..29fd4abb 100644 --- a/test/sort-named-exports.test.ts +++ b/test/sort-named-exports.test.ts @@ -151,6 +151,210 @@ describe(ruleName, () => { ], }, ) + + ruleTester.run( + `${ruleName}(${type}): allows to use new line as partition`, + rule, + { + valid: [], + invalid: [ + { + code: dedent` + export { + D, + A, + + C, + + E, + B, + } + `, + output: dedent` + export { + A, + D, + + C, + + B, + E, + } + `, + options: [ + { + ...options, + partitionByNewLine: true, + }, + ], + errors: [ + { + messageId: 'unexpectedNamedExportsOrder', + data: { + left: 'D', + right: 'A', + }, + }, + { + messageId: 'unexpectedNamedExportsOrder', + data: { + left: 'E', + right: 'B', + }, + }, + ], + }, + ], + }, + ) + + describe('partition comments', () => { + ruleTester.run( + `${ruleName}(${type}): allows to use partition comments`, + rule, + { + valid: [], + invalid: [ + { + code: dedent` + export { + // Part: A + CC, + type D, + // Not partition comment + BBB, + // Part: B + AAAA, + E, + // Part: C + GG, + // Not partition comment + FFF, + } + `, + output: dedent` + export { + // Part: A + type D, + // Not partition comment + BBB, + CC, + // Part: B + AAAA, + E, + // Part: C + // Not partition comment + FFF, + GG, + } + `, + options: [ + { + ...options, + partitionByComment: 'Part**', + groupKind: 'types-first', + }, + ], + errors: [ + { + messageId: 'unexpectedNamedExportsOrder', + data: { + left: 'CC', + right: 'D', + }, + }, + { + messageId: 'unexpectedNamedExportsOrder', + data: { + left: 'GG', + right: 'FFF', + }, + }, + ], + }, + ], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): allows to use all comments as parts`, + rule, + { + valid: [ + { + code: dedent` + export { + // Comment + BB, + // Other comment + A, + } + `, + options: [ + { + ...options, + partitionByComment: true, + }, + ], + }, + ], + invalid: [], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): allows to use multiple partition comments`, + rule, + { + valid: [], + invalid: [ + { + code: dedent` + export { + /* Partition Comment */ + // Part: A + D, + // Part: B + AAA, + C, + BB, + /* Other */ + E, + } + `, + output: dedent` + export { + /* Partition Comment */ + // Part: A + D, + // Part: B + AAA, + BB, + C, + /* Other */ + E, + } + `, + options: [ + { + ...options, + partitionByComment: ['Partition Comment', 'Part: *', 'Other'], + }, + ], + errors: [ + { + messageId: 'unexpectedNamedExportsOrder', + data: { + left: 'C', + right: 'BB', + }, + }, + ], + }, + ], + }, + ) + }) }) describe(`${ruleName}: sorting by natural order`, () => {