@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,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-modernizer/plugin-solid - Service DIP Violation Rule
|
|
3
|
+
*
|
|
4
|
+
* Detects constructor injection of concrete `` classes instead of interfaces
|
|
5
|
+
* (Dependency Inversion Principle violation).
|
|
6
|
+
*
|
|
7
|
+
* Detection Pattern:
|
|
8
|
+
* - `` class whose constructor parameter type resolves to another `` class
|
|
9
|
+
* - Both the injecting class and the injected type must have ``
|
|
10
|
+
* - Optional: cross-layer detection via path-based layer extraction
|
|
11
|
+
*
|
|
12
|
+
* Sibling to DIPViolationRule (solid:dip-violation-direct-instantiation) which detects
|
|
13
|
+
* `new MyService()` usage. This rule detects `constructor(private svc: ConcreteService)`.
|
|
14
|
+
*
|
|
15
|
+
* TransformRule + Orchestrator is deferred to v2.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
Node,
|
|
20
|
+
type ClassDeclaration,
|
|
21
|
+
type ParameterDeclaration,
|
|
22
|
+
} from 'ts-morph';
|
|
23
|
+
import type {
|
|
24
|
+
AnalysisRule,
|
|
25
|
+
AnalysisContext,
|
|
26
|
+
AnalysisResult,
|
|
27
|
+
} from '@angular-modernizer/plugin-system';
|
|
28
|
+
|
|
29
|
+
export interface ServiceDipViolationConfig {
|
|
30
|
+
/**
|
|
31
|
+
* When true, only report violations that cross architectural layer boundaries.
|
|
32
|
+
* Layers are inferred from path segments: shared, features, core, lib, app, config.
|
|
33
|
+
* Default: false (report all concrete constructor injections).
|
|
34
|
+
*/
|
|
35
|
+
crossLayerOnly?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ServiceDipViolationMetadata {
|
|
39
|
+
violationType: 'concrete-injection';
|
|
40
|
+
injectedClass: string;
|
|
41
|
+
injectingClass: string;
|
|
42
|
+
parameterName: string;
|
|
43
|
+
suggestedInterfaceName: string;
|
|
44
|
+
confidence: number;
|
|
45
|
+
isBidirectional: boolean;
|
|
46
|
+
isCrossLayer: boolean;
|
|
47
|
+
injectingLayer?: string;
|
|
48
|
+
injectedLayer?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Service DIP Violation Rule — detects concrete `` constructor injection.
|
|
53
|
+
*
|
|
54
|
+
* Reports when an `` class injects another `` class directly instead
|
|
55
|
+
* of depending on an abstraction (interface). Provides a suggested interface name
|
|
56
|
+
* (`I` + injected class name) and a human-readable suggested fix.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Violation detected:
|
|
60
|
+
* class UserService {
|
|
61
|
+
* constructor(private orderService: OrderService) {}
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* // Suggested fix:
|
|
65
|
+
* class UserService {
|
|
66
|
+
* constructor(private orderService: IOrderService) {}
|
|
67
|
+
* }
|
|
68
|
+
*/
|
|
69
|
+
export class ServiceDipViolationRule implements AnalysisRule {
|
|
70
|
+
public readonly id = 'solid:service-dip-violation';
|
|
71
|
+
public readonly name = 'Service DIP Violation';
|
|
72
|
+
public readonly description =
|
|
73
|
+
'Detects @Injectable classes injecting other @Injectable classes by concrete type instead of interface';
|
|
74
|
+
public readonly severity = 'warning';
|
|
75
|
+
public readonly category = 'solid-violation';
|
|
76
|
+
public readonly tags = ['solid', 'dip', 'interfaces', 'dependency-injection'];
|
|
77
|
+
|
|
78
|
+
private readonly config: ServiceDipViolationConfig;
|
|
79
|
+
|
|
80
|
+
constructor(config: ServiceDipViolationConfig = {}) {
|
|
81
|
+
this.config = config;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
85
|
+
const sourceFile = context.sourceFile;
|
|
86
|
+
|
|
87
|
+
// Skip declaration files
|
|
88
|
+
if (sourceFile.getFilePath().endsWith('.d.ts')) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const violations: AnalysisResult[] = [];
|
|
93
|
+
const injectingFilePath = sourceFile.getFilePath();
|
|
94
|
+
|
|
95
|
+
for (const classDecl of sourceFile.getClasses()) {
|
|
96
|
+
if (!this.hasInjectableDecorator(classDecl)) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const ctor = classDecl.getConstructors()[0];
|
|
101
|
+
if (!ctor) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const param of ctor.getParameters()) {
|
|
106
|
+
const violation = this.checkParameter(
|
|
107
|
+
classDecl,
|
|
108
|
+
param,
|
|
109
|
+
injectingFilePath,
|
|
110
|
+
context,
|
|
111
|
+
);
|
|
112
|
+
if (violation) {
|
|
113
|
+
violations.push(violation);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return violations;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
// Core detection
|
|
123
|
+
// ---------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
private checkParameter(
|
|
126
|
+
injectingClass: ClassDeclaration,
|
|
127
|
+
param: ParameterDeclaration,
|
|
128
|
+
injectingFilePath: string,
|
|
129
|
+
_context: AnalysisContext,
|
|
130
|
+
): AnalysisResult | null {
|
|
131
|
+
const injectedDecl = this.resolveServiceDeclaration(param);
|
|
132
|
+
if (!injectedDecl) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
if (!this.hasInjectableDecorator(injectedDecl)) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const injectedClass = injectedDecl.getName() ?? 'Unknown';
|
|
140
|
+
const injectingClassName = injectingClass.getName() ?? 'Unknown';
|
|
141
|
+
const paramName = param.getName();
|
|
142
|
+
const suggestedInterfaceName = `I${injectedClass}`;
|
|
143
|
+
|
|
144
|
+
// Layer information
|
|
145
|
+
const injectedSourceFile = injectedDecl.getSourceFile();
|
|
146
|
+
const injectedFilePath = injectedSourceFile.getFilePath();
|
|
147
|
+
|
|
148
|
+
const injectingLayer = this.extractLayer(injectingFilePath);
|
|
149
|
+
const injectedLayer = this.extractLayer(injectedFilePath);
|
|
150
|
+
const isCrossLayer =
|
|
151
|
+
injectingLayer !== undefined &&
|
|
152
|
+
injectedLayer !== undefined &&
|
|
153
|
+
injectingLayer !== injectedLayer;
|
|
154
|
+
|
|
155
|
+
// Apply crossLayerOnly filter
|
|
156
|
+
if (this.config.crossLayerOnly && !isCrossLayer) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const metadata: ServiceDipViolationMetadata = {
|
|
161
|
+
violationType: 'concrete-injection',
|
|
162
|
+
injectedClass,
|
|
163
|
+
injectingClass: injectingClassName,
|
|
164
|
+
parameterName: paramName,
|
|
165
|
+
suggestedInterfaceName,
|
|
166
|
+
confidence: 0.95,
|
|
167
|
+
isBidirectional: false,
|
|
168
|
+
isCrossLayer,
|
|
169
|
+
...(injectingLayer !== undefined ? { injectingLayer } : {}),
|
|
170
|
+
...(injectedLayer !== undefined ? { injectedLayer } : {}),
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
ruleId: this.id,
|
|
175
|
+
message:
|
|
176
|
+
`${injectingClassName} injects concrete class ${injectedClass} via constructor. ` +
|
|
177
|
+
`Depend on an interface (${suggestedInterfaceName}) instead.`,
|
|
178
|
+
filePath: injectingFilePath,
|
|
179
|
+
line: param.getStartLineNumber(),
|
|
180
|
+
column: param.getStart() - param.getStartLinePos(),
|
|
181
|
+
suggestedFix:
|
|
182
|
+
`Extract interface ${suggestedInterfaceName} from ${injectedClass}, ` +
|
|
183
|
+
`add injection token, and replace the parameter type with ${suggestedInterfaceName}.`,
|
|
184
|
+
metadata: metadata as unknown as Record<string, unknown>,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Helpers
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Resolves the class declaration that a constructor parameter's type refers to.
|
|
194
|
+
* Uses ts-morph's type checker — works across files within the same Project.
|
|
195
|
+
*/
|
|
196
|
+
private resolveServiceDeclaration(
|
|
197
|
+
param: ParameterDeclaration,
|
|
198
|
+
): ClassDeclaration | null {
|
|
199
|
+
try {
|
|
200
|
+
const declarations = param.getType().getSymbol()?.getDeclarations();
|
|
201
|
+
if (!declarations || declarations.length === 0) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (const decl of declarations) {
|
|
206
|
+
if (Node.isClassDeclaration(decl)) {
|
|
207
|
+
return decl;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
} catch {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Checks whether a class has an `` decorator.
|
|
218
|
+
* Pattern copied from angular-service-without-injectable.rule.ts.
|
|
219
|
+
*/
|
|
220
|
+
private hasInjectableDecorator(classDecl: ClassDeclaration): boolean {
|
|
221
|
+
return classDecl
|
|
222
|
+
.getDecorators()
|
|
223
|
+
.some((decorator) => decorator.getName() === 'Injectable');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Extracts the architectural layer from a file path by scanning path segments.
|
|
228
|
+
* Path-based fallback adapted from import-direction-violation.rule.ts.
|
|
229
|
+
*
|
|
230
|
+
* Returns undefined when no known layer segment is found.
|
|
231
|
+
*/
|
|
232
|
+
private extractLayer(filePath: string): string | undefined {
|
|
233
|
+
const parts = filePath.split('/');
|
|
234
|
+
|
|
235
|
+
// Exact segment match (highest confidence)
|
|
236
|
+
const exactLayers = [
|
|
237
|
+
'shared',
|
|
238
|
+
'features',
|
|
239
|
+
'core',
|
|
240
|
+
'lib',
|
|
241
|
+
'libs',
|
|
242
|
+
'app',
|
|
243
|
+
'config',
|
|
244
|
+
'environments',
|
|
245
|
+
'domain',
|
|
246
|
+
];
|
|
247
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
248
|
+
const part = parts[i]?.toLowerCase();
|
|
249
|
+
if (!part) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (exactLayers.includes(part)) {
|
|
253
|
+
return part === 'libs' ? 'lib' : part;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Substring match (lower confidence — still useful)
|
|
258
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
259
|
+
const part = parts[i]?.toLowerCase();
|
|
260
|
+
if (!part) {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (part.includes('shared')) {
|
|
264
|
+
return 'shared';
|
|
265
|
+
}
|
|
266
|
+
if (part.includes('features')) {
|
|
267
|
+
return 'features';
|
|
268
|
+
}
|
|
269
|
+
if (part.includes('core')) {
|
|
270
|
+
return 'core';
|
|
271
|
+
}
|
|
272
|
+
if (part.includes('libs') || part.includes('lib')) {
|
|
273
|
+
return 'lib';
|
|
274
|
+
}
|
|
275
|
+
if (part.includes('app')) {
|
|
276
|
+
return 'app';
|
|
277
|
+
}
|
|
278
|
+
if (part.includes('config') || part.includes('environments')) {
|
|
279
|
+
return 'config';
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -0,0 +1,366 @@
|
|
|
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
|
+
* - Classes mixing multiple responsibility categories
|
|
9
|
+
*
|
|
10
|
+
* Responsibility Categories:
|
|
11
|
+
* - UI: render, display, show, hide, toggle, open, close
|
|
12
|
+
* - Business: calculate, validate, process, transform, compute
|
|
13
|
+
* - HTTP: fetch, get, post, put, delete, request, call
|
|
14
|
+
* - Data: save, load, store, retrieve, persist, cache
|
|
15
|
+
* - Event: emit, dispatch, trigger, fire, notify, publish
|
|
16
|
+
* - Lifecycle: init, destroy, cleanup, setup, teardown
|
|
17
|
+
*
|
|
18
|
+
* This rule helps enforce the Single Responsibility Principle by identifying
|
|
19
|
+
* classes that are doing too much or handling multiple unrelated concerns.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Violation: class mixes HTTP, business, and UI concerns
|
|
23
|
+
* class UserComponent {
|
|
24
|
+
* fetchUsers() {} // HTTP concern
|
|
25
|
+
* calculateTotal() {} // Business concern
|
|
26
|
+
* renderChart() {} // UI concern
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
SyntaxKind,
|
|
32
|
+
type ClassDeclaration,
|
|
33
|
+
type MethodDeclaration,
|
|
34
|
+
} from 'ts-morph';
|
|
35
|
+
import type {
|
|
36
|
+
AnalysisRule,
|
|
37
|
+
AnalysisContext,
|
|
38
|
+
AnalysisResult,
|
|
39
|
+
} from '@angular-modernizer/plugin-system';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Responsibility category for method classification.
|
|
43
|
+
*/
|
|
44
|
+
interface ResponsibilityCategory {
|
|
45
|
+
name: string;
|
|
46
|
+
keywords: string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* SRP Violation Rule - detects Single Responsibility Principle violations.
|
|
51
|
+
*
|
|
52
|
+
* This rule implements the Single Responsibility Principle by detecting
|
|
53
|
+
* classes with too many methods or mixed responsibilities.
|
|
54
|
+
*/
|
|
55
|
+
export class SingleResponsibilityRule implements AnalysisRule {
|
|
56
|
+
public readonly id = 'solid:srp-single-responsibility-violation';
|
|
57
|
+
public readonly name = 'Single Responsibility Principle Violation';
|
|
58
|
+
public readonly description =
|
|
59
|
+
'Detects classes with too many methods or mixed responsibilities';
|
|
60
|
+
public readonly severity = 'warning';
|
|
61
|
+
public readonly category = 'solid-violation';
|
|
62
|
+
public readonly tags = [
|
|
63
|
+
'solid',
|
|
64
|
+
'srp',
|
|
65
|
+
'single-responsibility',
|
|
66
|
+
'architecture',
|
|
67
|
+
'maintainability',
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Default threshold for maximum number of methods in a class.
|
|
72
|
+
*/
|
|
73
|
+
private readonly maxMethodsThreshold = 15;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Minimum number of different responsibility categories to flag as mixed.
|
|
77
|
+
*/
|
|
78
|
+
private readonly minMixedResponsibilities = 3;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Responsibility categories for method classification.
|
|
82
|
+
*/
|
|
83
|
+
private readonly responsibilityCategories: ResponsibilityCategory[] = [
|
|
84
|
+
{
|
|
85
|
+
name: 'UI',
|
|
86
|
+
keywords: [
|
|
87
|
+
'render',
|
|
88
|
+
'display',
|
|
89
|
+
'show',
|
|
90
|
+
'hide',
|
|
91
|
+
'toggle',
|
|
92
|
+
'open',
|
|
93
|
+
'close',
|
|
94
|
+
'view',
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'Business',
|
|
99
|
+
keywords: [
|
|
100
|
+
'calculate',
|
|
101
|
+
'validate',
|
|
102
|
+
'process',
|
|
103
|
+
'transform',
|
|
104
|
+
'compute',
|
|
105
|
+
'check',
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'HTTP',
|
|
110
|
+
keywords: [
|
|
111
|
+
'fetch',
|
|
112
|
+
'get',
|
|
113
|
+
'post',
|
|
114
|
+
'put',
|
|
115
|
+
'delete',
|
|
116
|
+
'request',
|
|
117
|
+
'call',
|
|
118
|
+
'api',
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: 'Data',
|
|
123
|
+
keywords: [
|
|
124
|
+
'save',
|
|
125
|
+
'load',
|
|
126
|
+
'store',
|
|
127
|
+
'retrieve',
|
|
128
|
+
'persist',
|
|
129
|
+
'cache',
|
|
130
|
+
'read',
|
|
131
|
+
'write',
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: 'Event',
|
|
136
|
+
keywords: [
|
|
137
|
+
'emit',
|
|
138
|
+
'dispatch',
|
|
139
|
+
'trigger',
|
|
140
|
+
'fire',
|
|
141
|
+
'notify',
|
|
142
|
+
'publish',
|
|
143
|
+
'subscribe',
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
name: 'Lifecycle',
|
|
148
|
+
keywords: [
|
|
149
|
+
'init',
|
|
150
|
+
'destroy',
|
|
151
|
+
'cleanup',
|
|
152
|
+
'setup',
|
|
153
|
+
'teardown',
|
|
154
|
+
'start',
|
|
155
|
+
'stop',
|
|
156
|
+
],
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
161
|
+
const violations: AnalysisResult[] = [];
|
|
162
|
+
const sourceFile = context.sourceFile;
|
|
163
|
+
|
|
164
|
+
// Find all class declarations in the file
|
|
165
|
+
const classes = sourceFile.getDescendantsOfKind(
|
|
166
|
+
SyntaxKind.ClassDeclaration,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
for (const classDecl of classes) {
|
|
170
|
+
const classViolations = this.checkClass(
|
|
171
|
+
classDecl,
|
|
172
|
+
sourceFile.getFilePath(),
|
|
173
|
+
);
|
|
174
|
+
violations.push(...classViolations);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return Promise.resolve(violations);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Check if a class violates SRP.
|
|
182
|
+
*/
|
|
183
|
+
private checkClass(
|
|
184
|
+
classDecl: ClassDeclaration,
|
|
185
|
+
filePath: string,
|
|
186
|
+
): AnalysisResult[] {
|
|
187
|
+
const violations: AnalysisResult[] = [];
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
const className = classDecl.getName() ?? '<anonymous>';
|
|
191
|
+
const methods = classDecl.getMethods();
|
|
192
|
+
|
|
193
|
+
// Skip empty or very small classes
|
|
194
|
+
if (methods.length === 0) {
|
|
195
|
+
return violations;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Check for too many methods
|
|
199
|
+
if (methods.length > this.maxMethodsThreshold) {
|
|
200
|
+
const startLine = classDecl.getStartLineNumber();
|
|
201
|
+
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
202
|
+
|
|
203
|
+
violations.push({
|
|
204
|
+
ruleId: this.id,
|
|
205
|
+
message: `Class '${className}' has ${methods.length} methods (threshold: ${this.maxMethodsThreshold}). Consider splitting into smaller, focused classes.`,
|
|
206
|
+
filePath,
|
|
207
|
+
line: startLine,
|
|
208
|
+
column: startColumn,
|
|
209
|
+
suggestedFix: `Refactor '${className}' by extracting related methods into separate classes, each with a single responsibility.`,
|
|
210
|
+
metadata: {
|
|
211
|
+
className,
|
|
212
|
+
methodCount: methods.length,
|
|
213
|
+
threshold: this.maxMethodsThreshold,
|
|
214
|
+
violationType: 'too-many-methods',
|
|
215
|
+
principle: 'SRP',
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Check for single-method multi-concern (Cat 2)
|
|
221
|
+
for (const method of methods) {
|
|
222
|
+
const methodViolation = this.analyzeMethodBodyConcerns(
|
|
223
|
+
method,
|
|
224
|
+
className,
|
|
225
|
+
filePath,
|
|
226
|
+
);
|
|
227
|
+
if (methodViolation) {
|
|
228
|
+
violations.push(methodViolation);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Check for mixed responsibilities
|
|
233
|
+
const responsibilities = this.analyzeResponsibilities(methods);
|
|
234
|
+
if (responsibilities.size >= this.minMixedResponsibilities) {
|
|
235
|
+
const startLine = classDecl.getStartLineNumber();
|
|
236
|
+
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
237
|
+
const responsibilityList = Array.from(responsibilities).join(', ');
|
|
238
|
+
|
|
239
|
+
violations.push({
|
|
240
|
+
ruleId: this.id,
|
|
241
|
+
message: `Class '${className}' appears to have mixed responsibilities: ${responsibilityList}. Consider separating these concerns.`,
|
|
242
|
+
filePath,
|
|
243
|
+
line: startLine,
|
|
244
|
+
column: startColumn,
|
|
245
|
+
suggestedFix: `Extract methods into separate classes based on their responsibilities.`,
|
|
246
|
+
metadata: {
|
|
247
|
+
className,
|
|
248
|
+
responsibilities: Array.from(responsibilities),
|
|
249
|
+
violationType: 'mixed-responsibilities',
|
|
250
|
+
principle: 'SRP',
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return violations;
|
|
256
|
+
} catch (_error) {
|
|
257
|
+
// If we can't analyze the class, skip it
|
|
258
|
+
return violations;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Detect a single method that mixes ≥4 distinct concern categories AND is ≥20 lines.
|
|
264
|
+
* Scans the full method body text for concern keywords (same categories as class-level).
|
|
265
|
+
*/
|
|
266
|
+
private analyzeMethodBodyConcerns(
|
|
267
|
+
method: MethodDeclaration,
|
|
268
|
+
className: string,
|
|
269
|
+
filePath: string,
|
|
270
|
+
): AnalysisResult | null {
|
|
271
|
+
const methodName = method.getName();
|
|
272
|
+
if (!methodName) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Skip Angular lifecycle hooks
|
|
277
|
+
if (methodName.startsWith('ng')) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const body = method.getBody();
|
|
282
|
+
if (!body) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Too short to meaningfully mix concerns
|
|
287
|
+
const lineCount = method.getEndLineNumber() - method.getStartLineNumber();
|
|
288
|
+
if (lineCount < 20) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const bodyText = body.getFullText().toLowerCase();
|
|
293
|
+
const detectedConcerns: string[] = [];
|
|
294
|
+
|
|
295
|
+
for (const category of this.responsibilityCategories) {
|
|
296
|
+
for (const keyword of category.keywords) {
|
|
297
|
+
if (bodyText.includes(keyword)) {
|
|
298
|
+
if (!detectedConcerns.includes(category.name)) {
|
|
299
|
+
detectedConcerns.push(category.name);
|
|
300
|
+
}
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (detectedConcerns.length < 4) {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
ruleId: this.id,
|
|
312
|
+
message: `Method '${methodName}' in '${className}' mixes ${detectedConcerns.length} concerns (${detectedConcerns.join(', ')}) in one body.`,
|
|
313
|
+
filePath,
|
|
314
|
+
line: method.getStartLineNumber(),
|
|
315
|
+
column: method.getStart() - method.getStartLinePos(),
|
|
316
|
+
suggestedFix: `Extract each concern into a dedicated private method or service to honour the Single Responsibility Principle.`,
|
|
317
|
+
metadata: {
|
|
318
|
+
className,
|
|
319
|
+
methodName,
|
|
320
|
+
concerns: detectedConcerns,
|
|
321
|
+
violationType: 'single-method-multi-concern',
|
|
322
|
+
principle: 'SRP',
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Analyze the responsibilities of methods in a class.
|
|
329
|
+
* Returns a set of responsibility category names found.
|
|
330
|
+
*/
|
|
331
|
+
private analyzeResponsibilities(methods: MethodDeclaration[]): Set<string> {
|
|
332
|
+
const responsibilities = new Set<string>();
|
|
333
|
+
|
|
334
|
+
for (const method of methods) {
|
|
335
|
+
try {
|
|
336
|
+
const methodName = method.getName();
|
|
337
|
+
if (!methodName) {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Skip Angular lifecycle methods (interface methods)
|
|
342
|
+
if (methodName.startsWith('ng')) {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Convert to lowercase for case-insensitive matching
|
|
347
|
+
const lowerName = methodName.toLowerCase();
|
|
348
|
+
|
|
349
|
+
// Check each responsibility category
|
|
350
|
+
for (const category of this.responsibilityCategories) {
|
|
351
|
+
for (const keyword of category.keywords) {
|
|
352
|
+
if (lowerName.includes(keyword)) {
|
|
353
|
+
responsibilities.add(category.name);
|
|
354
|
+
break; // Found a match for this category, move to next category
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
} catch (_error) {
|
|
359
|
+
// Skip methods that can't be analyzed
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return responsibilities;
|
|
365
|
+
}
|
|
366
|
+
}
|