@angular-modernizer/plugin-solid 0.1.3 → 0.2.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.
|
@@ -4,8 +4,14 @@
|
|
|
4
4
|
* Detects Single Responsibility Principle violations in classes.
|
|
5
5
|
*
|
|
6
6
|
* Detection Patterns:
|
|
7
|
-
* - Classes with too many methods (configurable threshold, default: 15)
|
|
7
|
+
* - Classes with too many methods (configurable threshold, default: 15).
|
|
8
|
+
* Get/set accessors and arrow-function / function-expression properties
|
|
9
|
+
* count as methods.
|
|
8
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`.
|
|
9
15
|
*
|
|
10
16
|
* Responsibility Categories:
|
|
11
17
|
* - UI: render, display, show, hide, toggle, open, close
|
|
@@ -48,6 +54,19 @@ export declare class SingleResponsibilityRule implements AnalysisRule {
|
|
|
48
54
|
* Minimum number of different responsibility categories to flag as mixed.
|
|
49
55
|
*/
|
|
50
56
|
private readonly minMixedResponsibilities;
|
|
57
|
+
/**
|
|
58
|
+
* Minimum number of mixed categories for base classes (abstract or
|
|
59
|
+
* subclassed >= `minSubclassesForBase` times) with a large surface.
|
|
60
|
+
*/
|
|
61
|
+
private readonly minMixedResponsibilitiesForBase;
|
|
62
|
+
/**
|
|
63
|
+
* Number of subclasses in the project that make a class a base class.
|
|
64
|
+
*/
|
|
65
|
+
private readonly minSubclassesForBase;
|
|
66
|
+
/**
|
|
67
|
+
* Public/protected callable members a base class needs to be evaluated.
|
|
68
|
+
*/
|
|
69
|
+
private readonly baseClassSurfaceThreshold;
|
|
51
70
|
/**
|
|
52
71
|
* Responsibility categories for method classification.
|
|
53
72
|
*/
|
|
@@ -63,7 +82,21 @@ export declare class SingleResponsibilityRule implements AnalysisRule {
|
|
|
63
82
|
*/
|
|
64
83
|
private analyzeMethodBodyConcerns;
|
|
65
84
|
/**
|
|
66
|
-
*
|
|
85
|
+
* Collects methods, get/set accessors (a get/set pair counts once) and
|
|
86
|
+
* properties initialized with arrow functions or function expressions.
|
|
87
|
+
*/
|
|
88
|
+
private collectCallableMembers;
|
|
89
|
+
/**
|
|
90
|
+
* Base-class signal: abstract classes, or classes extended by at least
|
|
91
|
+
* `minSubclassesForBase` classes in the project, with a public/protected
|
|
92
|
+
* callable surface of at least `baseClassSurfaceThreshold` members.
|
|
93
|
+
* Subclasses are counted via references to the class name inside
|
|
94
|
+
* `extends` heritage clauses (only computed for large surfaces).
|
|
95
|
+
*/
|
|
96
|
+
private detectBaseClassSignal;
|
|
97
|
+
private countSubclasses;
|
|
98
|
+
/**
|
|
99
|
+
* Analyze the responsibilities of callable members in a class.
|
|
67
100
|
* Returns a set of responsibility category names found.
|
|
68
101
|
*/
|
|
69
102
|
private analyzeResponsibilities;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"srp-violation.rule.d.ts","sourceRoot":"","sources":["../../src/rules/srp-violation.rule.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"srp-violation.rule.d.ts","sourceRoot":"","sources":["../../src/rules/srp-violation.rule.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AASH,OAAO,KAAK,EACV,YAAY,EACZ,eAAe,EACf,cAAc,EACf,MAAM,mCAAmC,CAAC;AA8B3C;;;;;GAKG;AACH,qBAAa,wBAAyB,YAAW,YAAY;IAC3D,SAAgB,EAAE,+CAA+C;IACjE,SAAgB,IAAI,+CAA+C;IACnE,SAAgB,WAAW,qEACyC;IACpE,SAAgB,QAAQ,aAAa;IACrC,SAAgB,QAAQ,qBAAqB;IAC7C,SAAgB,IAAI,WAMlB;IAEF;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAM;IAE1C;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAAK;IAE9C;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,+BAA+B,CAAK;IAErD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAK;IAE1C;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAK;IAE/C;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CA2EvC;IAEI,OAAO,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAoBlE;;OAEG;IACH,OAAO,CAAC,UAAU;IAiGlB;;;OAGG;IACH,OAAO,CAAC,yBAAyB;IA6DjC;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAkD9B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IAsB7B,OAAO,CAAC,eAAe;IAuBvB;;;OAGG;IACH,OAAO,CAAC,uBAAuB;CAkChC"}
|
|
@@ -5,8 +5,14 @@
|
|
|
5
5
|
* Detects Single Responsibility Principle violations in classes.
|
|
6
6
|
*
|
|
7
7
|
* Detection Patterns:
|
|
8
|
-
* - Classes with too many methods (configurable threshold, default: 15)
|
|
8
|
+
* - Classes with too many methods (configurable threshold, default: 15).
|
|
9
|
+
* Get/set accessors and arrow-function / function-expression properties
|
|
10
|
+
* count as methods.
|
|
9
11
|
* - Classes mixing multiple responsibility categories
|
|
12
|
+
* - Base classes (abstract, or extended by >= 3 classes in the project) with
|
|
13
|
+
* a large public/protected surface: every subclass inherits all concerns,
|
|
14
|
+
* so the category check already fires at 2 categories. Metadata carries
|
|
15
|
+
* `isBaseClass`, `isAbstract` and `subclassCount`.
|
|
10
16
|
*
|
|
11
17
|
* Responsibility Categories:
|
|
12
18
|
* - UI: render, display, show, hide, toggle, open, close
|
|
@@ -57,6 +63,19 @@ class SingleResponsibilityRule {
|
|
|
57
63
|
* Minimum number of different responsibility categories to flag as mixed.
|
|
58
64
|
*/
|
|
59
65
|
minMixedResponsibilities = 3;
|
|
66
|
+
/**
|
|
67
|
+
* Minimum number of mixed categories for base classes (abstract or
|
|
68
|
+
* subclassed >= `minSubclassesForBase` times) with a large surface.
|
|
69
|
+
*/
|
|
70
|
+
minMixedResponsibilitiesForBase = 2;
|
|
71
|
+
/**
|
|
72
|
+
* Number of subclasses in the project that make a class a base class.
|
|
73
|
+
*/
|
|
74
|
+
minSubclassesForBase = 3;
|
|
75
|
+
/**
|
|
76
|
+
* Public/protected callable members a base class needs to be evaluated.
|
|
77
|
+
*/
|
|
78
|
+
baseClassSurfaceThreshold = 8;
|
|
60
79
|
/**
|
|
61
80
|
* Responsibility categories for method classification.
|
|
62
81
|
*/
|
|
@@ -155,24 +174,28 @@ class SingleResponsibilityRule {
|
|
|
155
174
|
try {
|
|
156
175
|
const className = classDecl.getName() ?? '<anonymous>';
|
|
157
176
|
const methods = classDecl.getMethods();
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (methods.length > this.maxMethodsThreshold) {
|
|
177
|
+
const members = this.collectCallableMembers(classDecl);
|
|
178
|
+
const accessorCount = members.filter((m) => m.kind === 'accessor').length;
|
|
179
|
+
const functionPropertyCount = members.filter((m) => m.kind === 'function-property').length;
|
|
180
|
+
// Check for too many methods (accessors and function properties count)
|
|
181
|
+
if (members.length > this.maxMethodsThreshold) {
|
|
164
182
|
const startLine = classDecl.getStartLineNumber();
|
|
165
183
|
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
184
|
+
const breakdown = accessorCount + functionPropertyCount > 0
|
|
185
|
+
? ` (incl. ${accessorCount} accessors, ${functionPropertyCount} function properties)`
|
|
186
|
+
: '';
|
|
166
187
|
violations.push({
|
|
167
188
|
ruleId: this.id,
|
|
168
|
-
message: `Class '${className}' has ${
|
|
189
|
+
message: `Class '${className}' has ${members.length} methods${breakdown} (threshold: ${this.maxMethodsThreshold}). Consider splitting into smaller, focused classes.`,
|
|
169
190
|
filePath,
|
|
170
191
|
line: startLine,
|
|
171
192
|
column: startColumn,
|
|
172
193
|
suggestedFix: `Refactor '${className}' by extracting related methods into separate classes, each with a single responsibility.`,
|
|
173
194
|
metadata: {
|
|
174
195
|
className,
|
|
175
|
-
methodCount:
|
|
196
|
+
methodCount: members.length,
|
|
197
|
+
accessorCount,
|
|
198
|
+
functionPropertyCount,
|
|
176
199
|
threshold: this.maxMethodsThreshold,
|
|
177
200
|
violationType: 'too-many-methods',
|
|
178
201
|
principle: 'SRP',
|
|
@@ -186,15 +209,21 @@ class SingleResponsibilityRule {
|
|
|
186
209
|
violations.push(methodViolation);
|
|
187
210
|
}
|
|
188
211
|
}
|
|
189
|
-
// Check for mixed responsibilities
|
|
190
|
-
const responsibilities = this.analyzeResponsibilities(
|
|
191
|
-
|
|
212
|
+
// Check for mixed responsibilities (base classes use a lower bar)
|
|
213
|
+
const responsibilities = this.analyzeResponsibilities(members.map((m) => m.name));
|
|
214
|
+
const baseSignal = this.detectBaseClassSignal(classDecl, members);
|
|
215
|
+
const minMixed = baseSignal.isBaseClass
|
|
216
|
+
? this.minMixedResponsibilitiesForBase
|
|
217
|
+
: this.minMixedResponsibilities;
|
|
218
|
+
if (responsibilities.size >= minMixed) {
|
|
192
219
|
const startLine = classDecl.getStartLineNumber();
|
|
193
220
|
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
194
221
|
const responsibilityList = Array.from(responsibilities).join(', ');
|
|
195
222
|
violations.push({
|
|
196
223
|
ruleId: this.id,
|
|
197
|
-
message:
|
|
224
|
+
message: baseSignal.isBaseClass
|
|
225
|
+
? `Base class '${className}' (${baseSignal.isAbstract ? 'abstract, ' : ''}${baseSignal.subclassCount} subclasses) appears to have mixed responsibilities: ${responsibilityList}. Every subclass inherits these concerns.`
|
|
226
|
+
: `Class '${className}' appears to have mixed responsibilities: ${responsibilityList}. Consider separating these concerns.`,
|
|
198
227
|
filePath,
|
|
199
228
|
line: startLine,
|
|
200
229
|
column: startColumn,
|
|
@@ -204,6 +233,10 @@ class SingleResponsibilityRule {
|
|
|
204
233
|
responsibilities: Array.from(responsibilities),
|
|
205
234
|
violationType: 'mixed-responsibilities',
|
|
206
235
|
principle: 'SRP',
|
|
236
|
+
isBaseClass: baseSignal.isBaseClass,
|
|
237
|
+
isAbstract: baseSignal.isAbstract,
|
|
238
|
+
subclassCount: baseSignal.subclassCount,
|
|
239
|
+
nonPrivateSurface: baseSignal.nonPrivateSurface,
|
|
207
240
|
},
|
|
208
241
|
});
|
|
209
242
|
}
|
|
@@ -268,14 +301,106 @@ class SingleResponsibilityRule {
|
|
|
268
301
|
};
|
|
269
302
|
}
|
|
270
303
|
/**
|
|
271
|
-
*
|
|
304
|
+
* Collects methods, get/set accessors (a get/set pair counts once) and
|
|
305
|
+
* properties initialized with arrow functions or function expressions.
|
|
306
|
+
*/
|
|
307
|
+
collectCallableMembers(classDecl) {
|
|
308
|
+
const members = [];
|
|
309
|
+
const isNonPrivate = (scope, name) => scope !== ts_morph_1.Scope.Private && !name.startsWith('#');
|
|
310
|
+
for (const method of classDecl.getMethods()) {
|
|
311
|
+
members.push({
|
|
312
|
+
name: method.getName(),
|
|
313
|
+
kind: 'method',
|
|
314
|
+
isNonPrivate: isNonPrivate(method.getScope(), method.getName()),
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
const accessorNames = new Set();
|
|
318
|
+
const accessors = [
|
|
319
|
+
...classDecl.getGetAccessors(),
|
|
320
|
+
...classDecl.getSetAccessors(),
|
|
321
|
+
];
|
|
322
|
+
for (const accessor of accessors) {
|
|
323
|
+
const name = accessor.getName();
|
|
324
|
+
if (accessorNames.has(name)) {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
accessorNames.add(name);
|
|
328
|
+
members.push({
|
|
329
|
+
name,
|
|
330
|
+
kind: 'accessor',
|
|
331
|
+
isNonPrivate: isNonPrivate(accessor.getScope(), name),
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
for (const property of classDecl.getProperties()) {
|
|
335
|
+
const initializer = property.getInitializer();
|
|
336
|
+
const isFunction = initializer !== undefined &&
|
|
337
|
+
(ts_morph_1.Node.isArrowFunction(initializer) ||
|
|
338
|
+
ts_morph_1.Node.isFunctionExpression(initializer));
|
|
339
|
+
if (!isFunction) {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
members.push({
|
|
343
|
+
name: property.getName(),
|
|
344
|
+
kind: 'function-property',
|
|
345
|
+
isNonPrivate: isNonPrivate(property.getScope(), property.getName()),
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
return members;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Base-class signal: abstract classes, or classes extended by at least
|
|
352
|
+
* `minSubclassesForBase` classes in the project, with a public/protected
|
|
353
|
+
* callable surface of at least `baseClassSurfaceThreshold` members.
|
|
354
|
+
* Subclasses are counted via references to the class name inside
|
|
355
|
+
* `extends` heritage clauses (only computed for large surfaces).
|
|
356
|
+
*/
|
|
357
|
+
detectBaseClassSignal(classDecl, members) {
|
|
358
|
+
const nonPrivateSurface = members.filter((m) => m.isNonPrivate).length;
|
|
359
|
+
const isAbstract = classDecl.isAbstract();
|
|
360
|
+
const notBase = {
|
|
361
|
+
isBaseClass: false,
|
|
362
|
+
isAbstract,
|
|
363
|
+
subclassCount: 0,
|
|
364
|
+
nonPrivateSurface,
|
|
365
|
+
};
|
|
366
|
+
if (nonPrivateSurface < this.baseClassSurfaceThreshold) {
|
|
367
|
+
return notBase;
|
|
368
|
+
}
|
|
369
|
+
const subclassCount = this.countSubclasses(classDecl);
|
|
370
|
+
const isBaseClass = isAbstract || subclassCount >= this.minSubclassesForBase;
|
|
371
|
+
return { isBaseClass, isAbstract, subclassCount, nonPrivateSurface };
|
|
372
|
+
}
|
|
373
|
+
countSubclasses(classDecl) {
|
|
374
|
+
const nameNode = classDecl.getNameNode();
|
|
375
|
+
if (!nameNode) {
|
|
376
|
+
return 0;
|
|
377
|
+
}
|
|
378
|
+
try {
|
|
379
|
+
const subclasses = new Set();
|
|
380
|
+
for (const ref of nameNode.findReferencesAsNodes()) {
|
|
381
|
+
const heritage = ref.getFirstAncestorByKind(ts_morph_1.SyntaxKind.HeritageClause);
|
|
382
|
+
if (heritage?.getToken() !== ts_morph_1.SyntaxKind.ExtendsKeyword) {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const owner = heritage.getParent();
|
|
386
|
+
if (ts_morph_1.Node.isClassDeclaration(owner) || ts_morph_1.Node.isClassExpression(owner)) {
|
|
387
|
+
subclasses.add(owner);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return subclasses.size;
|
|
391
|
+
}
|
|
392
|
+
catch {
|
|
393
|
+
return 0;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Analyze the responsibilities of callable members in a class.
|
|
272
398
|
* Returns a set of responsibility category names found.
|
|
273
399
|
*/
|
|
274
|
-
analyzeResponsibilities(
|
|
400
|
+
analyzeResponsibilities(memberNames) {
|
|
275
401
|
const responsibilities = new Set();
|
|
276
|
-
for (const
|
|
402
|
+
for (const methodName of memberNames) {
|
|
277
403
|
try {
|
|
278
|
-
const methodName = method.getName();
|
|
279
404
|
if (!methodName) {
|
|
280
405
|
continue;
|
|
281
406
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"srp-violation.rule.js","sourceRoot":"","sources":["../../src/rules/srp-violation.rule.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"srp-violation.rule.js","sourceRoot":"","sources":["../../src/rules/srp-violation.rule.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;;;AAEH,uCAMkB;AAmClB;;;;;GAKG;AACH,MAAa,wBAAwB;IACnB,EAAE,GAAG,2CAA2C,CAAC;IACjD,IAAI,GAAG,2CAA2C,CAAC;IAC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QAAC,MAAM,CAAC;YACP,OAAO,CAAC,CAAC;QACX,CAAC;IACH,CAAC;IAED;;;OAGG;IACK,uBAAuB,CAAC,WAAqB;QACnD,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAU,CAAC;QAE3C,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE,CAAC;YACrC,IAAI,CAAC;gBACH,IAAI,CAAC,UAAU,EAAE,CAAC;oBAChB,SAAS;gBACX,CAAC;gBAED,qDAAqD;gBACrD,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;oBAChC,SAAS;gBACX,CAAC;gBAED,qDAAqD;gBACrD,MAAM,SAAS,GAAG,UAAU,CAAC,WAAW,EAAE,CAAC;gBAE3C,qCAAqC;gBACrC,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,wBAAwB,EAAE,CAAC;oBACrD,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;wBACxC,IAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;4BAChC,gBAAgB,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;4BACpC,MAAM,CAAC,yDAAyD;wBAClE,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;YAAC,OAAO,MAAM,EAAE,CAAC;gBAChB,sCAAsC;gBACtC,SAAS;YACX,CAAC;QACH,CAAC;QAED,OAAO,gBAAgB,CAAC;IAC1B,CAAC;CACF;AAlcD,4DAkcC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angular-modernizer/plugin-solid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Analysis plugin for detecting SOLID principle violations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"!src/**/*.spec.ts"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@angular-modernizer/core": "0.
|
|
33
|
-
"@angular-modernizer/
|
|
34
|
-
"@angular-modernizer/
|
|
32
|
+
"@angular-modernizer/core": "0.2.0",
|
|
33
|
+
"@angular-modernizer/plugin-system": "0.2.0",
|
|
34
|
+
"@angular-modernizer/api": "0.2.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/jest": "^30.0.0",
|
|
@@ -4,8 +4,14 @@
|
|
|
4
4
|
* Detects Single Responsibility Principle violations in classes.
|
|
5
5
|
*
|
|
6
6
|
* Detection Patterns:
|
|
7
|
-
* - Classes with too many methods (configurable threshold, default: 15)
|
|
7
|
+
* - Classes with too many methods (configurable threshold, default: 15).
|
|
8
|
+
* Get/set accessors and arrow-function / function-expression properties
|
|
9
|
+
* count as methods.
|
|
8
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`.
|
|
9
15
|
*
|
|
10
16
|
* Responsibility Categories:
|
|
11
17
|
* - UI: render, display, show, hide, toggle, open, close
|
|
@@ -28,6 +34,8 @@
|
|
|
28
34
|
*/
|
|
29
35
|
|
|
30
36
|
import {
|
|
37
|
+
Node,
|
|
38
|
+
Scope,
|
|
31
39
|
SyntaxKind,
|
|
32
40
|
type ClassDeclaration,
|
|
33
41
|
type MethodDeclaration,
|
|
@@ -46,6 +54,26 @@ interface ResponsibilityCategory {
|
|
|
46
54
|
keywords: string[];
|
|
47
55
|
}
|
|
48
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
|
+
|
|
49
77
|
/**
|
|
50
78
|
* SRP Violation Rule - detects Single Responsibility Principle violations.
|
|
51
79
|
*
|
|
@@ -77,6 +105,22 @@ export class SingleResponsibilityRule implements AnalysisRule {
|
|
|
77
105
|
*/
|
|
78
106
|
private readonly minMixedResponsibilities = 3;
|
|
79
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
|
+
|
|
80
124
|
/**
|
|
81
125
|
* Responsibility categories for method classification.
|
|
82
126
|
*/
|
|
@@ -189,27 +233,33 @@ export class SingleResponsibilityRule implements AnalysisRule {
|
|
|
189
233
|
try {
|
|
190
234
|
const className = classDecl.getName() ?? '<anonymous>';
|
|
191
235
|
const methods = classDecl.getMethods();
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
// Check for too many methods
|
|
199
|
-
if (
|
|
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) {
|
|
200
244
|
const startLine = classDecl.getStartLineNumber();
|
|
201
245
|
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
246
|
+
const breakdown =
|
|
247
|
+
accessorCount + functionPropertyCount > 0
|
|
248
|
+
? ` (incl. ${accessorCount} accessors, ${functionPropertyCount} function properties)`
|
|
249
|
+
: '';
|
|
202
250
|
|
|
203
251
|
violations.push({
|
|
204
252
|
ruleId: this.id,
|
|
205
|
-
message: `Class '${className}' has ${
|
|
253
|
+
message: `Class '${className}' has ${members.length} methods${breakdown} (threshold: ${this.maxMethodsThreshold}). Consider splitting into smaller, focused classes.`,
|
|
206
254
|
filePath,
|
|
207
255
|
line: startLine,
|
|
208
256
|
column: startColumn,
|
|
209
257
|
suggestedFix: `Refactor '${className}' by extracting related methods into separate classes, each with a single responsibility.`,
|
|
210
258
|
metadata: {
|
|
211
259
|
className,
|
|
212
|
-
methodCount:
|
|
260
|
+
methodCount: members.length,
|
|
261
|
+
accessorCount,
|
|
262
|
+
functionPropertyCount,
|
|
213
263
|
threshold: this.maxMethodsThreshold,
|
|
214
264
|
violationType: 'too-many-methods',
|
|
215
265
|
principle: 'SRP',
|
|
@@ -229,16 +279,24 @@ export class SingleResponsibilityRule implements AnalysisRule {
|
|
|
229
279
|
}
|
|
230
280
|
}
|
|
231
281
|
|
|
232
|
-
// Check for mixed responsibilities
|
|
233
|
-
const responsibilities = this.analyzeResponsibilities(
|
|
234
|
-
|
|
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) {
|
|
235
291
|
const startLine = classDecl.getStartLineNumber();
|
|
236
292
|
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
237
293
|
const responsibilityList = Array.from(responsibilities).join(', ');
|
|
238
294
|
|
|
239
295
|
violations.push({
|
|
240
296
|
ruleId: this.id,
|
|
241
|
-
message:
|
|
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.`,
|
|
242
300
|
filePath,
|
|
243
301
|
line: startLine,
|
|
244
302
|
column: startColumn,
|
|
@@ -248,6 +306,10 @@ export class SingleResponsibilityRule implements AnalysisRule {
|
|
|
248
306
|
responsibilities: Array.from(responsibilities),
|
|
249
307
|
violationType: 'mixed-responsibilities',
|
|
250
308
|
principle: 'SRP',
|
|
309
|
+
isBaseClass: baseSignal.isBaseClass,
|
|
310
|
+
isAbstract: baseSignal.isAbstract,
|
|
311
|
+
subclassCount: baseSignal.subclassCount,
|
|
312
|
+
nonPrivateSurface: baseSignal.nonPrivateSurface,
|
|
251
313
|
},
|
|
252
314
|
});
|
|
253
315
|
}
|
|
@@ -325,15 +387,120 @@ export class SingleResponsibilityRule implements AnalysisRule {
|
|
|
325
387
|
}
|
|
326
388
|
|
|
327
389
|
/**
|
|
328
|
-
*
|
|
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.
|
|
329
497
|
* Returns a set of responsibility category names found.
|
|
330
498
|
*/
|
|
331
|
-
private analyzeResponsibilities(
|
|
499
|
+
private analyzeResponsibilities(memberNames: string[]): Set<string> {
|
|
332
500
|
const responsibilities = new Set<string>();
|
|
333
501
|
|
|
334
|
-
for (const
|
|
502
|
+
for (const methodName of memberNames) {
|
|
335
503
|
try {
|
|
336
|
-
const methodName = method.getName();
|
|
337
504
|
if (!methodName) {
|
|
338
505
|
continue;
|
|
339
506
|
}
|