-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy patheslint.config.mjs
More file actions
348 lines (337 loc) · 12.2 KB
/
eslint.config.mjs
File metadata and controls
348 lines (337 loc) · 12.2 KB
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
import base, { createConfig } from '@metamask/eslint-config';
import jest from '@metamask/eslint-config-jest';
import nodejs from '@metamask/eslint-config-nodejs';
import typescript from '@metamask/eslint-config-typescript';
const NODE_LTS_VERSION = 22;
/**
* Arguments to the `no-restricted` syntax rule that advises use of
* `${Controller}:stateChanged` instead of `:stateChange`.
*/
const NO_CONTROLLER_STATE_CHANGE_SELECTOR_OBJECTS = [
{
selector:
'CallExpression[callee.property.name="subscribe"] > Literal[value=/^.+:stateChange$/]',
message:
"Subscribing to ':stateChange' events is deprecated. Use ':stateChanged' instead.",
},
{
selector:
'CallExpression[callee.property.name="delegate"] Property[key.name="events"] ArrayExpression > Literal[value=/^.+:stateChange$/]',
message:
"Delegating ':stateChange' events is deprecated. Use ':stateChanged' instead.",
},
];
/**
* Arguments to the `no-restricted-syntax` rule that prevents messsenger actions
* from being called in constructors.
*/
const NO_MESSENGER_ACTIONS_IN_CONSTRUCTORS_SELECTOR_OBJECTS = [
{
selector:
'MethodDefinition[kind="constructor"] CallExpression[callee.type="MemberExpression"][callee.property.name="call"][callee.object.type="MemberExpression"][callee.object.object.type="ThisExpression"][callee.object.property.name="messenger"]',
message:
'Do not call messenger actions in the constructor, as this forces clients to instantiate controllers or services in a specific order. Move this call to an init() method instead. Read the controller guidelines for more: https://github.com/MetaMask/core/blob/main/docs/code-guidelines/controller-guidelines.md#do-not-call-messenger-actions-in-the-constructor',
},
{
selector:
'MethodDefinition[kind="constructor"] CallExpression[callee.type="MemberExpression"][callee.property.name="call"][callee.object.type="Identifier"][callee.object.name="messenger"]',
message:
'Do not call messenger actions in the constructor, as this forces clients to instantiate controllers or services in a specific order. Move this call to an init() method instead. Read the controller guidelines for more: https://github.com/MetaMask/core/blob/main/docs/code-guidelines/controller-guidelines.md#do-not-call-messenger-actions-in-the-constructor',
},
];
/**
* Collects all options for a given array-valued rule across one or more flat
* config arrays, excluding the leading severity element.
*
* ESLint flat config does not merge array-valued rules across config objects —
* a later config silently replaces earlier ones. This helper makes it possible
* to extend an upstream rule configuration rather than copy-pasting its options.
*
* @param {string} ruleName - The rule to collect options for.
* @param {import('eslint').Linter.Config[][]} configs - Flat config arrays to
* collect options from.
* @returns {unknown[]} The options from all matching rule entries, with the
* leading severity element omitted.
*/
function collectExistingRuleOptions(ruleName, configs) {
return configs.flat().flatMap((config) => {
const rule = config.rules?.[ruleName];
if (!Array.isArray(rule)) {
return [];
}
// Rule entries are ['error' | 'warn' | number, ...options].
// Skip the first element (severity) and collect the rest.
return rule.slice(1);
});
}
const config = createConfig([
...base,
{
ignores: [
'**/dist/**',
'**/docs/**',
'**/coverage/**',
'merged-packages/**',
'.yarn/**',
'scripts/create-package/package-template/**',
],
},
{
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
},
{
rules: {
// Handled by Oxfmt.
'prettier/prettier': 'off',
'import-x/order': 'off',
},
},
{
rules: {
// TODO: Re-enable this rule
// Enabling it with error suppression breaks `--fix`, because the autofixer for this rule
// does not work very well.
'jsdoc/require-jsdoc': 'off',
},
settings: {
jsdoc: {
mode: 'typescript',
},
},
},
{
files: [
'**/*.{js,cjs,mjs}',
'**/*.test.{js,ts}',
'**/test/**/*.{js,ts}',
'**/tests/**/*.{js,ts}',
'scripts/*.ts',
'scripts/create-package/**/*.ts',
],
extends: [nodejs],
},
{
files: ['**/*.{js,cjs}'],
languageOptions: {
sourceType: 'script',
ecmaVersion: 2020,
},
},
{
files: ['**/*.ts'],
extends: [typescript],
languageOptions: {
parserOptions: {
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
// TODO: Disable in `eslint-config-typescript`, tracked here: https://github.com/MetaMask/eslint-config/issues/413
'@typescript-eslint/no-unnecessary-type-arguments': 'off',
// This rule does not detect multiple imports of the same file where types
// are being imported in one case and runtime values are being imported in
// another
'import-x/no-duplicates': 'off',
// Enable rules that are disabled in `@metamask/eslint-config-typescript`
'@typescript-eslint/no-explicit-any': 'error',
// TODO: auto-fix breaks stuff
'@typescript-eslint/promise-function-async': 'off',
// TODO: Re-enable these rules
// Enabling them with error suppression breaks `--fix`, because the autofixer for these rules
// do not work very well.
'jsdoc/check-tag-names': 'off',
'jsdoc/require-jsdoc': 'off',
// Add custom rule for deprecating `${Controller}:stateChange` in favor of
// `:stateChanged`.
'no-restricted-syntax': [
'error',
...collectExistingRuleOptions('no-restricted-syntax', [
base,
typescript,
]),
...NO_CONTROLLER_STATE_CHANGE_SELECTOR_OBJECTS,
],
},
},
{
files: ['**/*.test.{js,ts}', '**/tests/**/*.{js,ts}'],
extends: [jest],
rules: {
// TODO: Upgrade these from warning to error in shared config
'jest/expect-expect': 'error',
'jest/no-alias-methods': 'error',
'jest/no-commented-out-tests': 'error',
'jest/no-disabled-tests': 'error',
},
settings: {
node: {
version: `^${NODE_LTS_VERSION}`,
},
},
},
{
// These files are test helpers, not tests. We still use the Jest ESLint
// config here to ensure that ESLint expects a test-like environment, but
// various rules meant just to apply to tests have been disabled.
files: ['**/tests/**/*.{js,ts}'],
ignores: ['**/*.test.{js,ts}'],
rules: {
'jest/no-export': 'off',
'jest/require-top-level-describe': 'off',
'jest/no-if': 'off',
},
},
{
files: ['tests/setupAfterEnv/matchers.ts'],
languageOptions: {
sourceType: 'script',
},
},
// This should really be in `@metamask/eslint-config-typescript`
{
files: ['**/*.d.ts'],
rules: {
'import-x/unambiguous': 'off',
},
},
{
files: ['scripts/*.ts'],
rules: {
// Scripts may be self-executable and thus have hashbangs.
'n/hashbang': 'off',
},
},
{
files: ['**/jest.environment.js'],
rules: {
// These files run under Node, and thus `require(...)` is expected.
'n/global-require': 'off',
},
},
{
files: ['**/*.mjs'],
languageOptions: {
sourceType: 'module',
},
},
// Prevent cross-package relative imports
{
files: ['packages/*/src/**/*.ts'],
ignores: ['**/*.test.ts', '**/tests/**/*.ts'],
rules: {
'import-x/no-relative-packages': 'error',
},
},
// Prevent calling messenger actions in controller/service constructors
{
files: ['packages/*/src/**/*.ts'],
ignores: ['**/*.test.ts', '**/tests/**/*.ts'],
rules: {
'no-restricted-syntax': [
'error',
...collectExistingRuleOptions('no-restricted-syntax', [
base,
typescript,
]),
...NO_CONTROLLER_STATE_CHANGE_SELECTOR_OBJECTS,
...NO_MESSENGER_ACTIONS_IN_CONSTRUCTORS_SELECTOR_OBJECTS,
],
},
},
{
// Prohibit exporting *AllowedActions, *AllowedEvents, and *MethodActions
// from package index files
files: ['packages/*/src/index.ts'],
rules: {
'no-restricted-syntax': [
'error',
...collectExistingRuleOptions('no-restricted-syntax', [
base,
typescript,
]),
...NO_CONTROLLER_STATE_CHANGE_SELECTOR_OBJECTS,
...NO_MESSENGER_ACTIONS_IN_CONSTRUCTORS_SELECTOR_OBJECTS,
{
selector:
'ExportNamedDeclaration > ExportSpecifier[local.name=/AllowedActions$/]',
message:
'Do not export AllowedActions types from package index files. These types describe external messenger dependencies and are obtainable from the packages that define them directly. Read the controller guidelines for more: https://github.com/MetaMask/core/blob/main/docs/code-guidelines/controller-guidelines.md#define-but-do-not-export-a-type-union-for-external-action-types',
},
{
selector:
'ExportNamedDeclaration > ExportSpecifier[local.name=/AllowedEvents$/]',
message:
'Do not export AllowedEvents types from package index files. These types describe external messenger dependencies and are obtainable from the packages that define them directly. Read the controller guidelines for more: https://github.com/MetaMask/core/blob/main/docs/code-guidelines/controller-guidelines.md#define-but-do-not-export-a-type-union-for-external-event-types',
},
{
selector:
'ExportNamedDeclaration > ExportSpecifier[local.name=/MethodActions$/]',
message:
'Do not export *MethodActions types from package index files. Internal messenger actions are already available via the *Actions type. Export the individual action types (along with *Actions) instead. Read the controller guidelines for more: https://github.com/MetaMask/core/blob/main/docs/code-guidelines/controller-guidelines.md#expose-controller-methods-through-messenger-in-bulk',
},
],
},
},
{
files: ['packages/foundryup/**/*.{js,ts}'],
rules: {
'import-x/no-nodejs-modules': 'off',
'n/no-unsupported-features/node-builtins': 'off',
'n/no-missing-import': 'off',
'n/no-restricted-import': 'off',
'n/no-deprecated-api': 'off',
},
},
{
files: ['packages/messenger/src/generate-action-types/**/*.{js,ts}'],
rules: {
'import-x/no-nodejs-modules': 'off',
},
},
{
files: ['packages/messenger-cli/src/**/*.{js,ts}'],
rules: {
'import-x/no-nodejs-modules': 'off',
},
},
{
files: [
'packages/notification-services-controller/src/NotificationServicesPushController/services/push/*-web.ts',
'packages/notification-services-controller/src/NotificationServicesPushController/web/**/*.ts',
],
rules: {
// These files use `self` because they're written for a service worker context.
// TODO: Move these files to the extension repository, `core` is just for platform-agnostic code.
'consistent-this': 'off',
},
},
{
files: [
'packages/assets-controllers/src/NftDetectionController.ts',
'packages/assets-controllers/src/TokenRatesController.ts',
'packages/assets-controllers/src/TokensController.ts',
'packages/controller-utils/src/siwe.ts',
'packages/ens-controller/src/EnsController.ts',
'packages/gas-fee-controller/src/GasFeeController.ts',
'packages/logging-controller/src/LoggingController.ts',
'packages/message-manager/src/AbstractMessageManager.ts',
'packages/message-manager/src/DecryptMessageManager.ts',
'packages/message-manager/src/EncryptionPublicKeyManager.ts',
'packages/permission-log-controller/src/PermissionLogController.ts',
'packages/phishing-controller/src/PhishingController.ts',
'packages/rate-limit-controller/src/RateLimitController.ts',
'tests/fake-provider.ts',
'tests/mock-network.ts',
],
rules: {
// TODO: Re-enable this rule
// This has been temporarily disabled because the auto-fix mangles pre-existing JSDoc blocks
// for types that don't follow TSDoc properly.
// See https://github.com/gajus/eslint-plugin-jsdoc/issues/1054
'jsdoc/check-tag-names': 'off',
},
},
]);
export default config;