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