-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy patheslint.config.mjs
366 lines (325 loc) · 9.55 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import base, { createConfig } from '@metamask/eslint-config';
import browser from '@metamask/eslint-config-browser';
import jest from '@metamask/eslint-config-jest';
import nodejs from '@metamask/eslint-config-nodejs';
import typescript from '@metamask/eslint-config-typescript';
const config = createConfig([
{
ignores: [
'**/assembly',
'**/build',
'**/coverage',
'**/dist',
'**/docs',
'**/public',
'.yarn',
],
},
// Base configuration
{
extends: base,
languageOptions: {
sourceType: 'module',
parserOptions: {
tsconfigRootDir: import.meta.dirname,
project: ['./tsconfig.packages.json'],
},
},
settings: {
'import-x/extensions': ['.js', '.mjs', '.wasm'],
},
rules: {
// This allows `Promise.catch().finally()` to be used without a return
// statement.
// TODO: Upstream this change to `@metamask/eslint-config`.
'promise/catch-or-return': [
'error',
{
allowFinally: true,
},
],
// By default the `resolve` and `reject` parameters of a Promise are
// expected to be named `resolve` and `reject`. This rule allows the
// parameters to be named `resolveSomething` and `rejectSomething`.
// TODO: Upstream this change to `@metamask/eslint-config`.
'promise/param-names': [
'error',
{
resolvePattern: '^_?resolve',
rejectPattern: '^_?reject',
},
],
},
},
// TypeScript source files
{
files: ['**/*.ts', '**/*.mts', '**/*.tsx'],
extends: typescript,
rules: {
// This prevents using the `console.log` and similar functions. All logging
// should be done through the module logger, or `logError` function in
// `@metamask/snaps-utils`.
'no-console': 'error',
// This allows `@property` despite being "redundant" in a type system.
// We use it to document the properties of an object that are not declared
// directly in the type.
// TODO: Upstream this change to `@metamask/eslint-config`.
'jsdoc/check-tag-names': [
'error',
{
definedTags: ['property'],
},
],
// This is too strict for some cases, like when a Promise is used to
// perform a side effect.
// TODO: Upstream this change to `@metamask/eslint-config`.
'promise/always-return': 'off',
// TODO: Consider enabling this rule.
'@typescript-eslint/explicit-function-return-type': 'off',
// This allows importing the `Text` JSX component.
'@typescript-eslint/no-shadow': [
'error',
{
allow: ['Text'],
},
],
// When enabled, this rule disallows comparing strings to enums. This is
// too strict for some cases.
// TODO: Upstream this change to `@metamask/eslint-config-typescript`.
'@typescript-eslint/no-unsafe-enum-comparison': 'off',
// Without the `allowAny` option, this rule causes a lot of false
// positives.
'@typescript-eslint/restrict-template-expressions': [
'error',
{
allowAny: true,
allowBoolean: true,
allowNumber: true,
},
],
// Copied from `@metamask/eslint-config-typescript`, but modified to allow
// more flexibility in imports.
// TODO: Upstream this change to `@metamask/eslint-config-typescript`.
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'default',
format: ['camelCase'],
leadingUnderscore: 'allow',
trailingUnderscore: 'forbid',
},
{
selector: 'enumMember',
format: ['PascalCase'],
},
{
selector: 'import',
format: ['camelCase', 'PascalCase', 'snake_case', 'UPPER_CASE'],
},
{
selector: 'interface',
format: ['PascalCase'],
custom: {
regex: '^I[A-Z]',
match: false,
},
},
{
selector: 'objectLiteralMethod',
format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
},
{
selector: 'objectLiteralProperty',
format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
},
{
selector: 'typeLike',
format: ['PascalCase'],
},
{
selector: 'typeParameter',
format: ['PascalCase'],
custom: {
regex: '^.{3,}',
match: true,
},
},
{
selector: 'variable',
format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
leadingUnderscore: 'allow',
},
{
selector: 'parameter',
format: ['camelCase', 'PascalCase'],
leadingUnderscore: 'allow',
},
{
selector: [
'classProperty',
'objectLiteralProperty',
'typeProperty',
'classMethod',
'objectLiteralMethod',
'typeMethod',
'accessor',
'enumMember',
],
format: null,
modifiers: ['requiresQuotes'],
},
],
// Allow `Promise.reject` to be called with any value.
// TODO: Upstream this change to `@metamask/eslint-config-typescript`.
'@typescript-eslint/prefer-promise-reject-errors': 'off',
// Copied from `@metamask/eslint-config-typescript`, but modified to
// consider `default` as exhaustive in switch statements.
// TODO: Upstream this change to `@metamask/eslint-config-typescript`.
'@typescript-eslint/switch-exhaustiveness-check': [
'error',
{
considerDefaultExhaustiveForUnions: true,
},
],
},
},
// Node.js + TypeScript scripts
{
files: [
'**/scripts/**/*.ts',
'**/scripts/**/*.mts',
'packages/snaps-execution-environments/scripts/**/*.ts',
],
extends: nodejs,
languageOptions: {
sourceType: 'module',
parserOptions: {
tsconfigRootDir: import.meta.dirname,
project: ['./tsconfig.json'],
},
},
rules: {
'n/hashbang': 'off',
},
},
// CommonJS Node.js scripts
{
files: ['**/*.js', '**/*.cjs'],
extends: nodejs,
languageOptions: {
sourceType: 'script',
},
rules: {
'n/hashbang': 'off',
},
},
// ESM Node.js scripts
{
files: ['**/*.mjs'],
extends: nodejs,
languageOptions: {
sourceType: 'module',
},
rules: {
'n/hashbang': 'off',
},
},
// Test files
{
files: [
'**/*.test.ts',
'**/*.test.tsx',
'**/*.test.browser.ts',
'**/*.test.js',
],
extends: [jest, nodejs],
rules: {
// This rule is too strict for test files.
// TODO: Upstream this change to `@metamask/eslint-config-jest`.
'@typescript-eslint/unbound-method': 'off',
'jest/expect-expect': [
'error',
{
assertFunctionNames: ['expect', 'expectSaga', 'expectTypeOf'],
},
],
// This rule is too strict for test files.
// TODO: Upstream this change to `@metamask/eslint-config-jest`.
'jest/no-conditional-in-test': 'off',
// This rule is too strict for test files.
'n/no-unsupported-features/node-builtins': 'off',
// This rule is too strict for test files.
'no-console': 'off',
},
},
// Files that contain Node.js functionality
{
files: [
'packages/create-snap/src/**/*',
'packages/snaps-browserify-plugin/src/**/*',
'packages/snaps-cli/src/**/*',
'packages/snaps-controllers/src/services/node-js/**/*',
'packages/snaps-jest/src/**/*',
'packages/snaps-rollup-plugin/src/**/*',
'packages/snaps-simulation/src/**/*',
'packages/snaps-utils/src/**/*',
'packages/snaps-webpack-plugin/src/**/*',
'packages/test-snaps/src/**/*',
'**/scripts/**/*.ts',
'**/test-utils/**/*.ts',
'**/webpack.config.ts',
'**/snap.config.ts',
],
extends: nodejs,
rules: {
// These rules are too strict for some cases.
// TODO: Re-investigate these rules.
'n/callback-return': 'off',
'n/hashbang': 'off',
'n/no-unsupported-features/node-builtins': 'off',
'n/no-process-env': 'off',
'n/no-process-exit': 'off',
'n/no-sync': 'off',
'no-restricted-globals': 'off',
},
},
// Files that contain browser functionality
{
files: [
'packages/snaps-controllers/src/services/iframe/**/*',
'packages/snaps-controllers/src/services/webworker/**/*',
'packages/snaps-execution-environments/src/**/*',
'packages/snaps-simulator/src/**/*',
'packages/snaps-simulator/jest.setup.js',
'packages/test-snaps/src/**/*',
'**/*.test.browser.ts',
],
extends: [browser],
},
// Entrypoint files for legacy build tools
{
files: [
'packages/snaps-controllers/react-native.d.ts',
'packages/snaps-controllers/react-native.js',
'packages/snaps-sdk/jsx-dev-runtime.d.ts',
'packages/snaps-sdk/jsx-dev-runtime.js',
'packages/snaps-sdk/jsx-runtime.d.ts',
'packages/snaps-sdk/jsx-runtime.js',
'packages/snaps-sdk/jsx.d.ts',
'packages/snaps-sdk/jsx.js',
],
rules: {
'import-x/extensions': 'off',
'import-x/no-unresolved': 'off',
},
},
// Wasm example
{
files: ['packages/examples/packages/wasm/src/index.ts'],
rules: {
// This rule changes depending on whether the build files exist or not.
'import-x/extensions': 'off',
},
},
]);
export default config;