forked from i3roly/firefox-dynasty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.config.mjs
More file actions
499 lines (483 loc) · 14.9 KB
/
eslint.config.mjs
File metadata and controls
499 lines (483 loc) · 14.9 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import sdl from "@microsoft/eslint-plugin-sdl";
import eslintConfigPrettier from "eslint-config-prettier/flat";
import html from "eslint-plugin-html";
import importPlugin from "eslint-plugin-import";
import json from "@eslint/json";
import lit from "eslint-plugin-lit";
import mozilla from "eslint-plugin-mozilla";
import reactHooks from "eslint-plugin-react-hooks";
import fs from "fs";
import globals from "globals";
import path from "path";
import globalIgnores from "./eslint-ignores.config.mjs";
import testPathsConfig from "./eslint-test-paths.config.mjs";
import repositoryGlobals from "./eslint-file-globals.config.mjs";
import rollouts from "./eslint-rollouts.config.mjs";
import subdirConfigs from "./eslint-subdirs.config.mjs";
const testPaths = testPathsConfig.testPaths;
function readFile(filePath) {
return fs
.readFileSync(filePath, { encoding: "utf-8" })
.split("\n")
.filter(p => p && !p.startsWith("#"));
}
const httpTestingPaths = [
`**/*mixedcontent*.{${mozilla.allFileExtensions.join(",")}}`,
`**/*CrossOrigin*.{${mozilla.allFileExtensions.join(",")}}`,
`**/*crossorigin*.{${mozilla.allFileExtensions.join(",")}}`,
`**/*cors*.{${mozilla.allFileExtensions.join(",")}}`,
`**/*downgrade*.{${mozilla.allFileExtensions.join(",")}}`,
`**/*Downgrade*.{${mozilla.allFileExtensions.join(",")}}`,
];
/**
* Takes each path in the paths array, and expands it with the list of extensions
* that ESLint is watching.
*
* @param {object} options
* @param {string[]} options.paths
* The list of paths to wrap.
* @param {string[]} [options.excludedExtensions]
* The list of extensions to be excluded from the wrapping.
*/
function wrapPaths({ paths, excludedExtensions }) {
let extensions = excludedExtensions
? mozilla.allFileExtensions.filter(f => !excludedExtensions.includes(f))
: mozilla.allFileExtensions;
return paths.map(p => {
if (p.endsWith("**")) {
return p + `/*.{${extensions.join(",")}}`;
}
if (p.endsWith("/")) {
return p + `**/*.{${extensions.join(",")}}`;
}
if (p.endsWith("*")) {
return p + `.{${extensions.join(",")}}`;
}
return p;
});
}
/**
* Wraps the paths listed in the files section of a configuration with the
* file extensions that ESLint is watching.
*
* @param {object} configs
*/
function wrapPathsInConfig(configs) {
for (let config of configs) {
config.files = wrapPaths({ paths: config.files });
}
return configs;
}
let config = [
{
name: "import-plugin-settings",
settings: {
"import/extensions": [".mjs"],
"import/resolver": {
[path.resolve(import.meta.dirname, "srcdir-resolver.js")]: {},
node: {},
},
},
},
{
name: "ignores",
ignores: [
...globalIgnores,
...readFile(
path.join(
import.meta.dirname,
"tools",
"rewriting",
"ThirdPartyPaths.txt"
)
),
...readFile(
path.join(import.meta.dirname, "tools", "rewriting", "Generated.txt")
),
...readFile(
path.join(
import.meta.dirname,
"devtools",
"client",
"debugger",
"src",
".eslintignore"
)
).map(p => `devtools/client/debugger/src/${p}`),
],
},
{
name: "all-files",
files: wrapPaths({ paths: ["**"] }),
linterOptions: {
// With this option on, if an inline comment disables a rule, and the
// rule is able to be automatically fixed, then ESLint will remove the
// inline comment and apply the fix. We don't want this because we have
// some rules that intentionally need to be turned off in specific cases,
// e.g. @microsoft/sdl/no-insecure-url.
reportUnusedDisableDirectives: "off",
},
plugins: { lit },
rules: {
"lit/quoted-expressions": ["error", "never"],
"lit/no-invalid-html": "error",
"lit/no-value-attribute": "error",
},
},
{
name: "source-type-script",
files: ["**/*.{js,json,html,sjs,xhtml}"],
languageOptions: {
sourceType: "script",
},
},
...mozilla.configs["flat/recommended"],
{
name: "json-recommended-with-comments",
files: ["**/*.json"],
language: "json/jsonc",
...json.configs.recommended,
},
{
name: "json-recommended-no-comments",
files: ["**/package.json"],
language: "json/json",
...json.configs.recommended,
},
{
name: "json-empty-keys-off-for-image_builder",
files: ["taskcluster/docker/image_builder/policy.json"],
rules: {
"json/no-empty-keys": "off",
},
},
{
name: "eslint-plugin-html",
files: ["**/*.html", "**/*.xhtml"],
plugins: { html },
},
{
name: "define-globals-for-browser-env",
// Not available for sjs files.
files: wrapPaths({ paths: ["**"], excludedExtensions: ["sjs"] }),
ignores: [
// Also not available for various other scopes and tools.
"**/*.sys.mjs",
"**/?(*.)worker.?(m)js",
"**/?(*.)serviceworker.?(m)js",
...wrapPaths({
paths: testPaths.xpcshell,
excludedExtensions: ["mjs", "sjs"],
}),
"tools/lint/eslint/**",
],
languageOptions: {
globals: globals.browser,
},
},
{
// Generally we assume that all files, except mjs ones are in our
// privileged and specific environment. mjs are handled separately by
// the recommended configuration in eslint-plugin-mozilla.
name: "define-privileged-and-specific-globals-for-most-files",
files: wrapPaths({ paths: ["**"], excludedExtensions: ["json"] }),
ignores: ["browser/components/storybook/**", "tools"],
languageOptions: {
globals: {
...mozilla.environments.privileged.globals,
...mozilla.environments.specific.globals,
},
},
},
{
name: "define-globals-for-node-files",
files: [
// All .eslintrc.mjs files are in the node environment, so turn that
// on here.
"**/.eslintrc*.mjs",
// .js files in the top-level are generally assumed to be node.
"\.*.js",
// *.config.js files are generally assumed to be configuration files
// based for node.
"**/*.config.js",
// The resolver for moz-src for eslint, vscode etc.
"srcdir-resolver.js",
],
languageOptions: {
globals: { ...globals.node, ...mozilla.turnOff(globals.browser) },
},
},
{
name: "browser-no-more-globals",
files: ["browser/base/content/browser.js"],
rules: {
"mozilla/no-more-globals": "error",
},
},
{
name: "jsx-files",
files: ["**/*.jsx", "browser/components/storybook/.storybook/**/*.mjs"],
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
},
{
name: "eslint-plugin-import-rules",
files: ["**/*.mjs"],
plugins: { import: importPlugin },
rules: {
"import/default": "error",
"import/export": "error",
"import/named": "error",
"import/namespace": "error",
"import/newline-after-import": "error",
"import/no-duplicates": "error",
"import/no-absolute-path": "error",
"import/no-named-default": "error",
"import/no-named-as-default": "error",
"import/no-named-as-default-member": "error",
"import/no-self-import": "error",
"import/no-unassigned-import": "error",
"import/no-unresolved": [
"error",
// Bug 1773473 - Ignore resolver URLs for chrome and resource as we
// do not yet have a resolver for them.
{ ignore: ["chrome://", "resource://"] },
],
"import/no-useless-path-segments": "error",
},
},
{
name: "turn-off-unassigned-import-for-stories",
// Turn off no-unassigned-import for files that typically test our
// custom elements, which are imported for the side effects (ie
// the custom element being registered) rather than any particular
// export:
files: ["**/*.stories.mjs"],
plugins: { import: importPlugin },
rules: {
"import/no-unassigned-import": "off",
},
},
{
...mozilla.configs["flat/general-test"],
files: wrapPaths({ paths: ["**/test/**", "**/tests/**"] }),
},
{
...mozilla.configs["flat/xpcshell-test"],
files: wrapPaths({
paths: testPaths.xpcshell,
excludedExtensions: ["mjs", "sjs"],
}),
},
{
name: "no-unused-vars-disable-on-headjs",
// If it is an xpcshell head file, we turn off global unused variable checks, as it
// would require searching the other test files to know if they are used or not.
// This would be expensive and slow, and it isn't worth it for head files.
// We could get developers to declare as exported, but that doesn't seem worth it.
files: testPaths.xpcshell.map(filePath => `${filePath}head*.js`),
rules: {
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
caughtErrors: "none",
vars: "local",
},
],
},
},
{
name: "no-unused-vars-for-xpcshell",
// This section enables errors of no-unused-vars globally for all test*.js
// files in xpcshell test paths.
// This is not done in the xpcshell-test configuration as we cannot pull
// in overrides from there. We should at some stage, aim to enable this
// for all files in xpcshell-tests.
files: testPaths.xpcshell.map(filePath => `${filePath}test*.js`),
rules: {
// No declaring variables that are never used
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
caughtErrors: "none",
vars: "all",
},
],
},
},
{
...mozilla.configs["flat/browser-test"],
files: wrapPaths({
paths: testPaths.browser,
excludedExtensions: ["mjs", "sjs"],
}),
},
{
...mozilla.configs["flat/mochitest-test"],
files: wrapPaths({
paths: testPaths.mochitest,
excludedExtensions: ["mjs"],
}),
ignores: ["security/manager/ssl/tests/mochitest/browser/**"],
},
{
...mozilla.configs["flat/chrome-test"],
files: wrapPaths({
paths: testPaths.chrome,
excludedExtensions: ["mjs", "sjs"],
}),
},
{
name: "simpletest",
languageOptions: {
globals: {
...mozilla.environments.simpletest.globals,
},
},
files: [
...testPaths.mochitest.map(filePath => `${filePath}/**/*.js`),
...testPaths.chrome.map(filePath => `${filePath}/**/*.js`),
],
},
{
name: "multiple-test-kinds",
// Some directories have multiple kinds of tests, and some rules
// don't work well for HTML-based mochitests, so disable those.
files: testPaths.xpcshell
.concat(testPaths.browser)
.map(filePath => [`${filePath}/**/*.html`, `${filePath}/**/*.xhtml`])
.flat(),
rules: {
// plain/chrome mochitests don't automatically include Assert, so
// autofixing `ok()` to Assert.something is bad.
"mozilla/no-comparison-or-assignment-inside-ok": "off",
},
},
{
name: "test-file-reuse",
// Some directories reuse `test_foo.js` files between mochitest-plain and
// unit tests, or use custom postMessage-based assertion propagation into
// browser tests. Ignore those too:
files: wrapPaths({
paths: [
// Reuses xpcshell unit test scripts in mochitest-plain HTML files.
"dom/indexedDB/test/**",
// Dispatches functions to the webpage in ways that are hard to detect.
"toolkit/components/antitracking/test/**",
],
}),
rules: {
"mozilla/no-comparison-or-assignment-inside-ok": "off",
},
},
{
// Rules of Hooks broadly checks for camelCase "use" identifiers, so
// enable only for paths actually using React to avoid false positives.
name: "react-hooks",
files: [
"browser/components/aboutwelcome/**",
"browser/components/asrouter/**",
"browser/extensions/newtab/**",
"devtools/**",
],
...reactHooks.configs["recommended-latest"],
plugins: { "react-hooks": reactHooks },
rules: {
// react-hooks/recommended has exhaustive-deps as a warning, we prefer
// errors, so that raised issues get addressed one way or the other.
"react-hooks/exhaustive-deps": "error",
},
},
{
name: "disable-no-insecure-url-for-http-testing",
// Exempt files with these paths since they have to use http for full coverage
files: httpTestingPaths,
plugins: { "@microsoft/sdl": sdl },
rules: {
"@microsoft/sdl/no-insecure-url": "off",
},
},
{
name: "mozilla/valid-jsdoc",
files: wrapPaths({ paths: ["**"] }),
...mozilla.configs["flat/valid-jsdoc"],
},
{
name: "mozilla/require-jsdoc",
files: wrapPaths({ paths: ["**"] }),
...mozilla.configs["flat/require-jsdoc"],
},
{
name: "rollout-no-browser-refs-in-toolkit",
files: ["toolkit/**"],
ignores: ["toolkit/**/test/**", "toolkit/**/tests/**"],
plugins: { mozilla },
rules: {
"mozilla/no-browser-refs-in-toolkit": "error",
},
},
{
name: "no-newtab-refs-outside-newtab",
files: ["**/*.mjs", "**/*.js", "**/*.sys.mjs"],
ignores: [
"tools/@types/generated/**",
"browser/base/content/test/static/browser_all_files_referenced.js",
"tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-newtab-refs-outside-newtab.mjs",
"tools/lint/eslint/eslint-plugin-mozilla/tests/no-newtab-refs-outside-newtab.mjs",
],
plugins: { mozilla },
rules: {
"mozilla/no-newtab-refs-outside-newtab": "error",
},
},
...wrapPathsInConfig(subdirConfigs),
...wrapPathsInConfig(repositoryGlobals),
/**
* The items below should always be the last items in this order:
*
* - Enable eslint-config-prettier.
* - Enable curly.
* - Rollouts
*/
// Turn off rules that conflict with Prettier.
{ name: "eslint-config-prettier", ...eslintConfigPrettier },
{
name: "enable-curly",
files: wrapPaths({ paths: ["**/"] }),
rules: {
// Require braces around blocks that start a new line. This must be
// configured after eslint-config-prettier is included, as otherwise
// eslint-config-prettier disables the curly rule. Hence, we do
// not include it in
// `tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js`.
curly: ["error", "all"],
},
},
...wrapPathsInConfig(rollouts),
];
// The various places we get our globals from use true/false rather than
// the strings required by ESLint, so translate those here.
config.map(entry => {
if (entry.languageOptions?.globals) {
let newGlobals = {};
for (let [key, value] of Object.entries(entry.languageOptions.globals)) {
if (typeof entry.languageOptions.globals[key] == "boolean") {
newGlobals[key] = value ? "writable" : "readonly";
} else {
newGlobals[key] = value;
}
}
}
return entry;
});
export default config;