Skip to content

Commit

Permalink
feat: make partition by comment support line and block
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Dec 30, 2024
1 parent c9f74b0 commit dd71f11
Show file tree
Hide file tree
Showing 59 changed files with 4,668 additions and 120 deletions.
1 change: 1 addition & 0 deletions docs/content/rules/sort-array-includes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ Allows you to use comments to separate the members of arrays into logical groups
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Allows you to use comments to separate the class members into logical groups. Th
- `true` — All comments will be treated as delimiters, creating partitions.
- `false` — Comments will not be used as delimiters.
- string — A regexp pattern to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-decorators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ Allows you to use comments to separate class decorators into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — An array of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### groups

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-enums.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Allows you to use comments to separate the members of enums into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-exports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Allows you to use comments to separate the exports into logical groups. This can
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Allows you to use comments to separate imports into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — An array of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Allows you to use comments to separate the properties of interfaces into logical
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — An array of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-intersection-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Allows you to use comments to separate the members of intersection types into lo
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Allows you to use comments to separate the members of maps into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-modules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Allows you to use comments to separate the module members into logical groups. T
- `true` — All comments will be treated as delimiters, creating partitions.
- `false` — Comments will not be used as delimiters.
- string — A regexp pattern to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-named-exports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Allows you to use comments to separate the members of named exports into logical
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-named-imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Allows you to use comments to separate the members of named imports into logical
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-object-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Allows you to use comments to separate the members of types into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Allows you to use comments to separate the keys of objects into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — An array of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-sets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Allows you to use comments to separate the members of sets into logical groups.
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-union-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Allows you to use comments to separate the members of union types into logical g
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
1 change: 1 addition & 0 deletions docs/content/rules/sort-variable-declarations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Allows you to use comments to separate the members of variable declarations into
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: boolean | string | string[]; line: boolean | string | string[] }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
10 changes: 5 additions & 5 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { getMatchingContextOptions } from '../utils/get-matching-context-options
import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { hasPartitionComment } from '../utils/has-partition-comment'
import { createNodeIndexMap } from '../utils/create-node-index-map'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
Expand Down Expand Up @@ -265,13 +265,13 @@ export let sortArray = <MessageIds extends string>({

let lastSortingNode = accumulator.at(-1)?.at(-1)
if (
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
hasPartitionComment({
comments: getCommentsBefore({
node: element,
sourceCode,
}),
) ||
partitionByComment: options.partitionByComment,
}) ||
(options.partitionByNewLine &&
lastSortingNode &&
getLinesBetween(sourceCode, lastSortingNode, sortingNode))
Expand Down
9 changes: 8 additions & 1 deletion rules/sort-array-includes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import {
} from '../../utils/common-json-schemas'

export type Options = Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
useConfigurationIf: {
allNamesMatchPattern?: string
Expand All @@ -14,7 +22,6 @@ export type Options = Partial<{
* @deprecated for {@link `groups`}
*/
groupKind: 'literals-first' | 'spreads-first' | 'mixed'
partitionByComment: string[] | boolean | string
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
customGroups: CustomGroup[]
Expand Down
10 changes: 5 additions & 5 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
import { doesCustomGroupMatch } from './sort-classes/does-custom-group-match'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { hasPartitionComment } from '../utils/has-partition-comment'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
import { makeNewlinesFixes } from '../utils/make-newlines-fixes'
Expand Down Expand Up @@ -567,13 +567,13 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
(options.partitionByNewLine &&
lastMember &&
getLinesBetween(sourceCode, lastMember, sortingNode)) ||
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
hasPartitionComment({
comments: getCommentsBefore({
node: member,
sourceCode,
}),
)
partitionByComment: options.partitionByComment,
})
) {
accumulator.push([])
}
Expand Down
9 changes: 8 additions & 1 deletion rules/sort-classes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import {

export type SortClassesOptions = [
Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
partitionByComment: string[] | boolean | string
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
ignoreCallbackDependenciesPatterns: string[]
Expand Down
19 changes: 13 additions & 6 deletions rules/sort-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { validateGroupsConfiguration } from '../utils/validate-groups-configurat
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { getDecoratorName } from './sort-decorators/get-decorator-name'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { hasPartitionComment } from '../utils/has-partition-comment'
import { createNodeIndexMap } from '../utils/create-node-index-map'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
Expand All @@ -38,9 +38,16 @@ import { pairwise } from '../utils/pairwise'

export type Options<T extends string[]> = [
Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
customGroups: Record<T[number], string[] | string>
partitionByComment: string[] | boolean | string
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
groups: (Group<T>[] | Group<T>)[]
Expand Down Expand Up @@ -219,13 +226,13 @@ let sortDecorators = (
let formattedMembers: SortDecoratorsSortingNode[][] = decorators.reduce(
(accumulator: SortDecoratorsSortingNode[][], decorator) => {
if (
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
hasPartitionComment({
comments: getCommentsBefore({
node: decorator,
sourceCode,
}),
)
partitionByComment: options.partitionByComment,
})
) {
accumulator.push([])
}
Expand Down
19 changes: 13 additions & 6 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { hasPartitionComment } from '../utils/has-partition-comment'
import { createNodeIndexMap } from '../utils/create-node-index-map'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
Expand All @@ -38,8 +38,15 @@ import { pairwise } from '../utils/pairwise'

export type Options = [
Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
partitionByComment: string[] | boolean | string
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
partitionByNewLine: boolean
Expand Down Expand Up @@ -153,13 +160,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
}

if (
hasPartitionComment(
options.partitionByComment,
getCommentsBefore({
hasPartitionComment({
comments: getCommentsBefore({
node: member,
sourceCode,
}),
) ||
partitionByComment: options.partitionByComment,
}) ||
(options.partitionByNewLine &&
lastSortingNode &&
getLinesBetween(sourceCode, lastSortingNode, sortingNode))
Expand Down
19 changes: 13 additions & 6 deletions rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { hasPartitionComment } from '../utils/has-partition-comment'
import { createNodeIndexMap } from '../utils/create-node-index-map'
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'
Expand All @@ -30,9 +30,16 @@ import { pairwise } from '../utils/pairwise'

type Options = [
Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
groupKind: 'values-first' | 'types-first' | 'mixed'
partitionByComment: string[] | boolean | string
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
partitionByNewLine: boolean
Expand Down Expand Up @@ -95,13 +102,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
let lastNode = parts.at(-1)?.at(-1)
if (
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore({
hasPartitionComment({
comments: getCommentsBefore({
sourceCode,
node,
}),
)) ||
partitionByComment: options.partitionByComment,
})) ||
(options.partitionByNewLine &&
lastNode &&
getLinesBetween(sourceCode, lastNode, sortingNode))
Expand Down
Loading

0 comments on commit dd71f11

Please sign in to comment.