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