@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,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);
|
|
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
|
+
});
|