Skip to content

Commit

Permalink
fix(sort-decorators): keep jsdoc blocks in place
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Nov 30, 2024
1 parent 9800e26 commit 54dfdd9
Show file tree
Hide file tree
Showing 21 changed files with 336 additions and 106 deletions.
5 changes: 4 additions & 1 deletion rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ export let sortArray = <MessageIds extends string>(
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(element, sourceCode),
getCommentsBefore({
node: element,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
9 changes: 7 additions & 2 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,20 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
name,
}

let comments = getCommentsBefore(member, sourceCode)
let lastMember = accumulator.at(-1)?.at(-1)

if (
(options.partitionByNewLine &&
lastMember &&
getLinesBetween(sourceCode, lastMember, sortingNode)) ||
(options.partitionByComment &&
hasPartitionComment(options.partitionByComment, comments))
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
node: member,
sourceCode,
}),
))
) {
accumulator.push([])
}
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ let sortDecorators = (
options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(decorator, sourceCode),
getCommentsBefore({
node: decorator,
sourceCode,
}),
)
) {
accumulator.push([])
Expand Down Expand Up @@ -270,6 +273,7 @@ let sortDecorators = (
fix: fixer =>
makeFixes({
sortedNodes: sortedNodesExcludingEslintDisabled,
ignoreFirstNodeHighestBlockComment: true,
sourceCode,
options,
fixer,
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(member, sourceCode),
getCommentsBefore({
node: member,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore(node, sourceCode),
getCommentsBefore({
sourceCode,
node,
}),
)) ||
(options.partitionByNewLine &&
lastNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(sortingNode.node, sourceCode),
getCommentsBefore({
node: sortingNode.node,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(element, sourceCode),
getCommentsBefore({
node: element,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastElement &&
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(type, sourceCode, '&'),
getCommentsBefore({
tokenValueToIgnoreBefore: '&',
node: type,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(element, sourceCode),
getCommentsBefore({
node: element,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
9 changes: 7 additions & 2 deletions rules/sort-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,19 @@ let analyzeModule = ({
node,
name,
}
let comments = getCommentsBefore(node, sourceCode)
let lastSortingNode = formattedNodes.at(-1)?.at(-1)
if (
(options.partitionByNewLine &&
lastSortingNode &&
getLinesBetween(sourceCode, lastSortingNode, sortingNode)) ||
(options.partitionByComment &&
hasPartitionComment(options.partitionByComment, comments))
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
sourceCode,
node,
}),
))
) {
formattedNodes.push([])
}
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-named-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(specifier, sourceCode),
getCommentsBefore({
node: specifier,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-named-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(specifier, sourceCode),
getCommentsBefore({
node: specifier,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(member, sourceCode),
getCommentsBefore({
node: member,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
9 changes: 7 additions & 2 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
return accumulator
}

let comments = getCommentsBefore(property, sourceCode)
let lastProperty = accumulator.at(-1)?.at(-1)

let name: string
Expand Down Expand Up @@ -353,7 +352,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
propertySortingNode,
)) ||
(options.partitionByComment &&
hasPartitionComment(options.partitionByComment, comments))
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
node: property,
sourceCode,
}),
))
) {
accumulator.push([])
}
Expand Down
6 changes: 5 additions & 1 deletion rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(type, sourceCode, '|'),
getCommentsBefore({
tokenValueToIgnoreBefore: '|',
node: type,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-variable-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
(options.partitionByComment &&
hasPartitionComment(
options.partitionByComment,
getCommentsBefore(declaration, sourceCode),
getCommentsBefore({
node: declaration,
sourceCode,
}),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
Expand Down
Loading

0 comments on commit 54dfdd9

Please sign in to comment.