-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.config.mjs
More file actions
503 lines (462 loc) · 16.9 KB
/
Copy patheslint.config.mjs
File metadata and controls
503 lines (462 loc) · 16.9 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* @fileoverview ESLint Flat Configuration for figma-connecter
*
* A comprehensive ESLint configuration optimized for TypeScript CLI tool development
* with strict code quality, documentation, and maintainability rules.
*
* @version 2.0.0
* @license MIT
* @see https://eslint.org/docs/latest/use/configure/configuration-files-new
*/
// ============================================================================
// IMPORTS
// ============================================================================
import eslint from "@eslint/js";
import zeroTolerancePlugin from "@coderrob/eslint-plugin-zero-tolerance";
import tseslint from "typescript-eslint";
import importPlugin from "eslint-plugin-import";
import jsdocPlugin from "eslint-plugin-jsdoc";
import globals from "globals";
// ============================================================================
// CONFIGURATION CONSTANTS
// ============================================================================
/**
* TypeScript project configuration path.
*/
const TS_PROJECT_PATH = "./tsconfig.json";
/**
* Glob patterns matching all TypeScript file extensions.
*/
const TS_FILE_PATTERNS = ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"];
/**
* Glob patterns for source files (excluding tests).
*/
const SOURCE_FILE_GLOBS = ["src/**/*.ts", "src/**/*.tsx", "bin/**/*.ts"];
/**
* Glob patterns for test and mock files.
*/
const TEST_FILE_GLOBS = [
"**/__tests__/**/*.ts",
"**/__mocks__/**/*.ts",
"**/*.test.ts",
"**/*.spec.ts",
];
/**
* Complexity thresholds for maintainable code.
* Set to reasonable limits for a CLI tool with AST parsing logic.
*/
const COMPLEXITY_LIMITS = {
cyclomatic: 15,
maxDepth: 5,
maxNestedCallbacks: 4,
maxParams: 4,
};
/**
* Enhanced complexity for parser/emitter files (complex AST traversal).
*/
const PARSER_COMPLEXITY_LIMITS = {
cyclomatic: 25,
maxDepth: 6,
};
// ============================================================================
// SHARED RULE CONFIGURATIONS
// ============================================================================
/**
* TypeScript unused variables configuration.
* Allows underscore-prefixed variables to be intentionally unused.
*/
const UNUSED_VARS_CONFIG = {
argsIgnorePattern: "^_",
caughtErrors: "all",
caughtErrorsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
ignoreRestSiblings: true,
varsIgnorePattern: "^_",
};
/**
* Restricted syntax patterns to enforce explicit typing and clean imports.
*/
const RESTRICTED_SYNTAX_PATTERNS = [
{
message:
"Do not use ReturnType; prefer explicit function return types or exported interfaces.",
selector: 'TSTypeReference[typeName.name="ReturnType"]',
},
{
message:
"Do not use indexed access types (e.g., Interface['property']); define explicit types or interfaces instead.",
selector:
'TSIndexedAccessType[objectType.type="TSTypeReference"][objectType.typeName.type="Identifier"]:not([objectType.typeName.name=/^(Parameters|ReturnType|Awaited|ConstructorParameters|InstanceType|ThisParameterType|OmitThisParameter)$/])',
},
{
message:
"Do not re-export from parent directories (../). Re-exports should only reference sibling or child modules to prevent circular dependencies.",
selector: "ExportAllDeclaration[source.value=/^[.][.]/]",
},
{
message:
"Do not re-export from parent directories (../). Re-exports should only reference sibling or child modules to prevent circular dependencies.",
selector: "ExportNamedDeclaration[source.value=/^[.][.]/]",
},
{
message: "Do not use unions of literal types. Use an enum instead.",
selector:
'TSTypeAliasDeclaration[typeAnnotation.type="TSUnionType"] > TSUnionType > TSLiteralType',
},
{
selector: "ForInStatement",
message:
"for..in loops iterate over the entire prototype chain. Use Object.{keys,values,entries} instead.",
},
{
selector: "LabeledStatement",
message:
"Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.",
},
{
selector: "WithStatement",
message:
"`with` is disallowed in strict mode because it makes code impossible to predict and optimize.",
},
];
// ============================================================================
// RULE DEFINITIONS BY CATEGORY
// ============================================================================
/**
* Core TypeScript rules for type safety and code quality.
*/
const typescriptRules = {
// Type Safety
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-unused-vars": ["error", UNUSED_VARS_CONFIG],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-use-before-define": [
"error",
{
allowNamedExports: true,
classes: true,
functions: false,
variables: true,
},
],
// Disable base rule in favor of TypeScript version
"no-duplicate-imports": "off",
"no-undef": "off", // TypeScript handles this
"no-use-before-define": "off",
};
/**
* Code complexity and maintainability rules.
*/
const complexityRules = {
complexity: ["warn", { max: COMPLEXITY_LIMITS.cyclomatic }],
"max-depth": ["warn", { max: COMPLEXITY_LIMITS.maxDepth }],
"max-len": [
"error",
{ code: 120, ignorePattern: String.raw`^\s*(//|/\*|\*)` },
],
"max-nested-callbacks": [
"error",
{ max: COMPLEXITY_LIMITS.maxNestedCallbacks },
],
"max-params": ["error", { max: COMPLEXITY_LIMITS.maxParams }],
"no-restricted-syntax": ["error", ...RESTRICTED_SYNTAX_PATTERNS],
};
/**
* JSDoc documentation rules for exported APIs.
*/
const jsdocRules = {
// Required Documentation
"jsdoc/require-jsdoc": [
"error",
{
contexts: [
"FunctionDeclaration",
"FunctionExpression",
"ArrowFunctionExpression",
"MethodDefinition",
],
publicOnly: false,
require: {
ArrowFunctionExpression: true,
ClassDeclaration: true,
FunctionDeclaration: true,
FunctionExpression: true,
MethodDefinition: true,
},
},
],
// Validation Rules
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "off", // Too strict for license headers
"jsdoc/check-line-alignment": ["error", "never"],
"jsdoc/check-param-names": "error",
"jsdoc/check-tag-names": "error",
"jsdoc/no-blank-blocks": "error",
// Required Documentation Components
"jsdoc/require-description": "error",
"jsdoc/require-param": "error",
"jsdoc/require-param-description": "error",
"jsdoc/require-returns": "error",
"jsdoc/require-returns-description": "error",
// Disabled Rules (TypeScript handles types)
"jsdoc/check-types": "off",
"jsdoc/no-blank-block-descriptions": "off",
"jsdoc/no-undefined-types": "off",
"jsdoc/require-param-type": "off",
"jsdoc/require-returns-type": "off",
"jsdoc/tag-lines": "off",
"jsdoc/valid-types": "off",
};
/**
* Import organization and validation rules.
*/
const importRules = {
"import/default": "error",
"import/export": "off", // TypeScript handles duplicate exports
"import/first": "error",
"import/named": "error",
"import/namespace": "error",
"import/newline-after-import": "error",
"import/no-cycle": "error",
"import/no-duplicates": "error",
"import/no-extraneous-dependencies": "off",
"import/no-mutable-exports": "error",
"import/no-unresolved": "off", // TypeScript handles module resolution
"import/order": "off", // Using simple-import-sort instead
"sort-imports": "off", // Using simple-import-sort instead
};
/**
* Code quality and style rules.
*/
const codeQualityRules = {
camelcase: ["error", { ignoreDestructuring: true, properties: "never" }],
curly: ["error", "all"],
eqeqeq: ["error", "always", { null: "ignore" }],
"global-require": "off",
"implicit-arrow-linebreak": "off", // Conflicts with max-len and Prettier
"no-alert": "error",
"no-bitwise": "off", // Allow bitwise ops for TypeScript modifier flags
"no-console": ["warn", { allow: ["warn", "error"] }],
"no-continue": "off", // Allow continue in loops
"no-debugger": "error",
"no-nested-ternary": "off", // Allow for concise conditional expressions
"no-param-reassign": ["error", { props: false }],
"no-var": "error",
"object-shorthand": "error",
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-template": "error",
};
/**
* Zero-tolerance rules (all rules enabled as warnings for gradual adoption).
* Includes all 36 rules from the plugin, not just the recommended preset.
*/
const zeroToleranceRules = {
// Enable ALL plugin rules as warnings (not just recommended)
...zeroTolerancePlugin.rules.strict,
// BDD sidecar files are not used in this repository.
"zero-tolerance/require-bdd-spec": "off",
};
// ============================================================================
// TYPESCRIPT CONFIGURATIONS
// ============================================================================
/**
* Type-checked TypeScript configuration for source files.
*/
const typeCheckedConfigs = tseslint.configs.recommendedTypeChecked.map(
(config) => ({
...config,
files: SOURCE_FILE_GLOBS,
languageOptions: {
...config.languageOptions,
globals: {
...globals.node,
...config.languageOptions?.globals,
},
parserOptions: {
...config.languageOptions?.parserOptions,
ecmaVersion: "latest",
projectService: true,
sourceType: "module",
tsconfigRootDir: import.meta.dirname,
},
},
}),
);
/**
* Basic TypeScript configuration for all TypeScript files (no type checking).
*/
const basicTsConfigs = tseslint.configs.recommended.map((config) => ({
...config,
files: TS_FILE_PATTERNS,
}));
// ============================================================================
// TEST FILE RULE OVERRIDES
// ============================================================================
/**
* Relaxed rules for test and mock files.
*/
const testFileRuleOverrides = {
// Complexity Rules - Disabled
complexity: "off",
"max-depth": "off",
"max-nested-callbacks": "off",
"max-params": "off",
// Documentation Rules - Disabled
"jsdoc/require-description": "off",
"jsdoc/require-jsdoc": "off",
"jsdoc/require-param": "off",
"jsdoc/require-param-description": "off",
"jsdoc/require-returns": "off",
"jsdoc/require-returns-description": "off",
// Type Safety Rules - Relaxed for mocking
"@typescript-eslint/no-explicit-any": "off",
"@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",
"@typescript-eslint/no-unused-vars": ["error", UNUSED_VARS_CONFIG],
// Other Rules - Relaxed
"import/no-extraneous-dependencies": "off",
"no-console": "off",
"no-magic-numbers": "off",
};
// ============================================================================
// IGNORED PATHS
// ============================================================================
/**
* Paths to completely ignore from linting.
*/
const IGNORED_PATHS = [
"**/node_modules/**",
"**/dist/**",
"**/build/**",
"**/coverage/**",
"**/__fixtures__/**",
"**/__tests__/__output__/**",
"**/.cache/**",
"**/tmp/**",
"**/temp/**",
"**/*.min.js",
"**/*.d.ts",
"**/*.js",
"**/*.cjs",
"**/*.mjs",
"!eslint.config.mjs",
];
// ============================================================================
// EXPORTED CONFIGURATION
// ============================================================================
export default [
// ──────────────────────────────────────────────────────────────────────────
// Global Ignores
// ──────────────────────────────────────────────────────────────────────────
{
ignores: IGNORED_PATHS,
},
// ──────────────────────────────────────────────────────────────────────────
// Base Configurations
// ──────────────────────────────────────────────────────────────────────────
eslint.configs.recommended,
...basicTsConfigs,
...typeCheckedConfigs,
// ──────────────────────────────────────────────────────────────────────────
// Source File Configuration
// ──────────────────────────────────────────────────────────────────────────
{
files: SOURCE_FILE_GLOBS,
languageOptions: {
globals: {
...globals.node,
},
parserOptions: {
ecmaVersion: "latest",
projectService: true,
sourceType: "module",
tsconfigRootDir: import.meta.dirname,
},
},
plugins: {
"zero-tolerance": zeroTolerancePlugin,
import: importPlugin,
jsdoc: jsdocPlugin,
},
rules: {
...typescriptRules,
...complexityRules,
...jsdocRules,
...importRules,
...codeQualityRules,
...zeroToleranceRules,
},
settings: {
"import/resolver": {
node: true,
typescript: {
alwaysTryTypes: true,
project: TS_PROJECT_PATH,
},
},
jsdoc: {
mode: "typescript",
},
},
},
// ──────────────────────────────────────────────────────────────────────────
// Parser and Emitter Files Configuration (complex AST traversal)
// ──────────────────────────────────────────────────────────────────────────
{
files: ["src/parsers/**/*.ts", "src/emitters/**/*.ts"],
rules: {
complexity: ["warn", { max: PARSER_COMPLEXITY_LIMITS.cyclomatic }],
"max-depth": ["warn", { max: PARSER_COMPLEXITY_LIMITS.maxDepth }],
},
},
// ──────────────────────────────────────────────────────────────────────────
// CLI Entry Point Configuration
// ──────────────────────────────────────────────────────────────────────────
{
files: ["bin/**/*.ts"],
rules: {
"no-console": "off",
},
},
// ──────────────────────────────────────────────────────────────────────────
// Logger Configuration (console is the primary output mechanism)
// ──────────────────────────────────────────────────────────────────────────
{
files: ["src/core/logger.ts"],
rules: {
"no-console": "off",
},
},
// ──────────────────────────────────────────────────────────────────────────
// Test File Configuration
// ──────────────────────────────────────────────────────────────────────────
{
files: TEST_FILE_GLOBS,
languageOptions: {
globals: {
...globals.node,
...globals.jest,
},
parserOptions: {
ecmaVersion: "latest",
projectService: true,
sourceType: "module",
tsconfigRootDir: import.meta.dirname,
},
},
plugins: {
jsdoc: jsdocPlugin,
},
rules: {
...testFileRuleOverrides,
},
},
];