Skip to content

Commit

Permalink
feat: keep comments above elements being sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Sep 13, 2024
1 parent 83954a4 commit bd8ba3f
Show file tree
Hide file tree
Showing 12 changed files with 562 additions and 59 deletions.
9 changes: 4 additions & 5 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
customGroupSortJsonSchema,
} from './sort-classes.types'
import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies'
import { isPartitionComment } from '../utils/is-partition-comment'
import { getCommentBefore } from '../utils/get-comment-before'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -365,12 +365,11 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({

let formattedNodes: SortingNodeWithDependencies[][] = node.body.reduce(
(accumulator: SortingNodeWithDependencies[][], member) => {
let comment = getCommentBefore(member, sourceCode)
let comments = getCommentsBefore(member, sourceCode)

if (
options.partitionByComment &&
comment &&
isPartitionComment(options.partitionByComment, comment.value)
hasPartitionComment(options.partitionByComment, comments)
) {
accumulator.push([])
}
Expand Down
9 changes: 4 additions & 5 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-depende
import type { CompareOptions } from '../utils/compare'

import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies'
import { isPartitionComment } from '../utils/is-partition-comment'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getCommentBefore } from '../utils/get-comment-before'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
Expand Down Expand Up @@ -176,12 +176,11 @@ export default createEslintRule<Options, MESSAGE_ID>({

let formattedMembers: SortingNodeWithDependencies[][] = members.reduce(
(accumulator: SortingNodeWithDependencies[][], member) => {
let comment = getCommentBefore(member, sourceCode)
let comments = getCommentsBefore(member, sourceCode)

if (
partitionComment &&
comment &&
isPartitionComment(partitionComment, comment.value)
hasPartitionComment(partitionComment, comments)
) {
accumulator.push([])
}
Expand Down
13 changes: 5 additions & 8 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { minimatch } from 'minimatch'
import type { SortingNode } from '../typings'

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { getCommentBefore } from '../utils/get-comment-before'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
Expand Down Expand Up @@ -511,13 +511,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
left: SortingNode,
right: SortingNode,
): boolean =>
!!sourceCode.getTokensBetween(
left.node,
getCommentBefore(right.node, sourceCode) || right.node,
{
includeComments: true,
},
).length
getCommentsBefore(right.node, sourceCode).length > 0 ||
!!sourceCode.getTokensBetween(left.node, right.node, {
includeComments: false,
}).length

let fix = (
fixer: TSESLint.RuleFixer,
Expand Down
9 changes: 4 additions & 5 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-depende

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies'
import { isPartitionComment } from '../utils/is-partition-comment'
import { getCommentBefore } from '../utils/get-comment-before'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
Expand Down Expand Up @@ -369,13 +369,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
return accumulator
}

let comment = getCommentBefore(prop, sourceCode)
let comments = getCommentsBefore(prop, sourceCode)
let lastProp = accumulator.at(-1)?.at(-1)

if (
options.partitionByComment &&
comment &&
isPartitionComment(options.partitionByComment, comment.value)
hasPartitionComment(options.partitionByComment, comments)
) {
accumulator.push([])
}
Expand Down
144 changes: 144 additions & 0 deletions test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6680,5 +6680,149 @@ describe(ruleName, () => {
},
],
})

describe('handles complex comment cases', () => {
ruleTester.run(`keeps comments associated to their node`, rule, {
valid: [],
invalid: [
{
code: dedent`
class Class {
// Ignore this comment
// B1
b
/**
* Ignore this comment as well
*/
// A4
// A3
/*
* A2
*/
// A1
a
}
`,
output: dedent`
class Class {
// Ignore this comment
// A4
// A3
/*
* A2
*/
// A1
a
/**
* Ignore this comment as well
*/
// B1
b
}
`,
options: [
{
type: 'alphabetical',
},
],
errors: [
{
messageId: 'unexpectedClassesOrder',
data: {
left: 'b',
right: 'a',
},
},
],
},
],
})

ruleTester.run(`handles partition comments`, rule, {
valid: [],
invalid: [
{
code: dedent`
class Class {
// Ignore this comment
// C2
// C1
c
// B2
/**
* B1
*/
b
// Above a partition comment ignore me
// PartitionComment: 1
/**
* D2
*/
// D1
d
a
}
`,
output: dedent`
class Class {
// Ignore this comment
// B2
/**
* B1
*/
b
// C2
// C1
c
// Above a partition comment ignore me
// PartitionComment: 1
a
/**
* D2
*/
// D1
d
}
`,
options: [
{
type: 'alphabetical',
partitionByComment: 'PartitionComment:*',
},
],
errors: [
{
messageId: 'unexpectedClassesOrder',
data: {
left: 'c',
right: 'b',
},
},
{
messageId: 'unexpectedClassesOrder',
data: {
left: 'd',
right: 'a',
},
},
],
},
],
})
})
})
})
Loading

0 comments on commit bd8ba3f

Please sign in to comment.