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/. */
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 { testPaths } = require("./.eslintrc-test-paths.js");
12 const { rollouts } = require("./.eslintrc-rollouts.js");
13 const fs = require("fs");
14 const path = require("path");
17 * Some configurations have overrides, which can't be specified within overrides,
18 * so we need to remove them.
20 * @param {object} config
21 * The configuration to remove overrides from.
23 * The new configuration.
25 function removeOverrides(config) {
26 config = { ...config };
27 delete config.overrides;
31 function readFile(path) {
33 .readFileSync(path, { encoding: "utf-8" })
35 .filter(p => p && !p.startsWith("#"));
38 const ignorePatterns = [
40 path.join(__dirname, "tools", "rewriting", "ThirdPartyPaths.txt")
42 ...readFile(path.join(__dirname, "tools", "rewriting", "Generated.txt")),
52 ).map(p => `devtools/client/debugger/src/${p}`),
54 const httpTestingPaths = [
65 "import/extensions": [".mjs"],
68 // Ignore eslint configurations in parent directories.
70 // New rules and configurations should generally be added in
71 // tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js to
72 // allow external repositories that use the plugin to pick them up as well.
73 extends: ["plugin:mozilla/recommended"],
74 plugins: ["mozilla", "import", "json"],
78 // All .eslintrc.js files are in the node environment, so turn that
80 // https://github.com/eslint/eslint/issues/13008
82 // *.config.js files are generally assumed to be configuration files
92 files: ["browser/base/content/browser.js"],
94 "mozilla/no-more-globals": "error",
100 "browser/components/pocket/content/**/*.js",
101 "browser/components/storybook/.storybook/**/*.mjs",
110 files: ["browser/components/storybook/**"],
112 "mozilla/privileged": false,
118 "import/default": "error",
119 "import/export": "error",
120 "import/named": "error",
121 "import/namespace": "error",
122 "import/newline-after-import": "error",
123 "import/no-duplicates": "error",
124 "import/no-absolute-path": "error",
125 "import/no-named-default": "error",
126 "import/no-named-as-default": "error",
127 "import/no-named-as-default-member": "error",
128 "import/no-self-import": "error",
129 "import/no-unassigned-import": "error",
130 "import/no-unresolved": [
132 // Bug 1773473 - Ignore resolver URLs for chrome and resource as we
133 // do not yet have a resolver for them.
134 { ignore: ["chrome://", "resource://"] },
136 "import/no-useless-path-segments": "error",
140 // Turn off no-unassigned-import for files that typically test our
141 // custom elements, which are imported for the side effects (ie
142 // the custom element being registered) rather than any particular
144 files: ["**/*.stories.mjs"],
146 "import/no-unassigned-import": "off",
150 files: ["**/test/**", "**/tests/**"],
151 extends: ["plugin:mozilla/general-test"],
154 ...removeOverrides(xpcshellTestConfig),
155 files: testPaths.xpcshell.map(path => `${path}**`),
156 excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
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: testPaths.xpcshell.map(path => `${path}head*.js`),
168 argsIgnorePattern: "^_",
175 // This section enables errors of no-unused-vars globally for all test*.js
176 // files in xpcshell test paths.
177 // This is not done in the xpcshell-test configuration as we cannot pull
178 // in overrides from there. We should at some stage, aim to enable this
179 // for all files in xpcshell-tests.
180 files: testPaths.xpcshell.map(path => `${path}test*.js`),
182 // No declaring variables that are never used
186 argsIgnorePattern: "^_",
193 ...removeOverrides(browserTestConfig),
194 files: testPaths.browser.map(path => `${path}**`),
195 excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
198 ...removeOverrides(mochitestTestConfig),
199 files: testPaths.mochitest.map(path => `${path}**`),
203 "security/manager/ssl/tests/mochitest/browser/**",
207 ...removeOverrides(chromeTestConfig),
208 files: testPaths.chrome.map(path => `${path}**`),
209 excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
213 // Ideally we wouldn't be using the simpletest env here, but our uses of
214 // js files mean we pick up everything from the global scope, which could
215 // be any one of a number of html files. So we just allow the basics...
216 "mozilla/simpletest": true,
219 ...testPaths.mochitest.map(path => `${path}/**/*.js`),
220 ...testPaths.chrome.map(path => `${path}/**/*.js`),
222 excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
225 // Some directories have multiple kinds of tests, and some rules
226 // don't work well for HTML-based mochitests, so disable those.
227 files: testPaths.xpcshell
228 .concat(testPaths.browser)
229 .map(path => [`${path}/**/*.html`, `${path}/**/*.xhtml`])
232 // plain/chrome mochitests don't automatically include Assert, so
233 // autofixing `ok()` to Assert.something is bad.
234 "mozilla/no-comparison-or-assignment-inside-ok": "off",
238 // Some directories reuse `test_foo.js` files between mochitest-plain and
239 // unit tests, or use custom postMessage-based assertion propagation into
240 // browser tests. Ignore those too:
242 // Reuses xpcshell unit test scripts in mochitest-plain HTML files.
243 "dom/indexedDB/test/**",
244 // Dispatches functions to the webpage in ways that are hard to detect.
245 "toolkit/components/antitracking/test/**",
248 "mozilla/no-comparison-or-assignment-inside-ok": "off",
252 // Rules of Hooks broadly checks for camelCase "use" identifiers, so
253 // enable only for paths actually using React to avoid false positives.
254 extends: ["plugin:react-hooks/recommended"],
256 "browser/components/aboutwelcome/**",
257 "browser/components/asrouter/**",
258 "browser/components/newtab/**",
259 "browser/components/pocket/**",
263 // react-hooks/recommended has exhaustive-deps as a warning, we prefer
264 // errors, so that raised issues get addressed one way or the other.
265 "react-hooks/exhaustive-deps": "error",
269 // Exempt files with these paths since they have to use http for full coverage
270 files: httpTestingPaths.map(path => `${path}**`),
272 "@microsoft/sdl/no-insecure-url": "off",