@futdevpro/dynamo-eslint 1.15.25 → 1.15.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.c8rc.json +26 -26
- package/.cursor/rules/__assistant_guide.mdc +30 -30
- package/.cursor/rules/__main.mdc +64 -64
- package/.cursor/rules/_ag_backend-structure.mdc +85 -85
- package/.cursor/rules/_ag_backend.mdc +16 -16
- package/.cursor/rules/_ag_debug.mdc +7 -7
- package/.cursor/rules/_ag_documentation_writing_rules.mdc +372 -372
- package/.cursor/rules/_ag_file-refactoring.mdc +113 -113
- package/.cursor/rules/_ag_fixes_rules.mdc +5 -5
- package/.cursor/rules/_ag_frontend-structure.mdc +86 -86
- package/.cursor/rules/_ag_frontend.mdc +39 -39
- package/.cursor/rules/_ag_import-rules.mdc +44 -44
- package/.cursor/rules/_ag_naming.mdc +115 -115
- package/.cursor/rules/_ag_running_commands.mdc +4 -4
- package/.cursor/rules/_ag_server-controller.mdc +5 -5
- package/.cursor/rules/_ag_should-be.mdc +6 -6
- package/.cursor/rules/_ag_swearing.mdc +47 -47
- package/.cursor/rules/ai_development_guide.md +60 -60
- package/.cursor/rules/ai_directives.md +114 -114
- package/.cursor/rules/cursor-rules.md +160 -160
- package/.cursor/rules/default-command.mdc +464 -464
- package/.cursor/rules/error_code_pattern.md +39 -39
- package/.cursor/rules/saved rule mcp server use.md +15 -15
- package/.dynamo/pipeline.cicd.config.json +184 -184
- package/.dynamo/version-bump.config.json +5 -5
- package/.eslintrc.json +4 -4
- package/.github/workflows/main.yml +436 -436
- package/.husky/pre-commit +2 -2
- package/.vscode/settings.json +11 -11
- package/README.md +319 -319
- package/__documentations/2026-04-18-ccap-review-integration.md +173 -173
- package/__documentations/2026-08-18-bfr-ctm-005-eslint9-validator-report.md +45 -0
- package/build/configs/base.d.ts +9 -9
- package/build/configs/base.js +150 -150
- package/build/configs/fsm.d.ts +1 -1
- package/build/configs/fsm.js +7 -7
- package/build/configs/ngx-package.d.ts +1 -1
- package/build/configs/ngx-package.js +7 -7
- package/build/configs/ngx.d.ts +4 -4
- package/build/configs/ngx.js +91 -91
- package/build/configs/nts-package.d.ts +1 -1
- package/build/configs/nts-package.js +17 -17
- package/build/configs/nts.d.ts +1 -1
- package/build/configs/nts.js +31 -31
- package/build/index.d.ts +1 -1
- package/build/index.js +2 -2
- package/build/plugin/index.d.ts +52 -52
- package/build/plugin/index.js +80 -80
- package/build/plugin/rules/angular/angular-for-track-logic.d.ts +7 -7
- package/build/plugin/rules/angular/angular-for-track-logic.js +81 -81
- package/build/plugin/rules/angular/effect-signal-loop.d.ts +9 -9
- package/build/plugin/rules/angular/effect-signal-loop.js +112 -112
- package/build/plugin/rules/angular/no-async-ngoninit.d.ts +8 -8
- package/build/plugin/rules/angular/no-async-ngoninit.js +42 -42
- package/build/plugin/rules/angular/no-component-direct-api.d.ts +9 -9
- package/build/plugin/rules/angular/no-component-direct-api.js +75 -75
- package/build/plugin/rules/angular/no-method-call-in-template.d.ts +8 -8
- package/build/plugin/rules/angular/no-method-call-in-template.js +79 -79
- package/build/plugin/rules/angular/no-ngfor-directive.d.ts +7 -7
- package/build/plugin/rules/angular/no-ngfor-directive.js +59 -59
- package/build/plugin/rules/angular/no-ngif-directive.d.ts +7 -7
- package/build/plugin/rules/angular/no-ngif-directive.js +59 -59
- package/build/plugin/rules/explicit-types.d.ts +3 -3
- package/build/plugin/rules/explicit-types.js +178 -178
- package/build/plugin/rules/import/import-order.d.ts +3 -3
- package/build/plugin/rules/import/import-order.js +154 -154
- package/build/plugin/rules/import/no-import-type.d.ts +3 -3
- package/build/plugin/rules/import/no-import-type.js +45 -45
- package/build/plugin/rules/import/no-js-import.d.ts +3 -3
- package/build/plugin/rules/import/no-js-import.js +40 -40
- package/build/plugin/rules/naming-patterns.d.ts +7 -7
- package/build/plugin/rules/naming-patterns.js +366 -366
- package/build/plugin/rules/no-getter-logic.d.ts +3 -3
- package/build/plugin/rules/no-getter-logic.js +194 -194
- package/build/plugin/rules/prefer-enum-over-string-union.d.ts +3 -3
- package/build/plugin/rules/prefer-enum-over-string-union.js +289 -289
- package/build/plugin/rules/prefer-interface-over-duplicate-types.d.ts +3 -3
- package/build/plugin/rules/prefer-interface-over-duplicate-types.js +264 -264
- package/build/plugin/rules/require-jsdoc-description.d.ts +3 -3
- package/build/plugin/rules/require-jsdoc-description.js +496 -496
- package/build/plugin/rules/require-test-description-prefix.d.ts +3 -3
- package/build/plugin/rules/require-test-description-prefix.js +135 -135
- package/build/plugin/rules/ts/data-model-extends.d.ts +8 -8
- package/build/plugin/rules/ts/data-model-extends.js +94 -94
- package/build/plugin/rules/ts/no-any-type.d.ts +8 -8
- package/build/plugin/rules/ts/no-any-type.js +36 -36
- package/build/plugin/rules/ts/no-as-cast.d.ts +8 -8
- package/build/plugin/rules/ts/no-as-cast.js +49 -49
- package/build/plugin/rules/ts/no-dynamic-imports.d.ts +8 -8
- package/build/plugin/rules/ts/no-dynamic-imports.js +50 -50
- package/build/plugin/rules/ts/no-native-browser-dialogs.d.ts +8 -8
- package/build/plugin/rules/ts/no-native-browser-dialogs.js +78 -78
- package/build/plugin/rules/ts/no-plain-function-export.d.ts +9 -9
- package/build/plugin/rules/ts/no-plain-function-export.js +69 -69
- package/build/plugin/rules/ts/one-export-per-file.d.ts +8 -8
- package/build/plugin/rules/ts/one-export-per-file.js +73 -73
- package/build/scripts/dynamo-fix.d.ts +26 -26
- package/build/scripts/dynamo-fix.js +146 -146
- package/build/scripts/eslintrc-audit.d.ts +26 -26
- package/build/scripts/eslintrc-audit.js +100 -100
- package/build/scripts/fix-return-types.d.ts +74 -74
- package/build/scripts/fix-return-types.js +226 -226
- package/build/scripts/validate-imports.d.ts +26 -26
- package/build/scripts/validate-imports.js +157 -146
- package/build/scripts/validate-imports.js.map +1 -1
- package/build/scripts/validate-naming.d.ts +26 -26
- package/build/scripts/validate-naming.js +157 -131
- package/build/scripts/validate-naming.js.map +1 -1
- package/eslint.config.js +15 -15
- package/package.json +105 -105
- package/samples/.vscode/settings.json +13 -13
- package/samples/base/.eslintrc.json +6 -6
- package/samples/base/eslint.config.js +3 -3
- package/samples/fsm/.eslintrc.json +4 -4
- package/samples/fsm/eslint.config.js +3 -3
- package/samples/ngx/.eslintrc.json +6 -6
- package/samples/ngx/eslint.config.js +3 -3
- package/samples/ngx-package/.eslintrc.json +4 -4
- package/samples/ngx-package/eslint.config.js +3 -3
- package/samples/nts/.eslintrc.json +6 -6
- package/samples/nts/eslint.config.js +3 -3
- package/samples/nts-package/.eslintrc.json +4 -4
- package/samples/nts-package/eslint.config.js +3 -3
- package/samples/package.json.example +26 -26
- package/samples/poc-sample.ts +38 -38
- package/samples/poc-violations.ts +54 -54
- package/scripts/run-coverage-tests.js +24 -24
- package/spec/support/helpers/ts-node-helper.js +9 -9
- package/spec/support/jasmine.coverage.json +23 -23
- package/spec/support/jasmine.json +24 -24
- package/src/configs/base.ts +157 -157
- package/src/configs/fsm.ts +10 -10
- package/src/configs/ngx-package.ts +10 -10
- package/src/configs/ngx.ts +96 -96
- package/src/configs/nts-package.ts +20 -20
- package/src/configs/nts.ts +34 -34
- package/src/index.ts +4 -4
- package/src/plugin/index.ts +86 -86
- package/src/plugin/rules/angular/angular-for-track-logic.spec.ts +44 -44
- package/src/plugin/rules/angular/angular-for-track-logic.ts +88 -88
- package/src/plugin/rules/angular/effect-signal-loop.spec.ts +89 -89
- package/src/plugin/rules/angular/effect-signal-loop.ts +125 -125
- package/src/plugin/rules/angular/no-async-ngoninit.spec.ts +50 -50
- package/src/plugin/rules/angular/no-async-ngoninit.ts +43 -43
- package/src/plugin/rules/angular/no-component-direct-api.spec.ts +78 -78
- package/src/plugin/rules/angular/no-component-direct-api.ts +82 -82
- package/src/plugin/rules/angular/no-method-call-in-template.spec.ts +63 -63
- package/src/plugin/rules/angular/no-method-call-in-template.ts +89 -89
- package/src/plugin/rules/angular/no-ngfor-directive.spec.ts +44 -44
- package/src/plugin/rules/angular/no-ngfor-directive.ts +62 -62
- package/src/plugin/rules/angular/no-ngif-directive.spec.ts +44 -44
- package/src/plugin/rules/angular/no-ngif-directive.ts +62 -62
- package/src/plugin/rules/explicit-types.spec.ts +190 -190
- package/src/plugin/rules/explicit-types.ts +186 -186
- package/src/plugin/rules/import/import-order.spec.ts +219 -219
- package/src/plugin/rules/import/import-order.ts +197 -197
- package/src/plugin/rules/import/no-import-type.spec.ts +69 -69
- package/src/plugin/rules/import/no-import-type.ts +46 -46
- package/src/plugin/rules/import/no-js-import.spec.ts +82 -82
- package/src/plugin/rules/import/no-js-import.ts +45 -45
- package/src/plugin/rules/naming-patterns.spec.ts +189 -189
- package/src/plugin/rules/naming-patterns.ts +426 -426
- package/src/plugin/rules/no-getter-logic.spec.ts +591 -591
- package/src/plugin/rules/no-getter-logic.ts +220 -220
- package/src/plugin/rules/prefer-enum-over-string-union.spec.ts +583 -583
- package/src/plugin/rules/prefer-enum-over-string-union.ts +333 -333
- package/src/plugin/rules/prefer-interface-over-duplicate-types.spec.ts +404 -404
- package/src/plugin/rules/prefer-interface-over-duplicate-types.ts +314 -314
- package/src/plugin/rules/require-jsdoc-description.spec.ts +779 -779
- package/src/plugin/rules/require-jsdoc-description.ts +572 -572
- package/src/plugin/rules/require-test-description-prefix.spec.ts +459 -459
- package/src/plugin/rules/require-test-description-prefix.ts +154 -154
- package/src/plugin/rules/ts/data-model-extends.spec.ts +84 -84
- package/src/plugin/rules/ts/data-model-extends.ts +100 -100
- package/src/plugin/rules/ts/no-any-type.spec.ts +28 -28
- package/src/plugin/rules/ts/no-any-type.ts +36 -36
- package/src/plugin/rules/ts/no-as-cast.spec.ts +51 -51
- package/src/plugin/rules/ts/no-as-cast.ts +51 -51
- package/src/plugin/rules/ts/no-dynamic-imports.spec.ts +54 -54
- package/src/plugin/rules/ts/no-dynamic-imports.ts +49 -49
- package/src/plugin/rules/ts/no-native-browser-dialogs.spec.ts +54 -54
- package/src/plugin/rules/ts/no-native-browser-dialogs.ts +84 -84
- package/src/plugin/rules/ts/no-plain-function-export.spec.ts +72 -72
- package/src/plugin/rules/ts/no-plain-function-export.ts +73 -73
- package/src/plugin/rules/ts/one-export-per-file.spec.ts +64 -64
- package/src/plugin/rules/ts/one-export-per-file.ts +77 -77
- package/src/scripts/dynamo-fix.ts +172 -172
- package/src/scripts/eslintrc-audit.ts +116 -116
- package/src/scripts/fix-return-types.spec.ts +65 -65
- package/src/scripts/fix-return-types.ts +288 -288
- package/src/scripts/validate-imports.ts +278 -261
- package/src/scripts/validate-naming.ts +204 -187
- package/src/scripts/validator-cli.integration.spec.ts +96 -0
- package/tsconfig.json +31 -31
|
@@ -1,314 +1,314 @@
|
|
|
1
|
-
import { Rule } from 'eslint';
|
|
2
|
-
|
|
3
|
-
interface TypeUsage {
|
|
4
|
-
node: any;
|
|
5
|
-
signature: string;
|
|
6
|
-
interfaceName?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
interface RuleOptions {
|
|
10
|
-
threshold?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const rule: Rule.RuleModule = {
|
|
14
|
-
meta: {
|
|
15
|
-
type: 'suggestion',
|
|
16
|
-
docs: {
|
|
17
|
-
description: 'Suggest creating interfaces for object types used more than the threshold number of times',
|
|
18
|
-
recommended: true,
|
|
19
|
-
},
|
|
20
|
-
schema: [
|
|
21
|
-
{
|
|
22
|
-
type: 'object',
|
|
23
|
-
properties: {
|
|
24
|
-
threshold: {
|
|
25
|
-
type: 'number',
|
|
26
|
-
minimum: 2,
|
|
27
|
-
default: 3,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
additionalProperties: false,
|
|
31
|
-
},
|
|
32
|
-
],
|
|
33
|
-
messages: {
|
|
34
|
-
preferInterface: 'Object type used {{count}} times. Consider creating interface "{{interfaceName}}"}}',
|
|
35
|
-
},
|
|
36
|
-
// BFR-ADVENTOR-009 (2. lépcső): a fixer forrás-szöveg-megőrzőre átírva (`sourceCode.getText` per property)
|
|
37
|
-
// → a komplex/union/qualified/function típusok EXAKT megőrződnek, a generált interface ÉRVÉNYES TS. Ezért
|
|
38
|
-
// az auto-fix (`fixable:'code'`) VISSZA — a bulk `eslint --fix` biztonságosan futtathatja (nem generál
|
|
39
|
-
// többé `reference`/`complex` literál-hibát). (1. lépcső: suggestion-degrade, most feloldva.)
|
|
40
|
-
fixable: 'code',
|
|
41
|
-
},
|
|
42
|
-
create(context) {
|
|
43
|
-
const options: RuleOptions = context.options[0] || {};
|
|
44
|
-
const threshold = options.threshold || 3;
|
|
45
|
-
const sourceCode = context.sourceCode;
|
|
46
|
-
|
|
47
|
-
// Map to track type signatures and their usages
|
|
48
|
-
const typeUsages = new Map<string, TypeUsage[]>();
|
|
49
|
-
const allTypeLiterals: any[] = [];
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Normalize object type to a signature string for comparison
|
|
53
|
-
*/
|
|
54
|
-
function normalizeObjectType(node: any): string {
|
|
55
|
-
if (!node || node.type !== 'TSTypeLiteral') {
|
|
56
|
-
return '';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!node.members || !Array.isArray(node.members)) {
|
|
60
|
-
return '';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Extract and sort properties
|
|
64
|
-
const properties = node.members
|
|
65
|
-
.filter((member: any) => member.type === 'TSPropertySignature')
|
|
66
|
-
.map((member: any) => {
|
|
67
|
-
const key = member.key?.name || member.key?.value || 'unknown';
|
|
68
|
-
const type = getTypeString(member.typeAnnotation);
|
|
69
|
-
const optional = member.optional ? '?' : '';
|
|
70
|
-
const readonly = member.readonly ? 'readonly ' : '';
|
|
71
|
-
|
|
72
|
-
return `${readonly}${key}${optional}:${type}`;
|
|
73
|
-
})
|
|
74
|
-
.sort(); // Sort alphabetically
|
|
75
|
-
|
|
76
|
-
return properties.join(';');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Get string representation of a type annotation
|
|
81
|
-
*/
|
|
82
|
-
function getTypeString(typeAnnotation: any): string {
|
|
83
|
-
if (!typeAnnotation || !typeAnnotation.typeAnnotation) {
|
|
84
|
-
return 'any';
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const type = typeAnnotation.typeAnnotation;
|
|
88
|
-
|
|
89
|
-
switch (type.type) {
|
|
90
|
-
case 'TSStringKeyword':
|
|
91
|
-
return 'string';
|
|
92
|
-
case 'TSNumberKeyword':
|
|
93
|
-
return 'number';
|
|
94
|
-
case 'TSBooleanKeyword':
|
|
95
|
-
return 'boolean';
|
|
96
|
-
case 'TSVoidKeyword':
|
|
97
|
-
return 'void';
|
|
98
|
-
case 'TSNullKeyword':
|
|
99
|
-
return 'null';
|
|
100
|
-
case 'TSUndefinedKeyword':
|
|
101
|
-
return 'undefined';
|
|
102
|
-
case 'TSAnyKeyword':
|
|
103
|
-
return 'any';
|
|
104
|
-
case 'TSUnknownKeyword':
|
|
105
|
-
return 'unknown';
|
|
106
|
-
case 'TSNeverKeyword':
|
|
107
|
-
return 'never';
|
|
108
|
-
case 'TSObjectKeyword':
|
|
109
|
-
return 'object';
|
|
110
|
-
case 'TSLiteralType':
|
|
111
|
-
return type.literal?.value?.toString() || 'literal';
|
|
112
|
-
case 'TSArrayType':
|
|
113
|
-
return `${getTypeString({ typeAnnotation: type.elementType })}[]`;
|
|
114
|
-
case 'TSTypeReference':
|
|
115
|
-
return type.typeName?.name || 'reference';
|
|
116
|
-
case 'TSTypeLiteral':
|
|
117
|
-
return normalizeObjectType(type);
|
|
118
|
-
case 'TSUnionType':
|
|
119
|
-
return type.types?.map((t: any) => getTypeString({ typeAnnotation: t })).join('|') || 'union';
|
|
120
|
-
case 'TSIntersectionType':
|
|
121
|
-
return type.types?.map((t: any) => getTypeString({ typeAnnotation: t })).join('&') || 'intersection';
|
|
122
|
-
|
|
123
|
-
default:
|
|
124
|
-
return 'complex';
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Generate interface name based on context
|
|
130
|
-
*/
|
|
131
|
-
function generateInterfaceName(signature: string, usageCount: number): string {
|
|
132
|
-
// Try to extract meaningful name from signature
|
|
133
|
-
const properties = signature.split(';');
|
|
134
|
-
|
|
135
|
-
if (properties.length === 1) {
|
|
136
|
-
const prop = properties[0].split(':')[0].replace('readonly ', '').replace('?', '');
|
|
137
|
-
|
|
138
|
-
if (prop && prop !== 'unknown') {
|
|
139
|
-
return `${prop.charAt(0).toUpperCase() + prop.slice(1)}Interface`;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Fallback to generic name
|
|
144
|
-
return `GeneratedInterface${usageCount}`;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Check if interface name conflicts with existing identifiers
|
|
149
|
-
*/
|
|
150
|
-
function isNameConflict(name: string): boolean {
|
|
151
|
-
try {
|
|
152
|
-
const scope = context.sourceCode.getScope(context.sourceCode.ast);
|
|
153
|
-
|
|
154
|
-
return scope.variables.some(variable => variable.name === name);
|
|
155
|
-
} catch {
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Generate unique interface name
|
|
162
|
-
*/
|
|
163
|
-
function generateUniqueInterfaceName(baseName: string): string {
|
|
164
|
-
let name = baseName;
|
|
165
|
-
let counter = 1;
|
|
166
|
-
|
|
167
|
-
while (isNameConflict(name)) {
|
|
168
|
-
name = `${baseName}${counter}`;
|
|
169
|
-
counter++;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return name;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* BFR-ADVENTOR-009 (2. lépcső): az interface-törzs a FORRÁS-SZÖVEGBŐL épül (sourceCode.getText per
|
|
177
|
-
* member), NEM a lossy normalizált signature-ből → a komplex/union/qualified/function típusok EXAKT
|
|
178
|
-
* megőrződnek (a korábbi `reference`/`complex`/`union` literál-hibák megszűnnek). A `getText` az eredeti
|
|
179
|
-
* property-szöveget adja (pl. `onError: (e: Error) => void`); a záró `;`/`,`-t normalizáljuk.
|
|
180
|
-
*/
|
|
181
|
-
function buildInterfaceBody(node: any): string {
|
|
182
|
-
const members: any[] = Array.isArray(node?.members) ? node.members : [];
|
|
183
|
-
|
|
184
|
-
return members
|
|
185
|
-
.map((member: any): string => {
|
|
186
|
-
const text: string = sourceCode.getText(member).trim().replace(/[;,]\s*$/, '');
|
|
187
|
-
return ` ${text};`;
|
|
188
|
-
})
|
|
189
|
-
.join('\n');
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Beszédes interface-név a property-kulcsokból (BFR-ADVENTOR-009): a nem-beszédes, ütköző
|
|
194
|
-
* `GeneratedInterfaceN` helyett az első 1-3 kulcsból PascalCase + `Params_Interface` (a projekt-konvenció).
|
|
195
|
-
*/
|
|
196
|
-
function deriveInterfaceName(node: any): string {
|
|
197
|
-
const members: any[] = Array.isArray(node?.members) ? node.members : [];
|
|
198
|
-
const keys: string[] = members
|
|
199
|
-
.filter((m: any): boolean => m.type === 'TSPropertySignature')
|
|
200
|
-
.map((m: any): string => String(m.key?.name ?? m.key?.value ?? ''))
|
|
201
|
-
.filter((k: string): boolean => !!k)
|
|
202
|
-
.slice(0, 3);
|
|
203
|
-
|
|
204
|
-
if (!keys.length) {
|
|
205
|
-
return 'Generated_Interface';
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const pascal: string = keys
|
|
209
|
-
.map((k: string): string => k.charAt(0).toUpperCase() + k.slice(1))
|
|
210
|
-
.join('');
|
|
211
|
-
|
|
212
|
-
return `${pascal}_Interface`;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return {
|
|
216
|
-
TSTypeLiteral(node: any, _context?: any, _sourceCode?: any): void {
|
|
217
|
-
try {
|
|
218
|
-
const signature = normalizeObjectType(node);
|
|
219
|
-
|
|
220
|
-
if (!signature) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
allTypeLiterals.push(node);
|
|
225
|
-
|
|
226
|
-
if (!typeUsages.has(signature)) {
|
|
227
|
-
typeUsages.set(signature, []);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
typeUsages.get(signature)!.push({
|
|
231
|
-
node,
|
|
232
|
-
signature,
|
|
233
|
-
});
|
|
234
|
-
} catch (error) {
|
|
235
|
-
console.error('[prefer-interface-over-duplicate-types] Error in TSTypeLiteral visitor:', error);
|
|
236
|
-
}
|
|
237
|
-
},
|
|
238
|
-
|
|
239
|
-
'Program:exit'(node: any, _context?: any, _sourceCode?: any): void {
|
|
240
|
-
try {
|
|
241
|
-
// Process all collected type usages
|
|
242
|
-
for (const [ signature, usages ] of typeUsages) {
|
|
243
|
-
if (usages.length >= threshold) {
|
|
244
|
-
// beszédes név a property-kulcsokból (ütközés-mentesre pontosítva)
|
|
245
|
-
const interfaceName = generateUniqueInterfaceName(
|
|
246
|
-
deriveInterfaceName(usages[0].node)
|
|
247
|
-
);
|
|
248
|
-
|
|
249
|
-
// a törzs a FORRÁS-SZÖVEGBŐL (getText) — a komplex/union/qualified típusok EXAKT megőrződnek
|
|
250
|
-
const interfaceBody: string = buildInterfaceBody(usages[0].node);
|
|
251
|
-
|
|
252
|
-
// Report on each usage
|
|
253
|
-
for (const usage of usages) {
|
|
254
|
-
context.report({
|
|
255
|
-
node: usage.node,
|
|
256
|
-
messageId: 'preferInterface',
|
|
257
|
-
data: {
|
|
258
|
-
count: usages.length.toString(),
|
|
259
|
-
interfaceName,
|
|
260
|
-
},
|
|
261
|
-
fix(fixer) {
|
|
262
|
-
try {
|
|
263
|
-
const interfaceDeclaration = `interface ${interfaceName} {\n${interfaceBody}\n}`;
|
|
264
|
-
|
|
265
|
-
// Find insertion point (after imports, before other code)
|
|
266
|
-
const firstNode = sourceCode.ast.body[0];
|
|
267
|
-
let insertPosition = 0;
|
|
268
|
-
|
|
269
|
-
if (firstNode && firstNode.type === 'ImportDeclaration') {
|
|
270
|
-
// Find last import
|
|
271
|
-
let lastImport = firstNode;
|
|
272
|
-
|
|
273
|
-
for (const node of sourceCode.ast.body) {
|
|
274
|
-
if (node.type === 'ImportDeclaration') {
|
|
275
|
-
lastImport = node;
|
|
276
|
-
} else {
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
insertPosition = lastImport.range![1];
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Add interface declaration
|
|
284
|
-
const fixes = [
|
|
285
|
-
fixer.insertTextAfterRange([ insertPosition, insertPosition ], `\n\n${interfaceDeclaration}\n`),
|
|
286
|
-
];
|
|
287
|
-
|
|
288
|
-
// Replace all usages with interface reference
|
|
289
|
-
for (const usage of usages) {
|
|
290
|
-
fixes.push(
|
|
291
|
-
fixer.replaceText(usage.node, interfaceName)
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return fixes;
|
|
296
|
-
} catch (fixError) {
|
|
297
|
-
console.error('[prefer-interface-over-duplicate-types] Error in fix function:', fixError);
|
|
298
|
-
|
|
299
|
-
return null;
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
} catch (error) {
|
|
307
|
-
console.error('[prefer-interface-over-duplicate-types] Error in Program:exit visitor:', error);
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
};
|
|
311
|
-
},
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
export default rule;
|
|
1
|
+
import { Rule } from 'eslint';
|
|
2
|
+
|
|
3
|
+
interface TypeUsage {
|
|
4
|
+
node: any;
|
|
5
|
+
signature: string;
|
|
6
|
+
interfaceName?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface RuleOptions {
|
|
10
|
+
threshold?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const rule: Rule.RuleModule = {
|
|
14
|
+
meta: {
|
|
15
|
+
type: 'suggestion',
|
|
16
|
+
docs: {
|
|
17
|
+
description: 'Suggest creating interfaces for object types used more than the threshold number of times',
|
|
18
|
+
recommended: true,
|
|
19
|
+
},
|
|
20
|
+
schema: [
|
|
21
|
+
{
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
threshold: {
|
|
25
|
+
type: 'number',
|
|
26
|
+
minimum: 2,
|
|
27
|
+
default: 3,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
messages: {
|
|
34
|
+
preferInterface: 'Object type used {{count}} times. Consider creating interface "{{interfaceName}}"}}',
|
|
35
|
+
},
|
|
36
|
+
// BFR-ADVENTOR-009 (2. lépcső): a fixer forrás-szöveg-megőrzőre átírva (`sourceCode.getText` per property)
|
|
37
|
+
// → a komplex/union/qualified/function típusok EXAKT megőrződnek, a generált interface ÉRVÉNYES TS. Ezért
|
|
38
|
+
// az auto-fix (`fixable:'code'`) VISSZA — a bulk `eslint --fix` biztonságosan futtathatja (nem generál
|
|
39
|
+
// többé `reference`/`complex` literál-hibát). (1. lépcső: suggestion-degrade, most feloldva.)
|
|
40
|
+
fixable: 'code',
|
|
41
|
+
},
|
|
42
|
+
create(context) {
|
|
43
|
+
const options: RuleOptions = context.options[0] || {};
|
|
44
|
+
const threshold = options.threshold || 3;
|
|
45
|
+
const sourceCode = context.sourceCode;
|
|
46
|
+
|
|
47
|
+
// Map to track type signatures and their usages
|
|
48
|
+
const typeUsages = new Map<string, TypeUsage[]>();
|
|
49
|
+
const allTypeLiterals: any[] = [];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Normalize object type to a signature string for comparison
|
|
53
|
+
*/
|
|
54
|
+
function normalizeObjectType(node: any): string {
|
|
55
|
+
if (!node || node.type !== 'TSTypeLiteral') {
|
|
56
|
+
return '';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!node.members || !Array.isArray(node.members)) {
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Extract and sort properties
|
|
64
|
+
const properties = node.members
|
|
65
|
+
.filter((member: any) => member.type === 'TSPropertySignature')
|
|
66
|
+
.map((member: any) => {
|
|
67
|
+
const key = member.key?.name || member.key?.value || 'unknown';
|
|
68
|
+
const type = getTypeString(member.typeAnnotation);
|
|
69
|
+
const optional = member.optional ? '?' : '';
|
|
70
|
+
const readonly = member.readonly ? 'readonly ' : '';
|
|
71
|
+
|
|
72
|
+
return `${readonly}${key}${optional}:${type}`;
|
|
73
|
+
})
|
|
74
|
+
.sort(); // Sort alphabetically
|
|
75
|
+
|
|
76
|
+
return properties.join(';');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Get string representation of a type annotation
|
|
81
|
+
*/
|
|
82
|
+
function getTypeString(typeAnnotation: any): string {
|
|
83
|
+
if (!typeAnnotation || !typeAnnotation.typeAnnotation) {
|
|
84
|
+
return 'any';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const type = typeAnnotation.typeAnnotation;
|
|
88
|
+
|
|
89
|
+
switch (type.type) {
|
|
90
|
+
case 'TSStringKeyword':
|
|
91
|
+
return 'string';
|
|
92
|
+
case 'TSNumberKeyword':
|
|
93
|
+
return 'number';
|
|
94
|
+
case 'TSBooleanKeyword':
|
|
95
|
+
return 'boolean';
|
|
96
|
+
case 'TSVoidKeyword':
|
|
97
|
+
return 'void';
|
|
98
|
+
case 'TSNullKeyword':
|
|
99
|
+
return 'null';
|
|
100
|
+
case 'TSUndefinedKeyword':
|
|
101
|
+
return 'undefined';
|
|
102
|
+
case 'TSAnyKeyword':
|
|
103
|
+
return 'any';
|
|
104
|
+
case 'TSUnknownKeyword':
|
|
105
|
+
return 'unknown';
|
|
106
|
+
case 'TSNeverKeyword':
|
|
107
|
+
return 'never';
|
|
108
|
+
case 'TSObjectKeyword':
|
|
109
|
+
return 'object';
|
|
110
|
+
case 'TSLiteralType':
|
|
111
|
+
return type.literal?.value?.toString() || 'literal';
|
|
112
|
+
case 'TSArrayType':
|
|
113
|
+
return `${getTypeString({ typeAnnotation: type.elementType })}[]`;
|
|
114
|
+
case 'TSTypeReference':
|
|
115
|
+
return type.typeName?.name || 'reference';
|
|
116
|
+
case 'TSTypeLiteral':
|
|
117
|
+
return normalizeObjectType(type);
|
|
118
|
+
case 'TSUnionType':
|
|
119
|
+
return type.types?.map((t: any) => getTypeString({ typeAnnotation: t })).join('|') || 'union';
|
|
120
|
+
case 'TSIntersectionType':
|
|
121
|
+
return type.types?.map((t: any) => getTypeString({ typeAnnotation: t })).join('&') || 'intersection';
|
|
122
|
+
|
|
123
|
+
default:
|
|
124
|
+
return 'complex';
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Generate interface name based on context
|
|
130
|
+
*/
|
|
131
|
+
function generateInterfaceName(signature: string, usageCount: number): string {
|
|
132
|
+
// Try to extract meaningful name from signature
|
|
133
|
+
const properties = signature.split(';');
|
|
134
|
+
|
|
135
|
+
if (properties.length === 1) {
|
|
136
|
+
const prop = properties[0].split(':')[0].replace('readonly ', '').replace('?', '');
|
|
137
|
+
|
|
138
|
+
if (prop && prop !== 'unknown') {
|
|
139
|
+
return `${prop.charAt(0).toUpperCase() + prop.slice(1)}Interface`;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Fallback to generic name
|
|
144
|
+
return `GeneratedInterface${usageCount}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Check if interface name conflicts with existing identifiers
|
|
149
|
+
*/
|
|
150
|
+
function isNameConflict(name: string): boolean {
|
|
151
|
+
try {
|
|
152
|
+
const scope = context.sourceCode.getScope(context.sourceCode.ast);
|
|
153
|
+
|
|
154
|
+
return scope.variables.some(variable => variable.name === name);
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generate unique interface name
|
|
162
|
+
*/
|
|
163
|
+
function generateUniqueInterfaceName(baseName: string): string {
|
|
164
|
+
let name = baseName;
|
|
165
|
+
let counter = 1;
|
|
166
|
+
|
|
167
|
+
while (isNameConflict(name)) {
|
|
168
|
+
name = `${baseName}${counter}`;
|
|
169
|
+
counter++;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return name;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* BFR-ADVENTOR-009 (2. lépcső): az interface-törzs a FORRÁS-SZÖVEGBŐL épül (sourceCode.getText per
|
|
177
|
+
* member), NEM a lossy normalizált signature-ből → a komplex/union/qualified/function típusok EXAKT
|
|
178
|
+
* megőrződnek (a korábbi `reference`/`complex`/`union` literál-hibák megszűnnek). A `getText` az eredeti
|
|
179
|
+
* property-szöveget adja (pl. `onError: (e: Error) => void`); a záró `;`/`,`-t normalizáljuk.
|
|
180
|
+
*/
|
|
181
|
+
function buildInterfaceBody(node: any): string {
|
|
182
|
+
const members: any[] = Array.isArray(node?.members) ? node.members : [];
|
|
183
|
+
|
|
184
|
+
return members
|
|
185
|
+
.map((member: any): string => {
|
|
186
|
+
const text: string = sourceCode.getText(member).trim().replace(/[;,]\s*$/, '');
|
|
187
|
+
return ` ${text};`;
|
|
188
|
+
})
|
|
189
|
+
.join('\n');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Beszédes interface-név a property-kulcsokból (BFR-ADVENTOR-009): a nem-beszédes, ütköző
|
|
194
|
+
* `GeneratedInterfaceN` helyett az első 1-3 kulcsból PascalCase + `Params_Interface` (a projekt-konvenció).
|
|
195
|
+
*/
|
|
196
|
+
function deriveInterfaceName(node: any): string {
|
|
197
|
+
const members: any[] = Array.isArray(node?.members) ? node.members : [];
|
|
198
|
+
const keys: string[] = members
|
|
199
|
+
.filter((m: any): boolean => m.type === 'TSPropertySignature')
|
|
200
|
+
.map((m: any): string => String(m.key?.name ?? m.key?.value ?? ''))
|
|
201
|
+
.filter((k: string): boolean => !!k)
|
|
202
|
+
.slice(0, 3);
|
|
203
|
+
|
|
204
|
+
if (!keys.length) {
|
|
205
|
+
return 'Generated_Interface';
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const pascal: string = keys
|
|
209
|
+
.map((k: string): string => k.charAt(0).toUpperCase() + k.slice(1))
|
|
210
|
+
.join('');
|
|
211
|
+
|
|
212
|
+
return `${pascal}_Interface`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
TSTypeLiteral(node: any, _context?: any, _sourceCode?: any): void {
|
|
217
|
+
try {
|
|
218
|
+
const signature = normalizeObjectType(node);
|
|
219
|
+
|
|
220
|
+
if (!signature) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
allTypeLiterals.push(node);
|
|
225
|
+
|
|
226
|
+
if (!typeUsages.has(signature)) {
|
|
227
|
+
typeUsages.set(signature, []);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
typeUsages.get(signature)!.push({
|
|
231
|
+
node,
|
|
232
|
+
signature,
|
|
233
|
+
});
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error('[prefer-interface-over-duplicate-types] Error in TSTypeLiteral visitor:', error);
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
'Program:exit'(node: any, _context?: any, _sourceCode?: any): void {
|
|
240
|
+
try {
|
|
241
|
+
// Process all collected type usages
|
|
242
|
+
for (const [ signature, usages ] of typeUsages) {
|
|
243
|
+
if (usages.length >= threshold) {
|
|
244
|
+
// beszédes név a property-kulcsokból (ütközés-mentesre pontosítva)
|
|
245
|
+
const interfaceName = generateUniqueInterfaceName(
|
|
246
|
+
deriveInterfaceName(usages[0].node)
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// a törzs a FORRÁS-SZÖVEGBŐL (getText) — a komplex/union/qualified típusok EXAKT megőrződnek
|
|
250
|
+
const interfaceBody: string = buildInterfaceBody(usages[0].node);
|
|
251
|
+
|
|
252
|
+
// Report on each usage
|
|
253
|
+
for (const usage of usages) {
|
|
254
|
+
context.report({
|
|
255
|
+
node: usage.node,
|
|
256
|
+
messageId: 'preferInterface',
|
|
257
|
+
data: {
|
|
258
|
+
count: usages.length.toString(),
|
|
259
|
+
interfaceName,
|
|
260
|
+
},
|
|
261
|
+
fix(fixer) {
|
|
262
|
+
try {
|
|
263
|
+
const interfaceDeclaration = `interface ${interfaceName} {\n${interfaceBody}\n}`;
|
|
264
|
+
|
|
265
|
+
// Find insertion point (after imports, before other code)
|
|
266
|
+
const firstNode = sourceCode.ast.body[0];
|
|
267
|
+
let insertPosition = 0;
|
|
268
|
+
|
|
269
|
+
if (firstNode && firstNode.type === 'ImportDeclaration') {
|
|
270
|
+
// Find last import
|
|
271
|
+
let lastImport = firstNode;
|
|
272
|
+
|
|
273
|
+
for (const node of sourceCode.ast.body) {
|
|
274
|
+
if (node.type === 'ImportDeclaration') {
|
|
275
|
+
lastImport = node;
|
|
276
|
+
} else {
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
insertPosition = lastImport.range![1];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Add interface declaration
|
|
284
|
+
const fixes = [
|
|
285
|
+
fixer.insertTextAfterRange([ insertPosition, insertPosition ], `\n\n${interfaceDeclaration}\n`),
|
|
286
|
+
];
|
|
287
|
+
|
|
288
|
+
// Replace all usages with interface reference
|
|
289
|
+
for (const usage of usages) {
|
|
290
|
+
fixes.push(
|
|
291
|
+
fixer.replaceText(usage.node, interfaceName)
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return fixes;
|
|
296
|
+
} catch (fixError) {
|
|
297
|
+
console.error('[prefer-interface-over-duplicate-types] Error in fix function:', fixError);
|
|
298
|
+
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} catch (error) {
|
|
307
|
+
console.error('[prefer-interface-over-duplicate-types] Error in Program:exit visitor:', error);
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
export default rule;
|