Skip to content

Commit 2a7eec2

Browse files
committed
feat!: make new config export
1 parent c0e8ab7 commit 2a7eec2

10 files changed

+107
-126
lines changed

configs/recommended-alphabetical.ts

-8
This file was deleted.

configs/recommended-line-length.ts

-8
This file was deleted.

configs/recommended-natural.ts

-8
This file was deleted.

docs/content/configs/recommended-alphabetical.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ It makes it just a tiny bit faster to find a declaration in a large list. Rememb
2727
{
2828
source: dedent`
2929
// eslint.config.js
30-
import perfectionistAlphabetical from 'eslint-plugin-perfectionist/configs/recommended-alphabetical'
30+
import perfectionist from 'eslint-plugin-perfectionist'
3131
3232
export default [
33-
perfectionistAlphabetical,
33+
perfectionist.configs['recommended-alphabetical'],
3434
]
3535
`,
3636
name: 'Flat Config',
@@ -41,7 +41,7 @@ It makes it just a tiny bit faster to find a declaration in a large list. Rememb
4141
// .eslintrc.js
4242
export default {
4343
extends: [
44-
'plugin:perfectionist/recommended-alphabetical',
44+
'plugin:perfectionist/recommended-alphabetical-legacy',
4545
],
4646
}
4747
`,

docs/content/configs/recommended-line-length.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ This configuration will make your code prettier and more pleasing to the eye.
3636
{
3737
source: dedent`
3838
// eslint.config.js
39-
import perfectionistLineLength from 'eslint-plugin-perfectionist/configs/recommended-line-length'
39+
import perfectionist from 'eslint-plugin-perfectionist'
4040
4141
export default [
42-
perfectionistLineLength,
42+
perfectionist.configs['recommended-line-length'],
4343
]
4444
`,
4545
name: 'Flat Config',
@@ -50,7 +50,7 @@ This configuration will make your code prettier and more pleasing to the eye.
5050
// .eslintrc.js
5151
export default {
5252
extends: [
53-
'plugin:perfectionist/recommended-line-length',
53+
'plugin:perfectionist/recommended-line-length-legacy',
5454
],
5555
}
5656
`,

docs/content/configs/recommended-natural.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ This configuration will allow you to navigate through your code faster because a
3434
{
3535
source: dedent`
3636
// eslint.config.js
37-
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
37+
import perfectionist from 'eslint-plugin-perfectionist'
3838
3939
export default [
40-
perfectionistNatural,
40+
perfectionist.configs['recommended-natural'],
4141
]
4242
`,
4343
name: 'Flat Config',
@@ -48,7 +48,7 @@ This configuration will allow you to navigate through your code faster because a
4848
// .eslintrc.js
4949
export default {
5050
extends: [
51-
'plugin:perfectionist/recommended-natural',
51+
'plugin:perfectionist/recommended-natural-legacy',
5252
],
5353
}
5454
`,

index.ts

+65-43
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,42 @@ import sortClasses, { RULE_NAME as sortClassesName } from './rules/sort-classes'
1616
import sortEnums, { RULE_NAME as sortEnumsName } from './rules/sort-enums'
1717
import sortMaps, { RULE_NAME as sortMapsName } from './rules/sort-maps'
1818

19+
interface BaseOptions {
20+
type: 'alphabetical' | 'line-length' | 'natural'
21+
order: 'desc' | 'asc'
22+
}
23+
1924
type RuleSeverity = 'error' | 'warn' | 'off'
2025

2126
type RuleDeclaration = [RuleSeverity, { [key: string]: unknown }?]
2227

23-
let createConfigWithOptions = (options: {
24-
type: 'alphabetical' | 'line-length' | 'natural'
25-
order: 'desc' | 'asc'
26-
ignoreCase?: boolean
27-
}): {
28+
let plugin = {
2829
rules: {
29-
[key: string]: RuleDeclaration
30-
}
31-
plugins: ['perfectionist']
30+
[sortIntersectionTypesName]: sortIntersectionTypes,
31+
[sortSvelteAttributesName]: sortSvelteAttributes,
32+
[sortAstroAttributesName]: sortAstroAttributes,
33+
[sortArrayIncludesName]: sortArrayIncludes,
34+
[sortVueAttributesName]: sortVueAttributes,
35+
[sortNamedExportsName]: sortNamedExports,
36+
[sortNamedImportsName]: sortNamedImports,
37+
[sortObjectTypesName]: sortObjectTypes,
38+
[sortInterfacesName]: sortInterfaces,
39+
[sortUnionTypesName]: sortUnionTypes,
40+
[sortJsxPropsName]: sortJsxProps,
41+
[sortClassesName]: sortClasses,
42+
[sortExportsName]: sortExports,
43+
[sortImportsName]: sortImports,
44+
[sortObjectsName]: sortObjects,
45+
[sortEnumsName]: sortEnums,
46+
[sortMapsName]: sortMaps,
47+
},
48+
name: 'perfectionist',
49+
}
50+
51+
let getRules = (
52+
options: BaseOptions,
53+
): {
54+
[key: string]: RuleDeclaration
3255
} => {
3356
let recommendedRules: {
3457
[key: string]: RuleDeclaration
@@ -97,53 +120,52 @@ let createConfigWithOptions = (options: {
97120
[sortEnumsName]: ['error'],
98121
[sortMapsName]: ['error'],
99122
}
100-
return {
101-
rules: Object.fromEntries(
102-
Object.entries(recommendedRules).map(([key, [message, baseOptions = {}]]) => [
103-
`perfectionist/${key}`,
104-
[message, Object.assign(baseOptions, options)],
105-
]),
106-
),
107-
plugins: ['perfectionist'],
108-
}
123+
return Object.fromEntries(
124+
Object.entries(recommendedRules).map(([key, [message, baseOptions = {}]]) => [
125+
`perfectionist/${key}`,
126+
[message, Object.assign(baseOptions, options)],
127+
]),
128+
)
109129
}
110130

111-
/* eslint-disable perfectionist/sort-objects */
112-
export default {
113-
rules: {
114-
[sortArrayIncludesName]: sortArrayIncludes,
115-
[sortAstroAttributesName]: sortAstroAttributes,
116-
[sortClassesName]: sortClasses,
117-
[sortEnumsName]: sortEnums,
118-
[sortExportsName]: sortExports,
119-
[sortImportsName]: sortImports,
120-
[sortInterfacesName]: sortInterfaces,
121-
[sortJsxPropsName]: sortJsxProps,
122-
[sortMapsName]: sortMaps,
123-
[sortNamedExportsName]: sortNamedExports,
124-
[sortNamedImportsName]: sortNamedImports,
125-
[sortObjectTypesName]: sortObjectTypes,
126-
[sortObjectsName]: sortObjects,
127-
[sortSvelteAttributesName]: sortSvelteAttributes,
128-
[sortIntersectionTypesName]: sortIntersectionTypes,
129-
[sortUnionTypesName]: sortUnionTypes,
130-
[sortVueAttributesName]: sortVueAttributes,
131+
let createConfig = (options: BaseOptions) => ({
132+
plugins: {
133+
perfectionist: plugin,
131134
},
135+
rules: getRules(options),
136+
})
137+
138+
let createLegacyConfig = (options: BaseOptions) => ({
139+
plugins: ['perfectionist'],
140+
rules: getRules(options),
141+
})
142+
143+
export default {
144+
...plugin,
132145
configs: {
133-
'recommended-alphabetical': createConfigWithOptions({
146+
'recommended-alphabetical-legacy': createLegacyConfig({
134147
type: 'alphabetical',
135148
order: 'asc',
136-
ignoreCase: true,
137149
}),
138-
'recommended-natural': createConfigWithOptions({
150+
'recommended-line-length-legacy': createLegacyConfig({
151+
type: 'line-length',
152+
order: 'desc',
153+
}),
154+
'recommended-natural-legacy': createLegacyConfig({
139155
type: 'natural',
140156
order: 'asc',
141-
ignoreCase: true,
142157
}),
143-
'recommended-line-length': createConfigWithOptions({
158+
'recommended-alphabetical': createConfig({
159+
type: 'alphabetical',
160+
order: 'asc',
161+
}),
162+
'recommended-line-length': createConfig({
144163
type: 'line-length',
145164
order: 'desc',
146165
}),
166+
'recommended-natural': createConfig({
167+
type: 'natural',
168+
order: 'asc',
169+
}),
147170
},
148-
name: 'eslint-plugin-perfectionist',
149171
}

package.json

-12
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@
4444
"require": "./dist/index.js",
4545
"import": "./dist/index.mjs"
4646
},
47-
"./configs/recommended-alphabetical": {
48-
"require": "./dist/configs/recommended-alphabetical.js",
49-
"import": "./dist/configs/recommended-alphabetical.mjs"
50-
},
51-
"./configs/recommended-line-length": {
52-
"require": "./dist/configs/recommended-line-length.js",
53-
"import": "./dist/configs/recommended-line-length.mjs"
54-
},
55-
"./configs/recommended-natural": {
56-
"require": "./dist/configs/recommended-natural.js",
57-
"import": "./dist/configs/recommended-natural.mjs"
58-
},
5947
"./package.json": "./package.json"
6048
},
6149
"peerDependenciesMeta": {

readme.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,7 @@ npm install --save-dev eslint-plugin-perfectionist
5555

5656
Add `eslint-plugin-perfectionist` to the plugins section of the ESLint configuration file and define the list of rules you will use.
5757

58-
### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files))
59-
60-
<!-- prettier-ignore -->
61-
```json
62-
{
63-
"plugins": [
64-
"perfectionist"
65-
],
66-
"rules": {
67-
"perfectionist/sort-objects": [
68-
"error",
69-
{
70-
"type": "natural",
71-
"order": "asc"
72-
}
73-
]
74-
}
75-
}
76-
```
77-
78-
### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files-new)) (requires eslint >= v8.23.0)
58+
### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files))
7959

8060
```js
8161
import perfectionist from 'eslint-plugin-perfectionist'
@@ -98,32 +78,52 @@ export default [
9878
]
9979
```
10080

101-
## ⚙️ Configs
102-
103-
The easiest way to use `eslint-plugin-perfectionist` is to use ready-made configs. Config files use all the rules of the current plugin, but you can override them.
104-
105-
### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files))
81+
### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files-deprecated))
10682

10783
<!-- prettier-ignore -->
10884
```json
10985
{
110-
"extends": [
111-
"plugin:perfectionist/recommended-natural"
112-
]
86+
"plugins": [
87+
"perfectionist"
88+
],
89+
"rules": {
90+
"perfectionist/sort-objects": [
91+
"error",
92+
{
93+
"type": "natural",
94+
"order": "asc"
95+
}
96+
]
97+
}
11398
}
11499
```
115100

116-
### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files-new))
101+
## ⚙️ Configs
102+
103+
The easiest way to use `eslint-plugin-perfectionist` is to use ready-made configs. Config files use all the rules of the current plugin, but you can override them.
104+
105+
### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files))
117106

118107
<!-- prettier-ignore -->
119108
```js
120-
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
109+
import perfectionist from 'eslint-plugin-perfectionist'
121110

122111
export default [
123-
perfectionistNatural,
112+
perfectionist.configs['recommended-natural'],
124113
]
125114
```
126115

116+
### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files-deprecated))
117+
118+
<!-- prettier-ignore -->
119+
```json
120+
{
121+
"extends": [
122+
"plugin:perfectionist/recommended-natural-legacy"
123+
]
124+
}
125+
```
126+
127127
### List of Configs
128128

129129
| Name | Description |

vite.config.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import path from 'node:path'
44
export default defineConfig({
55
build: {
66
lib: {
7-
entry: [
8-
path.resolve(__dirname, 'configs/recommended-alphabetical.ts'),
9-
path.resolve(__dirname, 'configs/recommended-line-length.ts'),
10-
path.resolve(__dirname, 'configs/recommended-natural.ts'),
11-
path.resolve(__dirname, 'index.ts'),
12-
],
137
fileName: (format, entryName) => {
148
let directory = ''
159

@@ -19,6 +13,7 @@ export default defineConfig({
1913

2014
return `${directory}${entryName}.${format === 'es' ? 'mjs' : 'js'}`
2115
},
16+
entry: path.resolve(__dirname, 'index.ts'),
2217
name: 'eslint-plugin-perfectionist',
2318
formats: ['cjs', 'es'],
2419
},

0 commit comments

Comments
 (0)