Skip to content

Commit

Permalink
feat(eslint-config): allow perfectionist to be overridden or disabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown authored Nov 30, 2024
1 parent 1b54cdc commit ebe5b31
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .changeset/nice-avocados-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/eslint-config": minor
---

Add options to override or disable the Perfectionist config.

86 changes: 56 additions & 30 deletions packages/eslint-config/src/configs/perfectionist.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,76 @@
import type {Config} from '../config'
import type {Flatten, OptionsIsInEditor, OptionsOverrides, OptionsPerfectionist} from '../options'
import {perfectionist as pluginPerfectionist} from '../plugins'

/**
* Represents the combined options for the Perfectionist ESLint plugin, including options for editor integration, overrides, and the Perfectionist plugin itself.
*/
export type PerfectionistOptions = Flatten<
OptionsIsInEditor & OptionsOverrides & OptionsPerfectionist
>

/**
* Perfectionist plugin for sorting items and properties.
*
* @see https://github.com/azat-io/eslint-plugin-perfectionist
*/
export async function perfectionist(): Promise<Config[]> {
export async function perfectionist(options: PerfectionistOptions = {}): Promise<Config[]> {
const {
isInEditor = false,
overrides = {},
sortExports = true,
sortImports = true,
sortNamedExports = true,
sortNamedImports = true,
} = options
return [
{
name: '@bfra.me/perfectionist',
plugins: {
perfectionist: pluginPerfectionist as any,
},
rules: {
'perfectionist/sort-named-exports': [
'error',
{groupKind: 'values-first', order: 'asc', type: 'natural'},
],
...(sortNamedExports && {
'perfectionist/sort-named-exports': [
isInEditor ? 'warn' : 'error',
{groupKind: 'values-first', type: 'natural'},
],
}),

...(sortNamedImports && {
'perfectionist/sort-named-imports': [
isInEditor ? 'warn' : 'error',
{groupKind: 'values-first', type: 'natural'},
],
}),

...(sortExports && {
'perfectionist/sort-exports': [isInEditor ? 'warn' : 'error', {type: 'natural'}],
}),

'perfectionist/sort-named-imports': [
'error',
{groupKind: 'values-first', order: 'asc', type: 'natural'},
],
'perfectionist/sort-exports': ['error', {type: 'natural'}],
...(sortImports && {
'perfectionist/sort-imports': [
isInEditor ? 'warn' : 'error',
{
groups: [
'type',
['parent-type', 'sibling-type', 'index-type'],
'builtin',
'external',
['internal', 'internal-type'],
['parent', 'sibling', 'index'],
'object',
'side-effect',
'side-effect-style',
],
internalPattern: ['^[~#]/.*'],
newlinesBetween: 'ignore',
type: 'natural',
},
],
}),

'perfectionist/sort-imports': [
'error',
{
groups: [
'type',
['parent-type', 'sibling-type', 'index-type'],
'builtin',
'external',
['internal', 'internal-type'],
['parent', 'sibling', 'index'],
'object',
'side-effect',
'side-effect-style',
],
internalPattern: ['^[~#]/.*'],
newlinesBetween: 'ignore',
order: 'asc',
type: 'natural',
},
],
...overrides,
},
},
]
Expand Down
12 changes: 11 additions & 1 deletion packages/eslint-config/src/define-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function defineConfig(
): ConfigComposer {
const {
gitignore: enableGitignore = true,
perfectionist: enablePerfectionist = true,
typescript: enableTypeScript = isPackageExists('typescript'),
} = options

Expand Down Expand Up @@ -80,9 +81,18 @@ export async function defineConfig(
jsdoc(),
imports(),
command(),
perfectionist(),
)

if (enablePerfectionist) {
configs.push(
perfectionist({
isInEditor,
overrides: getOverrides(options, 'perfectionist'),
...resolveSubOptions(options, 'perfectionist'),
}),
)
}

const typescriptOptions = resolveSubOptions(options, 'typescript')
// const tsconfigPath ='tsconfigPath' in typescriptOptions ? typescriptOptions.tsconfigPath : undefined

Expand Down
32 changes: 32 additions & 0 deletions packages/eslint-config/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ export interface OptionsOverrides {
overrides?: Config['rules']
}

/**
* Options for configuring the Perfectionist sorting behavior.
*/
export interface OptionsPerfectionist {
/**
* Whether to sort named exports.
*
* @default true
*/
sortNamedExports?: boolean
/**
* Whether to sort named imports.
* @default true
*/
sortNamedImports?: boolean
/**
* Whether to sort exports.
* @default true
*/
sortExports?: boolean
/**
* Whether to sort imports.
* @default true
*/
sortImports?: boolean
}

/**
* Provides options to configure the TypeScript parser and type-aware linting rules.
*
Expand Down Expand Up @@ -140,6 +167,11 @@ export type Options = Flatten<
*/
javascript?: OptionsOverrides

/**
* Options to override the behavior of Perfectionist sorting rules.
*/
perfectionist?: boolean | OptionsPerfectionist

/**
* Enable TypeScript support.
*
Expand Down

0 comments on commit ebe5b31

Please sign in to comment.