-
Notifications
You must be signed in to change notification settings - Fork 22
/
.eslintrc.js
350 lines (342 loc) · 11.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const { readFileSync } = require("node:fs");
const { resolve } = require("node:path");
const noRestrictedImports = require("eslint-config-pixiebrix/no-restricted-imports");
function extendNoRestrictedImports({ patterns = [], paths = [] }) {
// Clone object to avoid modifying the original
const customized = structuredClone(noRestrictedImports);
customized.patterns.push(...patterns);
customized.paths.push(...paths);
return customized;
}
const boundaries = [
"background",
"contentScript",
"pageEditor",
"extensionConsole",
"sidebar",
"pageScript",
];
const forbiddenDomPropsConfig = [
"error",
{
// Context: https://github.com/pixiebrix/pixiebrix-extension/pull/7832
forbid: [
{
propName: "target",
message:
'In this folder, `target="_blank"` already the default thanks to the `<base>` in the .html file',
},
{
propName: "rel",
message:
"This attribute was probably left behind after dropping the `target` attribute.",
},
],
},
];
module.exports = {
root: true,
extends: [
// Full config: https://github.com/pixiebrix/eslint-config-pixiebrix/blob/main/index.js
"pixiebrix",
],
plugins: ["local-rules"],
rules: {
"new-cap": [
"error",
{
capIsNewExceptionPattern:
"(TEST_|INTERNAL_|HACK_|UNSAFE_|API_PATHS.|UI_PATHS.)",
},
],
"no-param-reassign": [
"warn",
{
props: true,
ignorePropertyModificationsFor: ["draft", "state"],
},
],
"@eslint-community/eslint-comments/require-description": [
"error",
{ ignore: ["eslint-enable"] },
],
"jest/valid-title": [
"error",
{
mustNotMatch: {
test: [
/^it/.source,
'Test title should not begin with `it`. Use `it("should ...")` or omit `it` from the title instead.',
],
},
},
],
"local-rules/preferAxiosMockAdapter": "error",
"local-rules/noNullRtkQueryArgs": "error",
"local-rules/noInvalidDataTestId": "error",
"local-rules/noExpressionLiterals": "error",
"local-rules/notBothLabelAndLockableProps": "error",
"local-rules/preferNullish": "error",
"local-rules/preferNullishable": "error",
"local-rules/noCrossBoundaryImports": [
"warn",
{
// This rule is customized below for files in "src/platform"
boundaries,
allowedGlobs: [
"**/messenger/**",
"**/*.scss*",
// Allow contexts to declare/export context-specific platform protocol. For example, the content script
// platform exposes additional methods for selecting elements, etc.
// Alternatively, we'd need to move platform-specific bricks into their respective context's folder.
"**/platform/*Protocol",
],
},
],
// Avoid imports with side effects
"import/no-unassigned-import": [
"error",
{
allow: [
"**/*.css",
"**/*.scss",
"@/development/*",
"@/background/messenger/external/api",
"@/extensionContext", // Must be run before other code
"@/background/axiosFetch", // Must be run before other code
"@/telemetry/reportUncaughtErrors",
"@testing-library/jest-dom",
"regenerator-runtime/runtime", // Automatic registration
"@/vendors/hoverintent", // JQuery plugin
"iframe-resizer/js/iframeResizer.contentWindow", // Vendor library imported for side-effect
],
},
],
"no-restricted-imports": [
"error",
// If they're not specific to the extension, add them to the shared config instead:
// https://github.com/pixiebrix/eslint-config-pixiebrix/blob/main/no-restricted-imports.js
extendNoRestrictedImports({
patterns: [
{
group: ["axios"],
importNames: ["AxiosRequestConfig"],
message:
'Use this instead: import { NetworkRequestConfig } from "@/types/networkTypes"',
},
{
group: ["react-shadow/emotion"],
message:
'Use this instead: import EmotionShadowRoot from "@/components/EmotionShadowRoot"',
},
],
}),
],
"no-restricted-syntax": [
"error",
// If they're not specific to the extension, add them to the shared config instead:
// https://github.com/pixiebrix/eslint-config-pixiebrix/blob/main/no-restricted-syntax.js
...require("eslint-config-pixiebrix/no-restricted-syntax"),
{
message:
'Use `getExtensionConsoleUrl` instead of `browser.runtime.getURL("options.html")` because it automatically handles paths/routes',
selector:
"CallExpression[callee.object.property.name='runtime'][callee.property.name='getURL'][arguments.0.value='options.html']",
},
{
message:
"Prefer using `getSelectionRange()` helper or check `selection.rangeCount` first: https://github.com/pixiebrix/pixiebrix-extension/pull/7989",
selector: "CallExpression[callee.property.name='getRangeAt']",
},
// NOTE: If you add more rules, add the tests to eslint-local-rules/noRestrictedSyntax.tsx
],
},
overrides: [
{
files: ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"],
rules: {
// TODO: Gradually fix and then drop https://github.com/pixiebrix/eslint-config-pixiebrix/pull/150
"@typescript-eslint/no-unsafe-assignment": "warn",
"@typescript-eslint/no-unsafe-member-access": "warn",
},
},
{
// TODO: consider packaging e2e tests in a mono-repo structure for specific linting rules
files: ["end-to-end-tests/**"], // Or *.test.js
extends: "plugin:playwright/recommended",
rules: {
"no-restricted-imports": "off",
"unicorn/prefer-dom-node-dataset": "off",
"unicorn/prefer-module": "off", // `import.meta.dirname` throws "cannot use 'import meta' outside a module"
"no-await-in-loop": "off",
"security/detect-object-injection": "off",
"playwright/no-skipped-test": [
"error",
{
allowConditional: true,
},
],
"playwright/no-page-pause": "error",
"playwright/no-wait-for-timeout": "error",
"playwright/no-useless-not": "error",
"playwright/expect-expect": [
"error",
{ assertFunctionNames: ["checkUnavailibilityForNavigationMethod"] },
],
"playwright/no-conditional-in-test": "error",
"playwright/no-conditional-expect": "error",
"playwright/no-commented-out-tests": "error",
"playwright/no-hooks": "error", // Use fixtures instead to share common setup / teardown code
"playwright/no-get-by-title": "error",
// Disabled because raw locators are sometimes necessary for specific test scenarios, and we don't want noisy warnings in the IDE
// However, our README encourages writing tests using the recommended built-in locators where possible
// "playwright/no-raw-locators": "warn",
"playwright/prefer-comparison-matcher": "error",
"playwright/prefer-equality-matcher": "error",
"playwright/prefer-strict-equal": "error",
"playwright/prefer-to-be": "error",
"playwright/prefer-to-contain": "error",
"playwright/prefer-to-have-count": "error",
"playwright/prefer-to-have-length": "error",
"playwright/require-to-throw-message": "error",
"no-restricted-syntax": [
"error",
{
message:
"Define a value for the timeout options parameter to avoid waiting forever (`.toPass` by default will retry forever)",
selector:
"CallExpression[callee.property.name='toPass'][arguments.length=0]",
},
],
"local-rules/preferUsingStepsForLongTests": "error",
},
},
{
files: [
"webpack.*.js",
"*.config.js",
"scripts/*",
"eslint-local-rules/*",
],
// Full config: https://github.com/pixiebrix/eslint-config-pixiebrix/blob/main/development.js
extends: ["pixiebrix/development"],
rules: {
"local-rules/noCrossBoundaryImports": "off",
},
},
{
files: [
"*/scripts/*",
"**/__mocks__/**",
"**/testUtils/**",
"**/*.test.ts",
"**/*.test.tsx",
"**/testHelpers.*",
"**/*.stories.tsx",
],
// Full config: https://github.com/pixiebrix/eslint-config-pixiebrix/blob/main/tests.js
extends: ["pixiebrix/development", "pixiebrix/tests"],
rules: {
"unicorn/prefer-spread": "off",
"local-rules/noCrossBoundaryImports": "off",
},
},
{
files: ["./src/platform/**"],
rules: {
"local-rules/noCrossBoundaryImports": [
// Turn into error
"error",
{
boundaries,
// Do not allow Messenger imports either
allowedGlobs: ["**/*.scss*"],
},
],
},
},
{
files: [
"./src/background/**",
...readFileSync(
resolve(__dirname, "eslint-local-rules/persistBackgroundData.txt"),
"utf8",
)
.split("\n")
.filter((line) => line.startsWith("./src/")),
],
excludedFiles: ["**/*.test.*", "**/api.ts"],
rules: {
"local-rules/persistBackgroundData": "error",
},
},
{
// Settings for regular ts files that should only apply to react component tests
files: ["**/!(*.test)*.ts?(x)", "**/*.ts"],
rules: {
"testing-library/render-result-naming-convention": "off",
"testing-library/no-await-sync-queries": "off",
},
},
{
files: ["./src/*"],
rules: {
"no-restricted-imports": [
"error",
extendNoRestrictedImports({
patterns: [
{
group: ["./*"],
message:
'Use root-based imports (`import "@/something"`) instead of relative imports.',
},
{
group: ["../*"],
message:
'Use root-based imports (`import "@/something"`) instead of relative imports.',
},
],
}),
],
},
},
{
files: ["./src/**/*.tsx", "./src/**/use*.ts"],
rules: {
"no-restricted-imports": [
"error",
extendNoRestrictedImports({
patterns: [
{
group: ["@/auth/featureFlagStorage"],
message:
"Use useFlags instead of featureFlagStorage in React code.",
},
],
}),
],
},
},
{
files: ["./src/pageEditor/**.tsx", "./src/sidebar/**.tsx"],
rules: {
"react/forbid-dom-props": forbiddenDomPropsConfig,
"react/forbid-component-props": forbiddenDomPropsConfig,
},
},
{
files: ["**/*.test.ts?(x)", "**/*.spec.ts"],
rules: {
"@typescript-eslint/no-non-null-assertion": "off",
},
},
],
};
// `npm run lint:fast` will skip the (slow) import/* rules
// Useful if you're trying to iterate fixes over other rules
if (process.env.ESLINT_NO_IMPORTS) {
const importRules = Object.keys(require("eslint-plugin-import").rules);
for (const ruleName of importRules) {
module.exports.rules[`import/${ruleName}`] = "off";
}
}