Skip to content

Commit e5279ac

Browse files
hugop95azat-io
authored andcommittedNov 19, 2024
fix: respect comment boundaries with partitioning by comments
1 parent 93e0b53 commit e5279ac

5 files changed

+132
-4
lines changed
 

‎rules/sort-intersection-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
279279
(partitionComment &&
280280
hasPartitionComment(
281281
partitionComment,
282-
getCommentsBefore(type, sourceCode),
282+
getCommentsBefore(type, sourceCode, '&'),
283283
options.matcher,
284284
)) ||
285285
(options.partitionByNewLine &&

‎rules/sort-union-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
279279
(partitionComment &&
280280
hasPartitionComment(
281281
partitionComment,
282-
getCommentsBefore(type, sourceCode),
282+
getCommentsBefore(type, sourceCode, '|'),
283283
options.matcher,
284284
)) ||
285285
(options.partitionByNewLine &&

‎test/sort-intersection-types.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,60 @@ describe(ruleName, () => {
537537
},
538538
],
539539
},
540+
{
541+
code: dedent`
542+
type T =
543+
// Part: A
544+
& CC
545+
& D
546+
// Not partition comment
547+
& BBB
548+
// Part: B
549+
& AAA
550+
& E
551+
// Part: C
552+
& GG
553+
// Not partition comment
554+
& FFF
555+
`,
556+
output: dedent`
557+
type T =
558+
// Part: A
559+
& BBB
560+
& CC
561+
// Not partition comment
562+
& D
563+
// Part: B
564+
& AAA
565+
& E
566+
// Part: C
567+
& FFF
568+
// Not partition comment
569+
& GG
570+
`,
571+
options: [
572+
{
573+
...options,
574+
partitionByComment: 'Part**',
575+
},
576+
],
577+
errors: [
578+
{
579+
messageId: 'unexpectedIntersectionTypesOrder',
580+
data: {
581+
left: 'D',
582+
right: 'BBB',
583+
},
584+
},
585+
{
586+
messageId: 'unexpectedIntersectionTypesOrder',
587+
data: {
588+
left: 'GG',
589+
right: 'FFF',
590+
},
591+
},
592+
],
593+
},
540594
],
541595
},
542596
)

‎test/sort-union-types.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,60 @@ describe(ruleName, () => {
540540
},
541541
],
542542
},
543+
{
544+
code: dedent`
545+
type T =
546+
// Part: A
547+
| CC
548+
| D
549+
// Not partition comment
550+
| BBB
551+
// Part: B
552+
| AAA
553+
| E
554+
// Part: C
555+
| GG
556+
// Not partition comment
557+
| FFF
558+
`,
559+
output: dedent`
560+
type T =
561+
// Part: A
562+
| BBB
563+
| CC
564+
// Not partition comment
565+
| D
566+
// Part: B
567+
| AAA
568+
| E
569+
// Part: C
570+
| FFF
571+
// Not partition comment
572+
| GG
573+
`,
574+
options: [
575+
{
576+
...options,
577+
partitionByComment: 'Part**',
578+
},
579+
],
580+
errors: [
581+
{
582+
messageId: 'unexpectedUnionTypesOrder',
583+
data: {
584+
left: 'D',
585+
right: 'BBB',
586+
},
587+
},
588+
{
589+
messageId: 'unexpectedUnionTypesOrder',
590+
data: {
591+
left: 'GG',
592+
right: 'FFF',
593+
},
594+
},
595+
],
596+
},
543597
],
544598
},
545599
)

‎utils/get-comments-before.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@ import type { TSESTree } from '@typescript-eslint/types'
44
/**
55
* Returns a list of comments before a given node, excluding ones that are
66
* right after code. Includes comment blocks.
7+
* @param node The node to get comments before
8+
* @param source The source code
9+
* @param tokenValueToIgnoreBefore Allows the following token to directly precede the node
710
*/
8-
export let getCommentsBefore = (
11+
export const getCommentsBefore = (
912
node: TSESTree.Node,
1013
source: TSESLint.SourceCode,
11-
): TSESTree.Comment[] =>
14+
tokenValueToIgnoreBefore?: string,
15+
): TSESTree.Comment[] => {
16+
let commentsBefore = getCommentsBeforeNodeOrToken(source, node)
17+
let tokenBeforeNode = source.getTokenBefore(node)
18+
if (
19+
commentsBefore.length ||
20+
!tokenValueToIgnoreBefore ||
21+
tokenBeforeNode?.value !== tokenValueToIgnoreBefore
22+
) {
23+
return commentsBefore
24+
}
25+
return getCommentsBeforeNodeOrToken(source, tokenBeforeNode)
26+
}
27+
28+
const getCommentsBeforeNodeOrToken = (
29+
source: TSESLint.SourceCode,
30+
node: TSESTree.Token | TSESTree.Node,
31+
) =>
1232
source.getCommentsBefore(node).filter(comment => {
1333
// 'getCommentsBefore' also returns comments that are right after code, filter those out
1434
let tokenBeforeComment = source.getTokenBefore(comment)

0 commit comments

Comments
 (0)
Please sign in to comment.