forked from pixiebrix/eslint-config-pixiebrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (314 loc) · 9.46 KB
/
index.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
"use strict";
const xoTypeScriptConfig = require("eslint-config-xo-typescript");
function customize(config, rule, customizer) {
const [type, ruleConfig] = config.rules[rule];
// Spread shallow-clones the object
const newRuleConfig = { ...ruleConfig };
customizer(newRuleConfig);
return {
[rule]: [type, newRuleConfig],
};
}
const config = {
env: {
browser: true,
},
settings: {
"import/resolver": {
typescript: {},
},
"import/ignore": [
"react-select", // For some reason it points to a flow JS file
],
react: {
version: "detect",
},
},
ignorePatterns: [".idea", "dist", "**/__mocks__/**"],
plugins: ["filenames", "jsx-a11y"],
extends: [
"./xoPluginsConfig.js",
"xo", // Full config: https://github.com/xojs/eslint-config-xo/blob/main/index.js
"xo-typescript", // Full config: https://github.com/xojs/eslint-config-xo-typescript/blob/main/index.js
"prettier", // Disable style-related rules
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:security/recommended-legacy",
"plugin:unicorn/recommended", // Full config: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/configs/recommended.js
"plugin:jsx-a11y/recommended",
/**************************************************************
* Only add test rules and plugins to the "./tests.js" config *
**************************************************************/
],
rules: {
// Enable extra rules
"react/jsx-no-useless-fragment": "error",
"import/dynamic-import-chunkname": [
"error",
{
webpackChunknameFormat: "[a-zA-Z0-57-9-/_\\[\\].]+",
},
],
"no-restricted-imports": [
"error",
{
// Documentation: https://eslint.org/docs/rules/no-restricted-imports#options
patterns: [
{
group: ["*/__mocks__/*"],
message:
"Mocks should not be imported directly, they’re automatically picked up where needed.",
},
{
group: ["lodash/*"],
message: 'You can import "lodash" instead of "lodash/*".',
},
{
group: ["immer/*"],
importNames: ["WritableDraft"],
message: 'Use this instead: import { type Draft } from "immer"',
},
{
group: ["react-spinners"],
message:
"Use the local <Loader/> component instead, it's already centered.",
},
{
group: ["react-bootstrap/*", "!react-bootstrap/types"],
message:
'You can import "react-bootstrap" instead of "react-bootstrap/*".',
},
{
group: ["../*"],
message:
'Use root-based imports (`import "@/something"`) instead of relative imports.',
},
{
group: ["formik"],
importNames: ["Form", "Formik"],
message: 'Use this instead: import "@/components/form/Form"',
},
{
group: ["react-bootstrap"],
importNames: ["Form"],
message: 'Use this instead: import "@/components/form/Form"',
},
{
group: ["webext-detect-page"],
importNames: ["isDevToolsPage"],
message: 'Use this instead: import { isPageEditor } from "@/utils/expectContext";',
},
],
},
],
// Avoid imports with side effects
"import/no-unassigned-import": [
"error",
{
allow: [
"**/*.css",
"**/*.scss",
"**/reportUncaughtErrors",
"regenerator-runtime/runtime", // Automatic registration
],
},
],
"no-mixed-operators": [
"error",
{
// Customize the defaults to force being explicit about use of null-coalescing operator because its precedence
// is unintuitive. See: https://eslint.org/docs/rules/no-mixed-operators
groups: [
// Conflicts with Prettier: https://github.com/prettier/prettier/issues/3968
// ["+", "-", "*", "/", "%", "**", "??"],
["&", "|", "^", "~", "<<", ">>", ">>>", "??"],
["==", "!=", "===", "!==", ">", ">=", "<", "<=", "??"],
["&&", "||", "??"],
["in", "instanceof", "??"],
],
},
],
// Customize some rules
quotes: ["error", "double", { avoidEscape: true }], // Matches Prettier, but also replaces backticks
...customize(
xoTypeScriptConfig,
"@typescript-eslint/ban-types",
(config) => {
delete config.types.null;
}
),
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-explicit-any": [
"error",
{
fixToUnknown: true,
ignoreRestArgs: true,
},
],
"unicorn/prefer-export-from": [
"error",
{
ignoreUsedVariables: true,
},
],
"unicorn/prevent-abbreviations": [
"error",
{
replacements: {
acc: false,
arg: false,
args: false,
db: false,
dev: false,
doc: false,
docs: false,
env: false,
err: false,
ev: false,
evt: false,
ext: false,
exts: false,
$el: {
$elements: true,
},
$elt: {
$elements: true,
},
$element: {
$elements: true,
},
fn: false,
func: {
fn: true,
function: false,
},
i: false,
j: false,
mod: false,
num: false,
obj: false,
param: false,
params: false,
prev: false,
prod: false,
prop: false,
props: false,
ref: false,
refs: false,
str: false,
var: false,
vars: false,
},
ignore: ["semVer", "SemVer"],
},
],
"unicorn/filename-case": [
"error",
{
cases: {
camelCase: true,
pascalCase: true,
},
},
],
// Smart allows for != null. See: https://github.com/pixiebrix/pixiebrix-extension/pull/887#pullrequestreview-711873690
eqeqeq: ["error", "smart"],
// Disable recommended rules
"no-eq-null": "off", // `eqeqeq` covers it: https://github.com/pixiebrix/pixiebrix-extension/pull/887#pullrequestreview-711873690
"unicorn/no-null": "off", // We don't do that here
"react/prop-types": "off", // We don't do that here
"no-warning-comments": "off", // Only useful if there aren't hundreds of other real warnings
"security/detect-non-literal-fs-filename": "off", // 100% false positives, we never use the `fs` module
"unicorn/no-nested-ternary": "off", // Sometimes it conflicts with Prettier
"unicorn/prefer-set-has": "off", // Not always worth the extra code
"unicorn/prefer-top-level-await": "off", // No advantage in browsers
"import/no-extraneous-dependencies": "off", // Not worth it
"@typescript-eslint/triple-slash-reference": "off", // No alternative sometimes
"@typescript-eslint/consistent-type-definitions": "off", // `type` cannot be used to extend globals
"@typescript-eslint/no-dynamic-delete": "off", // Already covered by `security/detect-object-injection`
// Rules that duplicate TypeScript features
"import/default": "off",
"import/named": "off",
"import/no-named-as-default": "off", // Too slow
"import/no-named-as-default-member": "off", // It's common to use `React.memo` instead of just `memo`
"@typescript-eslint/consistent-type-assertions": "off", // Our current typing has too many `unknowns` for this to be applicable https://github.com/typescript-eslint/typescript-eslint/issues/4462
// Disable rule until we find a better config https://github.com/pixiebrix/eslint-config-pixiebrix/issues/5
"@typescript-eslint/naming-convention": "off",
// Requires strictNullChecks
"@typescript-eslint/prefer-nullish-coalescing": "off",
// Maybe later, opinionated
"unicorn/prefer-ternary": "off",
"@typescript-eslint/member-ordering": "off",
"@typescript-eslint/no-empty-function": "off",
"import/order": "off",
"import/extensions": "off",
"import/no-mutable-exports": "off",
"node/file-extension-in-import": "off",
"node/prefer-global/process": "off", // `process.env` is required by webpack
"node/prefer-global/buffer": "off",
"@typescript-eslint/no-use-before-define": [
"error",
{
// Disabling functions -- functions are hoisted and not a risk
// https://eslint.org/docs/latest/rules/no-use-before-define#options
functions: false,
// https://typescript-eslint.io/rules/no-use-before-define/#options
ignoreTypeReferences: false,
},
],
},
overrides: [
{
// JS files shouldn't have TypeScript rules, but it's bothersome to separate them properly
files: ["**/*.js"],
rules: {
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
},
},
{
files: ["**/*.tsx", "**/use*.ts"],
excludedFiles: ["*.test.tsx", "*.stories.tsx"],
rules: {
"filenames/match-exported": "error",
},
},
{
files: ["*.stories.tsx"],
extends: ["plugin:storybook/recommended"],
rules: {
"unicorn/filename-case": "off",
"unicorn/no-useless-spread": "off", // Clashes with getDefaultMiddleware().concat
"import/no-anonymous-default-export": "off",
},
},
/**************************************************************
* Only add test rules and plugins to the "./tests.js" config *
**************************************************************/
],
};
// Add override this way because it depends on the object above
config.overrides.push({
files: ["src/*"],
rules: {
"no-restricted-imports": [
"error",
{
// Documentation: https://eslint.org/docs/rules/no-restricted-imports#options
patterns: [
// Extend the existing patterns
...config.rules["no-restricted-imports"][1].patterns,
{
group: ["./*"],
message:
'Use root-based imports (`import "@/something"`) instead of relative imports.',
},
],
},
],
},
});
module.exports = config;