@angular-modernizer/plugin-solid 0.3.0 → 1.0.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/dist/orchestrators/service-dip-violation-transform.orchestrator.d.ts +1 -1
- package/dist/orchestrators/service-dip-violation-transform.orchestrator.js +1 -1
- package/dist/rules/service-dip-violation.analysis.rule.d.ts +6 -6
- package/dist/rules/service-dip-violation.analysis.rule.js +6 -6
- package/package.json +19 -9
- 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,533 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-solid - SRP Violation Rule
|
|
3
|
-
*
|
|
4
|
-
* Detects Single Responsibility Principle violations in classes.
|
|
5
|
-
*
|
|
6
|
-
* Detection Patterns:
|
|
7
|
-
* - Classes with too many methods (configurable threshold, default: 15).
|
|
8
|
-
* Get/set accessors and arrow-function / function-expression properties
|
|
9
|
-
* count as methods.
|
|
10
|
-
* - Classes mixing multiple responsibility categories
|
|
11
|
-
* - Base classes (abstract, or extended by >= 3 classes in the project) with
|
|
12
|
-
* a large public/protected surface: every subclass inherits all concerns,
|
|
13
|
-
* so the category check already fires at 2 categories. Metadata carries
|
|
14
|
-
* `isBaseClass`, `isAbstract` and `subclassCount`.
|
|
15
|
-
*
|
|
16
|
-
* Responsibility Categories:
|
|
17
|
-
* - UI: render, display, show, hide, toggle, open, close
|
|
18
|
-
* - Business: calculate, validate, process, transform, compute
|
|
19
|
-
* - HTTP: fetch, get, post, put, delete, request, call
|
|
20
|
-
* - Data: save, load, store, retrieve, persist, cache
|
|
21
|
-
* - Event: emit, dispatch, trigger, fire, notify, publish
|
|
22
|
-
* - Lifecycle: init, destroy, cleanup, setup, teardown
|
|
23
|
-
*
|
|
24
|
-
* This rule helps enforce the Single Responsibility Principle by identifying
|
|
25
|
-
* classes that are doing too much or handling multiple unrelated concerns.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* // Violation: class mixes HTTP, business, and UI concerns
|
|
29
|
-
* class UserComponent {
|
|
30
|
-
* fetchUsers() {} // HTTP concern
|
|
31
|
-
* calculateTotal() {} // Business concern
|
|
32
|
-
* renderChart() {} // UI concern
|
|
33
|
-
* }
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
import {
|
|
37
|
-
Node,
|
|
38
|
-
Scope,
|
|
39
|
-
SyntaxKind,
|
|
40
|
-
type ClassDeclaration,
|
|
41
|
-
type MethodDeclaration,
|
|
42
|
-
} from 'ts-morph';
|
|
43
|
-
import type {
|
|
44
|
-
AnalysisRule,
|
|
45
|
-
AnalysisContext,
|
|
46
|
-
AnalysisResult,
|
|
47
|
-
} from '@angular-modernizer/plugin-system';
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Responsibility category for method classification.
|
|
51
|
-
*/
|
|
52
|
-
interface ResponsibilityCategory {
|
|
53
|
-
name: string;
|
|
54
|
-
keywords: string[];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* A callable class member: method, get/set accessor, or a property whose
|
|
59
|
-
* initializer is an arrow function / function expression.
|
|
60
|
-
*/
|
|
61
|
-
interface CallableMember {
|
|
62
|
-
name: string;
|
|
63
|
-
kind: 'method' | 'accessor' | 'function-property';
|
|
64
|
-
isNonPrivate: boolean;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Base-class signal for a class.
|
|
69
|
-
*/
|
|
70
|
-
interface BaseClassSignal {
|
|
71
|
-
isBaseClass: boolean;
|
|
72
|
-
isAbstract: boolean;
|
|
73
|
-
subclassCount: number;
|
|
74
|
-
nonPrivateSurface: number;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* SRP Violation Rule - detects Single Responsibility Principle violations.
|
|
79
|
-
*
|
|
80
|
-
* This rule implements the Single Responsibility Principle by detecting
|
|
81
|
-
* classes with too many methods or mixed responsibilities.
|
|
82
|
-
*/
|
|
83
|
-
export class SingleResponsibilityRule implements AnalysisRule {
|
|
84
|
-
public readonly id = 'solid:srp-single-responsibility-violation';
|
|
85
|
-
public readonly name = 'Single Responsibility Principle Violation';
|
|
86
|
-
public readonly description =
|
|
87
|
-
'Detects classes with too many methods or mixed responsibilities';
|
|
88
|
-
public readonly severity = 'warning';
|
|
89
|
-
public readonly category = 'solid-violation';
|
|
90
|
-
public readonly tags = [
|
|
91
|
-
'solid',
|
|
92
|
-
'srp',
|
|
93
|
-
'single-responsibility',
|
|
94
|
-
'architecture',
|
|
95
|
-
'maintainability',
|
|
96
|
-
];
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Default threshold for maximum number of methods in a class.
|
|
100
|
-
*/
|
|
101
|
-
private readonly maxMethodsThreshold = 15;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Minimum number of different responsibility categories to flag as mixed.
|
|
105
|
-
*/
|
|
106
|
-
private readonly minMixedResponsibilities = 3;
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Minimum number of mixed categories for base classes (abstract or
|
|
110
|
-
* subclassed >= `minSubclassesForBase` times) with a large surface.
|
|
111
|
-
*/
|
|
112
|
-
private readonly minMixedResponsibilitiesForBase = 2;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Number of subclasses in the project that make a class a base class.
|
|
116
|
-
*/
|
|
117
|
-
private readonly minSubclassesForBase = 3;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Public/protected callable members a base class needs to be evaluated.
|
|
121
|
-
*/
|
|
122
|
-
private readonly baseClassSurfaceThreshold = 8;
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Responsibility categories for method classification.
|
|
126
|
-
*/
|
|
127
|
-
private readonly responsibilityCategories: ResponsibilityCategory[] = [
|
|
128
|
-
{
|
|
129
|
-
name: 'UI',
|
|
130
|
-
keywords: [
|
|
131
|
-
'render',
|
|
132
|
-
'display',
|
|
133
|
-
'show',
|
|
134
|
-
'hide',
|
|
135
|
-
'toggle',
|
|
136
|
-
'open',
|
|
137
|
-
'close',
|
|
138
|
-
'view',
|
|
139
|
-
],
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
name: 'Business',
|
|
143
|
-
keywords: [
|
|
144
|
-
'calculate',
|
|
145
|
-
'validate',
|
|
146
|
-
'process',
|
|
147
|
-
'transform',
|
|
148
|
-
'compute',
|
|
149
|
-
'check',
|
|
150
|
-
],
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
name: 'HTTP',
|
|
154
|
-
keywords: [
|
|
155
|
-
'fetch',
|
|
156
|
-
'get',
|
|
157
|
-
'post',
|
|
158
|
-
'put',
|
|
159
|
-
'delete',
|
|
160
|
-
'request',
|
|
161
|
-
'call',
|
|
162
|
-
'api',
|
|
163
|
-
],
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
name: 'Data',
|
|
167
|
-
keywords: [
|
|
168
|
-
'save',
|
|
169
|
-
'load',
|
|
170
|
-
'store',
|
|
171
|
-
'retrieve',
|
|
172
|
-
'persist',
|
|
173
|
-
'cache',
|
|
174
|
-
'read',
|
|
175
|
-
'write',
|
|
176
|
-
],
|
|
177
|
-
},
|
|
178
|
-
{
|
|
179
|
-
name: 'Event',
|
|
180
|
-
keywords: [
|
|
181
|
-
'emit',
|
|
182
|
-
'dispatch',
|
|
183
|
-
'trigger',
|
|
184
|
-
'fire',
|
|
185
|
-
'notify',
|
|
186
|
-
'publish',
|
|
187
|
-
'subscribe',
|
|
188
|
-
],
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
name: 'Lifecycle',
|
|
192
|
-
keywords: [
|
|
193
|
-
'init',
|
|
194
|
-
'destroy',
|
|
195
|
-
'cleanup',
|
|
196
|
-
'setup',
|
|
197
|
-
'teardown',
|
|
198
|
-
'start',
|
|
199
|
-
'stop',
|
|
200
|
-
],
|
|
201
|
-
},
|
|
202
|
-
];
|
|
203
|
-
|
|
204
|
-
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
205
|
-
const violations: AnalysisResult[] = [];
|
|
206
|
-
const sourceFile = context.sourceFile;
|
|
207
|
-
|
|
208
|
-
// Find all class declarations in the file
|
|
209
|
-
const classes = sourceFile.getDescendantsOfKind(
|
|
210
|
-
SyntaxKind.ClassDeclaration,
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
for (const classDecl of classes) {
|
|
214
|
-
const classViolations = this.checkClass(
|
|
215
|
-
classDecl,
|
|
216
|
-
sourceFile.getFilePath(),
|
|
217
|
-
);
|
|
218
|
-
violations.push(...classViolations);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return Promise.resolve(violations);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Check if a class violates SRP.
|
|
226
|
-
*/
|
|
227
|
-
private checkClass(
|
|
228
|
-
classDecl: ClassDeclaration,
|
|
229
|
-
filePath: string,
|
|
230
|
-
): AnalysisResult[] {
|
|
231
|
-
const violations: AnalysisResult[] = [];
|
|
232
|
-
|
|
233
|
-
try {
|
|
234
|
-
const className = classDecl.getName() ?? '<anonymous>';
|
|
235
|
-
const methods = classDecl.getMethods();
|
|
236
|
-
const members = this.collectCallableMembers(classDecl);
|
|
237
|
-
const accessorCount = members.filter((m) => m.kind === 'accessor').length;
|
|
238
|
-
const functionPropertyCount = members.filter(
|
|
239
|
-
(m) => m.kind === 'function-property',
|
|
240
|
-
).length;
|
|
241
|
-
|
|
242
|
-
// Check for too many methods (accessors and function properties count)
|
|
243
|
-
if (members.length > this.maxMethodsThreshold) {
|
|
244
|
-
const startLine = classDecl.getStartLineNumber();
|
|
245
|
-
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
246
|
-
const breakdown =
|
|
247
|
-
accessorCount + functionPropertyCount > 0
|
|
248
|
-
? ` (incl. ${accessorCount} accessors, ${functionPropertyCount} function properties)`
|
|
249
|
-
: '';
|
|
250
|
-
|
|
251
|
-
violations.push({
|
|
252
|
-
ruleId: this.id,
|
|
253
|
-
message: `Class '${className}' has ${members.length} methods${breakdown} (threshold: ${this.maxMethodsThreshold}). Consider splitting into smaller, focused classes.`,
|
|
254
|
-
filePath,
|
|
255
|
-
line: startLine,
|
|
256
|
-
column: startColumn,
|
|
257
|
-
suggestedFix: `Refactor '${className}' by extracting related methods into separate classes, each with a single responsibility.`,
|
|
258
|
-
metadata: {
|
|
259
|
-
className,
|
|
260
|
-
methodCount: members.length,
|
|
261
|
-
accessorCount,
|
|
262
|
-
functionPropertyCount,
|
|
263
|
-
threshold: this.maxMethodsThreshold,
|
|
264
|
-
violationType: 'too-many-methods',
|
|
265
|
-
principle: 'SRP',
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Check for single-method multi-concern (Cat 2)
|
|
271
|
-
for (const method of methods) {
|
|
272
|
-
const methodViolation = this.analyzeMethodBodyConcerns(
|
|
273
|
-
method,
|
|
274
|
-
className,
|
|
275
|
-
filePath,
|
|
276
|
-
);
|
|
277
|
-
if (methodViolation) {
|
|
278
|
-
violations.push(methodViolation);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// Check for mixed responsibilities (base classes use a lower bar)
|
|
283
|
-
const responsibilities = this.analyzeResponsibilities(
|
|
284
|
-
members.map((m) => m.name),
|
|
285
|
-
);
|
|
286
|
-
const baseSignal = this.detectBaseClassSignal(classDecl, members);
|
|
287
|
-
const minMixed = baseSignal.isBaseClass
|
|
288
|
-
? this.minMixedResponsibilitiesForBase
|
|
289
|
-
: this.minMixedResponsibilities;
|
|
290
|
-
if (responsibilities.size >= minMixed) {
|
|
291
|
-
const startLine = classDecl.getStartLineNumber();
|
|
292
|
-
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
293
|
-
const responsibilityList = Array.from(responsibilities).join(', ');
|
|
294
|
-
|
|
295
|
-
violations.push({
|
|
296
|
-
ruleId: this.id,
|
|
297
|
-
message: baseSignal.isBaseClass
|
|
298
|
-
? `Base class '${className}' (${baseSignal.isAbstract ? 'abstract, ' : ''}${baseSignal.subclassCount} subclasses) appears to have mixed responsibilities: ${responsibilityList}. Every subclass inherits these concerns.`
|
|
299
|
-
: `Class '${className}' appears to have mixed responsibilities: ${responsibilityList}. Consider separating these concerns.`,
|
|
300
|
-
filePath,
|
|
301
|
-
line: startLine,
|
|
302
|
-
column: startColumn,
|
|
303
|
-
suggestedFix: `Extract methods into separate classes based on their responsibilities.`,
|
|
304
|
-
metadata: {
|
|
305
|
-
className,
|
|
306
|
-
responsibilities: Array.from(responsibilities),
|
|
307
|
-
violationType: 'mixed-responsibilities',
|
|
308
|
-
principle: 'SRP',
|
|
309
|
-
isBaseClass: baseSignal.isBaseClass,
|
|
310
|
-
isAbstract: baseSignal.isAbstract,
|
|
311
|
-
subclassCount: baseSignal.subclassCount,
|
|
312
|
-
nonPrivateSurface: baseSignal.nonPrivateSurface,
|
|
313
|
-
},
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
return violations;
|
|
318
|
-
} catch (_error) {
|
|
319
|
-
// If we can't analyze the class, skip it
|
|
320
|
-
return violations;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Detect a single method that mixes ≥4 distinct concern categories AND is ≥20 lines.
|
|
326
|
-
* Scans the full method body text for concern keywords (same categories as class-level).
|
|
327
|
-
*/
|
|
328
|
-
private analyzeMethodBodyConcerns(
|
|
329
|
-
method: MethodDeclaration,
|
|
330
|
-
className: string,
|
|
331
|
-
filePath: string,
|
|
332
|
-
): AnalysisResult | null {
|
|
333
|
-
const methodName = method.getName();
|
|
334
|
-
if (!methodName) {
|
|
335
|
-
return null;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// Skip Angular lifecycle hooks
|
|
339
|
-
if (methodName.startsWith('ng')) {
|
|
340
|
-
return null;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const body = method.getBody();
|
|
344
|
-
if (!body) {
|
|
345
|
-
return null;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// Too short to meaningfully mix concerns
|
|
349
|
-
const lineCount = method.getEndLineNumber() - method.getStartLineNumber();
|
|
350
|
-
if (lineCount < 20) {
|
|
351
|
-
return null;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const bodyText = body.getFullText().toLowerCase();
|
|
355
|
-
const detectedConcerns: string[] = [];
|
|
356
|
-
|
|
357
|
-
for (const category of this.responsibilityCategories) {
|
|
358
|
-
for (const keyword of category.keywords) {
|
|
359
|
-
if (bodyText.includes(keyword)) {
|
|
360
|
-
if (!detectedConcerns.includes(category.name)) {
|
|
361
|
-
detectedConcerns.push(category.name);
|
|
362
|
-
}
|
|
363
|
-
break;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (detectedConcerns.length < 4) {
|
|
369
|
-
return null;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
return {
|
|
373
|
-
ruleId: this.id,
|
|
374
|
-
message: `Method '${methodName}' in '${className}' mixes ${detectedConcerns.length} concerns (${detectedConcerns.join(', ')}) in one body.`,
|
|
375
|
-
filePath,
|
|
376
|
-
line: method.getStartLineNumber(),
|
|
377
|
-
column: method.getStart() - method.getStartLinePos(),
|
|
378
|
-
suggestedFix: `Extract each concern into a dedicated private method or service to honour the Single Responsibility Principle.`,
|
|
379
|
-
metadata: {
|
|
380
|
-
className,
|
|
381
|
-
methodName,
|
|
382
|
-
concerns: detectedConcerns,
|
|
383
|
-
violationType: 'single-method-multi-concern',
|
|
384
|
-
principle: 'SRP',
|
|
385
|
-
},
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Collects methods, get/set accessors (a get/set pair counts once) and
|
|
391
|
-
* properties initialized with arrow functions or function expressions.
|
|
392
|
-
*/
|
|
393
|
-
private collectCallableMembers(classDecl: ClassDeclaration): CallableMember[] {
|
|
394
|
-
const members: CallableMember[] = [];
|
|
395
|
-
const isNonPrivate = (scope: Scope | undefined, name: string): boolean =>
|
|
396
|
-
scope !== Scope.Private && !name.startsWith('#');
|
|
397
|
-
|
|
398
|
-
for (const method of classDecl.getMethods()) {
|
|
399
|
-
members.push({
|
|
400
|
-
name: method.getName(),
|
|
401
|
-
kind: 'method',
|
|
402
|
-
isNonPrivate: isNonPrivate(method.getScope(), method.getName()),
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
const accessorNames = new Set<string>();
|
|
407
|
-
const accessors = [
|
|
408
|
-
...classDecl.getGetAccessors(),
|
|
409
|
-
...classDecl.getSetAccessors(),
|
|
410
|
-
];
|
|
411
|
-
for (const accessor of accessors) {
|
|
412
|
-
const name = accessor.getName();
|
|
413
|
-
if (accessorNames.has(name)) {
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
accessorNames.add(name);
|
|
417
|
-
members.push({
|
|
418
|
-
name,
|
|
419
|
-
kind: 'accessor',
|
|
420
|
-
isNonPrivate: isNonPrivate(accessor.getScope(), name),
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
for (const property of classDecl.getProperties()) {
|
|
425
|
-
const initializer = property.getInitializer();
|
|
426
|
-
const isFunction =
|
|
427
|
-
initializer !== undefined &&
|
|
428
|
-
(Node.isArrowFunction(initializer) ||
|
|
429
|
-
Node.isFunctionExpression(initializer));
|
|
430
|
-
if (!isFunction) {
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
members.push({
|
|
434
|
-
name: property.getName(),
|
|
435
|
-
kind: 'function-property',
|
|
436
|
-
isNonPrivate: isNonPrivate(property.getScope(), property.getName()),
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
return members;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* Base-class signal: abstract classes, or classes extended by at least
|
|
445
|
-
* `minSubclassesForBase` classes in the project, with a public/protected
|
|
446
|
-
* callable surface of at least `baseClassSurfaceThreshold` members.
|
|
447
|
-
* Subclasses are counted via references to the class name inside
|
|
448
|
-
* `extends` heritage clauses (only computed for large surfaces).
|
|
449
|
-
*/
|
|
450
|
-
private detectBaseClassSignal(
|
|
451
|
-
classDecl: ClassDeclaration,
|
|
452
|
-
members: CallableMember[],
|
|
453
|
-
): BaseClassSignal {
|
|
454
|
-
const nonPrivateSurface = members.filter((m) => m.isNonPrivate).length;
|
|
455
|
-
const isAbstract = classDecl.isAbstract();
|
|
456
|
-
const notBase: BaseClassSignal = {
|
|
457
|
-
isBaseClass: false,
|
|
458
|
-
isAbstract,
|
|
459
|
-
subclassCount: 0,
|
|
460
|
-
nonPrivateSurface,
|
|
461
|
-
};
|
|
462
|
-
if (nonPrivateSurface < this.baseClassSurfaceThreshold) {
|
|
463
|
-
return notBase;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
const subclassCount = this.countSubclasses(classDecl);
|
|
467
|
-
const isBaseClass =
|
|
468
|
-
isAbstract || subclassCount >= this.minSubclassesForBase;
|
|
469
|
-
return { isBaseClass, isAbstract, subclassCount, nonPrivateSurface };
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
private countSubclasses(classDecl: ClassDeclaration): number {
|
|
473
|
-
const nameNode = classDecl.getNameNode();
|
|
474
|
-
if (!nameNode) {
|
|
475
|
-
return 0;
|
|
476
|
-
}
|
|
477
|
-
try {
|
|
478
|
-
const subclasses = new Set<Node>();
|
|
479
|
-
for (const ref of nameNode.findReferencesAsNodes()) {
|
|
480
|
-
const heritage = ref.getFirstAncestorByKind(SyntaxKind.HeritageClause);
|
|
481
|
-
if (heritage?.getToken() !== SyntaxKind.ExtendsKeyword) {
|
|
482
|
-
continue;
|
|
483
|
-
}
|
|
484
|
-
const owner = heritage.getParent();
|
|
485
|
-
if (Node.isClassDeclaration(owner) || Node.isClassExpression(owner)) {
|
|
486
|
-
subclasses.add(owner);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
return subclasses.size;
|
|
490
|
-
} catch {
|
|
491
|
-
return 0;
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Analyze the responsibilities of callable members in a class.
|
|
497
|
-
* Returns a set of responsibility category names found.
|
|
498
|
-
*/
|
|
499
|
-
private analyzeResponsibilities(memberNames: string[]): Set<string> {
|
|
500
|
-
const responsibilities = new Set<string>();
|
|
501
|
-
|
|
502
|
-
for (const methodName of memberNames) {
|
|
503
|
-
try {
|
|
504
|
-
if (!methodName) {
|
|
505
|
-
continue;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
// Skip Angular lifecycle methods (interface methods)
|
|
509
|
-
if (methodName.startsWith('ng')) {
|
|
510
|
-
continue;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
// Convert to lowercase for case-insensitive matching
|
|
514
|
-
const lowerName = methodName.toLowerCase();
|
|
515
|
-
|
|
516
|
-
// Check each responsibility category
|
|
517
|
-
for (const category of this.responsibilityCategories) {
|
|
518
|
-
for (const keyword of category.keywords) {
|
|
519
|
-
if (lowerName.includes(keyword)) {
|
|
520
|
-
responsibilities.add(category.name);
|
|
521
|
-
break; // Found a match for this category, move to next category
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
} catch (_error) {
|
|
526
|
-
// Skip methods that can't be analyzed
|
|
527
|
-
continue;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
return responsibilities;
|
|
532
|
-
}
|
|
533
|
-
}
|
package/src/solid-plugin.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-solid - Main Plugin
|
|
3
|
-
*
|
|
4
|
-
* The main entry point for the SOLID principles analysis plugin.
|
|
5
|
-
* Provides analysis rules for detecting SOLID principle violations.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type {
|
|
9
|
-
Plugin,
|
|
10
|
-
AnalysisRule,
|
|
11
|
-
TransformRule,
|
|
12
|
-
ValidationRule,
|
|
13
|
-
IKernel
|
|
14
|
-
} from '@angular-modernizer/plugin-system';
|
|
15
|
-
import { DIPViolationRule } from './rules/dip-violation.rule.js';
|
|
16
|
-
import { SingleResponsibilityRule } from './rules/srp-violation.rule.js';
|
|
17
|
-
import { LSPViolationRule } from './rules/lsp-violation.rule.js';
|
|
18
|
-
import { OCPViolationRule } from './rules/ocp-violation.rule.js';
|
|
19
|
-
import { ISPViolationRule } from './rules/isp-violation.rule.js';
|
|
20
|
-
import { GodSwitchAnalysisRule } from './rules/god-switch-analysis.rule.js';
|
|
21
|
-
import { GodSwitchTransformRule } from './transform-rules/god-switch-transform.rule.js';
|
|
22
|
-
import { ServiceDipViolationTransformRule } from './transform-rules/service-dip-violation.transform.rule.js';
|
|
23
|
-
import { ServiceDipViolationRule } from './rules/service-dip-violation.analysis.rule.js';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* SOLID Principles Analysis Plugin
|
|
27
|
-
*
|
|
28
|
-
* @remarks
|
|
29
|
-
* This plugin provides analysis rules for detecting violations of SOLID principles
|
|
30
|
-
* in Angular applications. It focuses on architectural issues that can lead to
|
|
31
|
-
* maintainability problems.
|
|
32
|
-
*
|
|
33
|
-
* ## Current Rules
|
|
34
|
-
*
|
|
35
|
-
* ### Analysis Rules
|
|
36
|
-
* - {@link DIPViolationRule}: Detects direct service instantiation (DIP violation)
|
|
37
|
-
* - {@link ServiceDipViolationRule}: Detects concrete constructor injection instead of interfaces (DIP violation)
|
|
38
|
-
* - {@link SingleResponsibilityRule}: Detects classes with too many responsibilities (SRP violation)
|
|
39
|
-
* - {@link LSPViolationRule}: Detects inheritance and interface implementation issues (LSP violation)
|
|
40
|
-
* - {@link OCPViolationRule}: Detects code that violates the Open/Closed Principle (OCP violation)
|
|
41
|
-
* - {@link ISPViolationRule}: Detects interface segregation violations (ISP violation)
|
|
42
|
-
* - {@link GodSwitchAnalysisRule}: Detects large switch statements (God Switch anti-pattern)
|
|
43
|
-
*
|
|
44
|
-
* ### Transform Rules
|
|
45
|
-
* - {@link GodSwitchTransformRule}: Transforms God Switch violations into Factory Pattern implementations
|
|
46
|
-
*
|
|
47
|
-
* ## Future Rules (Planned)
|
|
48
|
-
*
|
|
49
|
-
* - MissingInterfaceRule: Services without interfaces (DIP)
|
|
50
|
-
* - CircularDependencyRule: Import cycles (architectural)
|
|
51
|
-
* - FatInterfaceRule: Interfaces with too many methods (ISP)
|
|
52
|
-
*
|
|
53
|
-
* @see {@link DIPViolationRule} for the DIP implementation
|
|
54
|
-
* @see {@link SingleResponsibilityRule} for the SRP implementation
|
|
55
|
-
* @see {@link GodSwitchTransformRule} for the God Switch transformation
|
|
56
|
-
*/
|
|
57
|
-
export class SolidPlugin implements Plugin {
|
|
58
|
-
/**
|
|
59
|
-
* Unique plugin identifier.
|
|
60
|
-
*/
|
|
61
|
-
public readonly name = '@angular-modernizer/plugin-solid';
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Plugin version (from package.json).
|
|
65
|
-
*/
|
|
66
|
-
public readonly version = '0.1.0';
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Plugin description.
|
|
70
|
-
*/
|
|
71
|
-
public readonly description =
|
|
72
|
-
'Provides SOLID principles analysis for architectural violations';
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Plugin author.
|
|
76
|
-
*/
|
|
77
|
-
public readonly author = 'Angular Modernization Platform';
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Initialize the plugin.
|
|
81
|
-
*
|
|
82
|
-
* @param _kernel - The Kernel instance for accessing infrastructure
|
|
83
|
-
*/
|
|
84
|
-
async initialize(_kernel: IKernel): Promise<void> {
|
|
85
|
-
// No initialization needed for now
|
|
86
|
-
// Future: Load configuration, register analyzers, etc.
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Get all analysis rules provided by this plugin.
|
|
91
|
-
*
|
|
92
|
-
* @returns Array of analysis rules for SOLID violations
|
|
93
|
-
*/
|
|
94
|
-
getAnalysisRules(): AnalysisRule[] {
|
|
95
|
-
return [
|
|
96
|
-
new DIPViolationRule(),
|
|
97
|
-
new SingleResponsibilityRule(),
|
|
98
|
-
new LSPViolationRule(),
|
|
99
|
-
new OCPViolationRule(),
|
|
100
|
-
new ISPViolationRule(),
|
|
101
|
-
new GodSwitchAnalysisRule(),
|
|
102
|
-
new ServiceDipViolationRule(),
|
|
103
|
-
];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Get all transformation rules provided by this plugin.
|
|
108
|
-
*
|
|
109
|
-
* @returns Array of transformation rules for SOLID violations
|
|
110
|
-
*/
|
|
111
|
-
getTransformRules(): TransformRule[] {
|
|
112
|
-
return [
|
|
113
|
-
new GodSwitchTransformRule(),
|
|
114
|
-
new ServiceDipViolationTransformRule(),
|
|
115
|
-
];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Get all validation rules provided by this plugin.
|
|
120
|
-
*
|
|
121
|
-
* @returns Empty array (no validation rules yet)
|
|
122
|
-
*/
|
|
123
|
-
getValidationRules(): ValidationRule[] {
|
|
124
|
-
// Future: Add validation rules to verify fixes
|
|
125
|
-
return [];
|
|
126
|
-
}
|
|
127
|
-
}
|