-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eslintrc.cjs
249 lines (200 loc) · 7.51 KB
/
.eslintrc.cjs
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
/* eslint-env node */
const eslintRules = {
// Require return statements for consistency and typo reduction
"consistent-return": "error",
// Disallow relative imports for better visibility into module coupling
"no-restricted-imports": ["error", { patterns: [".*"] }],
// Require destructuring from arrays and/or objects for brevity
"prefer-destructuring": "error",
// Disable eslint rules in favor of typescript-specific alternatives in @typescript-eslint
"default-param-last": "off",
"no-dupe-class-members": "off",
"no-invalid-this": "off",
"no-loop-func": "off",
"no-shadow": "off",
}
const tsdocRules = {
// Check TSDoc syntax for consistency
"tsdoc/syntax": "error",
}
const simpleInputSortRules = {
// Sort imports for consistency
"simple-import-sort/imports": [
"error",
{
groups: [
// Side effect imports
["^\\u0000"],
// 3rd party packages
[
"^api",
"^codemirror",
"^fs",
"^happy-dom",
"^jest-extended",
"^strong-mock",
"^@ts-belt",
"^type-fest",
"^@vitest",
"^vitest",
],
// 3rd party package extensions
["^@ext"],
// Inter-process communication
["^@cm-extension-ipc", "^@joplin-plugin-ipc"],
// Joplin plugin
["^@joplin-plugin"],
// Content script (Joplin -> CodeMirror)
["^@content-script"],
// CodeMirror extension
["^@cm-extension"],
// Test and test support
["^test"],
// Absolute imports and other imports
["^"],
// Relative imports (anything that starts with a dot)
["^\\."],
],
},
],
}
const typescriptEslintRules = {
// Allow typescript namespaces for module-defined code organization and declaration merging
"@typescript-eslint/no-namespace": "off",
// Disallow `if (value)` checks which match `""`, `0`, and `NaN` to prevent potential bugs
"@typescript-eslint/strict-boolean-expressions": [
"error",
{
allowNullableObject: false,
allowNumber: false,
allowString: false,
},
],
// Disable specification of `public` on members for brevity
"@typescript-eslint/explicit-member-accessibility": [
"error",
{
accessibility: "no-public",
},
],
// Require explicit return and argument types on exported functions and class members
// to define explicit interfaces that can't unintentionally change on refactor
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/explicit-function-return-type": [
"error",
{
allowConciseArrowFunctionExpressionsStartingWithVoid: true,
allowExpressions: true,
},
],
// Define naming conventions for variables, etc.
"@typescript-eslint/naming-convention": [
"error",
// Require strict camel case by default
{
selector: "default",
format: ["strictCamelCase"],
leadingUnderscore: "allow",
trailingUnderscore: "allow",
},
// Allow pascal case for functions which mimic class constructors / factories
{
selector: "function",
format: ["PascalCase", "strictCamelCase"],
},
// Disable for properties in object literals, since these are often library-defined
{
selector: "objectLiteralProperty",
format: null,
},
// Allow properties to have underscores to represent `unused` and `reserved` identifiers
{
selector: "property",
format: ["strictCamelCase"],
leadingUnderscore: "allow",
trailingUnderscore: "allow",
},
// Require types to be pascal case
{
selector: "typeLike",
format: ["PascalCase"],
},
// Allow pascal case for variables that mimic singleton objects and class instances
{
selector: "variable",
format: ["PascalCase", "strictCamelCase"],
leadingUnderscore: "allow",
trailingUnderscore: "allow",
},
],
// Allow unused vars that begin with an underscore
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
// Require default parameters to be last to allow function calls to omit optional tail arguments
"@typescript-eslint/default-param-last": "error",
// Prevent overwriting declarations due to typos
"@typescript-eslint/no-dupe-class-members": "error",
// Prevent use of `this` outside classes and class-like objects where it may be undefined
"@typescript-eslint/no-invalid-this": "error",
// Disallow unsafe closures inside of loops
"@typescript-eslint/no-loop-func": "error",
// Disallow shadowing variables which can make code unintuitive and can lead to unexpected results
"@typescript-eslint/no-shadow": "error",
// Disallow unnecessary qualifiers for brevity
"@typescript-eslint/no-unnecessary-qualifier": "error",
// Require `readonly` on private class members that are never modified for clarity
"@typescript-eslint/prefer-readonly": "error",
// Disallow `Array#sort` without params since it performs an unintuitive lexicographical sort,
// even for numbers (e.g. `[20, 10, 2].sort()` becomes `[10, 2, 20]` instead of `[2, 10, 20]`)
"@typescript-eslint/require-array-sort-compare": "error",
// Require type unions in switch statements to contain a case for every type in the union
// to prevent overlooking necessary changes when adding new types
"@typescript-eslint/switch-exhaustiveness-check": "error",
}
const unicornRules = {
// Disallow mixing destructuring with direct property access of the (same) object for consistency
"unicorn/consistent-destructuring": "error",
// Require function definition at the highest scope possible for performance and readability
"unicorn/consistent-function-scoping": "error",
// Require `new` with built-ins for consistency (e.g. `new RegExp()` instead of `RegExp()`)
"unicorn/new-for-builtins": "error",
// Require the use of `null` in favor of `undefined` for consistency, power, and less confusion
"unicorn/no-null": "error",
// Disallow use of `Array#indexOf` and `Array#some` when `Array#includes` is clearer
"unicorn/prefer-includes": "error",
// Disallow use of ternary operator when a logical operator serves the same purpose
"unicorn/prefer-logical-operator-over-ternary": "error",
// Prefer use of more-efficient and more-powerful DOM APIs
"unicorn/prefer-modern-dom-apis": "error",
// Prefer use of `String#trimStart/End` over `String#trimLeft/Right` since text direction varies
"unicorn/prefer-string-trim-start-end": "error",
// Prefer use of more descriptive and consistent `TypeError` over `Error` for type check failures
"unicorn/prefer-type-error": "error",
// Require explicit specification of separator in `Array#join` for clarity over the default of `,`
"unicorn/require-array-join-separator": "error",
// Require braces in non-empty cases of switch statements for clarity
"unicorn/switch-case-braces": "error",
// Require the use of `new` when throwing `Error`s for consistency
"unicorn/throw-new-error": "error",
}
module.exports = {
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"prettier",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
tsconfigRootDir: __dirname,
},
plugins: ["eslint-plugin-tsdoc", "simple-import-sort", "@typescript-eslint", "unicorn"],
root: true,
rules: {
...eslintRules,
...simpleInputSortRules,
...tsdocRules,
...typescriptEslintRules,
...unicornRules,
},
}