Skip to content

Commit 8e15730

Browse files
authored
feat: allow specifying cross-rule settings
1 parent b02d626 commit 8e15730

23 files changed

+229
-20
lines changed

docs/content/guide/getting-started.mdx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,81 @@ type="config-type"
131131
client:load
132132
lang="js"
133133
/>
134+
135+
## Settings
136+
137+
Many rules have common settings. You can set them in the `settings` field.
138+
139+
The highest priority is given to the settings of a particular rule. Next comes the settings field and the lowest priority has default values.
140+
141+
In settings you can set the following options:
142+
143+
- `type` — The type of sorting. Possible values are `'alphabetical'`, `'natural'` and `'line-length'`.
144+
- `order` — The order of sorting. Possible values are `'asc'` and `'desc'`.
145+
- `ignoreCase` — Ignore case when sorting.
146+
- `ignorePattern` — Ignore sorting for elements that match the pattern.
147+
- `partitionByComment` — Partition the sorted elements by comments. Values can be `true`, `false` or glob pattern string.
148+
- `partitionByNewLine` — Partition the sorted elements by new lines. Values can be `true` or `false`.
149+
150+
Example:
151+
152+
<CodeTabs
153+
code={[
154+
{
155+
source: dedent`
156+
// eslint.config.js
157+
import perfectionist from 'eslint-plugin-perfectionist'
158+
159+
export default [
160+
{
161+
plugins: {
162+
perfectionist,
163+
},
164+
rules: {
165+
'perfectionist/sort-objects': ['error', {
166+
type: 'alphabetical',
167+
}],
168+
'perfectionist/sort-interfaces': ['error'],
169+
},
170+
settings: {
171+
perfectionist: {
172+
type: 'line-length',
173+
partitionByComment: true,
174+
},
175+
},
176+
},
177+
]
178+
`,
179+
name: 'Flat Config',
180+
value: 'flat',
181+
},
182+
{
183+
source: dedent`
184+
// .eslintrc.js
185+
module.exports = {
186+
plugins: [
187+
'perfectionist',
188+
],
189+
rules: {
190+
'perfectionist/sort-objects': ['error', {
191+
type: 'alphabetical',
192+
}],
193+
'perfectionist/sort-interfaces': ['error'],
194+
},
195+
settings: {
196+
perfectionist: {
197+
type: 'line-length',
198+
partitionByComment: true,
199+
},
200+
},
201+
}
202+
`,
203+
name: 'Legacy Config',
204+
value: 'legacy',
205+
},
206+
207+
]}
208+
type="config-type"
209+
client:load
210+
lang="js"
211+
/>

rules/sort-array-includes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getGroupNumber } from '../utils/get-group-number'
77
import { getSourceCode } from '../utils/get-source-code'
88
import { toSingleLine } from '../utils/to-single-line'
99
import { rangeToDiff } from '../utils/range-to-diff'
10+
import { getSettings } from '../utils/get-settings'
1011
import { isPositive } from '../utils/is-positive'
1112
import { sortNodes } from '../utils/sort-nodes'
1213
import { makeFixes } from '../utils/make-fixes'
@@ -88,8 +89,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
8889
? node.object.elements
8990
: node.object.arguments
9091

92+
let settings = getSettings(context.settings)
93+
9194
if (elements.length > 1) {
92-
let options = complete(context.options.at(0), {
95+
let options = complete(context.options.at(0), settings, {
9396
groupKind: 'literals-first',
9497
type: 'alphabetical',
9598
ignoreCase: true,

rules/sort-astro-attributes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
99
import { getGroupNumber } from '../utils/get-group-number'
1010
import { getSourceCode } from '../utils/get-source-code'
1111
import { rangeToDiff } from '../utils/range-to-diff'
12+
import { getSettings } from '../utils/get-settings'
1213
import { isPositive } from '../utils/is-positive'
1314
import { useGroups } from '../utils/use-groups'
1415
import { makeFixes } from '../utils/make-fixes'
@@ -127,7 +128,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
127128
let { attributes } = node.openingElement
128129

129130
if (attributes.length > 1) {
130-
let options = complete(context.options.at(0), {
131+
let settings = getSettings(context.settings)
132+
133+
let options = complete(context.options.at(0), settings, {
131134
type: 'alphabetical',
132135
ignoreCase: true,
133136
customGroups: {},

rules/sort-classes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getGroupNumber } from '../utils/get-group-number'
1111
import { getSourceCode } from '../utils/get-source-code'
1212
import { toSingleLine } from '../utils/to-single-line'
1313
import { rangeToDiff } from '../utils/range-to-diff'
14+
import { getSettings } from '../utils/get-settings'
1415
import { isPositive } from '../utils/is-positive'
1516
import { useGroups } from '../utils/use-groups'
1617
import { sortNodes } from '../utils/sort-nodes'
@@ -238,7 +239,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
238239
create: context => ({
239240
ClassBody: node => {
240241
if (node.body.length > 1) {
241-
let options = complete(context.options.at(0), {
242+
let settings = getSettings(context.settings)
243+
244+
let options = complete(context.options.at(0), settings, {
242245
groups: [
243246
'index-signature',
244247
'static-property',

rules/sort-enums.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getCommentBefore } from '../utils/get-comment-before'
99
import { getSourceCode } from '../utils/get-source-code'
1010
import { toSingleLine } from '../utils/to-single-line'
1111
import { rangeToDiff } from '../utils/range-to-diff'
12+
import { getSettings } from '../utils/get-settings'
1213
import { isPositive } from '../utils/is-positive'
1314
import { sortNodes } from '../utils/sort-nodes'
1415
import { makeFixes } from '../utils/make-fixes'
@@ -113,7 +114,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
113114
members.length > 1 &&
114115
members.every(({ initializer }) => initializer)
115116
) {
116-
let options = complete(context.options.at(0), {
117+
let settings = getSettings(context.settings)
118+
119+
let options = complete(context.options.at(0), settings, {
117120
partitionByComment: false,
118121
type: 'alphabetical',
119122
ignoreCase: true,

rules/sort-exports.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { SortingNode } from '../typings'
55
import { createEslintRule } from '../utils/create-eslint-rule'
66
import { getSourceCode } from '../utils/get-source-code'
77
import { rangeToDiff } from '../utils/range-to-diff'
8+
import { getSettings } from '../utils/get-settings'
89
import { isPositive } from '../utils/is-positive'
910
import { sortNodes } from '../utils/sort-nodes'
1011
import { makeFixes } from '../utils/make-fixes'
@@ -66,7 +67,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
6667
},
6768
],
6869
create: context => {
69-
let options = complete(context.options.at(0), {
70+
let settings = getSettings(context.settings)
71+
72+
let options = complete(context.options.at(0), settings, {
7073
type: 'alphabetical',
7174
ignoreCase: true,
7275
order: 'asc',

rules/sort-imports.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getGroupNumber } from '../utils/get-group-number'
1313
import { getSourceCode } from '../utils/get-source-code'
1414
import { getNodeRange } from '../utils/get-node-range'
1515
import { rangeToDiff } from '../utils/range-to-diff'
16+
import { getSettings } from '../utils/get-settings'
1617
import { isPositive } from '../utils/is-positive'
1718
import { useGroups } from '../utils/use-groups'
1819
import { sortNodes } from '../utils/sort-nodes'
@@ -212,7 +213,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
212213
},
213214
],
214215
create: context => {
215-
let options = complete(context.options.at(0), {
216+
let settings = getSettings(context.settings)
217+
218+
let options = complete(context.options.at(0), settings, {
216219
groups: [
217220
'type',
218221
['builtin', 'external'],

rules/sort-interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getGroupNumber } from '../utils/get-group-number'
99
import { getSourceCode } from '../utils/get-source-code'
1010
import { toSingleLine } from '../utils/to-single-line'
1111
import { rangeToDiff } from '../utils/range-to-diff'
12+
import { getSettings } from '../utils/get-settings'
1213
import { isPositive } from '../utils/is-positive'
1314
import { useGroups } from '../utils/use-groups'
1415
import { sortNodes } from '../utils/sort-nodes'
@@ -138,7 +139,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
138139
create: context => ({
139140
TSInterfaceDeclaration: node => {
140141
if (node.body.body.length > 1) {
141-
let options = complete(context.options.at(0), {
142+
let settings = getSettings(context.settings)
143+
let options = complete(context.options.at(0), settings, {
142144
partitionByNewLine: false,
143145
type: 'alphabetical',
144146
groupKind: 'mixed',

rules/sort-intersection-types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getGroupNumber } from '../utils/get-group-number'
55
import { getSourceCode } from '../utils/get-source-code'
66
import { toSingleLine } from '../utils/to-single-line'
77
import { rangeToDiff } from '../utils/range-to-diff'
8+
import { getSettings } from '../utils/get-settings'
89
import { isPositive } from '../utils/is-positive'
910
import { sortNodes } from '../utils/sort-nodes'
1011
import { useGroups } from '../utils/use-groups'
@@ -103,7 +104,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
103104
],
104105
create: context => ({
105106
TSIntersectionType: node => {
106-
let options = complete(context.options.at(0), {
107+
let settings = getSettings(context.settings)
108+
109+
let options = complete(context.options.at(0), settings, {
107110
type: 'alphabetical',
108111
ignoreCase: true,
109112
order: 'asc',

rules/sort-jsx-props.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
88
import { getGroupNumber } from '../utils/get-group-number'
99
import { getSourceCode } from '../utils/get-source-code'
1010
import { rangeToDiff } from '../utils/range-to-diff'
11+
import { getSettings } from '../utils/get-settings'
1112
import { isPositive } from '../utils/is-positive'
1213
import { useGroups } from '../utils/use-groups'
1314
import { makeFixes } from '../utils/make-fixes'
@@ -127,7 +128,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
127128
create: context => ({
128129
JSXElement: node => {
129130
if (node.openingElement.attributes.length > 1) {
130-
let options = complete(context.options.at(0), {
131+
let settings = getSettings(context.settings)
132+
133+
let options = complete(context.options.at(0), settings, {
131134
type: 'alphabetical',
132135
ignorePattern: [],
133136
ignoreCase: true,

0 commit comments

Comments
 (0)