Skip to content

Commit

Permalink
fix: respect comment boundaries with partitioning by comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Oct 24, 2024
1 parent 5387d38 commit 1cbde14
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore(type, sourceCode),
getCommentsBefore(type, sourceCode, '&'),
options.matcher,
)) ||
(options.partitionByNewLine &&
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore(type, sourceCode),
getCommentsBefore(type, sourceCode, '|'),
options.matcher,
)) ||
(options.partitionByNewLine &&
Expand Down
54 changes: 54 additions & 0 deletions test/sort-intersection-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
],
},
],
},
)
Expand Down
54 changes: 54 additions & 0 deletions test/sort-union-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
],
},
],
},
)
Expand Down
24 changes: 22 additions & 2 deletions utils/get-comments-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1cbde14

Please sign in to comment.