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