@angular-modernizer/plugin-solid 0.3.0 → 1.1.0
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/README.md +34 -48
- package/dist/index.d.ts +15 -4
- package/dist/index.js +14 -3
- package/dist/orchestrators/god-switch-transform-orchestrator.d.ts +3 -14
- package/dist/orchestrators/god-switch-transform-orchestrator.js +1 -13
- package/dist/orchestrators/service-dip-violation-transform.orchestrator.d.ts +38 -24
- package/dist/orchestrators/service-dip-violation-transform.orchestrator.js +28 -24
- package/dist/rules/dip-violation.rule.d.ts +14 -17
- package/dist/rules/dip-violation.rule.js +16 -19
- package/dist/rules/god-switch-analysis.rule.d.ts +13 -26
- package/dist/rules/god-switch-analysis.rule.js +15 -28
- package/dist/rules/isp-violation.rule.d.ts +12 -17
- package/dist/rules/isp-violation.rule.js +14 -19
- package/dist/rules/lsp-violation.rule.d.ts +15 -21
- package/dist/rules/lsp-violation.rule.js +17 -23
- package/dist/rules/ocp-violation.rule.d.ts +9 -18
- package/dist/rules/ocp-violation.rule.js +11 -20
- package/dist/rules/service-dip-violation.analysis.rule.d.ts +29 -20
- package/dist/rules/service-dip-violation.analysis.rule.js +14 -21
- package/dist/rules/srp-violation.rule.d.ts +19 -29
- package/dist/rules/srp-violation.rule.js +21 -31
- package/dist/solid-plugin.d.ts +3 -14
- package/dist/solid-plugin.js +3 -14
- package/dist/transform-rules/god-switch-transform.rule.d.ts +0 -3
- package/dist/transform-rules/god-switch-transform.rule.js +0 -3
- package/dist/transform-rules/service-dip-violation.transform.rule.d.ts +0 -3
- package/dist/transform-rules/service-dip-violation.transform.rule.js +0 -3
- package/package.json +20 -16
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/orchestrators/god-switch-transform-orchestrator.d.ts.map +0 -1
- package/dist/orchestrators/god-switch-transform-orchestrator.js.map +0 -1
- package/dist/orchestrators/service-dip-violation-transform.orchestrator.d.ts.map +0 -1
- package/dist/orchestrators/service-dip-violation-transform.orchestrator.js.map +0 -1
- package/dist/rules/dip-violation.rule.d.ts.map +0 -1
- package/dist/rules/dip-violation.rule.js.map +0 -1
- package/dist/rules/god-switch-analysis.rule.d.ts.map +0 -1
- package/dist/rules/god-switch-analysis.rule.js.map +0 -1
- package/dist/rules/isp-violation.rule.d.ts.map +0 -1
- package/dist/rules/isp-violation.rule.js.map +0 -1
- package/dist/rules/lsp-violation.rule.d.ts.map +0 -1
- package/dist/rules/lsp-violation.rule.js.map +0 -1
- package/dist/rules/ocp-violation.rule.d.ts.map +0 -1
- package/dist/rules/ocp-violation.rule.js.map +0 -1
- package/dist/rules/service-dip-violation.analysis.rule.d.ts.map +0 -1
- package/dist/rules/service-dip-violation.analysis.rule.js.map +0 -1
- package/dist/rules/srp-violation.rule.d.ts.map +0 -1
- package/dist/rules/srp-violation.rule.js.map +0 -1
- package/dist/solid-plugin.d.ts.map +0 -1
- package/dist/solid-plugin.js.map +0 -1
- package/dist/transform-rules/god-switch-transform.rule.d.ts.map +0 -1
- package/dist/transform-rules/god-switch-transform.rule.js.map +0 -1
- package/dist/transform-rules/service-dip-violation.transform.rule.d.ts.map +0 -1
- package/dist/transform-rules/service-dip-violation.transform.rule.js.map +0 -1
- package/src/index.ts +0 -30
- package/src/orchestrators/god-switch-transform-orchestrator.ts +0 -1562
- package/src/orchestrators/service-dip-violation-transform.orchestrator.ts +0 -331
- package/src/rules/dip-violation.rule.ts +0 -194
- package/src/rules/god-switch-analysis.rule.ts +0 -209
- package/src/rules/isp-violation.rule.ts +0 -315
- package/src/rules/lsp-violation.rule.ts +0 -393
- package/src/rules/ocp-violation.rule.ts +0 -264
- package/src/rules/service-dip-violation.analysis.rule.ts +0 -285
- package/src/rules/srp-violation.rule.ts +0 -533
- package/src/solid-plugin.ts +0 -127
- package/src/transform-rules/god-switch-transform.rule.ts +0 -189
- package/src/transform-rules/service-dip-violation.transform.rule.ts +0 -134
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-solid - Liskov Substitution Principle Rule
|
|
3
|
-
*
|
|
4
|
-
* Detects classes that violate the Liskov Substitution Principle (LSP)
|
|
5
|
-
* by not being substitutable for their base types without changing program correctness.
|
|
6
|
-
*
|
|
7
|
-
* Detection Patterns:
|
|
8
|
-
* - Classes that don't implement all abstract methods from base classes
|
|
9
|
-
* - Methods that override base methods with incompatible signatures
|
|
10
|
-
* - Properties that change type incompatibly in derived classes
|
|
11
|
-
* - Missing interface implementations (excluding Angular internal properties starting with ɵ)
|
|
12
|
-
* - instanceof checks that indicate poor inheritance design
|
|
13
|
-
* - Abstract method implementations that throw exceptions or behave unexpectedly
|
|
14
|
-
*
|
|
15
|
-
* This rule helps ensure proper inheritance hierarchies and interface compliance.
|
|
16
|
-
* Note: Angular internal properties (starting with ɵ) are automatically ignored.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* // Violation: derived class weakens base contract
|
|
20
|
-
* class Bird { fly(): void {} }
|
|
21
|
-
* class Penguin extends Bird {
|
|
22
|
-
* fly(): void { throw new Error('Penguins cannot fly'); }
|
|
23
|
-
* }
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import {
|
|
27
|
-
SyntaxKind,
|
|
28
|
-
type ClassDeclaration,
|
|
29
|
-
type InterfaceDeclaration,
|
|
30
|
-
} from 'ts-morph';
|
|
31
|
-
import type {
|
|
32
|
-
AnalysisRule,
|
|
33
|
-
AnalysisContext,
|
|
34
|
-
AnalysisResult,
|
|
35
|
-
} from '@angular-modernizer/plugin-system';
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* LSP Violation Rule - detects Liskov Substitution Principle violations.
|
|
39
|
-
*
|
|
40
|
-
* This rule implements the Liskov Substitution Principle by detecting
|
|
41
|
-
* inheritance and interface implementation issues that break substitutability.
|
|
42
|
-
*/
|
|
43
|
-
export class LSPViolationRule implements AnalysisRule {
|
|
44
|
-
public readonly id = 'solid:lsp-violation';
|
|
45
|
-
public readonly name = 'Liskov Substitution Principle Violation';
|
|
46
|
-
public readonly description =
|
|
47
|
-
'Detects classes that violate the Liskov Substitution Principle';
|
|
48
|
-
public readonly severity = 'warning';
|
|
49
|
-
public readonly category = 'solid-violation';
|
|
50
|
-
public readonly tags = [
|
|
51
|
-
'solid',
|
|
52
|
-
'lsp',
|
|
53
|
-
'liskov-substitution',
|
|
54
|
-
'inheritance',
|
|
55
|
-
'interfaces',
|
|
56
|
-
'polymorphism',
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
60
|
-
const violations: AnalysisResult[] = [];
|
|
61
|
-
const sourceFile = context.sourceFile;
|
|
62
|
-
|
|
63
|
-
// Find all class declarations
|
|
64
|
-
const classes = sourceFile.getDescendantsOfKind(
|
|
65
|
-
SyntaxKind.ClassDeclaration,
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
for (const classDecl of classes) {
|
|
69
|
-
const classViolations = this.analyzeClass(
|
|
70
|
-
classDecl,
|
|
71
|
-
sourceFile.getFilePath(),
|
|
72
|
-
);
|
|
73
|
-
violations.push(...classViolations);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return Promise.resolve(violations);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Analyze a class for LSP violations.
|
|
81
|
-
*/
|
|
82
|
-
private analyzeClass(
|
|
83
|
-
classDecl: ClassDeclaration,
|
|
84
|
-
filePath: string,
|
|
85
|
-
): AnalysisResult[] {
|
|
86
|
-
const violations: AnalysisResult[] = [];
|
|
87
|
-
|
|
88
|
-
try {
|
|
89
|
-
// Check 1: Interface implementation violations
|
|
90
|
-
const interfaceViolations = this.checkInterfaceImplementation(
|
|
91
|
-
classDecl,
|
|
92
|
-
filePath,
|
|
93
|
-
);
|
|
94
|
-
violations.push(...interfaceViolations);
|
|
95
|
-
|
|
96
|
-
// Check 2: Abstract method implementation violations
|
|
97
|
-
const abstractViolations = this.checkAbstractMethodImplementation(
|
|
98
|
-
classDecl,
|
|
99
|
-
filePath,
|
|
100
|
-
);
|
|
101
|
-
violations.push(...abstractViolations);
|
|
102
|
-
|
|
103
|
-
// Check 3: Inheritance-based violations (instanceof checks)
|
|
104
|
-
const inheritanceViolations = this.checkInheritanceBasedViolations(
|
|
105
|
-
classDecl,
|
|
106
|
-
filePath,
|
|
107
|
-
);
|
|
108
|
-
violations.push(...inheritanceViolations);
|
|
109
|
-
|
|
110
|
-
// Note: Abstract method and inheritance-based checks are complex with ts-morph
|
|
111
|
-
// and may not work reliably in all contexts. Interface compliance is the
|
|
112
|
-
// most important LSP aspect that can be reliably detected.
|
|
113
|
-
} catch (_error) {
|
|
114
|
-
// If we can't analyze the class, skip it
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return violations;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Check interface implementation compliance.
|
|
122
|
-
*/
|
|
123
|
-
private checkInterfaceImplementation(
|
|
124
|
-
classDecl: ClassDeclaration,
|
|
125
|
-
filePath: string,
|
|
126
|
-
): AnalysisResult[] {
|
|
127
|
-
const violations: AnalysisResult[] = [];
|
|
128
|
-
const className = classDecl.getName() ?? 'AnonymousClass';
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
// Get implemented interfaces
|
|
132
|
-
const implementedInterfaces = classDecl.getImplements();
|
|
133
|
-
|
|
134
|
-
for (const interfaceImpl of implementedInterfaces) {
|
|
135
|
-
const interfaceType = interfaceImpl.getType();
|
|
136
|
-
const declarations = interfaceType.getSymbol()?.getDeclarations();
|
|
137
|
-
|
|
138
|
-
if (declarations && declarations.length > 0) {
|
|
139
|
-
const interfaceDecl = declarations[0] as InterfaceDeclaration;
|
|
140
|
-
// Check if all interface methods are implemented
|
|
141
|
-
const interfaceMethods = interfaceDecl.getMethods();
|
|
142
|
-
const interfaceProperties = interfaceDecl.getProperties();
|
|
143
|
-
|
|
144
|
-
// Check methods
|
|
145
|
-
for (const interfaceMethod of interfaceMethods) {
|
|
146
|
-
const methodName = interfaceMethod.getName();
|
|
147
|
-
const classMethod = classDecl.getMethod(methodName);
|
|
148
|
-
|
|
149
|
-
if (!classMethod) {
|
|
150
|
-
violations.push({
|
|
151
|
-
ruleId: this.id,
|
|
152
|
-
message: `Class '${className}' implements interface '${interfaceDecl.getName()}' but does not implement method '${methodName}'`,
|
|
153
|
-
filePath,
|
|
154
|
-
line: classDecl.getStartLineNumber(),
|
|
155
|
-
column: classDecl.getStart() - classDecl.getStartLinePos(),
|
|
156
|
-
suggestedFix: `Implement method '${methodName}' in class '${className}'`,
|
|
157
|
-
metadata: {
|
|
158
|
-
className,
|
|
159
|
-
interfaceName: interfaceDecl.getName(),
|
|
160
|
-
missingMethod: methodName,
|
|
161
|
-
violationType: 'missing-interface-method',
|
|
162
|
-
principle: 'LSP',
|
|
163
|
-
},
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Check properties
|
|
169
|
-
for (const interfaceProperty of interfaceProperties) {
|
|
170
|
-
const propertyName = interfaceProperty.getName();
|
|
171
|
-
// TODO: Implement in Phase 2 - proper handling of external/internal properties
|
|
172
|
-
// Currently we skip Angular internal properties (ɵ) but need better logic for:
|
|
173
|
-
// - Public vs private properties in interfaces
|
|
174
|
-
// - Optional properties (@Input() prop?: Type)
|
|
175
|
-
// - Properties with different visibility modifiers
|
|
176
|
-
// - Inherited properties from base interfaces
|
|
177
|
-
// Skip Angular internal properties (starting with ɵ)
|
|
178
|
-
if (propertyName.startsWith('ɵ')) {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
const classProperty =
|
|
182
|
-
classDecl.getProperty(propertyName) ??
|
|
183
|
-
classDecl.getGetAccessor(propertyName) ??
|
|
184
|
-
classDecl.getSetAccessor(propertyName);
|
|
185
|
-
|
|
186
|
-
if (!classProperty) {
|
|
187
|
-
violations.push({
|
|
188
|
-
ruleId: this.id,
|
|
189
|
-
message: `Class '${className}' implements interface '${interfaceDecl.getName()}' but does not implement property '${propertyName}'`,
|
|
190
|
-
filePath,
|
|
191
|
-
line: classDecl.getStartLineNumber(),
|
|
192
|
-
column: classDecl.getStart() - classDecl.getStartLinePos(),
|
|
193
|
-
suggestedFix: `Implement property '${propertyName}' in class '${className}'`,
|
|
194
|
-
metadata: {
|
|
195
|
-
className,
|
|
196
|
-
interfaceName: interfaceDecl.getName(),
|
|
197
|
-
missingProperty: propertyName,
|
|
198
|
-
violationType: 'missing-interface-property',
|
|
199
|
-
principle: 'LSP',
|
|
200
|
-
},
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
} catch (_error) {
|
|
207
|
-
// Skip if we can't analyze interfaces
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return violations;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Check abstract method implementation compliance.
|
|
215
|
-
*/
|
|
216
|
-
private checkAbstractMethodImplementation(
|
|
217
|
-
classDecl: ClassDeclaration,
|
|
218
|
-
filePath: string,
|
|
219
|
-
): AnalysisResult[] {
|
|
220
|
-
const violations: AnalysisResult[] = [];
|
|
221
|
-
const className = classDecl.getName() ?? 'AnonymousClass';
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
// Check if this class extends an abstract class
|
|
225
|
-
const heritageClauses = classDecl.getHeritageClauses();
|
|
226
|
-
const extendsClause = heritageClauses.find(
|
|
227
|
-
(h) => h.getToken() === SyntaxKind.ExtendsKeyword,
|
|
228
|
-
);
|
|
229
|
-
|
|
230
|
-
if (extendsClause) {
|
|
231
|
-
for (const typeNode of extendsClause.getTypeNodes()) {
|
|
232
|
-
const baseType = typeNode.getType();
|
|
233
|
-
const baseDeclarations = baseType.getSymbol()?.getDeclarations();
|
|
234
|
-
|
|
235
|
-
if (baseDeclarations) {
|
|
236
|
-
for (const baseDecl of baseDeclarations) {
|
|
237
|
-
if (baseDecl.getKind() === SyntaxKind.ClassDeclaration) {
|
|
238
|
-
const baseClassDecl = baseDecl as ClassDeclaration;
|
|
239
|
-
|
|
240
|
-
// Check if base class is abstract
|
|
241
|
-
if (baseClassDecl.isAbstract()) {
|
|
242
|
-
// Get all abstract methods from base class
|
|
243
|
-
const abstractMethods = baseClassDecl
|
|
244
|
-
.getMethods()
|
|
245
|
-
.filter((m) => m.isAbstract());
|
|
246
|
-
|
|
247
|
-
for (const abstractMethod of abstractMethods) {
|
|
248
|
-
const methodName = abstractMethod.getName();
|
|
249
|
-
const implementedMethod = classDecl.getMethod(methodName);
|
|
250
|
-
|
|
251
|
-
if (!implementedMethod) {
|
|
252
|
-
violations.push({
|
|
253
|
-
ruleId: this.id,
|
|
254
|
-
message: `Class '${className}' extends abstract class '${baseClassDecl.getName()}' but does not implement abstract method '${methodName}'`,
|
|
255
|
-
filePath,
|
|
256
|
-
line: classDecl.getStartLineNumber(),
|
|
257
|
-
column:
|
|
258
|
-
classDecl.getStart() - classDecl.getStartLinePos(),
|
|
259
|
-
suggestedFix: `Implement abstract method '${methodName}' in class '${className}'`,
|
|
260
|
-
metadata: {
|
|
261
|
-
className,
|
|
262
|
-
baseClassName: baseClassDecl.getName(),
|
|
263
|
-
missingMethod: methodName,
|
|
264
|
-
violationType: 'missing-abstract-method',
|
|
265
|
-
principle: 'LSP',
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
} else {
|
|
269
|
-
// Check if implementation throws exceptions (potential LSP violation)
|
|
270
|
-
const methodBody = implementedMethod.getBody();
|
|
271
|
-
if (methodBody) {
|
|
272
|
-
const throwStatements = methodBody.getDescendantsOfKind(
|
|
273
|
-
SyntaxKind.ThrowStatement,
|
|
274
|
-
);
|
|
275
|
-
if (throwStatements.length > 0) {
|
|
276
|
-
// Check if it's just throwing NotImplementedError or similar
|
|
277
|
-
const firstThrow = throwStatements[0];
|
|
278
|
-
const expression = firstThrow?.getExpression();
|
|
279
|
-
if (
|
|
280
|
-
expression &&
|
|
281
|
-
(expression.getText().includes('NotImplemented') ||
|
|
282
|
-
expression.getText().includes('not implemented'))
|
|
283
|
-
) {
|
|
284
|
-
violations.push({
|
|
285
|
-
ruleId: this.id,
|
|
286
|
-
message: `Method '${methodName}' in class '${className}' throws NotImplementedError, violating LSP expectations`,
|
|
287
|
-
filePath,
|
|
288
|
-
line: implementedMethod.getStartLineNumber(),
|
|
289
|
-
column:
|
|
290
|
-
implementedMethod.getStart() -
|
|
291
|
-
implementedMethod.getStartLinePos(),
|
|
292
|
-
suggestedFix: `Implement '${methodName}' properly or reconsider the inheritance hierarchy`,
|
|
293
|
-
metadata: {
|
|
294
|
-
className,
|
|
295
|
-
methodName,
|
|
296
|
-
violationType: 'throws-not-implemented',
|
|
297
|
-
principle: 'LSP',
|
|
298
|
-
},
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
} catch (_error) {
|
|
312
|
-
// Skip if we can't analyze abstract methods
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return violations;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Check for inheritance-based violations like instanceof checks.
|
|
320
|
-
*/
|
|
321
|
-
private checkInheritanceBasedViolations(
|
|
322
|
-
classDecl: ClassDeclaration,
|
|
323
|
-
filePath: string,
|
|
324
|
-
): AnalysisResult[] {
|
|
325
|
-
const violations: AnalysisResult[] = [];
|
|
326
|
-
const className = classDecl.getName() ?? 'AnonymousClass';
|
|
327
|
-
|
|
328
|
-
try {
|
|
329
|
-
// Find all methods in this class
|
|
330
|
-
const methods = classDecl.getMethods();
|
|
331
|
-
|
|
332
|
-
for (const method of methods) {
|
|
333
|
-
const methodBody = method.getBody();
|
|
334
|
-
if (methodBody) {
|
|
335
|
-
// Look for instanceof expressions
|
|
336
|
-
const instanceofExpressions = methodBody
|
|
337
|
-
.getDescendantsOfKind(SyntaxKind.BinaryExpression)
|
|
338
|
-
.filter(
|
|
339
|
-
(expr) =>
|
|
340
|
-
expr.getOperatorToken().getKind() ===
|
|
341
|
-
SyntaxKind.InstanceOfKeyword,
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
if (instanceofExpressions.length > 0) {
|
|
345
|
-
// Check if this is part of a larger instanceof chain (switch-like pattern)
|
|
346
|
-
const methodText = methodBody.getText();
|
|
347
|
-
const instanceofCount = (methodText.match(/instanceof/g) ?? [])
|
|
348
|
-
.length;
|
|
349
|
-
|
|
350
|
-
if (instanceofCount >= 3) {
|
|
351
|
-
violations.push({
|
|
352
|
-
ruleId: this.id,
|
|
353
|
-
message: `Method '${method.getName()}' in class '${className}' uses ${instanceofCount} instanceof checks, indicating poor inheritance design (LSP violation)`,
|
|
354
|
-
filePath,
|
|
355
|
-
line: method.getStartLineNumber(),
|
|
356
|
-
column: method.getStart() - method.getStartLinePos(),
|
|
357
|
-
suggestedFix: `Consider using polymorphism instead of instanceof checks. Implement common behavior in base class or use Strategy pattern.`,
|
|
358
|
-
metadata: {
|
|
359
|
-
className,
|
|
360
|
-
methodName: method.getName(),
|
|
361
|
-
instanceofCount,
|
|
362
|
-
violationType: 'excessive-instanceof-checks',
|
|
363
|
-
principle: 'LSP',
|
|
364
|
-
},
|
|
365
|
-
});
|
|
366
|
-
} else if (instanceofCount >= 1) {
|
|
367
|
-
// Single instanceof might still be problematic
|
|
368
|
-
violations.push({
|
|
369
|
-
ruleId: this.id,
|
|
370
|
-
message: `Method '${method.getName()}' in class '${className}' uses instanceof check, potentially violating LSP`,
|
|
371
|
-
filePath,
|
|
372
|
-
line: method.getStartLineNumber(),
|
|
373
|
-
column: method.getStart() - method.getStartLinePos(),
|
|
374
|
-
suggestedFix: `Consider using polymorphism or method overriding instead of instanceof checks`,
|
|
375
|
-
metadata: {
|
|
376
|
-
className,
|
|
377
|
-
methodName: method.getName(),
|
|
378
|
-
instanceofCount,
|
|
379
|
-
violationType: 'instanceof-check',
|
|
380
|
-
principle: 'LSP',
|
|
381
|
-
},
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
} catch (_error) {
|
|
388
|
-
// Skip if we can't analyze inheritance patterns
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
return violations;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-solid - Open/Closed Principle Rule
|
|
3
|
-
*
|
|
4
|
-
* Detects classes that violate the Open/Closed Principle (OCP)
|
|
5
|
-
* by being closed for extension but requiring modification for new features.
|
|
6
|
-
*
|
|
7
|
-
* Detection Patterns:
|
|
8
|
-
* - Switch statements with many cases (> 5)
|
|
9
|
-
* - Long if-else chains (> 4 conditions)
|
|
10
|
-
* - Methods with too many parameters (> 4)
|
|
11
|
-
* - Classes with hard dependencies on concrete implementations
|
|
12
|
-
*
|
|
13
|
-
* This rule helps enforce extensible code architecture.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* // Violation: requires modification for each new shape type
|
|
17
|
-
* function createShape(type: string) {
|
|
18
|
-
* switch (type) {
|
|
19
|
-
* case 'circle': return new Circle();
|
|
20
|
-
* case 'square': return new Square();
|
|
21
|
-
* // Every new shape requires editing this function
|
|
22
|
-
* }
|
|
23
|
-
* }
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import {
|
|
27
|
-
SyntaxKind,
|
|
28
|
-
type ClassDeclaration,
|
|
29
|
-
type MethodDeclaration,
|
|
30
|
-
type SwitchStatement,
|
|
31
|
-
type IfStatement,
|
|
32
|
-
} from 'ts-morph';
|
|
33
|
-
import type {
|
|
34
|
-
AnalysisRule,
|
|
35
|
-
AnalysisContext,
|
|
36
|
-
AnalysisResult,
|
|
37
|
-
} from '@angular-modernizer/plugin-system';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* OCP Violation Rule - detects Open/Closed Principle violations.
|
|
41
|
-
*
|
|
42
|
-
* This rule implements the Open/Closed Principle by detecting
|
|
43
|
-
* code that is closed for extension and requires modification for new features.
|
|
44
|
-
*/
|
|
45
|
-
export class OCPViolationRule implements AnalysisRule {
|
|
46
|
-
public readonly id = 'solid:ocp-violation';
|
|
47
|
-
public readonly name = 'Open/Closed Principle Violation';
|
|
48
|
-
public readonly description =
|
|
49
|
-
'Detects code that violates the Open/Closed Principle';
|
|
50
|
-
public readonly severity = 'warning';
|
|
51
|
-
public readonly category = 'solid-violation';
|
|
52
|
-
public readonly tags = [
|
|
53
|
-
'solid',
|
|
54
|
-
'ocp',
|
|
55
|
-
'open-closed',
|
|
56
|
-
'extensibility',
|
|
57
|
-
'architecture',
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Maximum number of switch cases allowed.
|
|
62
|
-
*/
|
|
63
|
-
private readonly maxSwitchCases = 5;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Maximum number of if-else conditions in a chain.
|
|
67
|
-
*/
|
|
68
|
-
private readonly maxIfElseChain = 4;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Maximum number of parameters allowed in a method.
|
|
72
|
-
*/
|
|
73
|
-
private readonly maxMethodParameters = 4;
|
|
74
|
-
|
|
75
|
-
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
76
|
-
const violations: AnalysisResult[] = [];
|
|
77
|
-
const sourceFile = context.sourceFile;
|
|
78
|
-
|
|
79
|
-
// Find all class declarations
|
|
80
|
-
const classDeclarations = sourceFile.getDescendantsOfKind(
|
|
81
|
-
SyntaxKind.ClassDeclaration,
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
for (const classDecl of classDeclarations) {
|
|
85
|
-
const classViolations = this.analyzeClass(
|
|
86
|
-
classDecl,
|
|
87
|
-
sourceFile.getFilePath(),
|
|
88
|
-
);
|
|
89
|
-
violations.push(...classViolations);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return Promise.resolve(violations);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Analyze a class for OCP violations.
|
|
97
|
-
*/
|
|
98
|
-
private analyzeClass(
|
|
99
|
-
classDecl: ClassDeclaration,
|
|
100
|
-
filePath: string,
|
|
101
|
-
): AnalysisResult[] {
|
|
102
|
-
const violations: AnalysisResult[] = [];
|
|
103
|
-
const className = classDecl.getName() ?? 'AnonymousClass';
|
|
104
|
-
|
|
105
|
-
try {
|
|
106
|
-
// Check methods for OCP violations
|
|
107
|
-
const methods = classDecl.getMethods();
|
|
108
|
-
for (const method of methods) {
|
|
109
|
-
const methodViolations = this.analyzeMethod(
|
|
110
|
-
method,
|
|
111
|
-
className,
|
|
112
|
-
filePath,
|
|
113
|
-
);
|
|
114
|
-
violations.push(...methodViolations);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Check for switch statements in the class
|
|
118
|
-
const switchStatements = classDecl.getDescendantsOfKind(
|
|
119
|
-
SyntaxKind.SwitchStatement,
|
|
120
|
-
);
|
|
121
|
-
for (const switchStmt of switchStatements) {
|
|
122
|
-
const violation = this.analyzeSwitchStatement(
|
|
123
|
-
switchStmt,
|
|
124
|
-
className,
|
|
125
|
-
filePath,
|
|
126
|
-
);
|
|
127
|
-
if (violation !== null) {
|
|
128
|
-
violations.push(violation);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Check for long if-else chains (only analyze top-level if statements)
|
|
133
|
-
const ifStatements = classDecl.getDescendantsOfKind(
|
|
134
|
-
SyntaxKind.IfStatement,
|
|
135
|
-
);
|
|
136
|
-
for (const ifStmt of ifStatements) {
|
|
137
|
-
// Only analyze if statements that are not part of an else-if chain
|
|
138
|
-
const parent = ifStmt.getParent();
|
|
139
|
-
if (parent.getKind() === SyntaxKind.IfStatement) {
|
|
140
|
-
continue; // Skip else-if statements, they are analyzed as part of the chain
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const violation = this.analyzeIfElseChain(ifStmt, className, filePath);
|
|
144
|
-
if (violation !== null) {
|
|
145
|
-
violations.push(violation);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
} catch (_error) {
|
|
149
|
-
// If we can't analyze the class, skip it
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return violations;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Analyze a method for OCP violations.
|
|
157
|
-
*/
|
|
158
|
-
private analyzeMethod(
|
|
159
|
-
method: MethodDeclaration,
|
|
160
|
-
className: string,
|
|
161
|
-
filePath: string,
|
|
162
|
-
): AnalysisResult[] {
|
|
163
|
-
const violations: AnalysisResult[] = [];
|
|
164
|
-
const methodName = method.getName();
|
|
165
|
-
|
|
166
|
-
// Check 1: Too many parameters
|
|
167
|
-
const parameters = method.getParameters();
|
|
168
|
-
if (parameters.length > this.maxMethodParameters) {
|
|
169
|
-
violations.push({
|
|
170
|
-
ruleId: this.id,
|
|
171
|
-
message: `Method '${methodName}' in class '${className}' has ${parameters.length} parameters, exceeding the recommended maximum of ${this.maxMethodParameters}. Consider using a parameter object or splitting the method.`,
|
|
172
|
-
filePath,
|
|
173
|
-
line: method.getStartLineNumber(),
|
|
174
|
-
column: method.getStart() - method.getStartLinePos(),
|
|
175
|
-
suggestedFix: `Refactor '${methodName}' to use a parameter object or reduce the number of parameters.`,
|
|
176
|
-
metadata: {
|
|
177
|
-
className,
|
|
178
|
-
methodName,
|
|
179
|
-
parameterCount: parameters.length,
|
|
180
|
-
maxAllowed: this.maxMethodParameters,
|
|
181
|
-
violationType: 'too-many-parameters',
|
|
182
|
-
principle: 'OCP',
|
|
183
|
-
},
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return violations;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Analyze a switch statement for OCP violations.
|
|
192
|
-
*/
|
|
193
|
-
private analyzeSwitchStatement(
|
|
194
|
-
switchStmt: SwitchStatement,
|
|
195
|
-
className: string,
|
|
196
|
-
filePath: string,
|
|
197
|
-
): AnalysisResult | null {
|
|
198
|
-
const cases = switchStmt.getClauses();
|
|
199
|
-
if (cases.length > this.maxSwitchCases) {
|
|
200
|
-
return {
|
|
201
|
-
ruleId: this.id,
|
|
202
|
-
message: `Switch statement in class '${className}' has ${cases.length} cases, exceeding the recommended maximum of ${this.maxSwitchCases}. Consider using polymorphism or a strategy pattern.`,
|
|
203
|
-
filePath,
|
|
204
|
-
line: switchStmt.getStartLineNumber(),
|
|
205
|
-
column: switchStmt.getStart() - switchStmt.getStartLinePos(),
|
|
206
|
-
suggestedFix: `Replace switch statement with polymorphism or strategy pattern to make the code open for extension.`,
|
|
207
|
-
metadata: {
|
|
208
|
-
className,
|
|
209
|
-
caseCount: cases.length,
|
|
210
|
-
maxAllowed: this.maxSwitchCases,
|
|
211
|
-
violationType: 'large-switch-statement',
|
|
212
|
-
principle: 'OCP',
|
|
213
|
-
},
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
return null;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Analyze if-else chains for OCP violations.
|
|
221
|
-
*/
|
|
222
|
-
private analyzeIfElseChain(
|
|
223
|
-
ifStmt: IfStatement,
|
|
224
|
-
className: string,
|
|
225
|
-
filePath: string,
|
|
226
|
-
): AnalysisResult | null {
|
|
227
|
-
let chainLength = 1; // Count the initial if
|
|
228
|
-
let currentStmt: IfStatement | undefined = ifStmt;
|
|
229
|
-
|
|
230
|
-
// Count the else-if and else statements in the chain
|
|
231
|
-
while (currentStmt) {
|
|
232
|
-
const elseStmt = currentStmt.getElseStatement();
|
|
233
|
-
if (elseStmt && elseStmt.getKind() === SyntaxKind.IfStatement) {
|
|
234
|
-
chainLength++;
|
|
235
|
-
currentStmt = elseStmt.asKind(SyntaxKind.IfStatement);
|
|
236
|
-
} else {
|
|
237
|
-
if (elseStmt) {
|
|
238
|
-
chainLength++; // Count the final else
|
|
239
|
-
}
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (chainLength > this.maxIfElseChain) {
|
|
245
|
-
return {
|
|
246
|
-
ruleId: this.id,
|
|
247
|
-
message: `If-else chain in class '${className}' has ${chainLength} conditions, exceeding the recommended maximum of ${this.maxIfElseChain}. Consider using polymorphism or strategy pattern.`,
|
|
248
|
-
filePath,
|
|
249
|
-
line: ifStmt.getStartLineNumber(),
|
|
250
|
-
column: ifStmt.getStart() - ifStmt.getStartLinePos(),
|
|
251
|
-
suggestedFix: `Replace if-else chain with polymorphism or strategy pattern to make the code open for extension.`,
|
|
252
|
-
metadata: {
|
|
253
|
-
className,
|
|
254
|
-
chainLength,
|
|
255
|
-
maxAllowed: this.maxIfElseChain,
|
|
256
|
-
violationType: 'long-if-else-chain',
|
|
257
|
-
principle: 'OCP',
|
|
258
|
-
},
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return null;
|
|
263
|
-
}
|
|
264
|
-
}
|