-
Notifications
You must be signed in to change notification settings - Fork 352
/
.eslintrc.js
355 lines (336 loc) · 11 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
351
352
353
354
355
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-commonjs */
const fs = require("fs");
const path = require("path");
const pkgNames = fs
.readdirSync(path.join(__dirname, "packages"))
.filter((name) => name !== ".DS_Store");
const pkgAliases = pkgNames.map((pkgName) => {
return [`@khanacademy/${pkgName}`, `./packages/${pkgName}/src/index.js`];
});
const vendorAliases = fs
.readdirSync(path.join(__dirname, "vendor"))
.map((name) => {
return [name, `./vendor/${name}`];
});
const allAliases = [...pkgAliases, ...vendorAliases];
function banImportExtension(extension) {
const message = `Unexpected use of file extension (.${extension}) in import`;
const literalAttributeMatcher = `Literal[value=/\\.${extension}$/]`;
return [
{
// import foo from 'bar.js';
selector: `ImportDeclaration > ${literalAttributeMatcher}.source`,
message,
},
{
// const foo = import('bar.js');
selector: `ImportExpression > ${literalAttributeMatcher}.source`,
message,
},
{
// type Foo = typeof import('bar.js');
selector: `TSImportType > TSLiteralType > ${literalAttributeMatcher}`,
message,
},
{
// const foo = require('foo.js');
selector: `CallExpression[callee.name = "require"] > ${literalAttributeMatcher}.arguments`,
message,
},
];
}
module.exports = {
extends: [
"@khanacademy",
// This config includes rules from storybook to enforce story best
// practices
"plugin:storybook/recommended",
// This config includes rules from @testing-library/jest-dom as well
"plugin:testing-library/react",
],
parserOptions: {
babelOptions: {
configFile: path.join(__dirname, "./config/build/babel.config.js"),
},
},
plugins: [
"@babel",
"cypress",
"disable",
"import",
"jest",
"jsdoc",
"monorepo",
"promise",
"react",
"react-hooks",
"react-native",
],
settings: {
"import/resolver": {
typescript: {
project: [
"packages/*/tsconfig.json",
"packages/tsconfig-shared.json",
],
extensions: [".js", ".jsx", ".ts", ".tsx"],
moduleDirectory: ["node_modules"],
},
node: {
project: [
"packages/*/tsconfig.json",
"packages/tsconfig-shared.json",
],
extensions: [".js", ".jsx", ".ts", ".tsx"],
moduleDirectory: ["node_modules"],
},
alias: {
map: allAliases,
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
},
},
react: {
version: "detect",
},
},
env: {
"cypress/globals": true,
"jest/globals": false,
node: true,
browser: true,
},
globals: {
// `no-undef` doesn't support `globalThis`, for details see
// https://github.com/eslint/eslint/issues/15199.
globalThis: false, // means it isn't writeable
// from node_modules/@types/react/index.d.ts
JSX: false,
// from lib.dom.d.t.s
ClientRect: false,
WindowProxy: false,
// from types/utility.d.ts
Empty: false,
SpreadType: false,
},
overrides: [
{
files: [
"*.cypress.tsx",
"*.cypress.ts",
"*.test.ts",
"*.test.tsx",
"*.stories.ts",
"*.stories.tsx",
],
rules: {
"max-lines": "off",
"import/no-extraneous-dependencies": "off",
"import/no-relative-packages": "off",
},
},
{
files: ["config/**", "utils/**", "testing/*"],
rules: {
"@typescript-eslint/no-var-requires": "off",
"import/no-commonjs": "off",
"import/no-extraneous-dependencies": "off",
"import/no-relative-packages": "off",
"no-console": "off",
"testing-library/no-debugging-utils": "off",
},
},
{
files: ["score-*.ts"],
rules: {
"no-restricted-syntax": [
"error",
{
selector:
"ImportDeclaration > ImportSpecifier[local.name='APIOptions']",
message:
"APIOptions is not available and should not be imported in scoring functions.",
},
{
selector: "ImportDeclaration[source.value='react']",
message:
"React is not available and should not be imported in scoring functions.",
},
],
},
},
],
rules: {
"max-lines": "off",
"new-cap": "off",
"no-invalid-this": "off",
"@typescript-eslint/no-this-alias": "off",
"no-unused-expressions": "off",
"object-curly-spacing": "off",
semi: "off",
/**
* @babel
*/
// "@babel/new-cap": "error",
"@babel/no-invalid-this": "error",
"@babel/object-curly-spacing": "error",
"@babel/semi": "error",
/**
* jest
*/
"jest/no-focused-tests": "error",
/**
* jsdoc
*/
"jsdoc/valid-types": "error",
/**
* import
*/
// "import/no-default-export": "error",
"import/no-unresolved": "error",
// NOTE(kevinb): This rule is confused by third-party TypeScript lib defs
"import/named": "off",
"import/default": "error",
"import/namespace": "error",
"import/no-absolute-path": "error",
"import/no-self-import": "error",
"import/no-useless-path-segments": "error",
"import/no-named-as-default": "error",
// NOTE(kevinb): This rule is causes issues with some uses of 'prop-types'
"import/no-named-as-default-member": "off",
"import/no-deprecated": "error",
"import/no-commonjs": "error",
// "import/first": "error",
"import/no-duplicates": "error",
"import/order": [
"error",
{
alphabetize: {
order: "asc",
},
groups: [
"builtin",
"external",
"internal",
"parent",
"sibling",
"type",
],
"newlines-between": "always",
},
],
"import/newline-after-import": "error",
"import/no-unassigned-import": [
"error",
{
allow: [
"**/*.css",
"**/*.less",
"@testing-library/jest-dom",
"@testing-library/jest-dom/extend-expect",
"whatwg-fetch",
],
},
],
"import/no-named-default": "error",
"import/no-relative-packages": "error",
"import/no-restricted-paths": [
"error",
{
zones: [
{
target: "./packages/(?!.*test.js).*",
from: "utils",
},
],
},
],
"no-restricted-syntax": [
"error",
{
selector:
"MemberExpression[property.name='render'][object.name='ReactDOM']",
message: "DEPRECATED: Use a React Portal instead.",
},
...banImportExtension("js"),
...banImportExtension("jsx"),
...banImportExtension("ts"),
...banImportExtension("tsx"),
{
selector: "TSQualifiedName[left.name='React'][right.name='FC']",
message:
"Use of React.FC<Props> is disallowed, use the following alternative: https://khanacademy.atlassian.net/wiki/spaces/ENG/pages/2201682693/TypeScript+for+Flow+Developers#Functional-Components",
},
],
/**
* monorepo
*/
"monorepo/no-internal-import": "error",
/**
* prettier - disabled in CI. W we run prettier manually here as its
* faster than through ESLint and covers more types of files than
* ESLint does.
*
* For Prettier, we include all files except those in
* `.prettierignore`.
* For ESLint we only consider TS/JS files (see `lint` script in
* package.json).
*/
"prettier/prettier": [process.env.CI === "true" ? "off" : "error"],
/**
* promise
*/
// "promise/always-return": "error",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
// "promise/catch-or-return": "error",
"promise/no-new-statics": "error",
"promise/no-return-in-finally": "error",
/**
* react
*/
// This rule results in false-positives when using some types of React
// components (such as functional components or hooks). Since
// TypeScript is already checking that components are only using props
// defined in the component's `Props` type.
"react/prop-types": "off",
"react/sort-comp": [
"error",
{
// TODO(kevinb): specify where "constructor" should go
order: [
"type-annotations",
"static-variables",
"instance-variables",
"static-methods",
"constructor",
"lifecycle",
"everything-else",
"render",
],
},
],
/**
* react-hooks
*/
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "error",
/**
* react-native
*/
"react-native/no-unused-styles": "error",
/**
* testing-library
*/
"testing-library/await-async-queries": "error",
"testing-library/prefer-user-event": "error",
"testing-library/prefer-explicit-assert": "error",
// These rules results in a lot of false positives
"testing-library/render-result-naming-convention": "off",
/**
* typescript
*/
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/consistent-type-imports": "error",
},
};