Skip to content

Commit

Permalink
feat: allow specifying cross-rule settings
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io authored Aug 14, 2024
1 parent b02d626 commit 8e15730
Show file tree
Hide file tree
Showing 23 changed files with 229 additions and 20 deletions.
78 changes: 78 additions & 0 deletions docs/content/guide/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,81 @@ type="config-type"
client:load
lang="js"
/>

## Settings

Many rules have common settings. You can set them in the `settings` field.

The highest priority is given to the settings of a particular rule. Next comes the settings field and the lowest priority has default values.

In settings you can set the following options:

- `type` — The type of sorting. Possible values are `'alphabetical'`, `'natural'` and `'line-length'`.
- `order` — The order of sorting. Possible values are `'asc'` and `'desc'`.
- `ignoreCase` — Ignore case when sorting.
- `ignorePattern` — Ignore sorting for elements that match the pattern.
- `partitionByComment` — Partition the sorted elements by comments. Values can be `true`, `false` or glob pattern string.
- `partitionByNewLine` — Partition the sorted elements by new lines. Values can be `true` or `false`.

Example:

<CodeTabs
code={[
{
source: dedent`
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-objects': ['error', {
type: 'alphabetical',
}],
'perfectionist/sort-interfaces': ['error'],
},
settings: {
perfectionist: {
type: 'line-length',
partitionByComment: true,
},
},
},
]
`,
name: 'Flat Config',
value: 'flat',
},
{
source: dedent`
// .eslintrc.js
module.exports = {
plugins: [
'perfectionist',
],
rules: {
'perfectionist/sort-objects': ['error', {
type: 'alphabetical',
}],
'perfectionist/sort-interfaces': ['error'],
},
settings: {
perfectionist: {
type: 'line-length',
partitionByComment: true,
},
},
}
`,
name: 'Legacy Config',
value: 'legacy',
},

]}
type="config-type"
client:load
lang="js"
/>
5 changes: 4 additions & 1 deletion rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -88,8 +89,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
? node.object.elements
: node.object.arguments

let settings = getSettings(context.settings)

if (elements.length > 1) {
let options = complete(context.options.at(0), {
let options = complete(context.options.at(0), settings, {
groupKind: 'literals-first',
type: 'alphabetical',
ignoreCase: true,
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -127,7 +128,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
let { attributes } = node.openingElement

if (attributes.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreCase: true,
customGroups: {},
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -238,7 +239,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
create: context => ({
ClassBody: node => {
if (node.body.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
groups: [
'index-signature',
'static-property',
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -113,7 +114,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
members.length > 1 &&
members.every(({ initializer }) => initializer)
) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
partitionByComment: false,
type: 'alphabetical',
ignoreCase: true,
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -66,7 +67,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
create: context => {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreCase: true,
order: 'asc',
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { getNodeRange } from '../utils/get-node-range'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -212,7 +213,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
create: context => {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
groups: [
'type',
['builtin', 'external'],
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -138,7 +139,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
create: context => ({
TSInterfaceDeclaration: node => {
if (node.body.body.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)
let options = complete(context.options.at(0), settings, {
partitionByNewLine: false,
type: 'alphabetical',
groupKind: 'mixed',
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { useGroups } from '../utils/use-groups'
Expand Down Expand Up @@ -103,7 +104,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
create: context => ({
TSIntersectionType: node => {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreCase: true,
order: 'asc',
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -127,7 +128,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
create: context => ({
JSXElement: node => {
if (node.openingElement.attributes.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignorePattern: [],
ignoreCase: true,
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -78,7 +79,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
let [{ elements }] = node.arguments

if (elements.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreCase: true,
order: 'asc',
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 @@ -4,6 +4,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -75,7 +76,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
create: context => ({
ExportNamedDeclaration: node => {
if (node.specifiers.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
groupKind: 'mixed',
ignoreCase: true,
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 @@ -4,6 +4,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -85,7 +86,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
)

if (specifiers.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreAlias: false,
groupKind: 'mixed',
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 @@ -8,6 +8,7 @@ import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -127,7 +128,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
create: context => ({
TSTypeLiteral: node => {
if (node.members.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
partitionByNewLine: false,
type: 'alphabetical',
groupKind: 'mixed',
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getSourceCode } from '../utils/get-source-code'
import { getNodeParent } from '../utils/get-node-parent'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -175,7 +176,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
let sortObject = (
node: TSESTree.ObjectExpression | TSESTree.ObjectPattern,
) => {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
partitionByNewLine: false,
partitionByComment: false,
styledComponents: true,
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-svelte-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -124,7 +125,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
return {
SvelteStartTag: (node: AST.SvelteStartTag) => {
if (node.attributes.length > 1) {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreCase: true,
customGroups: {},
Expand Down
5 changes: 4 additions & 1 deletion rules/sort-switch-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isPositive } from '../utils/is-positive'
import { makeFixes } from '../utils/make-fixes'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -69,7 +70,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
create: context => ({
SwitchStatement: node => {
let options = complete(context.options.at(0), {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
type: 'alphabetical',
ignoreCase: true,
order: 'asc',
Expand Down
Loading

0 comments on commit 8e15730

Please sign in to comment.