@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.
Files changed (194) hide show
  1. package/.c8rc.json +26 -26
  2. package/.cursor/rules/__assistant_guide.mdc +30 -30
  3. package/.cursor/rules/__main.mdc +64 -64
  4. package/.cursor/rules/_ag_backend-structure.mdc +85 -85
  5. package/.cursor/rules/_ag_backend.mdc +16 -16
  6. package/.cursor/rules/_ag_debug.mdc +7 -7
  7. package/.cursor/rules/_ag_documentation_writing_rules.mdc +372 -372
  8. package/.cursor/rules/_ag_file-refactoring.mdc +113 -113
  9. package/.cursor/rules/_ag_fixes_rules.mdc +5 -5
  10. package/.cursor/rules/_ag_frontend-structure.mdc +86 -86
  11. package/.cursor/rules/_ag_frontend.mdc +39 -39
  12. package/.cursor/rules/_ag_import-rules.mdc +44 -44
  13. package/.cursor/rules/_ag_naming.mdc +115 -115
  14. package/.cursor/rules/_ag_running_commands.mdc +4 -4
  15. package/.cursor/rules/_ag_server-controller.mdc +5 -5
  16. package/.cursor/rules/_ag_should-be.mdc +6 -6
  17. package/.cursor/rules/_ag_swearing.mdc +47 -47
  18. package/.cursor/rules/ai_development_guide.md +60 -60
  19. package/.cursor/rules/ai_directives.md +114 -114
  20. package/.cursor/rules/cursor-rules.md +160 -160
  21. package/.cursor/rules/default-command.mdc +464 -464
  22. package/.cursor/rules/error_code_pattern.md +39 -39
  23. package/.cursor/rules/saved rule mcp server use.md +15 -15
  24. package/.dynamo/pipeline.cicd.config.json +184 -184
  25. package/.dynamo/version-bump.config.json +5 -5
  26. package/.eslintrc.json +4 -4
  27. package/.github/workflows/main.yml +436 -436
  28. package/.husky/pre-commit +2 -2
  29. package/.vscode/settings.json +11 -11
  30. package/README.md +319 -319
  31. package/__documentations/2026-04-18-ccap-review-integration.md +173 -173
  32. package/__documentations/2026-08-18-bfr-ctm-005-eslint9-validator-report.md +45 -0
  33. package/build/configs/base.d.ts +9 -9
  34. package/build/configs/base.js +150 -150
  35. package/build/configs/fsm.d.ts +1 -1
  36. package/build/configs/fsm.js +7 -7
  37. package/build/configs/ngx-package.d.ts +1 -1
  38. package/build/configs/ngx-package.js +7 -7
  39. package/build/configs/ngx.d.ts +4 -4
  40. package/build/configs/ngx.js +91 -91
  41. package/build/configs/nts-package.d.ts +1 -1
  42. package/build/configs/nts-package.js +17 -17
  43. package/build/configs/nts.d.ts +1 -1
  44. package/build/configs/nts.js +31 -31
  45. package/build/index.d.ts +1 -1
  46. package/build/index.js +2 -2
  47. package/build/plugin/index.d.ts +52 -52
  48. package/build/plugin/index.js +80 -80
  49. package/build/plugin/rules/angular/angular-for-track-logic.d.ts +7 -7
  50. package/build/plugin/rules/angular/angular-for-track-logic.js +81 -81
  51. package/build/plugin/rules/angular/effect-signal-loop.d.ts +9 -9
  52. package/build/plugin/rules/angular/effect-signal-loop.js +112 -112
  53. package/build/plugin/rules/angular/no-async-ngoninit.d.ts +8 -8
  54. package/build/plugin/rules/angular/no-async-ngoninit.js +42 -42
  55. package/build/plugin/rules/angular/no-component-direct-api.d.ts +9 -9
  56. package/build/plugin/rules/angular/no-component-direct-api.js +75 -75
  57. package/build/plugin/rules/angular/no-method-call-in-template.d.ts +8 -8
  58. package/build/plugin/rules/angular/no-method-call-in-template.js +79 -79
  59. package/build/plugin/rules/angular/no-ngfor-directive.d.ts +7 -7
  60. package/build/plugin/rules/angular/no-ngfor-directive.js +59 -59
  61. package/build/plugin/rules/angular/no-ngif-directive.d.ts +7 -7
  62. package/build/plugin/rules/angular/no-ngif-directive.js +59 -59
  63. package/build/plugin/rules/explicit-types.d.ts +3 -3
  64. package/build/plugin/rules/explicit-types.js +178 -178
  65. package/build/plugin/rules/import/import-order.d.ts +3 -3
  66. package/build/plugin/rules/import/import-order.js +154 -154
  67. package/build/plugin/rules/import/no-import-type.d.ts +3 -3
  68. package/build/plugin/rules/import/no-import-type.js +45 -45
  69. package/build/plugin/rules/import/no-js-import.d.ts +3 -3
  70. package/build/plugin/rules/import/no-js-import.js +40 -40
  71. package/build/plugin/rules/naming-patterns.d.ts +7 -7
  72. package/build/plugin/rules/naming-patterns.js +366 -366
  73. package/build/plugin/rules/no-getter-logic.d.ts +3 -3
  74. package/build/plugin/rules/no-getter-logic.js +194 -194
  75. package/build/plugin/rules/prefer-enum-over-string-union.d.ts +3 -3
  76. package/build/plugin/rules/prefer-enum-over-string-union.js +289 -289
  77. package/build/plugin/rules/prefer-interface-over-duplicate-types.d.ts +3 -3
  78. package/build/plugin/rules/prefer-interface-over-duplicate-types.js +264 -264
  79. package/build/plugin/rules/require-jsdoc-description.d.ts +3 -3
  80. package/build/plugin/rules/require-jsdoc-description.js +496 -496
  81. package/build/plugin/rules/require-test-description-prefix.d.ts +3 -3
  82. package/build/plugin/rules/require-test-description-prefix.js +135 -135
  83. package/build/plugin/rules/ts/data-model-extends.d.ts +8 -8
  84. package/build/plugin/rules/ts/data-model-extends.js +94 -94
  85. package/build/plugin/rules/ts/no-any-type.d.ts +8 -8
  86. package/build/plugin/rules/ts/no-any-type.js +36 -36
  87. package/build/plugin/rules/ts/no-as-cast.d.ts +8 -8
  88. package/build/plugin/rules/ts/no-as-cast.js +49 -49
  89. package/build/plugin/rules/ts/no-dynamic-imports.d.ts +8 -8
  90. package/build/plugin/rules/ts/no-dynamic-imports.js +50 -50
  91. package/build/plugin/rules/ts/no-native-browser-dialogs.d.ts +8 -8
  92. package/build/plugin/rules/ts/no-native-browser-dialogs.js +78 -78
  93. package/build/plugin/rules/ts/no-plain-function-export.d.ts +9 -9
  94. package/build/plugin/rules/ts/no-plain-function-export.js +69 -69
  95. package/build/plugin/rules/ts/one-export-per-file.d.ts +8 -8
  96. package/build/plugin/rules/ts/one-export-per-file.js +73 -73
  97. package/build/scripts/dynamo-fix.d.ts +26 -26
  98. package/build/scripts/dynamo-fix.js +146 -146
  99. package/build/scripts/eslintrc-audit.d.ts +26 -26
  100. package/build/scripts/eslintrc-audit.js +100 -100
  101. package/build/scripts/fix-return-types.d.ts +74 -74
  102. package/build/scripts/fix-return-types.js +226 -226
  103. package/build/scripts/validate-imports.d.ts +26 -26
  104. package/build/scripts/validate-imports.js +157 -146
  105. package/build/scripts/validate-imports.js.map +1 -1
  106. package/build/scripts/validate-naming.d.ts +26 -26
  107. package/build/scripts/validate-naming.js +157 -131
  108. package/build/scripts/validate-naming.js.map +1 -1
  109. package/eslint.config.js +15 -15
  110. package/package.json +105 -105
  111. package/samples/.vscode/settings.json +13 -13
  112. package/samples/base/.eslintrc.json +6 -6
  113. package/samples/base/eslint.config.js +3 -3
  114. package/samples/fsm/.eslintrc.json +4 -4
  115. package/samples/fsm/eslint.config.js +3 -3
  116. package/samples/ngx/.eslintrc.json +6 -6
  117. package/samples/ngx/eslint.config.js +3 -3
  118. package/samples/ngx-package/.eslintrc.json +4 -4
  119. package/samples/ngx-package/eslint.config.js +3 -3
  120. package/samples/nts/.eslintrc.json +6 -6
  121. package/samples/nts/eslint.config.js +3 -3
  122. package/samples/nts-package/.eslintrc.json +4 -4
  123. package/samples/nts-package/eslint.config.js +3 -3
  124. package/samples/package.json.example +26 -26
  125. package/samples/poc-sample.ts +38 -38
  126. package/samples/poc-violations.ts +54 -54
  127. package/scripts/run-coverage-tests.js +24 -24
  128. package/spec/support/helpers/ts-node-helper.js +9 -9
  129. package/spec/support/jasmine.coverage.json +23 -23
  130. package/spec/support/jasmine.json +24 -24
  131. package/src/configs/base.ts +157 -157
  132. package/src/configs/fsm.ts +10 -10
  133. package/src/configs/ngx-package.ts +10 -10
  134. package/src/configs/ngx.ts +96 -96
  135. package/src/configs/nts-package.ts +20 -20
  136. package/src/configs/nts.ts +34 -34
  137. package/src/index.ts +4 -4
  138. package/src/plugin/index.ts +86 -86
  139. package/src/plugin/rules/angular/angular-for-track-logic.spec.ts +44 -44
  140. package/src/plugin/rules/angular/angular-for-track-logic.ts +88 -88
  141. package/src/plugin/rules/angular/effect-signal-loop.spec.ts +89 -89
  142. package/src/plugin/rules/angular/effect-signal-loop.ts +125 -125
  143. package/src/plugin/rules/angular/no-async-ngoninit.spec.ts +50 -50
  144. package/src/plugin/rules/angular/no-async-ngoninit.ts +43 -43
  145. package/src/plugin/rules/angular/no-component-direct-api.spec.ts +78 -78
  146. package/src/plugin/rules/angular/no-component-direct-api.ts +82 -82
  147. package/src/plugin/rules/angular/no-method-call-in-template.spec.ts +63 -63
  148. package/src/plugin/rules/angular/no-method-call-in-template.ts +89 -89
  149. package/src/plugin/rules/angular/no-ngfor-directive.spec.ts +44 -44
  150. package/src/plugin/rules/angular/no-ngfor-directive.ts +62 -62
  151. package/src/plugin/rules/angular/no-ngif-directive.spec.ts +44 -44
  152. package/src/plugin/rules/angular/no-ngif-directive.ts +62 -62
  153. package/src/plugin/rules/explicit-types.spec.ts +190 -190
  154. package/src/plugin/rules/explicit-types.ts +186 -186
  155. package/src/plugin/rules/import/import-order.spec.ts +219 -219
  156. package/src/plugin/rules/import/import-order.ts +197 -197
  157. package/src/plugin/rules/import/no-import-type.spec.ts +69 -69
  158. package/src/plugin/rules/import/no-import-type.ts +46 -46
  159. package/src/plugin/rules/import/no-js-import.spec.ts +82 -82
  160. package/src/plugin/rules/import/no-js-import.ts +45 -45
  161. package/src/plugin/rules/naming-patterns.spec.ts +189 -189
  162. package/src/plugin/rules/naming-patterns.ts +426 -426
  163. package/src/plugin/rules/no-getter-logic.spec.ts +591 -591
  164. package/src/plugin/rules/no-getter-logic.ts +220 -220
  165. package/src/plugin/rules/prefer-enum-over-string-union.spec.ts +583 -583
  166. package/src/plugin/rules/prefer-enum-over-string-union.ts +333 -333
  167. package/src/plugin/rules/prefer-interface-over-duplicate-types.spec.ts +404 -404
  168. package/src/plugin/rules/prefer-interface-over-duplicate-types.ts +314 -314
  169. package/src/plugin/rules/require-jsdoc-description.spec.ts +779 -779
  170. package/src/plugin/rules/require-jsdoc-description.ts +572 -572
  171. package/src/plugin/rules/require-test-description-prefix.spec.ts +459 -459
  172. package/src/plugin/rules/require-test-description-prefix.ts +154 -154
  173. package/src/plugin/rules/ts/data-model-extends.spec.ts +84 -84
  174. package/src/plugin/rules/ts/data-model-extends.ts +100 -100
  175. package/src/plugin/rules/ts/no-any-type.spec.ts +28 -28
  176. package/src/plugin/rules/ts/no-any-type.ts +36 -36
  177. package/src/plugin/rules/ts/no-as-cast.spec.ts +51 -51
  178. package/src/plugin/rules/ts/no-as-cast.ts +51 -51
  179. package/src/plugin/rules/ts/no-dynamic-imports.spec.ts +54 -54
  180. package/src/plugin/rules/ts/no-dynamic-imports.ts +49 -49
  181. package/src/plugin/rules/ts/no-native-browser-dialogs.spec.ts +54 -54
  182. package/src/plugin/rules/ts/no-native-browser-dialogs.ts +84 -84
  183. package/src/plugin/rules/ts/no-plain-function-export.spec.ts +72 -72
  184. package/src/plugin/rules/ts/no-plain-function-export.ts +73 -73
  185. package/src/plugin/rules/ts/one-export-per-file.spec.ts +64 -64
  186. package/src/plugin/rules/ts/one-export-per-file.ts +77 -77
  187. package/src/scripts/dynamo-fix.ts +172 -172
  188. package/src/scripts/eslintrc-audit.ts +116 -116
  189. package/src/scripts/fix-return-types.spec.ts +65 -65
  190. package/src/scripts/fix-return-types.ts +288 -288
  191. package/src/scripts/validate-imports.ts +278 -261
  192. package/src/scripts/validate-naming.ts +204 -187
  193. package/src/scripts/validator-cli.integration.spec.ts +96 -0
  194. package/tsconfig.json +31 -31
@@ -1,334 +1,334 @@
1
- import { Rule } from 'eslint';
2
-
3
- interface RuleOptions {
4
- minValues?: number;
5
- allowTypeAliases?: boolean;
6
- }
7
-
8
- const rule: Rule.RuleModule = {
9
- meta: {
10
- type: 'suggestion',
11
- docs: {
12
- description: 'Suggest using enums instead of union types of string literals',
13
- recommended: true,
14
- },
15
- schema: [
16
- {
17
- type: 'object',
18
- properties: {
19
- minValues: {
20
- type: 'number',
21
- minimum: 2,
22
- default: 3,
23
- },
24
- allowTypeAliases: {
25
- type: 'boolean',
26
- default: false,
27
- },
28
- },
29
- additionalProperties: false,
30
- },
31
- ],
32
- messages: {
33
- preferEnum: 'Consider using an enum instead of a union type of {{count}} string literals.',
34
- },
35
- fixable: 'code',
36
- },
37
- create(context) {
38
- const options: RuleOptions = context.options[0] || {};
39
- const minValues: number = options.minValues || 3;
40
- const allowTypeAliases: boolean = options.allowTypeAliases || false;
41
-
42
- /**
43
- * Check if a union type is composed solely of string literals
44
- */
45
- function isStringLiteralUnion(node: any): boolean {
46
- if (node.type !== 'TSUnionType') {
47
- return false;
48
- }
49
-
50
- if (!node.types || !Array.isArray(node.types)) {
51
- return false;
52
- }
53
-
54
- return node.types.every((type: any) =>
55
- type.type === 'TSLiteralType' &&
56
- typeof type.literal?.value === 'string'
57
- );
58
- }
59
-
60
- /**
61
- * Check if a property is an Angular component input
62
- */
63
- function isAngularInput(node: any): boolean {
64
- try {
65
- // Check for @Input() decorator (old style)
66
- if (node.decorators && Array.isArray(node.decorators)) {
67
- const hasInputDecorator = node.decorators.some((decorator: any) => {
68
- const expression = decorator.expression;
69
-
70
- return expression?.type === 'CallExpression' &&
71
- expression.callee?.name === 'Input';
72
- });
73
-
74
- if (hasInputDecorator) {
75
- return true;
76
- }
77
- }
78
-
79
- // Check for input() or input.required() signal (new style)
80
- if (node.value?.type === 'CallExpression') {
81
- const callee = node.value.callee;
82
-
83
- // Direct call: input()
84
- if (callee?.name === 'input') {
85
- return true;
86
- }
87
-
88
- // Member call: input.required()
89
- if (callee?.object?.name === 'input' && callee?.property?.name === 'required') {
90
- return true;
91
- }
92
- }
93
-
94
- return false;
95
- } catch (error) {
96
- console.error('[prefer-enum-over-string-union] Error checking Angular input:', error);
97
-
98
- return false;
99
- }
100
- }
101
-
102
- /**
103
- * Check if a union type meets the minimum threshold
104
- */
105
- function meetsThreshold(node: any): boolean {
106
- if (node.type !== 'TSUnionType' || !node.types) {
107
- return false;
108
- }
109
-
110
- const stringLiteralCount = node.types.filter((type: any) =>
111
- type.type === 'TSLiteralType' &&
112
- typeof type.literal?.value === 'string'
113
- ).length;
114
-
115
- return stringLiteralCount >= minValues;
116
- }
117
-
118
- /**
119
- * Generate enum name suggestion based on context
120
- */
121
- function generateEnumName(node: any, parent: any): string {
122
- try {
123
- // For type aliases, use the type name
124
- if (parent?.type === 'TSTypeAliasDeclaration' && parent.id?.name) {
125
- return parent.id.name;
126
- }
127
-
128
- // For properties, use property name + "Enum"
129
- if (node.type === 'TSPropertySignature' && node.key?.name) {
130
- return `${node.key.name.charAt(0).toUpperCase() + node.key.name.slice(1)}Enum`;
131
- }
132
-
133
- // For class properties, use property name + "Enum"
134
- if (parent?.type === 'PropertyDefinition' && parent.key?.name) {
135
- return `${parent.key.name.charAt(0).toUpperCase() + parent.key.name.slice(1)}Enum`;
136
- }
137
-
138
- // Default fallback
139
- return 'StatusEnum';
140
- } catch (error) {
141
- console.error('[prefer-enum-over-string-union] Error generating enum name:', error);
142
-
143
- return 'StatusEnum';
144
- }
145
- }
146
-
147
- /**
148
- * Generate enum values from string literals
149
- */
150
- function generateEnumValues(node: any): string[] {
151
- if (node.type !== 'TSUnionType' || !node.types) {
152
- return [];
153
- }
154
-
155
- return node.types
156
- .filter((type: any) =>
157
- type.type === 'TSLiteralType' &&
158
- typeof type.literal?.value === 'string'
159
- )
160
- .map((type: any) => {
161
- const value = type.literal.value;
162
-
163
- // Convert to PascalCase for enum key
164
- return value
165
- .split(/[-_\s]+/)
166
- .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
167
- .join('');
168
- });
169
- }
170
-
171
- /**
172
- * Report violation with enum suggestion
173
- */
174
- function reportViolation(node: any, parent: any, unionNode: any): void {
175
- const stringLiteralCount = unionNode.types.filter((type: any) =>
176
- type.type === 'TSLiteralType' &&
177
- typeof type.literal?.value === 'string'
178
- ).length;
179
-
180
- const enumName = generateEnumName(node, parent);
181
- const enumValues = generateEnumValues(unionNode);
182
-
183
- context.report({
184
- node: unionNode,
185
- messageId: 'preferEnum',
186
- data: {
187
- count: stringLiteralCount.toString(),
188
- },
189
- fix(fixer) {
190
- try {
191
- const enumDeclaration = `enum ${enumName} {\n ${enumValues.map((key, index) => {
192
- const value = unionNode.types[index].literal.value;
193
-
194
- return `${key} = '${value}'`;
195
- }).join(',\n ')}\n}`;
196
-
197
- // For type aliases, replace the entire declaration
198
- if (parent?.type === 'TSTypeAliasDeclaration') {
199
- const typeText = context.sourceCode.getText(parent);
200
- const newText = typeText.replace(/=.*/, `= ${enumName};`);
201
-
202
- return fixer.replaceText(parent, newText);
203
- }
204
-
205
- // For other cases, suggest enum creation (no auto-fix for now)
206
- return null;
207
- } catch (fixError) {
208
- console.error('[prefer-enum-over-string-union] Error in fix function:', fixError);
209
-
210
- return null;
211
- }
212
- },
213
- });
214
- }
215
-
216
- return {
217
- // Type aliases
218
- TSTypeAliasDeclaration(node: any, _context?: any, _sourceCode?: any): void {
219
- try {
220
- if (allowTypeAliases) {
221
- return;
222
- }
223
-
224
- if (isStringLiteralUnion(node.typeAnnotation) && meetsThreshold(node.typeAnnotation)) {
225
- reportViolation(node.typeAnnotation, node, node.typeAnnotation);
226
- }
227
- } catch (error) {
228
- console.error('[prefer-enum-over-string-union] Error in TSTypeAliasDeclaration visitor:', error);
229
- }
230
- },
231
-
232
- // Interface/type properties
233
- TSPropertySignature(node: any, _context?: any, _sourceCode?: any): void {
234
- try {
235
- if (isAngularInput(node)) {
236
- return;
237
- }
238
-
239
- if (node.typeAnnotation &&
240
- isStringLiteralUnion(node.typeAnnotation.typeAnnotation) &&
241
- meetsThreshold(node.typeAnnotation.typeAnnotation)) {
242
- reportViolation(node, node.parent, node.typeAnnotation.typeAnnotation);
243
- }
244
- } catch (error) {
245
- console.error('[prefer-enum-over-string-union] Error in TSPropertySignature visitor:', error);
246
- }
247
- },
248
-
249
- // Class properties
250
- PropertyDefinition(node: any, _context?: any, _sourceCode?: any): void {
251
- try {
252
- if (isAngularInput(node)) {
253
- return;
254
- }
255
-
256
- if (node.typeAnnotation &&
257
- isStringLiteralUnion(node.typeAnnotation.typeAnnotation) &&
258
- meetsThreshold(node.typeAnnotation.typeAnnotation)) {
259
- reportViolation(node, node.parent, node.typeAnnotation.typeAnnotation);
260
- }
261
- } catch (error) {
262
- console.error('[prefer-enum-over-string-union] Error in PropertyDefinition visitor:', error);
263
- }
264
- },
265
-
266
- // Function parameters
267
- FunctionDeclaration(node: any, _context?: any, _sourceCode?: any): void {
268
- try {
269
- if (node.params && Array.isArray(node.params)) {
270
- node.params.forEach((param: any) => {
271
- if (param.typeAnnotation &&
272
- isStringLiteralUnion(param.typeAnnotation.typeAnnotation) &&
273
- meetsThreshold(param.typeAnnotation.typeAnnotation)) {
274
- reportViolation(param, node, param.typeAnnotation.typeAnnotation);
275
- }
276
- });
277
- }
278
- } catch (error) {
279
- console.error('[prefer-enum-over-string-union] Error in FunctionDeclaration visitor:', error);
280
- }
281
- },
282
-
283
- // Arrow function parameters
284
- ArrowFunctionExpression(node: any, _context?: any, _sourceCode?: any): void {
285
- try {
286
- if (node.params && Array.isArray(node.params)) {
287
- node.params.forEach((param: any) => {
288
- if (param.typeAnnotation &&
289
- isStringLiteralUnion(param.typeAnnotation.typeAnnotation) &&
290
- meetsThreshold(param.typeAnnotation.typeAnnotation)) {
291
- reportViolation(param, node, param.typeAnnotation.typeAnnotation);
292
- }
293
- });
294
- }
295
- } catch (error) {
296
- console.error('[prefer-enum-over-string-union] Error in ArrowFunctionExpression visitor:', error);
297
- }
298
- },
299
-
300
- // Variable declarations
301
- VariableDeclarator(node: any, _context?: any, _sourceCode?: any): void {
302
- try {
303
- if (node.typeAnnotation &&
304
- isStringLiteralUnion(node.typeAnnotation.typeAnnotation) &&
305
- meetsThreshold(node.typeAnnotation.typeAnnotation)) {
306
- reportViolation(node, node.parent, node.typeAnnotation.typeAnnotation);
307
- }
308
- } catch (error) {
309
- console.error('[prefer-enum-over-string-union] Error in VariableDeclarator visitor:', error);
310
- }
311
- },
312
-
313
- // Method signatures
314
- TSMethodSignature(node: any, _context?: any, _sourceCode?: any): void {
315
- try {
316
- if (node.params && Array.isArray(node.params)) {
317
- node.params.forEach((param: any) => {
318
- if (param.typeAnnotation &&
319
- isStringLiteralUnion(param.typeAnnotation.typeAnnotation) &&
320
- meetsThreshold(param.typeAnnotation.typeAnnotation)) {
321
- reportViolation(param, node, param.typeAnnotation.typeAnnotation);
322
- }
323
- });
324
- }
325
- } catch (error) {
326
- console.error('[prefer-enum-over-string-union] Error in TSMethodSignature visitor:', error);
327
- }
328
- },
329
- };
330
- },
331
- };
332
-
333
- export default rule;
1
+ import { Rule } from 'eslint';
2
+
3
+ interface RuleOptions {
4
+ minValues?: number;
5
+ allowTypeAliases?: boolean;
6
+ }
7
+
8
+ const rule: Rule.RuleModule = {
9
+ meta: {
10
+ type: 'suggestion',
11
+ docs: {
12
+ description: 'Suggest using enums instead of union types of string literals',
13
+ recommended: true,
14
+ },
15
+ schema: [
16
+ {
17
+ type: 'object',
18
+ properties: {
19
+ minValues: {
20
+ type: 'number',
21
+ minimum: 2,
22
+ default: 3,
23
+ },
24
+ allowTypeAliases: {
25
+ type: 'boolean',
26
+ default: false,
27
+ },
28
+ },
29
+ additionalProperties: false,
30
+ },
31
+ ],
32
+ messages: {
33
+ preferEnum: 'Consider using an enum instead of a union type of {{count}} string literals.',
34
+ },
35
+ fixable: 'code',
36
+ },
37
+ create(context) {
38
+ const options: RuleOptions = context.options[0] || {};
39
+ const minValues: number = options.minValues || 3;
40
+ const allowTypeAliases: boolean = options.allowTypeAliases || false;
41
+
42
+ /**
43
+ * Check if a union type is composed solely of string literals
44
+ */
45
+ function isStringLiteralUnion(node: any): boolean {
46
+ if (node.type !== 'TSUnionType') {
47
+ return false;
48
+ }
49
+
50
+ if (!node.types || !Array.isArray(node.types)) {
51
+ return false;
52
+ }
53
+
54
+ return node.types.every((type: any) =>
55
+ type.type === 'TSLiteralType' &&
56
+ typeof type.literal?.value === 'string'
57
+ );
58
+ }
59
+
60
+ /**
61
+ * Check if a property is an Angular component input
62
+ */
63
+ function isAngularInput(node: any): boolean {
64
+ try {
65
+ // Check for @Input() decorator (old style)
66
+ if (node.decorators && Array.isArray(node.decorators)) {
67
+ const hasInputDecorator = node.decorators.some((decorator: any) => {
68
+ const expression = decorator.expression;
69
+
70
+ return expression?.type === 'CallExpression' &&
71
+ expression.callee?.name === 'Input';
72
+ });
73
+
74
+ if (hasInputDecorator) {
75
+ return true;
76
+ }
77
+ }
78
+
79
+ // Check for input() or input.required() signal (new style)
80
+ if (node.value?.type === 'CallExpression') {
81
+ const callee = node.value.callee;
82
+
83
+ // Direct call: input()
84
+ if (callee?.name === 'input') {
85
+ return true;
86
+ }
87
+
88
+ // Member call: input.required()
89
+ if (callee?.object?.name === 'input' && callee?.property?.name === 'required') {
90
+ return true;
91
+ }
92
+ }
93
+
94
+ return false;
95
+ } catch (error) {
96
+ console.error('[prefer-enum-over-string-union] Error checking Angular input:', error);
97
+
98
+ return false;
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Check if a union type meets the minimum threshold
104
+ */
105
+ function meetsThreshold(node: any): boolean {
106
+ if (node.type !== 'TSUnionType' || !node.types) {
107
+ return false;
108
+ }
109
+
110
+ const stringLiteralCount = node.types.filter((type: any) =>
111
+ type.type === 'TSLiteralType' &&
112
+ typeof type.literal?.value === 'string'
113
+ ).length;
114
+
115
+ return stringLiteralCount >= minValues;
116
+ }
117
+
118
+ /**
119
+ * Generate enum name suggestion based on context
120
+ */
121
+ function generateEnumName(node: any, parent: any): string {
122
+ try {
123
+ // For type aliases, use the type name
124
+ if (parent?.type === 'TSTypeAliasDeclaration' && parent.id?.name) {
125
+ return parent.id.name;
126
+ }
127
+
128
+ // For properties, use property name + "Enum"
129
+ if (node.type === 'TSPropertySignature' && node.key?.name) {
130
+ return `${node.key.name.charAt(0).toUpperCase() + node.key.name.slice(1)}Enum`;
131
+ }
132
+
133
+ // For class properties, use property name + "Enum"
134
+ if (parent?.type === 'PropertyDefinition' && parent.key?.name) {
135
+ return `${parent.key.name.charAt(0).toUpperCase() + parent.key.name.slice(1)}Enum`;
136
+ }
137
+
138
+ // Default fallback
139
+ return 'StatusEnum';
140
+ } catch (error) {
141
+ console.error('[prefer-enum-over-string-union] Error generating enum name:', error);
142
+
143
+ return 'StatusEnum';
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Generate enum values from string literals
149
+ */
150
+ function generateEnumValues(node: any): string[] {
151
+ if (node.type !== 'TSUnionType' || !node.types) {
152
+ return [];
153
+ }
154
+
155
+ return node.types
156
+ .filter((type: any) =>
157
+ type.type === 'TSLiteralType' &&
158
+ typeof type.literal?.value === 'string'
159
+ )
160
+ .map((type: any) => {
161
+ const value = type.literal.value;
162
+
163
+ // Convert to PascalCase for enum key
164
+ return value
165
+ .split(/[-_\s]+/)
166
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
167
+ .join('');
168
+ });
169
+ }
170
+
171
+ /**
172
+ * Report violation with enum suggestion
173
+ */
174
+ function reportViolation(node: any, parent: any, unionNode: any): void {
175
+ const stringLiteralCount = unionNode.types.filter((type: any) =>
176
+ type.type === 'TSLiteralType' &&
177
+ typeof type.literal?.value === 'string'
178
+ ).length;
179
+
180
+ const enumName = generateEnumName(node, parent);
181
+ const enumValues = generateEnumValues(unionNode);
182
+
183
+ context.report({
184
+ node: unionNode,
185
+ messageId: 'preferEnum',
186
+ data: {
187
+ count: stringLiteralCount.toString(),
188
+ },
189
+ fix(fixer) {
190
+ try {
191
+ const enumDeclaration = `enum ${enumName} {\n ${enumValues.map((key, index) => {
192
+ const value = unionNode.types[index].literal.value;
193
+
194
+ return `${key} = '${value}'`;
195
+ }).join(',\n ')}\n}`;
196
+
197
+ // For type aliases, replace the entire declaration
198
+ if (parent?.type === 'TSTypeAliasDeclaration') {
199
+ const typeText = context.sourceCode.getText(parent);
200
+ const newText = typeText.replace(/=.*/, `= ${enumName};`);
201
+
202
+ return fixer.replaceText(parent, newText);
203
+ }
204
+
205
+ // For other cases, suggest enum creation (no auto-fix for now)
206
+ return null;
207
+ } catch (fixError) {
208
+ console.error('[prefer-enum-over-string-union] Error in fix function:', fixError);
209
+
210
+ return null;
211
+ }
212
+ },
213
+ });
214
+ }
215
+
216
+ return {
217
+ // Type aliases
218
+ TSTypeAliasDeclaration(node: any, _context?: any, _sourceCode?: any): void {
219
+ try {
220
+ if (allowTypeAliases) {
221
+ return;
222
+ }
223
+
224
+ if (isStringLiteralUnion(node.typeAnnotation) && meetsThreshold(node.typeAnnotation)) {
225
+ reportViolation(node.typeAnnotation, node, node.typeAnnotation);
226
+ }
227
+ } catch (error) {
228
+ console.error('[prefer-enum-over-string-union] Error in TSTypeAliasDeclaration visitor:', error);
229
+ }
230
+ },
231
+
232
+ // Interface/type properties
233
+ TSPropertySignature(node: any, _context?: any, _sourceCode?: any): void {
234
+ try {
235
+ if (isAngularInput(node)) {
236
+ return;
237
+ }
238
+
239
+ if (node.typeAnnotation &&
240
+ isStringLiteralUnion(node.typeAnnotation.typeAnnotation) &&
241
+ meetsThreshold(node.typeAnnotation.typeAnnotation)) {
242
+ reportViolation(node, node.parent, node.typeAnnotation.typeAnnotation);
243
+ }
244
+ } catch (error) {
245
+ console.error('[prefer-enum-over-string-union] Error in TSPropertySignature visitor:', error);
246
+ }
247
+ },
248
+
249
+ // Class properties
250
+ PropertyDefinition(node: any, _context?: any, _sourceCode?: any): void {
251
+ try {
252
+ if (isAngularInput(node)) {
253
+ return;
254
+ }
255
+
256
+ if (node.typeAnnotation &&
257
+ isStringLiteralUnion(node.typeAnnotation.typeAnnotation) &&
258
+ meetsThreshold(node.typeAnnotation.typeAnnotation)) {
259
+ reportViolation(node, node.parent, node.typeAnnotation.typeAnnotation);
260
+ }
261
+ } catch (error) {
262
+ console.error('[prefer-enum-over-string-union] Error in PropertyDefinition visitor:', error);
263
+ }
264
+ },
265
+
266
+ // Function parameters
267
+ FunctionDeclaration(node: any, _context?: any, _sourceCode?: any): void {
268
+ try {
269
+ if (node.params && Array.isArray(node.params)) {
270
+ node.params.forEach((param: any) => {
271
+ if (param.typeAnnotation &&
272
+ isStringLiteralUnion(param.typeAnnotation.typeAnnotation) &&
273
+ meetsThreshold(param.typeAnnotation.typeAnnotation)) {
274
+ reportViolation(param, node, param.typeAnnotation.typeAnnotation);
275
+ }
276
+ });
277
+ }
278
+ } catch (error) {
279
+ console.error('[prefer-enum-over-string-union] Error in FunctionDeclaration visitor:', error);
280
+ }
281
+ },
282
+
283
+ // Arrow function parameters
284
+ ArrowFunctionExpression(node: any, _context?: any, _sourceCode?: any): void {
285
+ try {
286
+ if (node.params && Array.isArray(node.params)) {
287
+ node.params.forEach((param: any) => {
288
+ if (param.typeAnnotation &&
289
+ isStringLiteralUnion(param.typeAnnotation.typeAnnotation) &&
290
+ meetsThreshold(param.typeAnnotation.typeAnnotation)) {
291
+ reportViolation(param, node, param.typeAnnotation.typeAnnotation);
292
+ }
293
+ });
294
+ }
295
+ } catch (error) {
296
+ console.error('[prefer-enum-over-string-union] Error in ArrowFunctionExpression visitor:', error);
297
+ }
298
+ },
299
+
300
+ // Variable declarations
301
+ VariableDeclarator(node: any, _context?: any, _sourceCode?: any): void {
302
+ try {
303
+ if (node.typeAnnotation &&
304
+ isStringLiteralUnion(node.typeAnnotation.typeAnnotation) &&
305
+ meetsThreshold(node.typeAnnotation.typeAnnotation)) {
306
+ reportViolation(node, node.parent, node.typeAnnotation.typeAnnotation);
307
+ }
308
+ } catch (error) {
309
+ console.error('[prefer-enum-over-string-union] Error in VariableDeclarator visitor:', error);
310
+ }
311
+ },
312
+
313
+ // Method signatures
314
+ TSMethodSignature(node: any, _context?: any, _sourceCode?: any): void {
315
+ try {
316
+ if (node.params && Array.isArray(node.params)) {
317
+ node.params.forEach((param: any) => {
318
+ if (param.typeAnnotation &&
319
+ isStringLiteralUnion(param.typeAnnotation.typeAnnotation) &&
320
+ meetsThreshold(param.typeAnnotation.typeAnnotation)) {
321
+ reportViolation(param, node, param.typeAnnotation.typeAnnotation);
322
+ }
323
+ });
324
+ }
325
+ } catch (error) {
326
+ console.error('[prefer-enum-over-string-union] Error in TSMethodSignature visitor:', error);
327
+ }
328
+ },
329
+ };
330
+ },
331
+ };
332
+
333
+ export default rule;
334
334