forked from vltpkg/vltpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint.config.mjs
269 lines (267 loc) · 8.5 KB
/
eslint.config.mjs
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import globals from 'globals'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import jsdoc from 'eslint-plugin-jsdoc'
import importPlugin from 'eslint-plugin-import'
// 'error' to fix, or 'warn' to see
const BE_EXTRA = process.env.LINT_SUPER_CONSISTENT ?? 'off'
export default tseslint.config(
{
ignores: [
...readFileSync(resolve(import.meta.dirname, '.prettierignore'))
.toString()
.trim()
.split('\n')
.filter(Boolean)
.map(v => v.replace(/^(!?)\//, '$1')),
],
},
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
jsdoc.configs['flat/recommended-error'],
importPlugin.flatConfigs.recommended,
importPlugin.flatConfigs.typescript,
{
plugins: { jsdoc },
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
globals: {
...globals.node,
},
},
settings: {
'import/resolver': {
typescript: true,
node: true,
},
},
rules: {
/**
* All the following rules are changed from the defaults set by the eslint and tseslint
* installed configs. The comments above each one are the reason the default has been changed.
*/
// allow empty catch blocks
'no-empty': ['error', { allowEmptyCatch: true }],
// dont force it when destructuring some mutable vars
'prefer-const': [
'error',
{
destructuring: 'all',
},
],
// emulate TypeScript behavior of allowing unused prefixed with _
'@typescript-eslint/no-unused-vars': [
'error',
{
args: 'all',
argsIgnorePattern: '^_',
caughtErrors: 'all',
caughtErrorsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
varsIgnorePattern: '^_',
ignoreRestSiblings: true,
},
],
// allow void arrow functions to not need to be wrapped in braces
'@typescript-eslint/no-confusing-void-expression': [
'off',
{
ignoreArrowShorthand: true,
},
],
// while (true) is ok
'@typescript-eslint/no-unnecessary-condition': [
'error',
{
allowConstantLoopConditions: true,
},
],
// using both overload signatures and union types
'@typescript-eslint/unified-signatures': 'off',
// async functions that dont use await to signal its a Promise
'@typescript-eslint/require-await': 'off',
// objects in template expressions and have the default toString method called
'@typescript-eslint/restrict-template-expressions': 'off',
// its ok to delete properties. could be a source of slowness though
'@typescript-eslint/no-dynamic-delete': 'off',
// empty arrow functions are sometimes necessary
'@typescript-eslint/no-empty-function': 'off',
// prefer ?? over ||, except when using primitive values
'@typescript-eslint/prefer-nullish-coalescing': [
'error',
{
ignorePrimitives: true,
},
],
// this rule doesn't catch anything except useful patterns we might need
'@typescript-eslint/no-this-alias': 'off',
// allow us to use ts-expect-error directives without descriptions
'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-expect-error': false,
},
],
// no enums because they mix types/values
'no-restricted-syntax': [
'error',
{
selector: 'TSEnumDeclaration',
message: "Don't declare enums",
},
],
/**
* These rules should be turned on at some point in the future but are too much work currently.
*/
// TODO: doesn't play well with how we pass instance methods to error() to capture stack traces
'@typescript-eslint/unbound-method': 'off',
// TODO: there are good reasons to use any but this is helpful to turn on
// occassionally to see if there are some where unknown is better
'@typescript-eslint/no-explicit-any': 'off',
// TODO: turn this on
'@typescript-eslint/class-literal-property-style': 'off',
/**
* These rules were turned on originally for their autofixing capabilities and to start
* from a consistent baseline, but keeping them on creates too much friction day-to-day.
* They can be enabled temporarily to fix or warn on any questionable usage and then disabled.
*/
// prefer Record<string,string> over { [k: string]: string }
'@typescript-eslint/consistent-indexed-object-style': [
/**
* This is not worth the consistency so it is now always off. We use
* recursive types which break when autofixed to Record, and mapped
* types provide a name for the key which is nice in many cases.
*/ 0,
'record',
],
// prefer type over interface but force consistent use of one
'@typescript-eslint/consistent-type-definitions': [
BE_EXTRA,
'type',
],
// sort type intersections so named ones come before objects
'@typescript-eslint/sort-type-constituents': [
BE_EXTRA,
{
// unions don't pose the same readability issue and some cases can't be autofixed
checkUnions: false,
},
],
// use inline type imports
'@typescript-eslint/consistent-type-imports': [
2,
{
disallowTypeAnnotations: false,
fixStyle: 'inline-type-imports',
},
],
'import/no-duplicates': ['error', { 'prefer-inline': true }],
'import/consistent-type-specifier-style': [2, 'prefer-inline'],
// eslint-plugin-import
'import/no-named-as-default': 0,
'import/no-named-as-default-member': 0,
// eslint-plugin-jsdoc
'jsdoc/no-undefined-types': 2,
'jsdoc/require-param': 0,
'jsdoc/require-returns': 0,
'jsdoc/require-yields': 0,
'jsdoc/require-param-description': 0,
'jsdoc/require-returns-description': 0,
'jsdoc/require-jsdoc': 0,
},
},
{
/**
* Allow unsafe code here. Probably not worth fixing in tests
* but anywhere else is beneficial. Goal is to at least not
* have any src/ files in here.
*/
files: ['**/test/**/*.{ts,tsx}', '**/infra/**/*.ts'],
rules: {
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
},
},
{
/**
* Tests
*/
files: ['**/test/**/*.{ts,tsx}'],
rules: {
// tap handles these floating promises
'@typescript-eslint/no-floating-promises': [
'error',
{
allowForKnownSafeCalls: [
'test',
'rejects',
'resolveMatch',
'resolves',
'skip',
].map(name => ({
name,
from: 'package',
package: 'tap',
})),
},
],
// this is helpful and not really dangerous in tests
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/use-unknown-in-catch-callback-variable':
'off',
},
},
{
/**
* Front end code
*/
files: ['{src/gui,www/docs}/{src,test}/**/*.{tsx,ts}'],
rules: {
// TODO: get eslint import resolver working with workspace tsconfig paths
'import/no-unresolved': 'off',
// shadcn-ui specific patterns
'@typescript-eslint/no-empty-object-type': 'off',
},
},
{
/**
* Astro
*/
files: ['www/docs/src/env.d.ts'],
rules: {
'@typescript-eslint/triple-slash-reference': 'off',
},
},
{
files: ['src/*/src/**/*.ts'],
rules: {
'no-console': 2,
},
},
{
/**
* Plain JS code.
* TODO: there is a way to get typechecking with tseslint for
* JS code but I couldn't get it configured right. Might be worth
* looking in to for our JS scripts.
*/
files: ['**/*.{js,mjs}'],
...eslint.configs.recommended,
...tseslint.configs.disableTypeChecked,
rules: {
...tseslint.configs.disableTypeChecked.rules,
...eslint.configs.recommended.rules,
},
},
)