Bug 1771374 - Fix build and lint warnings. r=gfx-reviewers,aosmond
[gecko.git] / .eslintrc.js
bloba208cd4718e183af879ca506e2193a5bee76e892
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const xpcshellTestConfig = require("eslint-plugin-mozilla/lib/configs/xpcshell-test.js");
8 const browserTestConfig = require("eslint-plugin-mozilla/lib/configs/browser-test.js");
9 const mochitestTestConfig = require("eslint-plugin-mozilla/lib/configs/mochitest-test.js");
10 const chromeTestConfig = require("eslint-plugin-mozilla/lib/configs/chrome-test.js");
11 const fs = require("fs");
12 const path = require("path");
14 /**
15  * Some configurations have overrides, which can't be specified within overrides,
16  * so we need to remove them.
17  */
18 function removeOverrides(config) {
19   config = { ...config };
20   delete config.overrides;
21   return config;
24 const xpcshellTestPaths = [
25   "**/test*/unit*/**/",
26   "**/test*/*/unit*/",
27   "**/test*/xpcshell/**/",
30 const browserTestPaths = ["**/test*/**/browser*/"];
32 const mochitestTestPaths = [
33   // Note: we do not want to match testing/mochitest as that would apply
34   // too many globals for that directory.
35   "**/test/mochitest/",
36   "**/tests/mochitest/",
37   "**/test/mochitests/",
38   "testing/mochitest/tests/SimpleTest/",
39   "testing/mochitest/tests/Harness_sanity/",
42 const chromeTestPaths = ["**/test*/chrome/"];
44 const ignorePatterns = [
45   ...fs
46     .readFileSync(
47       path.join(__dirname, "tools", "rewriting", "ThirdPartyPaths.txt")
48     )
49     .toString("utf-8")
50     .split("\n"),
51   ...fs
52     .readFileSync(
53       path.join(
54         __dirname,
55         "devtools",
56         "client",
57         "debugger",
58         "src",
59         ".eslintignore"
60       )
61     )
62     .toString("utf-8")
63     .split("\n")
64     .filter(p => p && !p.startsWith("#"))
65     .map(p => `devtools/client/debugger/src/${p}`),
68 module.exports = {
69   parser: "@babel/eslint-parser",
70   parserOptions: {
71     sourceType: "script",
72     babelOptions: {
73       configFile: path.join(__dirname, ".babel-eslint.rc.js"),
74     },
75   },
76   settings: {
77     "import/extensions": [".mjs"],
78   },
79   ignorePatterns,
80   // Ignore eslint configurations in parent directories.
81   root: true,
82   // New rules and configurations should generally be added in
83   // tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js to
84   // allow external repositories that use the plugin to pick them up as well.
85   extends: ["plugin:mozilla/recommended"],
86   plugins: ["mozilla", "import"],
87   overrides: [
88     {
89       // All .eslintrc.js files are in the node environment, so turn that
90       // on here.
91       // https://github.com/eslint/eslint/issues/13008
92       files: [".eslintrc.js"],
93       env: {
94         node: true,
95         browser: false,
96       },
97     },
98     {
99       files: ["*.mjs"],
100       rules: {
101         "import/default": "error",
102         "import/export": "error",
103         "import/named": "error",
104         "import/namespace": "error",
105         "import/newline-after-import": "error",
106         "import/no-anonymous-default-export": "error",
107         "import/no-duplicates": "error",
108         "import/no-absolute-path": "error",
109         "import/no-named-default": "error",
110         "import/no-named-as-default": "error",
111         "import/no-named-as-default-member": "error",
112         "import/no-self-import": "error",
113         "import/no-unassigned-import": "error",
114         "import/no-unresolved": "error",
115         "import/no-useless-path-segments": "error",
116       },
117     },
118     {
119       files: [
120         // Bug 1773473 - Turn off no-unresolved for system mjs modules, as we
121         // do not yet have a resolver for resource:// uris.
122         "*.sys.mjs",
123         // Bug 1773475 - For now, turn off no-unresolved on some paths where we import
124         // from node_modules, as the ESLint setup only installs modules at the
125         // top-level.
126         "devtools/shared/compatibility/**",
127       ],
128       rules: {
129         "import/no-unresolved": "off",
130       },
131     },
132     {
133       files: [
134         "*.html",
135         "*.xhtml",
136         "*.xml",
137         "js/src/builtin/**/*.js",
138         "js/src/shell/**/*.js",
139       ],
140       rules: {
141         // Curly brackets are required for all the tree via recommended.js,
142         // however these files aren't auto-fixable at the moment.
143         curly: "off",
144       },
145     },
146     {
147       // TODO: Bug 1515949. Enable no-undef for gfx/
148       files: "gfx/layers/apz/test/mochitest/**",
149       rules: {
150         "no-undef": "off",
151       },
152     },
153     {
154       ...removeOverrides(xpcshellTestConfig),
155       files: xpcshellTestPaths.map(path => `${path}**`),
156       excludedFiles: "devtools/**",
157     },
158     {
159       // If it is an xpcshell head file, we turn off global unused variable checks, as it
160       // would require searching the other test files to know if they are used or not.
161       // This would be expensive and slow, and it isn't worth it for head files.
162       // We could get developers to declare as exported, but that doesn't seem worth it.
163       files: xpcshellTestPaths.map(path => `${path}head*.js`),
164       rules: {
165         "no-unused-vars": [
166           "error",
167           {
168             args: "none",
169             vars: "local",
170           },
171         ],
172       },
173     },
174     {
175       // This section enables warning of no-unused-vars globally for all test*.js
176       // files in xpcshell test paths.
177       // These are turned into errors with selected exclusions in the next
178       // section.
179       // Bug 1612907: This section should go away once the exclusions are removed
180       // from the following section.
181       files: xpcshellTestPaths.map(path => `${path}test*.js`),
182       rules: {
183         // No declaring variables that are never used
184         "no-unused-vars": [
185           "warn",
186           {
187             args: "none",
188             vars: "all",
189           },
190         ],
191       },
192     },
193     {
194       // This section makes global issues with no-unused-vars be reported as
195       // errors - except for the excluded lists which are being fixed in the
196       // dependencies of bug 1612907.
197       files: xpcshellTestPaths.map(path => `${path}test*.js`),
198       excludedFiles: [
199         // These are suitable as good first bugs, take one or two related lines
200         // per bug.
201         "caps/tests/unit/test_origin.js",
202         "extensions/permissions/**",
203         "image/test/unit/**",
204         "intl/uconv/tests/unit/test_bug340714.js",
205         "modules/libjar/test/unit/test_empty_jar_telemetry.js",
206         "modules/libjar/zipwriter/test/unit/test_alignment.js",
207         "modules/libjar/zipwriter/test/unit/test_bug419769_2.js",
208         "modules/libjar/zipwriter/test/unit/test_storedata.js",
209         "modules/libjar/zipwriter/test/unit/test_zippermissions.js",
210         "modules/libpref/test/unit/test_dirtyPrefs.js",
211         "toolkit/crashreporter/test/unit/test_crash_AsyncShutdown.js",
212         "toolkit/mozapps/update/tests/unit_aus_update/testConstants.js",
214         // These are more complicated bugs which may require some in-depth
215         // investigation or different solutions. They are also likely to be
216         // a reasonable size.
217         "browser/components/**",
218         "browser/modules/**",
219         "dom/**",
220         "netwerk/**",
221         "security/manager/ssl/tests/unit/**",
222         "testing/xpcshell/**",
223         "toolkit/components/**",
224         "toolkit/modules/**",
225       ],
226       rules: {
227         // No declaring variables that are never used
228         "no-unused-vars": [
229           "error",
230           {
231             args: "none",
232             vars: "all",
233           },
234         ],
235       },
236     },
237     {
238       ...browserTestConfig,
239       files: browserTestPaths.map(path => `${path}**`),
240     },
241     {
242       ...removeOverrides(mochitestTestConfig),
243       files: mochitestTestPaths.map(path => `${path}**`),
244       excludedFiles: ["security/manager/ssl/tests/mochitest/browser/**"],
245     },
246     {
247       ...removeOverrides(chromeTestConfig),
248       files: chromeTestPaths.map(path => `${path}**`),
249     },
250     {
251       env: {
252         // Ideally we wouldn't be using the simpletest env here, but our uses of
253         // js files mean we pick up everything from the global scope, which could
254         // be any one of a number of html files. So we just allow the basics...
255         "mozilla/simpletest": true,
256       },
257       files: [
258         ...mochitestTestPaths.map(path => `${path}/**/*.js`),
259         ...chromeTestPaths.map(path => `${path}/**/*.js`),
260       ],
261     },
262     {
263       files: [
264         "netwerk/cookie/test/browser/**",
265         "netwerk/test/browser/**",
266         "netwerk/test/mochitests/**",
267         "netwerk/test/unit*/**",
268       ],
269       rules: {
270         "mozilla/no-arbitrary-setTimeout": "off",
271         "mozilla/no-define-cc-etc": "off",
272         "consistent-return": "off",
273         "no-eval": "off",
274         "no-global-assign": "off",
275         "no-nested-ternary": "off",
276         "no-redeclare": "off",
277         "no-shadow": "off",
278         "no-throw-literal": "off",
279       },
280     },
281     {
282       files: ["layout/**"],
283       rules: {
284         "object-shorthand": "off",
285         "mozilla/avoid-removeChild": "off",
286         "mozilla/consistent-if-bracing": "off",
287         "mozilla/reject-importGlobalProperties": "off",
288         "mozilla/no-arbitrary-setTimeout": "off",
289         "mozilla/no-define-cc-etc": "off",
290         "mozilla/use-chromeutils-generateqi": "off",
291         "mozilla/use-default-preference-values": "off",
292         "mozilla/use-includes-instead-of-indexOf": "off",
293         "mozilla/use-services": "off",
294         "mozilla/use-ownerGlobal": "off",
295         complexity: "off",
296         "consistent-return": "off",
297         "no-array-constructor": "off",
298         "no-caller": "off",
299         "no-cond-assign": "off",
300         "no-extra-boolean-cast": "off",
301         "no-eval": "off",
302         "no-func-assign": "off",
303         "no-global-assign": "off",
304         "no-implied-eval": "off",
305         "no-lonely-if": "off",
306         "no-nested-ternary": "off",
307         "no-new-wrappers": "off",
308         "no-redeclare": "off",
309         "no-restricted-globals": "off",
310         "no-return-await": "off",
311         "no-sequences": "off",
312         "no-throw-literal": "off",
313         "no-useless-concat": "off",
314         "no-undef": "off",
315         "no-unreachable": "off",
316         "no-unsanitized/method": "off",
317         "no-unsanitized/property": "off",
318         "no-unsafe-negation": "off",
319         "no-unused-vars": "off",
320         "no-useless-return": "off",
321       },
322     },
323     {
324       files: [
325         "dom/animation/**",
326         "dom/base/test/*.*",
327         "dom/base/test/unit/test_serializers_entities*.js",
328         "dom/base/test/unit_ipc/**",
329         "dom/base/test/jsmodules/**",
330         "dom/base/*.*",
331         "dom/canvas/**",
332         "dom/encoding/**",
333         "dom/events/**",
334         "dom/fetch/**",
335         "dom/file/**",
336         "dom/html/**",
337         "dom/jsurl/**",
338         "dom/media/tests/**",
339         "dom/media/webaudio/**",
340         "dom/media/webrtc/tests/**",
341         "dom/media/webspeech/**",
342         "dom/messagechannel/**",
343         "dom/midi/**",
344         "dom/network/**",
345         "dom/payments/**",
346         "dom/performance/**",
347         "dom/permission/**",
348         "dom/quota/test/browser/**",
349         "dom/quota/test/common/**",
350         "dom/quota/test/mochitest/**",
351         "dom/quota/test/xpcshell/**",
352         "dom/security/test/cors/**",
353         "dom/security/test/csp/**",
354         "dom/security/test/mixedcontentblocker/**",
355         "dom/serviceworkers/**",
356         "dom/smil/**",
357         "dom/tests/mochitest/**",
358         "dom/u2f/**",
359         "dom/vr/**",
360         "dom/webauthn/**",
361         "dom/webgpu/**",
362         "dom/websocket/**",
363         "dom/workers/**",
364         "dom/worklet/**",
365         "dom/xml/**",
366         "dom/xslt/**",
367         "dom/xul/**",
368         "dom/ipc/test.xhtml",
369       ],
370       rules: {
371         "consistent-return": "off",
372         "mozilla/avoid-removeChild": "off",
373         "mozilla/consistent-if-bracing": "off",
374         "mozilla/no-arbitrary-setTimeout": "off",
375         "mozilla/no-compare-against-boolean-literals": "off",
376         "mozilla/no-define-cc-etc": "off",
377         "mozilla/reject-importGlobalProperties": "off",
378         "mozilla/use-cc-etc": "off",
379         "mozilla/use-chromeutils-generateqi": "off",
380         "mozilla/use-includes-instead-of-indexOf": "off",
381         "mozilla/use-ownerGlobal": "off",
382         "mozilla/use-services": "off",
383         "no-array-constructor": "off",
384         "no-caller": "off",
385         "no-cond-assign": "off",
386         "no-control-regex": "off",
387         "no-debugger": "off",
388         "no-else-return": "off",
389         "no-empty": "off",
390         "no-eval": "off",
391         "no-func-assign": "off",
392         "no-global-assign": "off",
393         "no-implied-eval": "off",
394         "no-lone-blocks": "off",
395         "no-lonely-if": "off",
396         "no-nested-ternary": "off",
397         "no-new-object": "off",
398         "no-new-wrappers": "off",
399         "no-redeclare": "off",
400         "no-return-await": "off",
401         "no-restricted-globals": "off",
402         "no-self-assign": "off",
403         "no-self-compare": "off",
404         "no-sequences": "off",
405         "no-shadow": "off",
406         "no-shadow-restricted-names": "off",
407         "no-sparse-arrays": "off",
408         "no-throw-literal": "off",
409         "no-unreachable": "off",
410         "no-unsanitized/method": "off",
411         "no-unsanitized/property": "off",
412         "no-undef": "off",
413         "no-unused-vars": "off",
414         "no-useless-call": "off",
415         "no-useless-concat": "off",
416         "no-useless-return": "off",
417         "no-with": "off",
418       },
419     },
420     {
421       files: [
422         "testing/mochitest/browser-harness.xhtml",
423         "testing/mochitest/chrome/test_chromeGetTestFile.xhtml",
424         "testing/mochitest/chrome/test_sanityEventUtils.xhtml",
425         "testing/mochitest/chrome/test_sanityException.xhtml",
426         "testing/mochitest/chrome/test_sanityException2.xhtml",
427         "testing/mochitest/harness.xhtml",
428       ],
429       rules: {
430         "dot-notation": "off",
431         "object-shorthand": "off",
432         "mozilla/use-services": "off",
433         "mozilla/no-compare-against-boolean-literals": "off",
434         "mozilla/no-useless-parameters": "off",
435         "mozilla/no-useless-removeEventListener": "off",
436         "mozilla/use-cc-etc": "off",
437         "consistent-return": "off",
438         "no-fallthrough": "off",
439         "no-nested-ternary": "off",
440         "no-redeclare": "off",
441         "no-sequences": "off",
442         "no-shadow": "off",
443         "no-throw-literal": "off",
444         "no-undef": "off",
445         "no-unsanitized/property": "off",
446         "no-unused-vars": "off",
447         "no-useless-call": "off",
448       },
449     },
450     {
451       files: [
452         "dom/base/test/chrome/file_bug1139964.xhtml",
453         "dom/base/test/chrome/file_bug549682.xhtml",
454         "dom/base/test/chrome/file_bug616841.xhtml",
455         "dom/base/test/chrome/file_bug990812-1.xhtml",
456         "dom/base/test/chrome/file_bug990812-2.xhtml",
457         "dom/base/test/chrome/file_bug990812-3.xhtml",
458         "dom/base/test/chrome/file_bug990812-4.xhtml",
459         "dom/base/test/chrome/file_bug990812-5.xhtml",
460         "dom/base/test/chrome/file_bug990812.xhtml",
461         "dom/base/test/chrome/test_bug1098074_throw_from_ReceiveMessage.xhtml",
462         "dom/base/test/chrome/test_bug339494.xhtml",
463         "dom/base/test/chrome/test_bug429785.xhtml",
464         "dom/base/test/chrome/test_bug467123.xhtml",
465         "dom/base/test/chrome/test_bug683852.xhtml",
466         "dom/base/test/chrome/test_bug780529.xhtml",
467         "dom/base/test/chrome/test_bug800386.xhtml",
468         "dom/base/test/chrome/test_bug884693.xhtml",
469         "dom/base/test/chrome/test_document-element-inserted.xhtml",
470         "dom/base/test/chrome/test_domparsing.xhtml",
471         "dom/base/test/chrome/title_window.xhtml",
472         "dom/base/test/chrome/window_nsITextInputProcessor.xhtml",
473         "dom/base/test/chrome/window_swapFrameLoaders.xhtml",
474         "dom/base/test/test_domrequesthelper.xhtml",
475         "dom/bindings/test/test_bug1123516_maplikesetlikechrome.xhtml",
476         "dom/console/tests/test_jsm.xhtml",
477         "dom/events/test/test_bug1412775.xhtml",
478         "dom/events/test/test_bug336682_2.xhtml",
479         "dom/events/test/test_bug415498.xhtml",
480         "dom/events/test/test_bug602962.xhtml",
481         "dom/events/test/test_bug617528.xhtml",
482         "dom/events/test/test_bug679494.xhtml",
483         "dom/indexedDB/test/test_globalObjects_chrome.xhtml",
484         "dom/indexedDB/test/test_wrappedArray.xhtml",
485         "dom/ipc/test.xhtml",
486         "dom/ipc/tests/test_process_error.xhtml",
487         "dom/notification/test/chrome/test_notification_system_principal.xhtml",
488         "dom/security/test/general/test_bug1277803.xhtml",
489         "dom/serviceworkers/test/test_serviceworkerinfo.xhtml",
490         "dom/serviceworkers/test/test_serviceworkermanager.xhtml",
491         "dom/system/tests/test_constants.xhtml",
492         "dom/tests/mochitest/chrome/DOMWindowCreated_chrome.xhtml",
493         "dom/tests/mochitest/chrome/MozDomFullscreen_chrome.xhtml",
494         "dom/tests/mochitest/chrome/sizemode_attribute.xhtml",
495         "dom/tests/mochitest/chrome/test_cyclecollector.xhtml",
496         "dom/tests/mochitest/chrome/test_docshell_swap.xhtml",
497         "dom/tests/mochitest/chrome/window_focus.xhtml",
498         "dom/url/tests/test_bug883784.xhtml",
499         "dom/workers/test/test_WorkerDebugger.xhtml",
500         "dom/workers/test/test_WorkerDebugger_console.xhtml",
501         "dom/workers/test/test_fileReadSlice.xhtml",
502         "dom/workers/test/test_fileReaderSync.xhtml",
503         "dom/workers/test/test_fileSlice.xhtml",
504       ],
505       rules: {
506         "mozilla/no-useless-parameters": "off",
507         "mozilla/no-useless-removeEventListener": "off",
508         "mozilla/use-chromeutils-generateqi": "off",
509         "mozilla/use-services": "off",
510         complexity: "off",
511         "no-array-constructor": "off",
512         "no-caller": "off",
513         "no-empty": "off",
514         "no-eval": "off",
515         "no-lone-blocks": "off",
516         "no-redeclare": "off",
517         "no-shadow": "off",
518         "no-throw-literal": "off",
519         "no-unsanitized/method": "off",
520         "no-useless-return": "off",
521         "object-shorthand": "off",
522       },
523     },
524     {
525       files: [
526         "accessible/**",
527         "devtools/**",
528         "dom/**",
529         "docshell/**",
530         "editor/libeditor/tests/**",
531         "editor/spellchecker/tests/test_bug338427.html",
532         "gfx/**",
533         "image/test/browser/browser_image.js",
534         "js/src/builtin/**",
535         "layout/**",
536         "mobile/android/**",
537         "modules/**",
538         "netwerk/**",
539         "remote/**",
540         "security/manager/**",
541         "services/**",
542         "storage/test/unit/test_vacuum.js",
543         "taskcluster/docker/periodic-updates/scripts/**",
544         "testing/**",
545         "tools/**",
546         "widget/tests/test_assign_event_data.html",
547       ],
548       rules: {
549         "mozilla/prefer-boolean-length-check": "off",
550       },
551     },
552     {
553       // Rules of Hooks broadly checks for camelCase "use" identifiers, so
554       // enable only for paths actually using React to avoid false positives.
555       extends: ["plugin:react-hooks/recommended"],
556       files: [
557         "browser/components/newtab/**",
558         "browser/components/pocket/**",
559         "devtools/**",
560       ],
561     },
562     {
563       // Turn off the osfile rule for osfile.
564       files: ["toolkit/components/osfile/**"],
565       rules: {
566         "mozilla/reject-osfile": "off",
567       },
568     },
569   ],