-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path.eslintrc.cjs
More file actions
342 lines (324 loc) · 9.41 KB
/
.eslintrc.cjs
File metadata and controls
342 lines (324 loc) · 9.41 KB
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
const path = require('node:path');
const MAX_LINE_LENGTH = 120;
/**
* ESLint rules.
*
* @see {@link https://eslint.org/docs/v8.x/rules/}
*/
const ESLINT_RULES = {
indent: 'off',
'no-bitwise': 'off',
'no-new': 'off',
'no-continue': 'off',
'arrow-body-style': 'off',
'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
'no-constant-condition': ['error', { checkLoops: false }],
'max-len': [
'error',
{
code: MAX_LINE_LENGTH,
comments: MAX_LINE_LENGTH,
tabWidth: 4,
ignoreUrls: true,
ignoreTrailingComments: false,
ignoreComments: false,
/**
* Ignore calls to logger, e.g. logger.error(), because of the long string.
*/
ignorePattern: 'logger\\.',
},
],
// Sort members of import statements, e.g. `import { B, A } from 'module';` -> `import { A, B } from 'module';`
// Note: imports themself are sorted by import/order rule
'sort-imports': ['error', {
ignoreCase: true,
// Avoid conflict with import/order rule
ignoreDeclarationSort: true,
ignoreMemberSort: false,
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
}],
};
/**
* TypeScript ESLint rules.
*
* @see {@link https://typescript-eslint.io/rules/}
*/
const TYPESCRIPT_ESLINT_RULES = {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/member-delimiter-style': 'error',
'@typescript-eslint/indent': ['error', 4],
'@typescript-eslint/explicit-member-accessibility': [
'error',
{
accessibility: 'explicit',
overrides: {
accessors: 'explicit',
constructors: 'no-public',
methods: 'explicit',
properties: 'off',
parameterProperties: 'explicit',
},
},
],
// Force proper import and export of types
'@typescript-eslint/consistent-type-imports': [
'error',
{
prefer: 'type-imports',
fixStyle: 'inline-type-imports',
},
],
'@typescript-eslint/consistent-type-exports': [
'error',
{
fixMixedExportsWithInlineTypeSpecifier: true,
},
],
};
/**
* Import plugin rules.
*
* @see {@link https://github.com/import-js/eslint-plugin-import/tree/main/docs/rules}
*/
const IMPORT_PLUGIN_RULES = {
'import/prefer-default-export': 'off',
'import-newlines/enforce': ['error', 3, MAX_LINE_LENGTH],
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
// Split external and internal imports with an empty line
'import/order': [
'error',
{
groups: [
// Built-in Node.js modules
'builtin',
// External packages
'external',
// Parent modules, e.g. `import { foo } from '../bar';`
'parent',
// Sibling modules, e.g. `import { foo } from './bar';`
'sibling',
// All other imports
],
alphabetize: { order: 'asc', caseInsensitive: true },
'newlines-between': 'always',
},
],
};
/**
* JSDoc plugin rules.
*
* @see {@link https://github.com/gajus/eslint-plugin-jsdoc?tab=readme-ov-file#user-content-eslint-plugin-jsdoc-rules}
*/
const JSDOC_PLUGIN_RULES = {
// Types are described in TypeScript
'jsdoc/require-param-type': 'off',
'jsdoc/no-undefined-types': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/require-param-description': 'error',
'jsdoc/require-property-description': 'error',
'jsdoc/require-returns-description': 'error',
'jsdoc/require-returns': 'error',
'jsdoc/require-param': 'error',
'jsdoc/require-returns-check': 'error',
'jsdoc/check-tag-names': [
'warn',
{
// Define additional tags
// https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-tag-names.md#definedtags
definedTags: ['note'],
},
],
'jsdoc/require-hyphen-before-param-description': ['error', 'never'],
'jsdoc/require-jsdoc': [
'error',
{
contexts: [
'ClassDeclaration',
'ClassProperty',
'PropertyDefinition',
'FunctionDeclaration',
'MethodDefinition',
],
},
],
'jsdoc/require-description': [
'error',
{
contexts: [
'ClassDeclaration',
'ClassProperty',
'PropertyDefinition',
'FunctionDeclaration',
'MethodDefinition',
],
},
],
'jsdoc/require-description-complete-sentence': [
'error',
{
abbreviations: [
'e.g.',
'i.e.',
],
},
],
'jsdoc/multiline-blocks': [
'error',
{
noSingleLineBlocks: true,
singleLineTags: [
'inheritdoc',
],
},
],
'jsdoc/tag-lines': [
'error',
'any',
{
startLines: 1,
},
],
'jsdoc/sort-tags': [
'error',
{
linesBetween: 1,
tagSequence: [
{ tags: ['file'] },
{ tags: ['template'] },
{ tags: ['see'] },
{ tags: ['param'] },
{ tags: ['returns'] },
{ tags: ['throws'] },
{ tags: ['example'] },
],
},
],
};
/**
* N plugin rules.
*
* @see {@link https://github.com/eslint-community/eslint-plugin-n?tab=readme-ov-file#-rules}
*/
const N_PLUGIN_RULES = {
// Import plugin is enough, also, this rule requires extensions in ESM, but we use bundler resolution
'n/no-missing-import': 'off',
// Require using node protocol for node modules, e.g. `node:fs` instead of `fs`.
'n/prefer-node-protocol': 'error',
// Prefer `/promises` API for `fs` and `dns` modules, if the corresponding imports are used.
'n/prefer-promises/fs': 'error',
'n/prefer-promises/dns': 'error',
'n/hashbang': [
'error',
{
// This rule reads the bin property from package.json, and only allows shebangs for that file.
// But since we transform the source files to the dist folder, we need to convert the paths.
convertPath: [
{
include: ['src/**/*.ts'],
replace: ['^src/(.+)\\.ts$', 'dist/$1.js'],
},
],
},
],
};
/**
* Boundaries plugin rules.
*
* @see {@link https://github.com/javierbrea/eslint-plugin-boundaries#readme}
*/
const BOUNDARIES_PLUGIN_RULES = {
'boundaries/element-types': ['error', {
default: 'allow',
rules: [
// Do not allow too general imports in tests, e.g. import something from `src` directly.
{
from: 'test-folder',
disallow: ['src-index'],
message: 'Do not import directly from src/. Use specific submodules like src/utils instead.',
},
// TODO: Add more rules, like helpers only can import helpers, etc.
],
}],
};
/**
* Logger context plugin rules.
*
* @see {@link https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/eslint-plugin-logger-context#readme}
*/
const LOGGER_CONTEXT_PLUGIN_RULES = {
// Check that every logger call has a context tag.
'@adguard/logger-context/require-logger-context': ['error', {
contextModuleName: 'tsurl',
}],
};
/**
* Merges multiple rule sets into a single object.
*
* @param ruleSets The rule sets to merge.
*
* @returns The merged rule set.
*/
function mergeRules(...ruleSets) {
const merged = {};
for (const rules of ruleSets) {
for (const [key, value] of Object.entries(rules)) {
if (merged[key]) {
throw new Error(`Duplicate ESLint rule: ${key}`);
}
merged[key] = value;
}
}
return merged;
}
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: path.join(__dirname),
project: 'tsconfig.json',
},
plugins: [
'import',
'import-newlines',
'@adguard/logger-context',
'@typescript-eslint',
'n',
'boundaries',
],
extends: [
'airbnb-base',
'airbnb-typescript/base',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:jsdoc/recommended',
'plugin:jsdoc/recommended-typescript',
'plugin:n/recommended',
],
ignorePatterns: [
'dist',
'coverage',
],
settings: {
'boundaries/elements': [
{
type: 'src-index',
pattern: 'src/index.ts',
mode: 'file',
},
{
type: 'test-folder',
pattern: 'test',
mode: 'folder',
},
],
},
rules: mergeRules(
ESLINT_RULES,
TYPESCRIPT_ESLINT_RULES,
IMPORT_PLUGIN_RULES,
JSDOC_PLUGIN_RULES,
N_PLUGIN_RULES,
BOUNDARIES_PLUGIN_RULES,
LOGGER_CONTEXT_PLUGIN_RULES,
),
};