-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
.eslintrc.js
277 lines (271 loc) · 8.8 KB
/
.eslintrc.js
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
/* 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/. */
"use strict";
const xpcshellTestConfig = require("eslint-plugin-mozilla/lib/configs/xpcshell-test.js");
const browserTestConfig = require("eslint-plugin-mozilla/lib/configs/browser-test.js");
const mochitestTestConfig = require("eslint-plugin-mozilla/lib/configs/mochitest-test.js");
const chromeTestConfig = require("eslint-plugin-mozilla/lib/configs/chrome-test.js");
const { testPaths } = require("./.eslintrc-test-paths.js");
const { rollouts } = require("./.eslintrc-rollouts.js");
const fs = require("fs");
const path = require("path");
/**
* Some configurations have overrides, which can't be specified within overrides,
* so we need to remove them.
*
* @param {object} config
* The configuration to remove overrides from.
* @returns {object}
* The new configuration.
*/
function removeOverrides(config) {
config = { ...config };
delete config.overrides;
return config;
}
function readFile(path) {
return fs
.readFileSync(path, { encoding: "utf-8" })
.split("\n")
.filter(p => p && !p.startsWith("#"));
}
const ignorePatterns = [
...readFile(
path.join(__dirname, "tools", "rewriting", "ThirdPartyPaths.txt")
),
...readFile(path.join(__dirname, "tools", "rewriting", "Generated.txt")),
...readFile(
path.join(
__dirname,
"devtools",
"client",
"debugger",
"src",
".eslintignore"
)
).map(p => `devtools/client/debugger/src/${p}`),
];
const httpTestingPaths = [
"**/*mixedcontent",
"**/*CrossOrigin",
"**/*crossorigin",
"**/*cors",
"**/*downgrade",
"**/*Downgrade",
];
module.exports = {
settings: {
"import/extensions": [".mjs"],
},
ignorePatterns,
// Ignore eslint configurations in parent directories.
root: true,
// New rules and configurations should generally be added in
// tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js to
// allow external repositories that use the plugin to pick them up as well.
extends: ["plugin:mozilla/recommended"],
plugins: ["mozilla", "import", "json"],
overrides: [
{
files: [
// All .eslintrc.js files are in the node environment, so turn that
// on here.
// https://github.com/eslint/eslint/issues/13008
".eslintrc*.js",
// *.config.js files are generally assumed to be configuration files
// based for node.
"*.config.?(m)js",
],
env: {
node: true,
browser: false,
},
},
{
files: ["browser/base/content/browser.js"],
rules: {
"mozilla/no-more-globals": "error",
},
},
{
files: [
"**/*.jsx",
"browser/components/pocket/content/**/*.js",
"browser/components/storybook/.storybook/**/*.mjs",
],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
files: ["browser/components/storybook/**"],
env: {
"mozilla/privileged": false,
},
},
{
files: ["*.mjs"],
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",
},
},
{
// 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"],
rules: {
"import/no-unassigned-import": "off",
},
},
{
files: ["**/test/**", "**/tests/**"],
extends: ["plugin:mozilla/general-test"],
},
{
...removeOverrides(xpcshellTestConfig),
files: testPaths.xpcshell.map(path => `${path}**`),
excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
},
{
// 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(path => `${path}head*.js`),
rules: {
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
vars: "local",
},
],
},
},
{
// 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(path => `${path}test*.js`),
rules: {
// No declaring variables that are never used
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
vars: "all",
},
],
},
},
{
...removeOverrides(browserTestConfig),
files: testPaths.browser.map(path => `${path}**`),
excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
},
{
...removeOverrides(mochitestTestConfig),
files: testPaths.mochitest.map(path => `${path}**`),
excludedFiles: [
"**/*.jsm",
"**/*.mjs",
"security/manager/ssl/tests/mochitest/browser/**",
],
},
{
...removeOverrides(chromeTestConfig),
files: testPaths.chrome.map(path => `${path}**`),
excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
},
{
env: {
// Ideally we wouldn't be using the simpletest env here, but our uses of
// js files mean we pick up everything from the global scope, which could
// be any one of a number of html files. So we just allow the basics...
"mozilla/simpletest": true,
},
files: [
...testPaths.mochitest.map(path => `${path}/**/*.js`),
...testPaths.chrome.map(path => `${path}/**/*.js`),
],
excludedFiles: ["**/*.jsm", "**/*.mjs", "**/*.sjs"],
},
{
// 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(path => [`${path}/**/*.html`, `${path}/**/*.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",
},
},
{
// 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: [
// 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.
extends: ["plugin:react-hooks/recommended"],
files: [
"browser/components/aboutwelcome/**",
"browser/components/asrouter/**",
"browser/components/newtab/**",
"browser/components/pocket/**",
"devtools/**",
],
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",
},
},
{
// Exempt files with these paths since they have to use http for full coverage
files: httpTestingPaths.map(path => `${path}**`),
rules: {
"@microsoft/sdl/no-insecure-url": "off",
},
},
...rollouts,
],
};