Skip to content

Commit 73dd08b

Browse files
hugop95azat-io
authored andcommitted
feat: support annotation-based config
1 parent 525658a commit 73dd08b

35 files changed

+724
-225
lines changed

docs/content/rules/sort-interfaces.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ This option is only applicable when [`partitionByNewLine`](#partitionbynewline)
319319
{
320320
allNamesMatchPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
321321
declarationMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
322+
declarationCommentMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
322323
}
323324
```
324325
</sub>
@@ -381,6 +382,26 @@ Example configuration:
381382
}
382383
```
383384

385+
- `declarationCommentMatchesPattern` — A regexp pattern to specify which comments above the interface declaration should match.
386+
387+
Example configuration:
388+
```ts
389+
{
390+
'perfectionist/sort-interfaces': [
391+
'error',
392+
{
393+
type: 'unsorted', // Don't sort interfaces with a "do not sort" comment
394+
useConfigurationIf: {
395+
declarationCommentMatchesPattern: '^do not sort$',
396+
},
397+
},
398+
{
399+
type: 'alphabetical' // Fallback configuration
400+
}
401+
],
402+
}
403+
```
404+
384405
### groups
385406

386407
<sub>

docs/content/rules/sort-object-types.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ This option is only applicable when [`partitionByNewLine`](#partitionbynewline)
284284
{
285285
allNamesMatchPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
286286
declarationMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
287+
declarationCommentMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
287288
}
288289
```
289290
</sub>
@@ -347,6 +348,26 @@ Example configuration:
347348
}
348349
```
349350

351+
- `declarationCommentMatchesPattern` — A regexp pattern to specify which comments above the type declaration should match.
352+
353+
Example configuration:
354+
```ts
355+
{
356+
'perfectionist/sort-object-types': [
357+
'error',
358+
{
359+
type: 'unsorted', // Don't sort object types with a "do not sort" comment
360+
useConfigurationIf: {
361+
declarationCommentMatchesPattern: '^do not sort$',
362+
},
363+
},
364+
{
365+
type: 'alphabetical' // Fallback configuration
366+
}
367+
],
368+
}
369+
```
370+
350371
### groups
351372

352373
<sub>

docs/content/rules/sort-objects.mdx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ Specifies whether this rule should be applied to styled-components like librarie
304304
<sub>
305305
type:
306306
```
307-
{
308-
allNamesMatchPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
309-
}
307+
string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
310308
```
311309
</sub>
312310
<sub>default: `[]`</sub>
@@ -349,6 +347,7 @@ The `groups` attribute specifies whether to use groups to sort destructured obje
349347
{
350348
allNamesMatchPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
351349
callingFunctionNamePattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
350+
declarationCommentMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
352351
}
353352
```
354353
</sub>
@@ -394,6 +393,7 @@ Example configuration:
394393

395394
- `callingFunctionNamePattern` — A regexp pattern for matching objects that are passed as arguments to a function with a specific name.
396395

396+
Example configuration:
397397
```ts
398398
{
399399
'perfectionist/sort-objects': [
@@ -411,6 +411,26 @@ Example configuration:
411411
}
412412
```
413413

414+
- `declarationCommentMatchesPattern` — A regexp pattern to specify which comments above the object declaration should match.
415+
416+
Example configuration:
417+
```ts
418+
{
419+
'perfectionist/sort-objects': [
420+
'error',
421+
{
422+
type: 'unsorted', // Don't sort objects with a "do not sort" comment
423+
useConfigurationIf: {
424+
declarationCommentMatchesPattern: '^do not sort$',
425+
},
426+
},
427+
{
428+
type: 'alphabetical' // Fallback configuration
429+
}
430+
],
431+
}
432+
```
433+
414434
### groups
415435

416436
<sub>

rules/sort-array-includes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
singleCustomGroupJsonSchema,
3030
allSelectors,
3131
} from './sort-array-includes/types'
32-
import { getMatchingContextOptions } from '../utils/get-matching-context-options'
32+
import { filterOptionsByAllNamesMatch } from '../utils/filter-options-by-all-names-match'
3333
import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
3434
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
3535
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
@@ -57,7 +57,7 @@ type SortArrayIncludesSortingNode = SortingNode<
5757
TSESTree.SpreadElement | TSESTree.Expression
5858
>
5959

60-
export let defaultOptions: Required<Options[0]> = {
60+
export let defaultOptions: Required<Options[number]> = {
6161
fallbackSort: { type: 'unsorted' },
6262
specialCharacters: 'keep',
6363
partitionByComment: false,
@@ -162,7 +162,7 @@ export function sortArray<MessageIds extends string>({
162162
let { sourceCode, id } = context
163163
let settings = getSettings(context.settings)
164164

165-
let matchedContextOptions = getMatchingContextOptions({
165+
let matchedContextOptions = filterOptionsByAllNamesMatch({
166166
nodeNames: elements
167167
.filter(element => element !== null)
168168
.map(element => getNodeName({ sourceCode, element })),

rules/sort-classes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type MessageId =
6262
| 'unexpectedClassesGroupOrder'
6363
| 'unexpectedClassesOrder'
6464

65-
let defaultOptions: Required<SortClassesOptions[0]> = {
65+
let defaultOptions: Required<SortClassesOptions[number]> = {
6666
groups: [
6767
'index-signature',
6868
['static-property', 'static-accessor-property'],

rules/sort-decorators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type MessageId =
4646
| 'extraSpacingBetweenDecorators'
4747
| 'unexpectedDecoratorsOrder'
4848

49-
let defaultOptions: Required<Options[0]> = {
49+
let defaultOptions: Required<Options[number]> = {
5050
fallbackSort: { type: 'unsorted' },
5151
specialCharacters: 'keep',
5252
partitionByComment: false,
@@ -190,7 +190,7 @@ export default createEslintRule<Options, MessageId>({
190190

191191
function sortDecorators(
192192
context: Readonly<RuleContext<MessageId, Options>>,
193-
options: Required<Options[0]>,
193+
options: Required<Options[number]>,
194194
decorators: TSESTree.Decorator[],
195195
): void {
196196
if (!isSortable(decorators)) {

rules/sort-enums.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface SortEnumsSortingNode
5353
value: string | null
5454
}
5555

56-
let defaultOptions: Required<Options[0]> = {
56+
let defaultOptions: Required<Options[number]> = {
5757
fallbackSort: { type: 'unsorted' },
5858
partitionByComment: false,
5959
partitionByNewLine: false,
@@ -364,7 +364,7 @@ function computeNodeValueGetter({
364364
isNumericEnum,
365365
options,
366366
}: {
367-
options: Pick<Required<Options[0]>, 'forceNumericSort' | 'sortByValue'>
367+
options: Pick<Required<Options[number]>, 'forceNumericSort' | 'sortByValue'>
368368
isNumericEnum: boolean
369369
}): NodeValueGetterFunction<SortEnumsSortingNode> | null {
370370
return options.sortByValue || (isNumericEnum && options.forceNumericSort)
@@ -382,7 +382,7 @@ function computeOptionType({
382382
options,
383383
}: {
384384
options: Pick<
385-
Required<Options[0]>,
385+
Required<Options[number]>,
386386
'forceNumericSort' | 'sortByValue' | 'type'
387387
>
388388
isNumericEnum: boolean

rules/sort-exports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type SortExportsSortingNode = SortingNode<
5151
TSESTree.ExportNamedDeclarationWithSource | TSESTree.ExportAllDeclaration
5252
>
5353

54-
let defaultOptions: Required<Options[0]> = {
54+
let defaultOptions: Required<Options[number]> = {
5555
fallbackSort: { type: 'unsorted' },
5656
specialCharacters: 'keep',
5757
partitionByComment: false,

rules/sort-heritage-clauses.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type MessageId =
4242
| 'extraSpacingBetweenHeritageClauses'
4343
| 'unexpectedHeritageClausesOrder'
4444

45-
let defaultOptions: Required<Options[0]> = {
45+
let defaultOptions: Required<Options[number]> = {
4646
fallbackSort: { type: 'unsorted' },
4747
specialCharacters: 'keep',
4848
newlinesBetween: 'ignore',
@@ -116,7 +116,7 @@ export default createEslintRule<Options, MessageId>({
116116

117117
function sortHeritageClauses(
118118
context: Readonly<RuleContext<MessageId, Options>>,
119-
options: Required<Options[0]>,
119+
options: Required<Options[number]>,
120120
heritageClauses:
121121
| TSESTree.TSInterfaceHeritage[]
122122
| TSESTree.TSClassImplements[]

rules/sort-imports.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ export type MessageId =
6464
| 'missedCommentAboveImport'
6565
| 'unexpectedImportsOrder'
6666

67-
let defaultOptions: Required<Omit<Options[0], 'maxLineLength' | 'tsconfig'>> &
68-
Pick<Options[0], 'maxLineLength' | 'tsconfig'> = {
67+
let defaultOptions: Required<
68+
Omit<Options[number], 'maxLineLength' | 'tsconfig'>
69+
> &
70+
Pick<Options[number], 'maxLineLength' | 'tsconfig'> = {
6971
groups: [
7072
'type-import',
7173
['value-builtin', 'value-external'],
@@ -533,7 +535,7 @@ function computeGroupExceptUnknown({
533535
options,
534536
name,
535537
}: {
536-
options: Omit<Required<Options[0]>, 'maxLineLength' | 'tsconfig'>
538+
options: Omit<Required<Options[number]>, 'maxLineLength' | 'tsconfig'>
537539
selectors: Selector[]
538540
modifiers: Modifier[]
539541
name: string

0 commit comments

Comments
 (0)