@futdevpro/dynamo-eslint 1.15.25 → 1.20.2

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 (146) 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 +312 -315
  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/__documentations/2026-08-22-angular-20-phase2-release.md +79 -0
  34. package/build/configs/base.d.ts +1 -2
  35. package/build/configs/base.d.ts.map +1 -1
  36. package/build/configs/base.js +3 -4
  37. package/build/configs/base.js.map +1 -1
  38. package/build/configs/ngx.d.ts +1 -3
  39. package/build/configs/ngx.d.ts.map +1 -1
  40. package/build/configs/ngx.js +4 -7
  41. package/build/configs/ngx.js.map +1 -1
  42. package/build/plugin/index.d.ts +24 -24
  43. package/build/plugin/rules/no-getter-logic.js +1 -1
  44. package/build/plugin/rules/no-getter-logic.js.map +1 -1
  45. package/build/plugin/rules/prefer-enum-over-string-union.js +7 -7
  46. package/build/plugin/rules/prefer-enum-over-string-union.js.map +1 -1
  47. package/build/plugin/rules/prefer-interface-over-duplicate-types.js +2 -2
  48. package/build/plugin/rules/prefer-interface-over-duplicate-types.js.map +1 -1
  49. package/build/plugin/rules/require-jsdoc-description.js +7 -7
  50. package/build/plugin/rules/require-jsdoc-description.js.map +1 -1
  51. package/build/scripts/dynamo-fix.js +1 -1
  52. package/build/scripts/dynamo-fix.js.map +1 -1
  53. package/build/scripts/fix-return-types.d.ts.map +1 -1
  54. package/build/scripts/fix-return-types.js.map +1 -1
  55. package/build/scripts/validate-imports.js +51 -27
  56. package/build/scripts/validate-imports.js.map +1 -1
  57. package/build/scripts/validate-naming.js +49 -10
  58. package/build/scripts/validate-naming.js.map +1 -1
  59. package/eslint.config.js +15 -15
  60. package/package.json +27 -28
  61. package/pnpm-workspace.yaml +2 -0
  62. package/samples/.vscode/settings.json +13 -13
  63. package/samples/base/.eslintrc.json +6 -6
  64. package/samples/base/eslint.config.js +3 -3
  65. package/samples/fsm/.eslintrc.json +4 -4
  66. package/samples/fsm/eslint.config.js +3 -3
  67. package/samples/ngx/.eslintrc.json +6 -6
  68. package/samples/ngx/eslint.config.js +3 -3
  69. package/samples/ngx-package/.eslintrc.json +4 -4
  70. package/samples/ngx-package/eslint.config.js +3 -3
  71. package/samples/nts/.eslintrc.json +6 -6
  72. package/samples/nts/eslint.config.js +3 -3
  73. package/samples/nts-package/.eslintrc.json +4 -4
  74. package/samples/nts-package/eslint.config.js +3 -3
  75. package/samples/package.json.example +22 -26
  76. package/samples/poc-sample.ts +38 -38
  77. package/samples/poc-violations.ts +54 -54
  78. package/scripts/run-coverage-tests.js +24 -24
  79. package/spec/support/helpers/ts-node-helper.js +9 -9
  80. package/spec/support/jasmine.coverage.json +23 -23
  81. package/spec/support/jasmine.json +24 -24
  82. package/src/configs/angular20-compatibility.spec.ts +62 -0
  83. package/src/configs/base.ts +156 -157
  84. package/src/configs/fsm.ts +10 -10
  85. package/src/configs/ngx-package.ts +10 -10
  86. package/src/configs/ngx.ts +92 -95
  87. package/src/configs/nts-package.ts +20 -20
  88. package/src/configs/nts.ts +34 -34
  89. package/src/index.ts +4 -4
  90. package/src/plugin/index.ts +86 -86
  91. package/src/plugin/rules/angular/angular-for-track-logic.spec.ts +44 -44
  92. package/src/plugin/rules/angular/angular-for-track-logic.ts +88 -88
  93. package/src/plugin/rules/angular/effect-signal-loop.spec.ts +89 -89
  94. package/src/plugin/rules/angular/effect-signal-loop.ts +125 -125
  95. package/src/plugin/rules/angular/no-async-ngoninit.spec.ts +50 -50
  96. package/src/plugin/rules/angular/no-async-ngoninit.ts +43 -43
  97. package/src/plugin/rules/angular/no-component-direct-api.spec.ts +78 -78
  98. package/src/plugin/rules/angular/no-component-direct-api.ts +82 -82
  99. package/src/plugin/rules/angular/no-method-call-in-template.spec.ts +63 -63
  100. package/src/plugin/rules/angular/no-method-call-in-template.ts +89 -89
  101. package/src/plugin/rules/angular/no-ngfor-directive.spec.ts +44 -44
  102. package/src/plugin/rules/angular/no-ngfor-directive.ts +62 -62
  103. package/src/plugin/rules/angular/no-ngif-directive.spec.ts +44 -44
  104. package/src/plugin/rules/angular/no-ngif-directive.ts +62 -62
  105. package/src/plugin/rules/explicit-types.spec.ts +190 -190
  106. package/src/plugin/rules/explicit-types.ts +186 -186
  107. package/src/plugin/rules/import/import-order.spec.ts +219 -219
  108. package/src/plugin/rules/import/import-order.ts +197 -197
  109. package/src/plugin/rules/import/no-import-type.spec.ts +69 -69
  110. package/src/plugin/rules/import/no-import-type.ts +46 -46
  111. package/src/plugin/rules/import/no-js-import.spec.ts +82 -82
  112. package/src/plugin/rules/import/no-js-import.ts +45 -45
  113. package/src/plugin/rules/naming-patterns.spec.ts +189 -189
  114. package/src/plugin/rules/naming-patterns.ts +426 -426
  115. package/src/plugin/rules/no-getter-logic.spec.ts +591 -591
  116. package/src/plugin/rules/no-getter-logic.ts +220 -220
  117. package/src/plugin/rules/prefer-enum-over-string-union.spec.ts +583 -583
  118. package/src/plugin/rules/prefer-enum-over-string-union.ts +333 -334
  119. package/src/plugin/rules/prefer-interface-over-duplicate-types.spec.ts +404 -404
  120. package/src/plugin/rules/prefer-interface-over-duplicate-types.ts +314 -314
  121. package/src/plugin/rules/require-jsdoc-description.spec.ts +779 -779
  122. package/src/plugin/rules/require-jsdoc-description.ts +572 -572
  123. package/src/plugin/rules/require-test-description-prefix.spec.ts +459 -459
  124. package/src/plugin/rules/require-test-description-prefix.ts +154 -154
  125. package/src/plugin/rules/ts/data-model-extends.spec.ts +84 -84
  126. package/src/plugin/rules/ts/data-model-extends.ts +100 -100
  127. package/src/plugin/rules/ts/no-any-type.spec.ts +28 -28
  128. package/src/plugin/rules/ts/no-any-type.ts +36 -36
  129. package/src/plugin/rules/ts/no-as-cast.spec.ts +51 -51
  130. package/src/plugin/rules/ts/no-as-cast.ts +51 -51
  131. package/src/plugin/rules/ts/no-dynamic-imports.spec.ts +54 -54
  132. package/src/plugin/rules/ts/no-dynamic-imports.ts +49 -49
  133. package/src/plugin/rules/ts/no-native-browser-dialogs.spec.ts +54 -54
  134. package/src/plugin/rules/ts/no-native-browser-dialogs.ts +84 -84
  135. package/src/plugin/rules/ts/no-plain-function-export.spec.ts +72 -72
  136. package/src/plugin/rules/ts/no-plain-function-export.ts +73 -73
  137. package/src/plugin/rules/ts/one-export-per-file.spec.ts +64 -64
  138. package/src/plugin/rules/ts/one-export-per-file.ts +77 -77
  139. package/src/scripts/dynamo-fix.ts +172 -172
  140. package/src/scripts/eslintrc-audit.ts +116 -116
  141. package/src/scripts/fix-return-types.spec.ts +65 -65
  142. package/src/scripts/fix-return-types.ts +286 -285
  143. package/src/scripts/validate-imports.ts +288 -257
  144. package/src/scripts/validate-naming.ts +209 -183
  145. package/src/scripts/validator-cli.integration.spec.ts +99 -0
  146. package/tsconfig.json +31 -31
@@ -1,583 +1,583 @@
1
- import preferEnumRule from './prefer-enum-over-string-union';
2
-
3
- describe('| prefer-enum-over-string-union', () => {
4
- it('| should be a valid ESLint rule', () => {
5
- expect(preferEnumRule.meta.type).toBe('suggestion');
6
- expect(preferEnumRule.meta.docs.description).toBe('Suggest using enums instead of union types of string literals');
7
- expect(preferEnumRule.meta.fixable).toBe('code');
8
- });
9
-
10
- it('| should not report when union has less than threshold values', () => {
11
- let reportCalled = false;
12
- const mockContext = {
13
- report: (options: any) => {
14
- reportCalled = true;
15
- },
16
- options: [{ minValues: 3 }],
17
- sourceCode: {
18
- getText: () => '',
19
- },
20
- } as any;
21
-
22
- const rule = preferEnumRule.create(mockContext);
23
-
24
- const mockNode = {
25
- type: 'TSTypeAliasDeclaration',
26
- id: { name: 'Status' },
27
- typeAnnotation: {
28
- type: 'TSUnionType',
29
- types: [
30
- { type: 'TSLiteralType', literal: { value: 'active' } },
31
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
32
- ],
33
- },
34
- } as any;
35
-
36
- rule.TSTypeAliasDeclaration(mockNode);
37
-
38
- expect(reportCalled).toBe(false);
39
- });
40
-
41
- it('| should report when union has threshold or more string literals', () => {
42
- let reportCalled = false;
43
- const mockContext = {
44
- report: (options: any) => {
45
- reportCalled = true;
46
- expect(options.messageId).toBe('preferEnum');
47
- expect(options.data.count).toBe('3');
48
- expect(options.fix).toBeDefined();
49
- },
50
- options: [{ minValues: 3 }],
51
- sourceCode: {
52
- getText: () => 'type Status = \'active\' | \'inactive\' | \'pending\'',
53
- },
54
- } as any;
55
-
56
- const rule = preferEnumRule.create(mockContext);
57
-
58
- const mockNode = {
59
- type: 'TSTypeAliasDeclaration',
60
- id: { name: 'Status' },
61
- typeAnnotation: {
62
- type: 'TSUnionType',
63
- types: [
64
- { type: 'TSLiteralType', literal: { value: 'active' } },
65
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
66
- { type: 'TSLiteralType', literal: { value: 'pending' } },
67
- ],
68
- },
69
- } as any;
70
-
71
- rule.TSTypeAliasDeclaration(mockNode);
72
-
73
- expect(reportCalled).toBe(true);
74
- });
75
-
76
- it('| should not report when union contains non-string literals', () => {
77
- let reportCalled = false;
78
- const mockContext = {
79
- report: (options: any) => {
80
- reportCalled = true;
81
- },
82
- options: [{ minValues: 3 }],
83
- sourceCode: {
84
- getText: () => '',
85
- },
86
- } as any;
87
-
88
- const rule = preferEnumRule.create(mockContext);
89
-
90
- const mockNode = {
91
- type: 'TSTypeAliasDeclaration',
92
- id: { name: 'Value' },
93
- typeAnnotation: {
94
- type: 'TSUnionType',
95
- types: [
96
- { type: 'TSLiteralType', literal: { value: 'active' } },
97
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
98
- { type: 'TSNumberKeyword' },
99
- ],
100
- },
101
- } as any;
102
-
103
- rule.TSTypeAliasDeclaration(mockNode);
104
-
105
- expect(reportCalled).toBe(false);
106
- });
107
-
108
- it('| should not report when allowTypeAliases is true', () => {
109
- let reportCalled = false;
110
- const mockContext = {
111
- report: (options: any) => {
112
- reportCalled = true;
113
- },
114
- options: [{ minValues: 3, allowTypeAliases: true }],
115
- sourceCode: {
116
- getText: () => '',
117
- },
118
- } as any;
119
-
120
- const rule = preferEnumRule.create(mockContext);
121
-
122
- const mockNode = {
123
- type: 'TSTypeAliasDeclaration',
124
- id: { name: 'Status' },
125
- typeAnnotation: {
126
- type: 'TSUnionType',
127
- types: [
128
- { type: 'TSLiteralType', literal: { value: 'active' } },
129
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
130
- { type: 'TSLiteralType', literal: { value: 'pending' } },
131
- ],
132
- },
133
- } as any;
134
-
135
- rule.TSTypeAliasDeclaration(mockNode);
136
-
137
- expect(reportCalled).toBe(false);
138
- });
139
-
140
- it('| should report on interface properties', () => {
141
- let reportCalled = false;
142
- const mockContext = {
143
- report: (options: any) => {
144
- reportCalled = true;
145
- expect(options.messageId).toBe('preferEnum');
146
- },
147
- options: [{ minValues: 3 }],
148
- sourceCode: {
149
- getText: () => '',
150
- },
151
- } as any;
152
-
153
- const rule = preferEnumRule.create(mockContext);
154
-
155
- const mockNode = {
156
- type: 'TSPropertySignature',
157
- key: { name: 'role' },
158
- typeAnnotation: {
159
- typeAnnotation: {
160
- type: 'TSUnionType',
161
- types: [
162
- { type: 'TSLiteralType', literal: { value: 'admin' } },
163
- { type: 'TSLiteralType', literal: { value: 'user' } },
164
- { type: 'TSLiteralType', literal: { value: 'guest' } },
165
- ],
166
- },
167
- },
168
- parent: { type: 'TSInterfaceDeclaration' },
169
- } as any;
170
-
171
- rule.TSPropertySignature(mockNode);
172
-
173
- expect(reportCalled).toBe(true);
174
- });
175
-
176
- it('| should report on class properties', () => {
177
- let reportCalled = false;
178
- const mockContext = {
179
- report: (options: any) => {
180
- reportCalled = true;
181
- expect(options.messageId).toBe('preferEnum');
182
- },
183
- options: [{ minValues: 3 }],
184
- sourceCode: {
185
- getText: () => '',
186
- },
187
- } as any;
188
-
189
- const rule = preferEnumRule.create(mockContext);
190
-
191
- const mockNode = {
192
- type: 'PropertyDefinition',
193
- key: { name: 'status' },
194
- typeAnnotation: {
195
- typeAnnotation: {
196
- type: 'TSUnionType',
197
- types: [
198
- { type: 'TSLiteralType', literal: { value: 'active' } },
199
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
200
- { type: 'TSLiteralType', literal: { value: 'pending' } },
201
- ],
202
- },
203
- },
204
- parent: { type: 'ClassDeclaration' },
205
- } as any;
206
-
207
- rule.PropertyDefinition(mockNode);
208
-
209
- expect(reportCalled).toBe(true);
210
- });
211
-
212
- it('| should report on function parameters', () => {
213
- let reportCalled = false;
214
- const mockContext = {
215
- report: (options: any) => {
216
- reportCalled = true;
217
- expect(options.messageId).toBe('preferEnum');
218
- },
219
- options: [{ minValues: 3 }],
220
- sourceCode: {
221
- getText: () => '',
222
- },
223
- } as any;
224
-
225
- const rule = preferEnumRule.create(mockContext);
226
-
227
- const mockNode = {
228
- type: 'FunctionDeclaration',
229
- id: { name: 'setStatus' },
230
- params: [
231
- {
232
- type: 'Identifier',
233
- name: 'status',
234
- typeAnnotation: {
235
- typeAnnotation: {
236
- type: 'TSUnionType',
237
- types: [
238
- { type: 'TSLiteralType', literal: { value: 'active' } },
239
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
240
- { type: 'TSLiteralType', literal: { value: 'pending' } },
241
- ],
242
- },
243
- },
244
- },
245
- ],
246
- } as any;
247
-
248
- rule.FunctionDeclaration(mockNode);
249
-
250
- expect(reportCalled).toBe(true);
251
- });
252
-
253
- it('| should report on arrow function parameters', () => {
254
- let reportCalled = false;
255
- const mockContext = {
256
- report: (options: any) => {
257
- reportCalled = true;
258
- expect(options.messageId).toBe('preferEnum');
259
- },
260
- options: [{ minValues: 3 }],
261
- sourceCode: {
262
- getText: () => '',
263
- },
264
- } as any;
265
-
266
- const rule = preferEnumRule.create(mockContext);
267
-
268
- const mockNode = {
269
- type: 'ArrowFunctionExpression',
270
- params: [
271
- {
272
- type: 'Identifier',
273
- name: 'status',
274
- typeAnnotation: {
275
- typeAnnotation: {
276
- type: 'TSUnionType',
277
- types: [
278
- { type: 'TSLiteralType', literal: { value: 'active' } },
279
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
280
- { type: 'TSLiteralType', literal: { value: 'pending' } },
281
- ],
282
- },
283
- },
284
- },
285
- ],
286
- } as any;
287
-
288
- rule.ArrowFunctionExpression(mockNode);
289
-
290
- expect(reportCalled).toBe(true);
291
- });
292
-
293
- it('| should report on variable declarations', () => {
294
- let reportCalled = false;
295
- const mockContext = {
296
- report: (options: any) => {
297
- reportCalled = true;
298
- expect(options.messageId).toBe('preferEnum');
299
- },
300
- options: [{ minValues: 3 }],
301
- sourceCode: {
302
- getText: () => '',
303
- },
304
- } as any;
305
-
306
- const rule = preferEnumRule.create(mockContext);
307
-
308
- const mockNode = {
309
- type: 'VariableDeclarator',
310
- id: { name: 'status' },
311
- typeAnnotation: {
312
- typeAnnotation: {
313
- type: 'TSUnionType',
314
- types: [
315
- { type: 'TSLiteralType', literal: { value: 'active' } },
316
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
317
- { type: 'TSLiteralType', literal: { value: 'pending' } },
318
- ],
319
- },
320
- },
321
- parent: { type: 'VariableDeclaration' },
322
- } as any;
323
-
324
- rule.VariableDeclarator(mockNode);
325
-
326
- expect(reportCalled).toBe(true);
327
- });
328
-
329
- it('| should not report on Angular @Input() properties', () => {
330
- let reportCalled = false;
331
- const mockContext = {
332
- report: (options: any) => {
333
- reportCalled = true;
334
- },
335
- options: [{ minValues: 3 }],
336
- sourceCode: {
337
- getText: () => '',
338
- },
339
- } as any;
340
-
341
- const rule = preferEnumRule.create(mockContext);
342
-
343
- const mockNode = {
344
- type: 'PropertyDefinition',
345
- key: { name: 'mode' },
346
- decorators: [
347
- {
348
- expression: {
349
- type: 'CallExpression',
350
- callee: { name: 'Input' },
351
- },
352
- },
353
- ],
354
- typeAnnotation: {
355
- typeAnnotation: {
356
- type: 'TSUnionType',
357
- types: [
358
- { type: 'TSLiteralType', literal: { value: 'light' } },
359
- { type: 'TSLiteralType', literal: { value: 'dark' } },
360
- { type: 'TSLiteralType', literal: { value: 'auto' } },
361
- ],
362
- },
363
- },
364
- parent: { type: 'ClassDeclaration' },
365
- } as any;
366
-
367
- rule.PropertyDefinition(mockNode);
368
-
369
- expect(reportCalled).toBe(false);
370
- });
371
-
372
- it('| should not report on Angular input() signal properties', () => {
373
- let reportCalled = false;
374
- const mockContext = {
375
- report: (options: any) => {
376
- reportCalled = true;
377
- },
378
- options: [{ minValues: 3 }],
379
- sourceCode: {
380
- getText: () => '',
381
- },
382
- } as any;
383
-
384
- const rule = preferEnumRule.create(mockContext);
385
-
386
- const mockNode = {
387
- type: 'PropertyDefinition',
388
- key: { name: 'size' },
389
- value: {
390
- type: 'CallExpression',
391
- callee: { name: 'input' },
392
- },
393
- typeAnnotation: {
394
- typeAnnotation: {
395
- type: 'TSUnionType',
396
- types: [
397
- { type: 'TSLiteralType', literal: { value: 'small' } },
398
- { type: 'TSLiteralType', literal: { value: 'medium' } },
399
- { type: 'TSLiteralType', literal: { value: 'large' } },
400
- ],
401
- },
402
- },
403
- parent: { type: 'ClassDeclaration' },
404
- } as any;
405
-
406
- rule.PropertyDefinition(mockNode);
407
-
408
- expect(reportCalled).toBe(false);
409
- });
410
-
411
- it('| should not report on Angular input.required() signal properties', () => {
412
- let reportCalled = false;
413
- const mockContext = {
414
- report: (options: any) => {
415
- reportCalled = true;
416
- },
417
- options: [{ minValues: 3 }],
418
- sourceCode: {
419
- getText: () => '',
420
- },
421
- } as any;
422
-
423
- const rule = preferEnumRule.create(mockContext);
424
-
425
- const mockNode = {
426
- type: 'PropertyDefinition',
427
- key: { name: 'theme' },
428
- value: {
429
- type: 'CallExpression',
430
- callee: {
431
- type: 'MemberExpression',
432
- object: { name: 'input' },
433
- property: { name: 'required' },
434
- },
435
- },
436
- typeAnnotation: {
437
- typeAnnotation: {
438
- type: 'TSUnionType',
439
- types: [
440
- { type: 'TSLiteralType', literal: { value: 'light' } },
441
- { type: 'TSLiteralType', literal: { value: 'dark' } },
442
- { type: 'TSLiteralType', literal: { value: 'auto' } },
443
- ],
444
- },
445
- },
446
- parent: { type: 'ClassDeclaration' },
447
- } as any;
448
-
449
- rule.PropertyDefinition(mockNode);
450
-
451
- expect(reportCalled).toBe(false);
452
- });
453
-
454
- it('| should handle custom minValues threshold', () => {
455
- let reportCalled = false;
456
- const mockContext = {
457
- report: (options: any) => {
458
- reportCalled = true;
459
- expect(options.data.count).toBe('2');
460
- },
461
- options: [{ minValues: 2 }],
462
- sourceCode: {
463
- getText: () => '',
464
- },
465
- } as any;
466
-
467
- const rule = preferEnumRule.create(mockContext);
468
-
469
- const mockNode = {
470
- type: 'TSTypeAliasDeclaration',
471
- id: { name: 'Binary' },
472
- typeAnnotation: {
473
- type: 'TSUnionType',
474
- types: [
475
- { type: 'TSLiteralType', literal: { value: 'yes' } },
476
- { type: 'TSLiteralType', literal: { value: 'no' } },
477
- ],
478
- },
479
- } as any;
480
-
481
- rule.TSTypeAliasDeclaration(mockNode);
482
-
483
- expect(reportCalled).toBe(true);
484
- });
485
-
486
- it('| should handle edge cases with empty strings and special characters', () => {
487
- let reportCalled = false;
488
- const mockContext = {
489
- report: (options: any) => {
490
- reportCalled = true;
491
- expect(options.data.count).toBe('3');
492
- },
493
- options: [{ minValues: 3 }],
494
- sourceCode: {
495
- getText: () => '',
496
- },
497
- } as any;
498
-
499
- const rule = preferEnumRule.create(mockContext);
500
-
501
- const mockNode = {
502
- type: 'TSTypeAliasDeclaration',
503
- id: { name: 'Special' },
504
- typeAnnotation: {
505
- type: 'TSUnionType',
506
- types: [
507
- { type: 'TSLiteralType', literal: { value: '' } },
508
- { type: 'TSLiteralType', literal: { value: 'hello-world' } },
509
- { type: 'TSLiteralType', literal: { value: 'test_value' } },
510
- ],
511
- },
512
- } as any;
513
-
514
- rule.TSTypeAliasDeclaration(mockNode);
515
-
516
- expect(reportCalled).toBe(true);
517
- });
518
-
519
- it('| should provide auto-fix for type aliases', () => {
520
- const mockContext = {
521
- report: (options: any) => {
522
- expect(options.fix).toBeDefined();
523
- expect(typeof options.fix).toBe('function');
524
-
525
- // Test the fix function
526
- const fixResult = options.fix({
527
- replaceText: (node: any, text: string) => {
528
- expect(text).toContain('Status');
529
-
530
- return [];
531
- },
532
- });
533
-
534
- expect(Array.isArray(fixResult)).toBe(true);
535
- },
536
- options: [{ minValues: 3 }],
537
- sourceCode: {
538
- getText: () => 'type Status = \'active\' | \'inactive\' | \'pending\'',
539
- },
540
- } as any;
541
-
542
- const rule = preferEnumRule.create(mockContext);
543
-
544
- const mockNode = {
545
- type: 'TSTypeAliasDeclaration',
546
- id: { name: 'Status' },
547
- typeAnnotation: {
548
- type: 'TSUnionType',
549
- types: [
550
- { type: 'TSLiteralType', literal: { value: 'active' } },
551
- { type: 'TSLiteralType', literal: { value: 'inactive' } },
552
- { type: 'TSLiteralType', literal: { value: 'pending' } },
553
- ],
554
- },
555
- } as any;
556
-
557
- rule.TSTypeAliasDeclaration(mockNode);
558
- });
559
-
560
- it('| should handle errors gracefully', () => {
561
- const mockContext = {
562
- report: (options: any) => {
563
- // Should not throw
564
- },
565
- options: [{ minValues: 3 }],
566
- sourceCode: {
567
- getText: () => '',
568
- },
569
- } as any;
570
-
571
- const rule = preferEnumRule.create(mockContext);
572
-
573
- // Test with malformed nodes
574
- const malformedNode = {
575
- type: 'TSTypeAliasDeclaration',
576
- // Missing required properties
577
- } as any;
578
-
579
- expect(() => {
580
- rule.TSTypeAliasDeclaration(malformedNode);
581
- }).not.toThrow();
582
- });
583
- });
1
+ import preferEnumRule from './prefer-enum-over-string-union';
2
+
3
+ describe('| prefer-enum-over-string-union', () => {
4
+ it('| should be a valid ESLint rule', () => {
5
+ expect(preferEnumRule.meta.type).toBe('suggestion');
6
+ expect(preferEnumRule.meta.docs.description).toBe('Suggest using enums instead of union types of string literals');
7
+ expect(preferEnumRule.meta.fixable).toBe('code');
8
+ });
9
+
10
+ it('| should not report when union has less than threshold values', () => {
11
+ let reportCalled = false;
12
+ const mockContext = {
13
+ report: (options: any) => {
14
+ reportCalled = true;
15
+ },
16
+ options: [{ minValues: 3 }],
17
+ sourceCode: {
18
+ getText: () => '',
19
+ },
20
+ } as any;
21
+
22
+ const rule = preferEnumRule.create(mockContext);
23
+
24
+ const mockNode = {
25
+ type: 'TSTypeAliasDeclaration',
26
+ id: { name: 'Status' },
27
+ typeAnnotation: {
28
+ type: 'TSUnionType',
29
+ types: [
30
+ { type: 'TSLiteralType', literal: { value: 'active' } },
31
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
32
+ ],
33
+ },
34
+ } as any;
35
+
36
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
37
+
38
+ expect(reportCalled).toBe(false);
39
+ });
40
+
41
+ it('| should report when union has threshold or more string literals', () => {
42
+ let reportCalled = false;
43
+ const mockContext = {
44
+ report: (options: any) => {
45
+ reportCalled = true;
46
+ expect(options.messageId).toBe('preferEnum');
47
+ expect(options.data.count).toBe('3');
48
+ expect(options.fix).toBeDefined();
49
+ },
50
+ options: [{ minValues: 3 }],
51
+ sourceCode: {
52
+ getText: () => 'type Status = \'active\' | \'inactive\' | \'pending\'',
53
+ },
54
+ } as any;
55
+
56
+ const rule = preferEnumRule.create(mockContext);
57
+
58
+ const mockNode = {
59
+ type: 'TSTypeAliasDeclaration',
60
+ id: { name: 'Status' },
61
+ typeAnnotation: {
62
+ type: 'TSUnionType',
63
+ types: [
64
+ { type: 'TSLiteralType', literal: { value: 'active' } },
65
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
66
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
67
+ ],
68
+ },
69
+ } as any;
70
+
71
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
72
+
73
+ expect(reportCalled).toBe(true);
74
+ });
75
+
76
+ it('| should not report when union contains non-string literals', () => {
77
+ let reportCalled = false;
78
+ const mockContext = {
79
+ report: (options: any) => {
80
+ reportCalled = true;
81
+ },
82
+ options: [{ minValues: 3 }],
83
+ sourceCode: {
84
+ getText: () => '',
85
+ },
86
+ } as any;
87
+
88
+ const rule = preferEnumRule.create(mockContext);
89
+
90
+ const mockNode = {
91
+ type: 'TSTypeAliasDeclaration',
92
+ id: { name: 'Value' },
93
+ typeAnnotation: {
94
+ type: 'TSUnionType',
95
+ types: [
96
+ { type: 'TSLiteralType', literal: { value: 'active' } },
97
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
98
+ { type: 'TSNumberKeyword' },
99
+ ],
100
+ },
101
+ } as any;
102
+
103
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
104
+
105
+ expect(reportCalled).toBe(false);
106
+ });
107
+
108
+ it('| should not report when allowTypeAliases is true', () => {
109
+ let reportCalled = false;
110
+ const mockContext = {
111
+ report: (options: any) => {
112
+ reportCalled = true;
113
+ },
114
+ options: [{ minValues: 3, allowTypeAliases: true }],
115
+ sourceCode: {
116
+ getText: () => '',
117
+ },
118
+ } as any;
119
+
120
+ const rule = preferEnumRule.create(mockContext);
121
+
122
+ const mockNode = {
123
+ type: 'TSTypeAliasDeclaration',
124
+ id: { name: 'Status' },
125
+ typeAnnotation: {
126
+ type: 'TSUnionType',
127
+ types: [
128
+ { type: 'TSLiteralType', literal: { value: 'active' } },
129
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
130
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
131
+ ],
132
+ },
133
+ } as any;
134
+
135
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
136
+
137
+ expect(reportCalled).toBe(false);
138
+ });
139
+
140
+ it('| should report on interface properties', () => {
141
+ let reportCalled = false;
142
+ const mockContext = {
143
+ report: (options: any) => {
144
+ reportCalled = true;
145
+ expect(options.messageId).toBe('preferEnum');
146
+ },
147
+ options: [{ minValues: 3 }],
148
+ sourceCode: {
149
+ getText: () => '',
150
+ },
151
+ } as any;
152
+
153
+ const rule = preferEnumRule.create(mockContext);
154
+
155
+ const mockNode = {
156
+ type: 'TSPropertySignature',
157
+ key: { name: 'role' },
158
+ typeAnnotation: {
159
+ typeAnnotation: {
160
+ type: 'TSUnionType',
161
+ types: [
162
+ { type: 'TSLiteralType', literal: { value: 'admin' } },
163
+ { type: 'TSLiteralType', literal: { value: 'user' } },
164
+ { type: 'TSLiteralType', literal: { value: 'guest' } },
165
+ ],
166
+ },
167
+ },
168
+ parent: { type: 'TSInterfaceDeclaration' },
169
+ } as any;
170
+
171
+ rule.TSPropertySignature(mockNode, undefined, undefined);
172
+
173
+ expect(reportCalled).toBe(true);
174
+ });
175
+
176
+ it('| should report on class properties', () => {
177
+ let reportCalled = false;
178
+ const mockContext = {
179
+ report: (options: any) => {
180
+ reportCalled = true;
181
+ expect(options.messageId).toBe('preferEnum');
182
+ },
183
+ options: [{ minValues: 3 }],
184
+ sourceCode: {
185
+ getText: () => '',
186
+ },
187
+ } as any;
188
+
189
+ const rule = preferEnumRule.create(mockContext);
190
+
191
+ const mockNode = {
192
+ type: 'PropertyDefinition',
193
+ key: { name: 'status' },
194
+ typeAnnotation: {
195
+ typeAnnotation: {
196
+ type: 'TSUnionType',
197
+ types: [
198
+ { type: 'TSLiteralType', literal: { value: 'active' } },
199
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
200
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
201
+ ],
202
+ },
203
+ },
204
+ parent: { type: 'ClassDeclaration' },
205
+ } as any;
206
+
207
+ rule.PropertyDefinition(mockNode);
208
+
209
+ expect(reportCalled).toBe(true);
210
+ });
211
+
212
+ it('| should report on function parameters', () => {
213
+ let reportCalled = false;
214
+ const mockContext = {
215
+ report: (options: any) => {
216
+ reportCalled = true;
217
+ expect(options.messageId).toBe('preferEnum');
218
+ },
219
+ options: [{ minValues: 3 }],
220
+ sourceCode: {
221
+ getText: () => '',
222
+ },
223
+ } as any;
224
+
225
+ const rule = preferEnumRule.create(mockContext);
226
+
227
+ const mockNode = {
228
+ type: 'FunctionDeclaration',
229
+ id: { name: 'setStatus' },
230
+ params: [
231
+ {
232
+ type: 'Identifier',
233
+ name: 'status',
234
+ typeAnnotation: {
235
+ typeAnnotation: {
236
+ type: 'TSUnionType',
237
+ types: [
238
+ { type: 'TSLiteralType', literal: { value: 'active' } },
239
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
240
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
241
+ ],
242
+ },
243
+ },
244
+ },
245
+ ],
246
+ } as any;
247
+
248
+ rule.FunctionDeclaration(mockNode);
249
+
250
+ expect(reportCalled).toBe(true);
251
+ });
252
+
253
+ it('| should report on arrow function parameters', () => {
254
+ let reportCalled = false;
255
+ const mockContext = {
256
+ report: (options: any) => {
257
+ reportCalled = true;
258
+ expect(options.messageId).toBe('preferEnum');
259
+ },
260
+ options: [{ minValues: 3 }],
261
+ sourceCode: {
262
+ getText: () => '',
263
+ },
264
+ } as any;
265
+
266
+ const rule = preferEnumRule.create(mockContext);
267
+
268
+ const mockNode = {
269
+ type: 'ArrowFunctionExpression',
270
+ params: [
271
+ {
272
+ type: 'Identifier',
273
+ name: 'status',
274
+ typeAnnotation: {
275
+ typeAnnotation: {
276
+ type: 'TSUnionType',
277
+ types: [
278
+ { type: 'TSLiteralType', literal: { value: 'active' } },
279
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
280
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
281
+ ],
282
+ },
283
+ },
284
+ },
285
+ ],
286
+ } as any;
287
+
288
+ rule.ArrowFunctionExpression(mockNode);
289
+
290
+ expect(reportCalled).toBe(true);
291
+ });
292
+
293
+ it('| should report on variable declarations', () => {
294
+ let reportCalled = false;
295
+ const mockContext = {
296
+ report: (options: any) => {
297
+ reportCalled = true;
298
+ expect(options.messageId).toBe('preferEnum');
299
+ },
300
+ options: [{ minValues: 3 }],
301
+ sourceCode: {
302
+ getText: () => '',
303
+ },
304
+ } as any;
305
+
306
+ const rule = preferEnumRule.create(mockContext);
307
+
308
+ const mockNode = {
309
+ type: 'VariableDeclarator',
310
+ id: { name: 'status' },
311
+ typeAnnotation: {
312
+ typeAnnotation: {
313
+ type: 'TSUnionType',
314
+ types: [
315
+ { type: 'TSLiteralType', literal: { value: 'active' } },
316
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
317
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
318
+ ],
319
+ },
320
+ },
321
+ parent: { type: 'VariableDeclaration' },
322
+ } as any;
323
+
324
+ rule.VariableDeclarator(mockNode);
325
+
326
+ expect(reportCalled).toBe(true);
327
+ });
328
+
329
+ it('| should not report on Angular @Input() properties', () => {
330
+ let reportCalled = false;
331
+ const mockContext = {
332
+ report: (options: any) => {
333
+ reportCalled = true;
334
+ },
335
+ options: [{ minValues: 3 }],
336
+ sourceCode: {
337
+ getText: () => '',
338
+ },
339
+ } as any;
340
+
341
+ const rule = preferEnumRule.create(mockContext);
342
+
343
+ const mockNode = {
344
+ type: 'PropertyDefinition',
345
+ key: { name: 'mode' },
346
+ decorators: [
347
+ {
348
+ expression: {
349
+ type: 'CallExpression',
350
+ callee: { name: 'Input' },
351
+ },
352
+ },
353
+ ],
354
+ typeAnnotation: {
355
+ typeAnnotation: {
356
+ type: 'TSUnionType',
357
+ types: [
358
+ { type: 'TSLiteralType', literal: { value: 'light' } },
359
+ { type: 'TSLiteralType', literal: { value: 'dark' } },
360
+ { type: 'TSLiteralType', literal: { value: 'auto' } },
361
+ ],
362
+ },
363
+ },
364
+ parent: { type: 'ClassDeclaration' },
365
+ } as any;
366
+
367
+ rule.PropertyDefinition(mockNode);
368
+
369
+ expect(reportCalled).toBe(false);
370
+ });
371
+
372
+ it('| should not report on Angular input() signal properties', () => {
373
+ let reportCalled = false;
374
+ const mockContext = {
375
+ report: (options: any) => {
376
+ reportCalled = true;
377
+ },
378
+ options: [{ minValues: 3 }],
379
+ sourceCode: {
380
+ getText: () => '',
381
+ },
382
+ } as any;
383
+
384
+ const rule = preferEnumRule.create(mockContext);
385
+
386
+ const mockNode = {
387
+ type: 'PropertyDefinition',
388
+ key: { name: 'size' },
389
+ value: {
390
+ type: 'CallExpression',
391
+ callee: { name: 'input' },
392
+ },
393
+ typeAnnotation: {
394
+ typeAnnotation: {
395
+ type: 'TSUnionType',
396
+ types: [
397
+ { type: 'TSLiteralType', literal: { value: 'small' } },
398
+ { type: 'TSLiteralType', literal: { value: 'medium' } },
399
+ { type: 'TSLiteralType', literal: { value: 'large' } },
400
+ ],
401
+ },
402
+ },
403
+ parent: { type: 'ClassDeclaration' },
404
+ } as any;
405
+
406
+ rule.PropertyDefinition(mockNode);
407
+
408
+ expect(reportCalled).toBe(false);
409
+ });
410
+
411
+ it('| should not report on Angular input.required() signal properties', () => {
412
+ let reportCalled = false;
413
+ const mockContext = {
414
+ report: (options: any) => {
415
+ reportCalled = true;
416
+ },
417
+ options: [{ minValues: 3 }],
418
+ sourceCode: {
419
+ getText: () => '',
420
+ },
421
+ } as any;
422
+
423
+ const rule = preferEnumRule.create(mockContext);
424
+
425
+ const mockNode = {
426
+ type: 'PropertyDefinition',
427
+ key: { name: 'theme' },
428
+ value: {
429
+ type: 'CallExpression',
430
+ callee: {
431
+ type: 'MemberExpression',
432
+ object: { name: 'input' },
433
+ property: { name: 'required' },
434
+ },
435
+ },
436
+ typeAnnotation: {
437
+ typeAnnotation: {
438
+ type: 'TSUnionType',
439
+ types: [
440
+ { type: 'TSLiteralType', literal: { value: 'light' } },
441
+ { type: 'TSLiteralType', literal: { value: 'dark' } },
442
+ { type: 'TSLiteralType', literal: { value: 'auto' } },
443
+ ],
444
+ },
445
+ },
446
+ parent: { type: 'ClassDeclaration' },
447
+ } as any;
448
+
449
+ rule.PropertyDefinition(mockNode);
450
+
451
+ expect(reportCalled).toBe(false);
452
+ });
453
+
454
+ it('| should handle custom minValues threshold', () => {
455
+ let reportCalled = false;
456
+ const mockContext = {
457
+ report: (options: any) => {
458
+ reportCalled = true;
459
+ expect(options.data.count).toBe('2');
460
+ },
461
+ options: [{ minValues: 2 }],
462
+ sourceCode: {
463
+ getText: () => '',
464
+ },
465
+ } as any;
466
+
467
+ const rule = preferEnumRule.create(mockContext);
468
+
469
+ const mockNode = {
470
+ type: 'TSTypeAliasDeclaration',
471
+ id: { name: 'Binary' },
472
+ typeAnnotation: {
473
+ type: 'TSUnionType',
474
+ types: [
475
+ { type: 'TSLiteralType', literal: { value: 'yes' } },
476
+ { type: 'TSLiteralType', literal: { value: 'no' } },
477
+ ],
478
+ },
479
+ } as any;
480
+
481
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
482
+
483
+ expect(reportCalled).toBe(true);
484
+ });
485
+
486
+ it('| should handle edge cases with empty strings and special characters', () => {
487
+ let reportCalled = false;
488
+ const mockContext = {
489
+ report: (options: any) => {
490
+ reportCalled = true;
491
+ expect(options.data.count).toBe('3');
492
+ },
493
+ options: [{ minValues: 3 }],
494
+ sourceCode: {
495
+ getText: () => '',
496
+ },
497
+ } as any;
498
+
499
+ const rule = preferEnumRule.create(mockContext);
500
+
501
+ const mockNode = {
502
+ type: 'TSTypeAliasDeclaration',
503
+ id: { name: 'Special' },
504
+ typeAnnotation: {
505
+ type: 'TSUnionType',
506
+ types: [
507
+ { type: 'TSLiteralType', literal: { value: '' } },
508
+ { type: 'TSLiteralType', literal: { value: 'hello-world' } },
509
+ { type: 'TSLiteralType', literal: { value: 'test_value' } },
510
+ ],
511
+ },
512
+ } as any;
513
+
514
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
515
+
516
+ expect(reportCalled).toBe(true);
517
+ });
518
+
519
+ it('| should provide auto-fix for type aliases', () => {
520
+ const mockContext = {
521
+ report: (options: any) => {
522
+ expect(options.fix).toBeDefined();
523
+ expect(typeof options.fix).toBe('function');
524
+
525
+ // Test the fix function
526
+ const fixResult = options.fix({
527
+ replaceText: (node: any, text: string) => {
528
+ expect(text).toContain('Status');
529
+
530
+ return [];
531
+ },
532
+ });
533
+
534
+ expect(Array.isArray(fixResult)).toBe(true);
535
+ },
536
+ options: [{ minValues: 3 }],
537
+ sourceCode: {
538
+ getText: () => 'type Status = \'active\' | \'inactive\' | \'pending\'',
539
+ },
540
+ } as any;
541
+
542
+ const rule = preferEnumRule.create(mockContext);
543
+
544
+ const mockNode = {
545
+ type: 'TSTypeAliasDeclaration',
546
+ id: { name: 'Status' },
547
+ typeAnnotation: {
548
+ type: 'TSUnionType',
549
+ types: [
550
+ { type: 'TSLiteralType', literal: { value: 'active' } },
551
+ { type: 'TSLiteralType', literal: { value: 'inactive' } },
552
+ { type: 'TSLiteralType', literal: { value: 'pending' } },
553
+ ],
554
+ },
555
+ } as any;
556
+
557
+ rule.TSTypeAliasDeclaration(mockNode, undefined, undefined);
558
+ });
559
+
560
+ it('| should handle errors gracefully', () => {
561
+ const mockContext = {
562
+ report: (options: any) => {
563
+ // Should not throw
564
+ },
565
+ options: [{ minValues: 3 }],
566
+ sourceCode: {
567
+ getText: () => '',
568
+ },
569
+ } as any;
570
+
571
+ const rule = preferEnumRule.create(mockContext);
572
+
573
+ // Test with malformed nodes
574
+ const malformedNode = {
575
+ type: 'TSTypeAliasDeclaration',
576
+ // Missing required properties
577
+ } as any;
578
+
579
+ expect(() => {
580
+ rule.TSTypeAliasDeclaration(malformedNode, undefined, undefined);
581
+ }).not.toThrow();
582
+ });
583
+ });