@futdevpro/dynamo-eslint 1.14.6 → 1.14.7
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/build/configs/base.js +12 -1
- package/build/configs/base.js.map +1 -1
- package/build/plugin/index.d.ts +8 -0
- package/build/plugin/index.d.ts.map +1 -1
- package/build/plugin/index.js +12 -0
- package/build/plugin/index.js.map +1 -1
- package/build/plugin/rules/explicit-types.js +2 -2
- package/build/plugin/rules/explicit-types.js.map +1 -1
- package/build/plugin/rules/prefer-enum-over-string-union.d.ts +4 -0
- package/build/plugin/rules/prefer-enum-over-string-union.d.ts.map +1 -0
- package/build/plugin/rules/prefer-enum-over-string-union.js +290 -0
- package/build/plugin/rules/prefer-enum-over-string-union.js.map +1 -0
- package/build/plugin/rules/prefer-enum-over-string-union.spec.d.ts +2 -0
- package/build/plugin/rules/prefer-enum-over-string-union.spec.d.ts.map +1 -0
- package/build/plugin/rules/prefer-enum-over-string-union.spec.js +505 -0
- package/build/plugin/rules/prefer-enum-over-string-union.spec.js.map +1 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.d.ts +4 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.d.ts.map +1 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.js +231 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.js.map +1 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.spec.d.ts +2 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.spec.d.ts.map +1 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.spec.js +324 -0
- package/build/plugin/rules/prefer-interface-over-duplicate-types.spec.js.map +1 -0
- package/build/plugin/rules/require-jsdoc-description.d.ts +4 -0
- package/build/plugin/rules/require-jsdoc-description.d.ts.map +1 -0
- package/build/plugin/rules/require-jsdoc-description.js +379 -0
- package/build/plugin/rules/require-jsdoc-description.js.map +1 -0
- package/build/plugin/rules/require-jsdoc-description.spec.d.ts +2 -0
- package/build/plugin/rules/require-jsdoc-description.spec.d.ts.map +1 -0
- package/build/plugin/rules/require-jsdoc-description.spec.js +452 -0
- package/build/plugin/rules/require-jsdoc-description.spec.js.map +1 -0
- package/build/plugin/rules/require-test-description-prefix.d.ts +4 -0
- package/build/plugin/rules/require-test-description-prefix.d.ts.map +1 -0
- package/build/plugin/rules/require-test-description-prefix.js +135 -0
- package/build/plugin/rules/require-test-description-prefix.js.map +1 -0
- package/build/plugin/rules/require-test-description-prefix.spec.d.ts +2 -0
- package/build/plugin/rules/require-test-description-prefix.spec.d.ts.map +1 -0
- package/build/plugin/rules/require-test-description-prefix.spec.js +371 -0
- package/build/plugin/rules/require-test-description-prefix.spec.js.map +1 -0
- package/futdevpro-dynamo-eslint-1.14.7.tgz +0 -0
- package/package.json +1 -1
- package/src/configs/base.ts +12 -1
- package/src/plugin/index.ts +12 -0
- package/src/plugin/rules/explicit-types.ts +2 -2
- package/src/plugin/rules/prefer-enum-over-string-union.spec.ts +583 -0
- package/src/plugin/rules/prefer-enum-over-string-union.ts +333 -0
- package/src/plugin/rules/prefer-interface-over-duplicate-types.spec.ts +374 -0
- package/src/plugin/rules/prefer-interface-over-duplicate-types.ts +276 -0
- package/src/plugin/rules/require-jsdoc-description.spec.ts +542 -0
- package/src/plugin/rules/require-jsdoc-description.ts +436 -0
- package/src/plugin/rules/require-test-description-prefix.spec.ts +459 -0
- package/src/plugin/rules/require-test-description-prefix.ts +153 -0
- package/futdevpro-dynamo-eslint-1.14.6.tgz +0 -0
|
@@ -0,0 +1,333 @@
|
|
|
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) {
|
|
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) {
|
|
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) {
|
|
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) {
|
|
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) {
|
|
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) {
|
|
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) {
|
|
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;
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import preferInterfaceRule from './prefer-interface-over-duplicate-types';
|
|
2
|
+
|
|
3
|
+
describe('| prefer-interface-over-duplicate-types', () => {
|
|
4
|
+
it('| should be a valid ESLint rule', () => {
|
|
5
|
+
expect(preferInterfaceRule.meta?.type).toBe('suggestion');
|
|
6
|
+
expect(preferInterfaceRule.meta?.docs?.description).toContain('Suggest creating interfaces');
|
|
7
|
+
expect(preferInterfaceRule.meta?.fixable).toBe('code');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('| should have create function that returns visitor object', () => {
|
|
11
|
+
const mockContext = {
|
|
12
|
+
report: () => {},
|
|
13
|
+
options: [{ threshold: 3 }],
|
|
14
|
+
sourceCode: {
|
|
15
|
+
getText: () => '',
|
|
16
|
+
getScope: () => ({ variables: [] }),
|
|
17
|
+
ast: { body: [] },
|
|
18
|
+
},
|
|
19
|
+
} as any;
|
|
20
|
+
|
|
21
|
+
const result = preferInterfaceRule.create(mockContext);
|
|
22
|
+
|
|
23
|
+
expect(typeof result).toBe('object');
|
|
24
|
+
expect(typeof result.TSTypeLiteral).toBe('function');
|
|
25
|
+
expect(typeof result['Program:exit']).toBe('function');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('| should not report when object type is used less than threshold', () => {
|
|
29
|
+
let reportCalled = false;
|
|
30
|
+
const mockContext = {
|
|
31
|
+
report: (options: any) => {
|
|
32
|
+
reportCalled = true;
|
|
33
|
+
},
|
|
34
|
+
options: [{ threshold: 3 }],
|
|
35
|
+
sourceCode: {
|
|
36
|
+
getText: () => '{ name: string; age: number }',
|
|
37
|
+
getScope: () => ({ variables: [] }),
|
|
38
|
+
ast: { body: [] },
|
|
39
|
+
},
|
|
40
|
+
} as any;
|
|
41
|
+
|
|
42
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
43
|
+
|
|
44
|
+
// Simulate 2 usages (below threshold of 3)
|
|
45
|
+
const mockNode1 = {
|
|
46
|
+
type: 'TSTypeLiteral',
|
|
47
|
+
members: [
|
|
48
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
49
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
50
|
+
],
|
|
51
|
+
} as any;
|
|
52
|
+
|
|
53
|
+
const mockNode2 = {
|
|
54
|
+
type: 'TSTypeLiteral',
|
|
55
|
+
members: [
|
|
56
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
57
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
58
|
+
],
|
|
59
|
+
} as any;
|
|
60
|
+
|
|
61
|
+
rule.TSTypeLiteral(mockNode1);
|
|
62
|
+
rule.TSTypeLiteral(mockNode2);
|
|
63
|
+
rule['Program:exit']({} as any);
|
|
64
|
+
|
|
65
|
+
expect(reportCalled).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('| should report when object type is used at or above threshold', () => {
|
|
69
|
+
let reportCalled = false;
|
|
70
|
+
const mockContext = {
|
|
71
|
+
report: (options: any) => {
|
|
72
|
+
reportCalled = true;
|
|
73
|
+
expect(options.messageId).toBe('preferInterface');
|
|
74
|
+
expect(options.data.count).toBe('3');
|
|
75
|
+
expect(options.data.interfaceName).toContain('Interface');
|
|
76
|
+
},
|
|
77
|
+
options: [{ threshold: 3 }],
|
|
78
|
+
sourceCode: {
|
|
79
|
+
getText: () => '{ name: string; age: number }',
|
|
80
|
+
getScope: () => ({ variables: [] }),
|
|
81
|
+
ast: { body: [] },
|
|
82
|
+
},
|
|
83
|
+
} as any;
|
|
84
|
+
|
|
85
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
86
|
+
|
|
87
|
+
// Simulate 3 usages (at threshold)
|
|
88
|
+
const mockNode = {
|
|
89
|
+
type: 'TSTypeLiteral',
|
|
90
|
+
members: [
|
|
91
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
92
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
93
|
+
],
|
|
94
|
+
} as any;
|
|
95
|
+
|
|
96
|
+
rule.TSTypeLiteral(mockNode);
|
|
97
|
+
rule.TSTypeLiteral(mockNode);
|
|
98
|
+
rule.TSTypeLiteral(mockNode);
|
|
99
|
+
rule['Program:exit']({} as any);
|
|
100
|
+
|
|
101
|
+
expect(reportCalled).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('| should handle custom threshold', () => {
|
|
105
|
+
let reportCalled = false;
|
|
106
|
+
const mockContext = {
|
|
107
|
+
report: (options: any) => {
|
|
108
|
+
reportCalled = true;
|
|
109
|
+
expect(options.data.count).toBe('2');
|
|
110
|
+
},
|
|
111
|
+
options: [{ threshold: 2 }],
|
|
112
|
+
sourceCode: {
|
|
113
|
+
getText: () => '{ name: string }',
|
|
114
|
+
getScope: () => ({ variables: [] }),
|
|
115
|
+
ast: { body: [] },
|
|
116
|
+
},
|
|
117
|
+
} as any;
|
|
118
|
+
|
|
119
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
120
|
+
|
|
121
|
+
const mockNode = {
|
|
122
|
+
type: 'TSTypeLiteral',
|
|
123
|
+
members: [
|
|
124
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
125
|
+
],
|
|
126
|
+
} as any;
|
|
127
|
+
|
|
128
|
+
rule.TSTypeLiteral(mockNode);
|
|
129
|
+
rule.TSTypeLiteral(mockNode);
|
|
130
|
+
rule['Program:exit']({} as any);
|
|
131
|
+
|
|
132
|
+
expect(reportCalled).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('| should handle structural equivalence (different property order)', () => {
|
|
136
|
+
let reportCalled = false;
|
|
137
|
+
const mockContext = {
|
|
138
|
+
report: (options: any) => {
|
|
139
|
+
reportCalled = true;
|
|
140
|
+
expect(options.data.count).toBe('2');
|
|
141
|
+
},
|
|
142
|
+
options: [{ threshold: 2 }],
|
|
143
|
+
sourceCode: {
|
|
144
|
+
getText: () => '',
|
|
145
|
+
getScope: () => ({ variables: [] }),
|
|
146
|
+
ast: { body: [] },
|
|
147
|
+
},
|
|
148
|
+
} as any;
|
|
149
|
+
|
|
150
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
151
|
+
|
|
152
|
+
// First object: { name: string, age: number }
|
|
153
|
+
const mockNode1 = {
|
|
154
|
+
type: 'TSTypeLiteral',
|
|
155
|
+
members: [
|
|
156
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
157
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
158
|
+
],
|
|
159
|
+
} as any;
|
|
160
|
+
|
|
161
|
+
// Second object: { age: number, name: string } (different order)
|
|
162
|
+
const mockNode2 = {
|
|
163
|
+
type: 'TSTypeLiteral',
|
|
164
|
+
members: [
|
|
165
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
166
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
167
|
+
],
|
|
168
|
+
} as any;
|
|
169
|
+
|
|
170
|
+
rule.TSTypeLiteral(mockNode1);
|
|
171
|
+
rule.TSTypeLiteral(mockNode2);
|
|
172
|
+
rule['Program:exit']({} as any);
|
|
173
|
+
|
|
174
|
+
expect(reportCalled).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('| should handle optional properties', () => {
|
|
178
|
+
let reportCalled = false;
|
|
179
|
+
const mockContext = {
|
|
180
|
+
report: (options: any) => {
|
|
181
|
+
reportCalled = true;
|
|
182
|
+
expect(options.data.count).toBe('2');
|
|
183
|
+
},
|
|
184
|
+
options: [{ threshold: 2 }],
|
|
185
|
+
sourceCode: {
|
|
186
|
+
getText: () => '',
|
|
187
|
+
getScope: () => ({ variables: [] }),
|
|
188
|
+
ast: { body: [] },
|
|
189
|
+
},
|
|
190
|
+
} as any;
|
|
191
|
+
|
|
192
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
193
|
+
|
|
194
|
+
const mockNode = {
|
|
195
|
+
type: 'TSTypeLiteral',
|
|
196
|
+
members: [
|
|
197
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } }, optional: true },
|
|
198
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
199
|
+
],
|
|
200
|
+
} as any;
|
|
201
|
+
|
|
202
|
+
rule.TSTypeLiteral(mockNode);
|
|
203
|
+
rule.TSTypeLiteral(mockNode);
|
|
204
|
+
rule['Program:exit']({} as any);
|
|
205
|
+
|
|
206
|
+
expect(reportCalled).toBe(true);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('| should handle readonly properties', () => {
|
|
210
|
+
let reportCalled = false;
|
|
211
|
+
const mockContext = {
|
|
212
|
+
report: (options: any) => {
|
|
213
|
+
reportCalled = true;
|
|
214
|
+
expect(options.data.count).toBe('2');
|
|
215
|
+
},
|
|
216
|
+
options: [{ threshold: 2 }],
|
|
217
|
+
sourceCode: {
|
|
218
|
+
getText: () => '',
|
|
219
|
+
getScope: () => ({ variables: [] }),
|
|
220
|
+
ast: { body: [] },
|
|
221
|
+
},
|
|
222
|
+
} as any;
|
|
223
|
+
|
|
224
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
225
|
+
|
|
226
|
+
const mockNode = {
|
|
227
|
+
type: 'TSTypeLiteral',
|
|
228
|
+
members: [
|
|
229
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } }, readonly: true },
|
|
230
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
231
|
+
],
|
|
232
|
+
} as any;
|
|
233
|
+
|
|
234
|
+
rule.TSTypeLiteral(mockNode);
|
|
235
|
+
rule.TSTypeLiteral(mockNode);
|
|
236
|
+
rule['Program:exit']({} as any);
|
|
237
|
+
|
|
238
|
+
expect(reportCalled).toBe(true);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('| should provide auto-fix functionality', () => {
|
|
242
|
+
const mockContext = {
|
|
243
|
+
report: (options: any) => {
|
|
244
|
+
expect(options.fix).toBeDefined();
|
|
245
|
+
expect(typeof options.fix).toBe('function');
|
|
246
|
+
},
|
|
247
|
+
options: [{ threshold: 2 }],
|
|
248
|
+
sourceCode: {
|
|
249
|
+
getText: () => '{ name: string; age: number }',
|
|
250
|
+
getScope: () => ({ variables: [] }),
|
|
251
|
+
ast: { body: [] },
|
|
252
|
+
},
|
|
253
|
+
} as any;
|
|
254
|
+
|
|
255
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
256
|
+
|
|
257
|
+
const mockNode = {
|
|
258
|
+
type: 'TSTypeLiteral',
|
|
259
|
+
members: [
|
|
260
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
261
|
+
{ type: 'TSPropertySignature', key: { name: 'age' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
262
|
+
],
|
|
263
|
+
} as any;
|
|
264
|
+
|
|
265
|
+
rule.TSTypeLiteral(mockNode);
|
|
266
|
+
rule.TSTypeLiteral(mockNode);
|
|
267
|
+
rule['Program:exit']({} as any);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('| should handle default threshold when no options provided', () => {
|
|
271
|
+
let reportCalled = false;
|
|
272
|
+
const mockContext = {
|
|
273
|
+
report: (options: any) => {
|
|
274
|
+
reportCalled = true;
|
|
275
|
+
expect(options.data.count).toBe('3');
|
|
276
|
+
},
|
|
277
|
+
options: [],
|
|
278
|
+
sourceCode: {
|
|
279
|
+
getText: () => '',
|
|
280
|
+
getScope: () => ({ variables: [] }),
|
|
281
|
+
ast: { body: [] },
|
|
282
|
+
},
|
|
283
|
+
} as any;
|
|
284
|
+
|
|
285
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
286
|
+
|
|
287
|
+
const mockNode = {
|
|
288
|
+
type: 'TSTypeLiteral',
|
|
289
|
+
members: [
|
|
290
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
291
|
+
],
|
|
292
|
+
} as any;
|
|
293
|
+
|
|
294
|
+
rule.TSTypeLiteral(mockNode);
|
|
295
|
+
rule.TSTypeLiteral(mockNode);
|
|
296
|
+
rule.TSTypeLiteral(mockNode);
|
|
297
|
+
rule['Program:exit']({} as any);
|
|
298
|
+
|
|
299
|
+
expect(reportCalled).toBe(true);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('| should handle complex nested types', () => {
|
|
303
|
+
let reportCalled = false;
|
|
304
|
+
const mockContext = {
|
|
305
|
+
report: (options: any) => {
|
|
306
|
+
reportCalled = true;
|
|
307
|
+
expect(options.data.count).toBe('2');
|
|
308
|
+
},
|
|
309
|
+
options: [{ threshold: 2 }],
|
|
310
|
+
sourceCode: {
|
|
311
|
+
getText: () => '',
|
|
312
|
+
getScope: () => ({ variables: [] }),
|
|
313
|
+
ast: { body: [] },
|
|
314
|
+
},
|
|
315
|
+
} as any;
|
|
316
|
+
|
|
317
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
318
|
+
|
|
319
|
+
const mockNode = {
|
|
320
|
+
type: 'TSTypeLiteral',
|
|
321
|
+
members: [
|
|
322
|
+
{
|
|
323
|
+
type: 'TSPropertySignature',
|
|
324
|
+
key: { name: 'user' },
|
|
325
|
+
typeAnnotation: {
|
|
326
|
+
typeAnnotation: {
|
|
327
|
+
type: 'TSTypeLiteral',
|
|
328
|
+
members: [
|
|
329
|
+
{ type: 'TSPropertySignature', key: { name: 'id' }, typeAnnotation: { typeAnnotation: { type: 'TSNumberKeyword' } } },
|
|
330
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
331
|
+
],
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
],
|
|
336
|
+
} as any;
|
|
337
|
+
|
|
338
|
+
rule.TSTypeLiteral(mockNode);
|
|
339
|
+
rule.TSTypeLiteral(mockNode);
|
|
340
|
+
rule['Program:exit']({} as any);
|
|
341
|
+
|
|
342
|
+
expect(reportCalled).toBe(true);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('| should handle error cases gracefully', () => {
|
|
346
|
+
const mockContext = {
|
|
347
|
+
report: () => {},
|
|
348
|
+
options: [{ threshold: 2 }],
|
|
349
|
+
sourceCode: {
|
|
350
|
+
getText: () => '',
|
|
351
|
+
getScope: () => {
|
|
352
|
+
throw new Error('Scope error');
|
|
353
|
+
},
|
|
354
|
+
ast: { body: [] },
|
|
355
|
+
},
|
|
356
|
+
} as any;
|
|
357
|
+
|
|
358
|
+
const rule = preferInterfaceRule.create(mockContext);
|
|
359
|
+
|
|
360
|
+
const mockNode = {
|
|
361
|
+
type: 'TSTypeLiteral',
|
|
362
|
+
members: [
|
|
363
|
+
{ type: 'TSPropertySignature', key: { name: 'name' }, typeAnnotation: { typeAnnotation: { type: 'TSStringKeyword' } } },
|
|
364
|
+
],
|
|
365
|
+
} as any;
|
|
366
|
+
|
|
367
|
+
// Should not throw error
|
|
368
|
+
expect(() => {
|
|
369
|
+
rule.TSTypeLiteral(mockNode);
|
|
370
|
+
rule.TSTypeLiteral(mockNode);
|
|
371
|
+
rule['Program:exit']({} as any);
|
|
372
|
+
}).not.toThrow();
|
|
373
|
+
});
|
|
374
|
+
});
|