diff --git a/rules/sort-intersection-types.ts b/rules/sort-intersection-types.ts index 5d01f61f..566feb29 100644 --- a/rules/sort-intersection-types.ts +++ b/rules/sort-intersection-types.ts @@ -279,7 +279,7 @@ export default createEslintRule({ (partitionComment && hasPartitionComment( partitionComment, - getCommentsBefore(type, sourceCode), + getCommentsBefore(type, sourceCode, '&'), options.matcher, )) || (options.partitionByNewLine && diff --git a/rules/sort-union-types.ts b/rules/sort-union-types.ts index c79feaa1..6d41e429 100644 --- a/rules/sort-union-types.ts +++ b/rules/sort-union-types.ts @@ -279,7 +279,7 @@ export default createEslintRule({ (partitionComment && hasPartitionComment( partitionComment, - getCommentsBefore(type, sourceCode), + getCommentsBefore(type, sourceCode, '|'), options.matcher, )) || (options.partitionByNewLine && diff --git a/test/sort-intersection-types.test.ts b/test/sort-intersection-types.test.ts index be73e321..5262c69d 100644 --- a/test/sort-intersection-types.test.ts +++ b/test/sort-intersection-types.test.ts @@ -537,6 +537,60 @@ describe(ruleName, () => { }, ], }, + { + code: dedent` + type T = + // Part: A + & CC + & D + // Not partition comment + & BBB + // Part: B + & AAA + & E + // Part: C + & GG + // Not partition comment + & FFF + `, + output: dedent` + type T = + // Part: A + & BBB + & CC + // Not partition comment + & D + // Part: B + & AAA + & E + // Part: C + & FFF + // Not partition comment + & GG + `, + options: [ + { + ...options, + partitionByComment: 'Part**', + }, + ], + errors: [ + { + messageId: 'unexpectedIntersectionTypesOrder', + data: { + left: 'D', + right: 'BBB', + }, + }, + { + messageId: 'unexpectedIntersectionTypesOrder', + data: { + left: 'GG', + right: 'FFF', + }, + }, + ], + }, ], }, ) diff --git a/test/sort-union-types.test.ts b/test/sort-union-types.test.ts index c65b91ff..a853233f 100644 --- a/test/sort-union-types.test.ts +++ b/test/sort-union-types.test.ts @@ -540,6 +540,60 @@ describe(ruleName, () => { }, ], }, + { + code: dedent` + type T = + // Part: A + | CC + | D + // Not partition comment + | BBB + // Part: B + | AAA + | E + // Part: C + | GG + // Not partition comment + | FFF + `, + output: dedent` + type T = + // Part: A + | BBB + | CC + // Not partition comment + | D + // Part: B + | AAA + | E + // Part: C + | FFF + // Not partition comment + | GG + `, + options: [ + { + ...options, + partitionByComment: 'Part**', + }, + ], + errors: [ + { + messageId: 'unexpectedUnionTypesOrder', + data: { + left: 'D', + right: 'BBB', + }, + }, + { + messageId: 'unexpectedUnionTypesOrder', + data: { + left: 'GG', + right: 'FFF', + }, + }, + ], + }, ], }, ) diff --git a/utils/get-comments-before.ts b/utils/get-comments-before.ts index 7c9c72be..02a74368 100644 --- a/utils/get-comments-before.ts +++ b/utils/get-comments-before.ts @@ -4,11 +4,31 @@ import type { TSESTree } from '@typescript-eslint/types' /** * Returns a list of comments before a given node, excluding ones that are * right after code. Includes comment blocks. + * @param node The node to get comments before + * @param source The source code + * @param tokenValueToIgnoreBefore Allows the following token to directly precede the node */ -export let getCommentsBefore = ( +export const getCommentsBefore = ( node: TSESTree.Node, source: TSESLint.SourceCode, -): TSESTree.Comment[] => + tokenValueToIgnoreBefore?: string, +): TSESTree.Comment[] => { + let commentsBefore = getCommentsBeforeNodeOrToken(source, node) + let tokenBeforeNode = source.getTokenBefore(node) + if ( + commentsBefore.length || + !tokenValueToIgnoreBefore || + tokenBeforeNode?.value !== tokenValueToIgnoreBefore + ) { + return commentsBefore + } + return getCommentsBeforeNodeOrToken(source, tokenBeforeNode) +} + +const getCommentsBeforeNodeOrToken = ( + source: TSESLint.SourceCode, + node: TSESTree.Token | TSESTree.Node, +) => source.getCommentsBefore(node).filter(comment => { // 'getCommentsBefore' also returns comments that are right after code, filter those out let tokenBeforeComment = source.getTokenBefore(comment)