-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathutilities.ts
182 lines (155 loc) · 5.32 KB
/
utilities.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
177
178
179
180
181
182
import process from 'node:process'
import * as tseslint from 'typescript-eslint'
import { TsEslintConfigForVue } from './configs'
import groupVueFiles from './groupVueFiles'
import {
additionalRulesRequiringParserServices,
createBasicSetupConfigs,
createSkipTypeCheckingConfigs,
createTypeCheckingConfigs,
} from './internals'
import type { ScriptLang } from './internals'
type ConfigArray = ReturnType<typeof tseslint.config>
type Rules = NonNullable<ConfigArray[number]['rules']>
type ConfigObjectOrPlaceholder = ConfigArray[number] | TsEslintConfigForVue
type InfiniteDepthConfigWithVueSupport =
| ConfigObjectOrPlaceholder
| InfiniteDepthConfigWithVueSupport[]
export type ProjectOptions = {
scriptLangs?: ScriptLang[]
rootDir?: string
}
let projectOptions: ProjectOptions = {
scriptLangs: ['ts'],
rootDir: process.cwd(),
}
// This function, if called, is guaranteed to be executed before `defineConfigWithVueTs`,
// so mutating the `projectOptions` object is safe and will be reflected in the final ESLint config.
export function configureVueProject(userOptions: ProjectOptions): void {
if (userOptions.scriptLangs) {
projectOptions.scriptLangs = userOptions.scriptLangs
}
if (userOptions.rootDir) {
projectOptions.rootDir = userOptions.rootDir
}
}
export function defineConfigWithVueTs(
...configs: InfiniteDepthConfigWithVueSupport[]
): ConfigArray {
// @ts-ignore
const flattenedConfigs: ConfigObjectOrPlaceholder[] = configs.flat(Infinity)
const reorderedConfigs = insertAndReorderConfigs(flattenedConfigs)
const normalizedConfigs = reorderedConfigs.map(config =>
config instanceof TsEslintConfigForVue ? config.toConfigArray() : config,
)
return tseslint.config(...normalizedConfigs)
}
// This function reorders the config array to make sure it satisfies the following layout:
//
// [FIRST-EXTENDED-CONFIG]
// ...
// [LAST-EXTENDED-CONFIG]
//
// [BASIC SETUP]
// pluginVue.configs['flat/base'],
// '@vue/typescript/setup'
//
// [ALL-OTHER-TYPE-AWARE-RULES-CONFIGURED-BY-USERS]
//
// [ERROR PREVENTION & PERFORMANCE OPTIMIZATION]
// '@vue/typescript/skip-type-checking-for-js-files'
// '@vue/typescript/skip-type-checking-for-vue-files-without-ts'
//
// [ONLY REQUIRED WHEN ONE-OR-MORE TYPE-AWARE RULES ARE TURNED-ON]
// '@vue/typescript/default-project-service-for-ts-files'
// '@vue/typescript/default-project-service-for-vue-files'
// '@vue/typescript/type-aware-rules-in-conflit-with-vue'
type ExtractedConfig = {
files?: (string | string[])[]
rules: Rules
}
const userTypeAwareConfigs: ExtractedConfig[] = []
function insertAndReorderConfigs(
configs: ConfigObjectOrPlaceholder[],
): ConfigObjectOrPlaceholder[] {
const lastExtendedConfigIndex = configs.findLastIndex(
config => config instanceof TsEslintConfigForVue,
)
if (lastExtendedConfigIndex === -1) {
return configs
}
const vueFiles = groupVueFiles(projectOptions.rootDir!)
const configsWithoutTypeAwareRules = configs.map(extractTypeAwareRules)
const hasTypeAwareConfigs = configs.some(
config =>
config instanceof TsEslintConfigForVue && config.needsTypeChecking(),
)
const needsTypeAwareLinting =
hasTypeAwareConfigs || userTypeAwareConfigs.length > 0
return [
...configsWithoutTypeAwareRules.slice(0, lastExtendedConfigIndex + 1),
...createBasicSetupConfigs(projectOptions.scriptLangs!),
// user-turned-off type-aware rules must come after the last extended config
// in case some rules re-enabled by the extended config
// user-turned-on type-aware rules must come before skipping type-checking
// in case some rules targets those can't be type-checked files
// So we extract all type-aware rules by users and put them in the middle
...userTypeAwareConfigs,
...(needsTypeAwareLinting
? [
...createSkipTypeCheckingConfigs(vueFiles.nonTypeCheckable),
...createTypeCheckingConfigs(vueFiles.typeCheckable),
]
: []),
...configsWithoutTypeAwareRules.slice(lastExtendedConfigIndex + 1),
]
}
function extractTypeAwareRules(
config: ConfigObjectOrPlaceholder,
): ConfigObjectOrPlaceholder {
if (config instanceof TsEslintConfigForVue) {
return config
}
if (!config.rules) {
return config
}
const [typeAwareRuleEntries, otherRuleEntries] = partition(
Object.entries(config.rules),
([name]) => doesRuleRequireTypeInformation(name),
)
if (typeAwareRuleEntries.length > 0) {
userTypeAwareConfigs.push({
rules: Object.fromEntries(typeAwareRuleEntries),
...(config.files && { files: config.files }),
})
}
return {
...config,
rules: Object.fromEntries(otherRuleEntries),
}
}
const rulesRequiringTypeInformation = new Set(
Object.entries(tseslint.plugin.rules!)
// @ts-expect-error
.filter(([_name, def]) => def?.meta?.docs?.requiresTypeChecking)
.map(([name, _def]) => `@typescript-eslint/${name}`)
.concat(additionalRulesRequiringParserServices),
)
function doesRuleRequireTypeInformation(ruleName: string): boolean {
return rulesRequiringTypeInformation.has(ruleName)
}
function partition<T>(
array: T[],
predicate: (element: T) => boolean,
): [T[], T[]] {
const truthy: T[] = []
const falsy: T[] = []
for (const element of array) {
if (predicate(element)) {
truthy.push(element)
} else {
falsy.push(element)
}
}
return [truthy, falsy]
}