forked from azat-io/eslint-plugin-perfectionist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
176 lines (164 loc) · 5.65 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type { Linter, ESLint } from 'eslint'
import sortIntersectionTypes, { RULE_NAME as sortIntersectionTypesName } from './rules/sort-intersection-types'
import sortSvelteAttributes, { RULE_NAME as sortSvelteAttributesName } from './rules/sort-svelte-attributes'
import sortAstroAttributes, { RULE_NAME as sortAstroAttributesName } from './rules/sort-astro-attributes'
import sortArrayIncludes, { RULE_NAME as sortArrayIncludesName } from './rules/sort-array-includes'
import sortVueAttributes, { RULE_NAME as sortVueAttributesName } from './rules/sort-vue-attributes'
import sortNamedExports, { RULE_NAME as sortNamedExportsName } from './rules/sort-named-exports'
import sortNamedImports, { RULE_NAME as sortNamedImportsName } from './rules/sort-named-imports'
import sortObjectTypes, { RULE_NAME as sortObjectTypesName } from './rules/sort-object-types'
import sortSwitchCase, { RULE_NAME as sortSwitchCaseName } from './rules/sort-switch-case'
import sortUnionTypes, { RULE_NAME as sortUnionTypesName } from './rules/sort-union-types'
import sortInterfaces, { RULE_NAME as sortInterfacesName } from './rules/sort-interfaces'
import sortJsxProps, { RULE_NAME as sortJsxPropsName } from './rules/sort-jsx-props'
import sortExports, { RULE_NAME as sortExportsName } from './rules/sort-exports'
import sortImports, { RULE_NAME as sortImportsName } from './rules/sort-imports'
import sortObjects, { RULE_NAME as sortObjectsName } from './rules/sort-objects'
import sortClasses, { RULE_NAME as sortClassesName } from './rules/sort-classes'
import sortEnums, { RULE_NAME as sortEnumsName } from './rules/sort-enums'
import sortMaps, { RULE_NAME as sortMapsName } from './rules/sort-maps'
interface BaseOptions {
type: 'alphabetical' | 'line-length' | 'natural'
order: 'desc' | 'asc'
}
type RuleSeverity = 'error' | 'warn' | 'off'
type RuleDeclaration = [RuleSeverity, Record<string, unknown>?]
let name = 'perfectionist'
let formatRuleNames = <T extends unknown>(rules: Record<string, T>) =>
Object.fromEntries(Object.entries(rules).map(([key, value]) => [`${name}/${key}`, value]))
let plugin = {
rules: {
[sortIntersectionTypesName]: sortIntersectionTypes,
[sortSvelteAttributesName]: sortSvelteAttributes,
[sortAstroAttributesName]: sortAstroAttributes,
[sortArrayIncludesName]: sortArrayIncludes,
[sortVueAttributesName]: sortVueAttributes,
[sortNamedExportsName]: sortNamedExports,
[sortNamedImportsName]: sortNamedImports,
[sortObjectTypesName]: sortObjectTypes,
[sortInterfacesName]: sortInterfaces,
[sortSwitchCaseName]: sortSwitchCase,
[sortUnionTypesName]: sortUnionTypes,
[sortJsxPropsName]: sortJsxProps,
[sortClassesName]: sortClasses,
[sortExportsName]: sortExports,
[sortImportsName]: sortImports,
[sortObjectsName]: sortObjects,
[sortEnumsName]: sortEnums,
[sortMapsName]: sortMaps,
},
name,
} as unknown as ESLint.Plugin
let getRules = (options: BaseOptions): Record<string, RuleDeclaration> => {
let recommendedRules: Record<string, RuleDeclaration> = {
[sortImportsName]: [
'error',
{
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
customGroups: {
value: {},
type: {},
},
newlinesBetween: 'always',
internalPattern: ['~/**'],
},
],
[sortClassesName]: [
'error',
{
groups: [
'index-signature',
'static-property',
'private-property',
'property',
'constructor',
'static-method',
'private-method',
'method',
['get-method', 'set-method'],
'unknown',
],
},
],
[sortObjectsName]: [
'error',
{
partitionByComment: false,
},
],
[sortArrayIncludesName]: [
'error',
{
spreadLast: true,
},
],
[sortIntersectionTypesName]: ['error'],
[sortSvelteAttributesName]: ['error'],
[sortAstroAttributesName]: ['error'],
[sortVueAttributesName]: ['error'],
[sortNamedExportsName]: ['error'],
[sortNamedImportsName]: ['error'],
[sortObjectTypesName]: ['error'],
[sortUnionTypesName]: ['error'],
[sortInterfacesName]: ['error'],
[sortSwitchCaseName]: ['error'],
[sortJsxPropsName]: ['error'],
[sortExportsName]: ['error'],
[sortEnumsName]: ['error'],
[sortMapsName]: ['error'],
}
return Object.fromEntries(
Object.entries(formatRuleNames(recommendedRules)).map(([key, [message, baseOptions = {}]]) => [
key,
[message, Object.assign(baseOptions, options)],
]),
)
}
let createConfig = (options: BaseOptions): Linter.FlatConfig => ({
plugins: {
[name]: plugin,
},
rules: getRules(options),
})
let createLegacyConfig = (options: BaseOptions): Linter.Config => ({
rules: getRules(options),
plugins: [name],
})
export default {
...plugin,
configs: {
'recommended-alphabetical-legacy': createLegacyConfig({
type: 'alphabetical',
order: 'asc',
}),
'recommended-line-length-legacy': createLegacyConfig({
type: 'line-length',
order: 'desc',
}),
'recommended-natural-legacy': createLegacyConfig({
type: 'natural',
order: 'asc',
}),
'recommended-alphabetical': createConfig({
type: 'alphabetical',
order: 'asc',
}),
'recommended-line-length': createConfig({
type: 'line-length',
order: 'desc',
}),
'recommended-natural': createConfig({
type: 'natural',
order: 'asc',
}),
},
}