-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint.config.mjs
226 lines (213 loc) · 7.43 KB
/
eslint.config.mjs
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
import "dotenv/config";
import fs from "node:fs";
import path from "node:path";
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import globals from "globals";
import unicorn from "eslint-plugin-unicorn";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const { escapePath } = require("fast-glob");
const simpleImportSort = require("eslint-plugin-simple-import-sort");
const eslintConfigPrettier = require("eslint-config-prettier");
/** @type { import("eslint").Linter.Config[] } */
const config = [
{
ignores: [...getIgnoresFromGitignore(), "lib/__fixtures__", ".scripts/build-tests"],
},
{
files: ["**/*.{js,mjs,cjs,jsx,ts,tsx,mts}"],
rules: {
...eslint.configs.recommended.rules,
"no-warning-comments": "warn",
"no-console": "off",
"no-empty": ["error", { allowEmptyCatch: true }],
},
},
{
// assume js based files are node
files: ["**/*.{js,mjs,cjs}"],
languageOptions: {
ecmaVersion: 2023,
globals: { ...globals.node },
},
rules: { ...eslint.configs.recommended.rules },
},
{
// assume ts based files are web
files: ["**/*.{ts,tsx,mts}"],
plugins: {
"@typescript-eslint": tseslint.plugin,
"simple-import-sort": simpleImportSort,
unicorn,
},
languageOptions: {
ecmaVersion: 2023,
sourceType: "module",
parser: tseslint.parser,
globals: { ...globals.browser },
parserOptions: {
sourceType: "module",
ecmaFeatures: { jsx: true },
project: [path.join(__dirname, "tsconfig.eslint.json")],
extraFileExtensions: [".html"],
},
},
rules: {
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
...rulesFromConfig(tseslint.configs.recommendedTypeChecked),
...rulesFromConfig(tseslint.configs.stylisticTypeChecked),
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/dot-notation": ["error", { allowPrivateClassPropertyAccess: true }],
"@typescript-eslint/explicit-function-return-type": "off",
"no-throw-literal": "off",
"@typescript-eslint/only-throw-error": "error",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_.*", varsIgnorePattern: "^_.*" },
],
"@typescript-eslint/naming-convention": [
"warn",
{
selector: "variable",
types: ["boolean"],
format: ["PascalCase", "UPPER_CASE"],
/* prettier-ignore */
prefix: [
'is', 'are', 'should', 'has', 'can', 'did', 'does', 'will',
'IS_', 'ARE_', 'SHOULD_', 'HAS_', 'CAN_', 'DID_', 'DOES_', 'WILL_'
],
},
],
"@typescript-eslint/require-await": "off",
...rulesFromConfig(unicorn.configs.recommended),
"unicorn/consistent-destructuring": "error",
"unicorn/filename-case": "off",
"unicorn/no-array-callback-reference": "off",
"unicorn/no-array-reduce": ["warn", { allowSimpleOperations: true }],
"unicorn/no-empty-file": "off",
"unicorn/no-new-array": "off",
"unicorn/no-null": "off",
"unicorn/no-thenable": "off",
"unicorn/no-unreadable-array-destructuring": "off",
"unicorn/no-useless-fallback-in-spread": "warn",
"unicorn/no-useless-spread": "warn",
"unicorn/no-useless-undefined": "off",
"unicorn/no-await-expression-member": "off",
"unicorn/prefer-switch": ["error", { minimumCases: 5 }],
"unicorn/prefer-top-level-await": "off",
"unicorn/prevent-abbreviations": [
"warn",
{
extendDefaultReplacements: false,
replacements: {
def: {
defer: true,
deferred: true,
define: true,
definition: true,
},
dir: { direction: true, directory: true },
docs: { documentation: true, documents: true },
dst: {
daylightSavingTime: true,
destination: true,
distribution: true,
},
e: { error: true, event: true, end: true },
rel: { related: true, relationship: true, relative: true },
res: { response: true, result: true },
},
allowList: { e2e: true },
},
],
"unicorn/switch-case-braces": ["error", "avoid"],
// Must be last: it disables rules that disagree with prettier including some plugins' rules
...rulesFromConfig(eslintConfigPrettier),
},
settings: {
react: { pragma: "React", version: "detect" },
},
},
{
files: ["**/*.d.{ts,tsx,mts}"],
rules: {
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/no-empty-interface": "off",
},
},
{
files: ["**/*-spec.{js,ts,tsx}", "**/*.spec.{js,ts,tsx}", "**/tests/**/*.{js,ts,tsx}"],
languageOptions: {
ecmaVersion: 2023,
globals: { ...globals.jest, ...globals.browser },
},
rules: {
"@typescript-eslint/await-thenable": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-return": "off",
},
},
{
// Temporary. Remove this when all files are converted to TypeScript.
files: ["src/**/*.js"],
languageOptions: {
ecmaVersion: 2023,
globals: { ...globals.browser },
},
},
];
export default config;
// Some ESLint configs we use try to configure other linter settings besides the rules.
// This function converts them to a single rules record, so we can manage those settings ourselves.
/**
* @param { import("eslint").Linter.Config | import("eslint").Linter.Config[] } config_
* @param { (entry: import("eslint").Linter.RuleEntry, rule: string) => import("eslint").Linter.RuleEntry } [transformEntry]
* @returns { Readonly<import("eslint").Linter.RulesRecord> }
* */
function rulesFromConfig(config_, transformEntry = (entry) => entry) {
const config = Array.isArray(config_) ? config_ : [config_];
/** @type { import("eslint").Linter.RulesRecord } */
let rulesRecord = {};
for (const obj of config) {
if ("rules" in obj && obj.rules) {
for (const [rule, entry] of Object.entries(obj.rules)) {
if (entry) {
rulesRecord[rule] = transformEntry(entry, rule);
}
}
}
}
return rulesRecord;
}
// This reads our .gitignore and converts into ignore patterns that ESLint can use.
// Derived from: https://github.com/mysticatea/eslint-gitignore/blob/master/lib/index.ts
/**
* @returns { string[] }
* */
function getIgnoresFromGitignore() {
const filePath = path.join(__dirname, ".gitignore");
const dirPath = escapePath(
`/${path.relative(process.cwd(), path.dirname(filePath)).split(path.sep).join("/")}`
);
return fs
.readFileSync(filePath, "utf8")
.split("\n")
.map((line) => line.trim())
.filter((trimmedLine) => trimmedLine && !trimmedLine.startsWith("#"))
.map((pattern) => {
if (dirPath === "/") {
return pattern;
} else {
const isNegative = pattern.startsWith("!");
const body = isNegative ? pattern.slice(1) : pattern;
const prefix = body.startsWith("/") ? dirPath : `${dirPath}/**/`;
const converted = (isNegative ? "!" : "") + prefix + body;
return converted;
}
});
}