@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,209 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-solid - God Switch Detection Rule
|
|
3
|
-
*
|
|
4
|
-
* Detects switch statements with too many cases that violate the Open/Closed Principle.
|
|
5
|
-
* These "God Switches" are hard to maintain and extend, requiring modification for every new case.
|
|
6
|
-
*
|
|
7
|
-
* Detection Pattern:
|
|
8
|
-
* - Switch statements exceeding configurable threshold (default: 10 case clauses, excluding default)
|
|
9
|
-
* - Classification of switch type based on expression patterns
|
|
10
|
-
* - Confidence scoring for violations
|
|
11
|
-
*
|
|
12
|
-
* Configuration:
|
|
13
|
-
* - context.config.solid.godSwitch.maxCases - Maximum allowed case clauses (default: 10)
|
|
14
|
-
*
|
|
15
|
-
* Recommended Refactoring:
|
|
16
|
-
* - Factory Pattern with strategy objects for each case type
|
|
17
|
-
* - Polymorphism with inheritance hierarchy
|
|
18
|
-
* - Strategy Pattern with interface-based dispatch
|
|
19
|
-
* - Command Pattern for action-based switches
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* // Violation: switch with too many cases exceeds threshold
|
|
23
|
-
* function handleAction(action: string) {
|
|
24
|
-
* switch (action) {
|
|
25
|
-
* case 'create': return onCreate();
|
|
26
|
-
* case 'read': return onRead();
|
|
27
|
-
* // 9+ additional cases — "God Switch" pattern
|
|
28
|
-
* }
|
|
29
|
-
* }
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
import { SyntaxKind, type SwitchStatement } from 'ts-morph';
|
|
33
|
-
import type {
|
|
34
|
-
AnalysisRule,
|
|
35
|
-
AnalysisContext,
|
|
36
|
-
AnalysisResult,
|
|
37
|
-
} from '@angular-modernizer/plugin-system';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* God Switch Analysis Rule - detects switch statements with too many cases.
|
|
41
|
-
*
|
|
42
|
-
* This rule identifies large switch statements that violate OCP
|
|
43
|
-
* and provides classification and metadata to guide refactoring.
|
|
44
|
-
* The threshold is configurable via context.config.solid.godSwitch.maxCases (default: 10).
|
|
45
|
-
*/
|
|
46
|
-
export class GodSwitchAnalysisRule implements AnalysisRule {
|
|
47
|
-
public readonly id = 'solid:god-switch';
|
|
48
|
-
public readonly name = 'God Switch Detection';
|
|
49
|
-
public readonly description =
|
|
50
|
-
'Detects switch statements with too many cases that violate OCP (configurable threshold, default: 10)';
|
|
51
|
-
public readonly severity = 'warning';
|
|
52
|
-
public readonly category = 'solid-violation';
|
|
53
|
-
public readonly tags = [
|
|
54
|
-
'solid',
|
|
55
|
-
'ocp',
|
|
56
|
-
'god-switch',
|
|
57
|
-
'switch',
|
|
58
|
-
'refactoring',
|
|
59
|
-
];
|
|
60
|
-
|
|
61
|
-
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
62
|
-
const violations: AnalysisResult[] = [];
|
|
63
|
-
const sourceFile = context.sourceFile;
|
|
64
|
-
|
|
65
|
-
// Read maxSwitchCases from configuration, defaulting to 10
|
|
66
|
-
const solidConfig = context.config['solid'] as
|
|
67
|
-
| { godSwitch?: { maxCases?: number } }
|
|
68
|
-
| undefined;
|
|
69
|
-
const maxSwitchCases = solidConfig?.godSwitch?.maxCases ?? 10;
|
|
70
|
-
|
|
71
|
-
// Find all switch statements in the file
|
|
72
|
-
const switchStatements = sourceFile.getDescendantsOfKind(
|
|
73
|
-
SyntaxKind.SwitchStatement,
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
for (const switchStmt of switchStatements) {
|
|
77
|
-
const violation = this.analyzeSwitchStatement(
|
|
78
|
-
switchStmt,
|
|
79
|
-
context.filePath,
|
|
80
|
-
maxSwitchCases,
|
|
81
|
-
);
|
|
82
|
-
if (violation !== null) {
|
|
83
|
-
violations.push(violation);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return violations;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Analyze a switch statement for God Switch pattern.
|
|
92
|
-
*
|
|
93
|
-
* @param switchStmt - The switch statement to analyze
|
|
94
|
-
* @param filePath - Path to the file containing the switch statement
|
|
95
|
-
* @param maxSwitchCases - Maximum allowed case clauses (configurable threshold)
|
|
96
|
-
*/
|
|
97
|
-
private analyzeSwitchStatement(
|
|
98
|
-
switchStmt: SwitchStatement,
|
|
99
|
-
filePath: string,
|
|
100
|
-
maxSwitchCases: number,
|
|
101
|
-
): AnalysisResult | null {
|
|
102
|
-
const caseBlock = switchStmt.getCaseBlock();
|
|
103
|
-
const clauses = caseBlock.getClauses();
|
|
104
|
-
|
|
105
|
-
// Filter to get only case clauses (exclude default)
|
|
106
|
-
const caseClauses = clauses.filter(
|
|
107
|
-
(c) => c.getKind() === SyntaxKind.CaseClause,
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
const caseCount = caseClauses.length;
|
|
111
|
-
|
|
112
|
-
// Check if case count exceeds threshold
|
|
113
|
-
if (caseCount <= maxSwitchCases) {
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Get switch expression for classification
|
|
118
|
-
const expression = switchStmt.getExpression();
|
|
119
|
-
const expressionText = expression.getText();
|
|
120
|
-
const switchType = this.analyzeSwitchType(expressionText);
|
|
121
|
-
|
|
122
|
-
// Check for default clause
|
|
123
|
-
const hasDefault = clauses.some(
|
|
124
|
-
(c) => c.getKind() === SyntaxKind.DefaultClause,
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
// Extract case labels for metadata
|
|
128
|
-
const caseLabels = caseClauses.map((c) => {
|
|
129
|
-
const caseClause = c.asKind(SyntaxKind.CaseClause);
|
|
130
|
-
if (caseClause) {
|
|
131
|
-
const expr = caseClause.getExpression();
|
|
132
|
-
// Strip quotes from string literals for cleaner metadata
|
|
133
|
-
const text = expr.getText();
|
|
134
|
-
return text.replaceAll(/^['"]|['"]$/g, '');
|
|
135
|
-
}
|
|
136
|
-
return 'unknown';
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// Build violation result
|
|
140
|
-
return {
|
|
141
|
-
ruleId: this.id,
|
|
142
|
-
message: `God Switch detected: ${caseCount} cases (${switchType}). Consider Factory Pattern.`,
|
|
143
|
-
filePath,
|
|
144
|
-
line: switchStmt.getStartLineNumber(),
|
|
145
|
-
column: switchStmt.getStart() - switchStmt.getStartLinePos(),
|
|
146
|
-
suggestedFix:
|
|
147
|
-
'Refactor to Factory Pattern with strategy objects for each case type',
|
|
148
|
-
metadata: {
|
|
149
|
-
caseCount,
|
|
150
|
-
switchType,
|
|
151
|
-
switchExpression: expressionText,
|
|
152
|
-
hasDefault,
|
|
153
|
-
confidence: 0.9,
|
|
154
|
-
caseLabels,
|
|
155
|
-
violationType: 'god-switch',
|
|
156
|
-
principle: 'Open/Closed Principle',
|
|
157
|
-
},
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Classify the switch type based on the expression pattern.
|
|
163
|
-
*
|
|
164
|
-
* Classification helps guide appropriate refactoring strategies:
|
|
165
|
-
* - type-based: Use inheritance/polymorphism
|
|
166
|
-
* - status-based: Use State Pattern
|
|
167
|
-
* - action-based: Use Command Pattern
|
|
168
|
-
* - event-based: Use Observer Pattern
|
|
169
|
-
* - command-based: Use Command Pattern
|
|
170
|
-
* - state-based: Use State Pattern
|
|
171
|
-
* - kind-based: Use Factory Pattern
|
|
172
|
-
* - category-based: Use Strategy Pattern
|
|
173
|
-
* - discriminated-union: Use type-safe discriminated unions
|
|
174
|
-
*
|
|
175
|
-
* Note: Order matters! Check more specific patterns (event, command) before generic ones (type).
|
|
176
|
-
*/
|
|
177
|
-
private analyzeSwitchType(expression: string): string {
|
|
178
|
-
// Pattern matching on switch expression text
|
|
179
|
-
// Check specific patterns first to avoid false matches
|
|
180
|
-
if (/event/i.test(expression)) {
|
|
181
|
-
return 'event-based';
|
|
182
|
-
}
|
|
183
|
-
if (/command/i.test(expression)) {
|
|
184
|
-
return 'command-based';
|
|
185
|
-
}
|
|
186
|
-
if (/status/i.test(expression)) {
|
|
187
|
-
return 'status-based';
|
|
188
|
-
}
|
|
189
|
-
if (/action/i.test(expression)) {
|
|
190
|
-
return 'action-based';
|
|
191
|
-
}
|
|
192
|
-
if (/state/i.test(expression)) {
|
|
193
|
-
return 'state-based';
|
|
194
|
-
}
|
|
195
|
-
if (/kind/i.test(expression)) {
|
|
196
|
-
return 'kind-based';
|
|
197
|
-
}
|
|
198
|
-
if (/category/i.test(expression)) {
|
|
199
|
-
return 'category-based';
|
|
200
|
-
}
|
|
201
|
-
// Check generic pattern last
|
|
202
|
-
if (/type/i.test(expression)) {
|
|
203
|
-
return 'type-based';
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Default classification
|
|
207
|
-
return 'discriminated-union';
|
|
208
|
-
}
|
|
209
|
-
}
|
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-solid - ISP Violation Rule
|
|
3
|
-
*
|
|
4
|
-
* Detects violations of the Interface Segregation Principle.
|
|
5
|
-
* Identifies interfaces with too many methods or mixed responsibilities.
|
|
6
|
-
*
|
|
7
|
-
* Detection Patterns:
|
|
8
|
-
* - Interfaces with more than X methods (configurable threshold)
|
|
9
|
-
* - Interfaces that mix different concerns (UI, Data, Business logic)
|
|
10
|
-
* - Classes implementing interfaces they don't fully use
|
|
11
|
-
*
|
|
12
|
-
* This rule helps ensure interfaces are focused and clients don't depend on methods they don't use.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* // Violation: interface forces implementations to define unrelated methods
|
|
16
|
-
* interface IWorker {
|
|
17
|
-
* work(): void;
|
|
18
|
-
* eat(): void; // Irrelevant for robots
|
|
19
|
-
* sleep(): void; // Irrelevant for robots
|
|
20
|
-
* }
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import {
|
|
24
|
-
SyntaxKind,
|
|
25
|
-
type InterfaceDeclaration,
|
|
26
|
-
type ClassDeclaration,
|
|
27
|
-
type MethodDeclaration,
|
|
28
|
-
type PropertyDeclaration,
|
|
29
|
-
type MethodSignature,
|
|
30
|
-
type PropertySignature,
|
|
31
|
-
} from 'ts-morph';
|
|
32
|
-
import type {
|
|
33
|
-
AnalysisRule,
|
|
34
|
-
AnalysisContext,
|
|
35
|
-
AnalysisResult,
|
|
36
|
-
} from '@angular-modernizer/plugin-system';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* ISP Violation Rule - detects Interface Segregation Principle violations.
|
|
40
|
-
*
|
|
41
|
-
* This rule implements the Interface Segregation Principle by detecting
|
|
42
|
-
* interfaces that are too large or have mixed responsibilities.
|
|
43
|
-
*/
|
|
44
|
-
export class ISPViolationRule implements AnalysisRule {
|
|
45
|
-
public readonly id = 'solid:isp-violation';
|
|
46
|
-
public readonly name = 'Interface Segregation Principle Violation';
|
|
47
|
-
public readonly description =
|
|
48
|
-
'Detects interfaces that are too large or have mixed responsibilities';
|
|
49
|
-
public readonly severity = 'warning';
|
|
50
|
-
public readonly category = 'solid-violation';
|
|
51
|
-
public readonly tags = [
|
|
52
|
-
'solid',
|
|
53
|
-
'isp',
|
|
54
|
-
'interface',
|
|
55
|
-
'segregation',
|
|
56
|
-
'architecture',
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Maximum number of methods allowed in an interface.
|
|
61
|
-
*/
|
|
62
|
-
private readonly maxMethodsInInterface = 5;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Maximum number of interfaces a class can implement.
|
|
66
|
-
*/
|
|
67
|
-
private readonly maxInterfacesPerClass = 3;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Keywords that indicate different concerns.
|
|
71
|
-
*/
|
|
72
|
-
private readonly concernKeywords = {
|
|
73
|
-
ui: [
|
|
74
|
-
'render',
|
|
75
|
-
'display',
|
|
76
|
-
'show',
|
|
77
|
-
'hide',
|
|
78
|
-
'click',
|
|
79
|
-
'event',
|
|
80
|
-
'component',
|
|
81
|
-
'view',
|
|
82
|
-
],
|
|
83
|
-
data: [
|
|
84
|
-
'save',
|
|
85
|
-
'load',
|
|
86
|
-
'fetch',
|
|
87
|
-
'query',
|
|
88
|
-
'store',
|
|
89
|
-
'persist',
|
|
90
|
-
'cache',
|
|
91
|
-
'repository',
|
|
92
|
-
],
|
|
93
|
-
business: [
|
|
94
|
-
'calculate',
|
|
95
|
-
'validate',
|
|
96
|
-
'process',
|
|
97
|
-
'transform',
|
|
98
|
-
'convert',
|
|
99
|
-
'rule',
|
|
100
|
-
'logic',
|
|
101
|
-
],
|
|
102
|
-
lifecycle: ['init', 'destroy', 'create', 'setup', 'cleanup', 'initialize'],
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
106
|
-
const violations: AnalysisResult[] = [];
|
|
107
|
-
const sourceFile = context.sourceFile;
|
|
108
|
-
|
|
109
|
-
// Find all interfaces in the file
|
|
110
|
-
const interfaces = sourceFile.getDescendantsOfKind(
|
|
111
|
-
SyntaxKind.InterfaceDeclaration,
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
for (const interfaceDecl of interfaces) {
|
|
115
|
-
const interfaceViolations = this.analyzeInterface(
|
|
116
|
-
interfaceDecl,
|
|
117
|
-
sourceFile.getFilePath(),
|
|
118
|
-
);
|
|
119
|
-
violations.push(...interfaceViolations);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Find classes that implement interfaces they don't fully use
|
|
123
|
-
const classes = sourceFile.getDescendantsOfKind(
|
|
124
|
-
SyntaxKind.ClassDeclaration,
|
|
125
|
-
);
|
|
126
|
-
for (const classDecl of classes) {
|
|
127
|
-
const classViolations = this.analyzeClassInterfaceUsage(
|
|
128
|
-
classDecl,
|
|
129
|
-
sourceFile.getFilePath(),
|
|
130
|
-
);
|
|
131
|
-
violations.push(...classViolations);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return Promise.resolve(violations);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Analyze an interface for ISP violations.
|
|
139
|
-
*/
|
|
140
|
-
private analyzeInterface(
|
|
141
|
-
interfaceDecl: InterfaceDeclaration,
|
|
142
|
-
filePath: string,
|
|
143
|
-
): AnalysisResult[] {
|
|
144
|
-
const violations: AnalysisResult[] = [];
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
const interfaceName = interfaceDecl.getName();
|
|
148
|
-
const methods = interfaceDecl.getMethods();
|
|
149
|
-
const properties = interfaceDecl.getProperties();
|
|
150
|
-
|
|
151
|
-
// Check if interface has too many methods
|
|
152
|
-
if (methods.length > this.maxMethodsInInterface) {
|
|
153
|
-
const startLine = interfaceDecl.getStartLineNumber();
|
|
154
|
-
const startColumn =
|
|
155
|
-
interfaceDecl.getStart() - interfaceDecl.getStartLinePos();
|
|
156
|
-
|
|
157
|
-
violations.push({
|
|
158
|
-
ruleId: this.id,
|
|
159
|
-
message: `Interface '${interfaceName}' has ${methods.length} methods, exceeding the recommended maximum of ${this.maxMethodsInInterface}. Consider splitting into smaller, focused interfaces.`,
|
|
160
|
-
filePath,
|
|
161
|
-
line: startLine,
|
|
162
|
-
column: startColumn,
|
|
163
|
-
suggestedFix: `Split '${interfaceName}' into smaller interfaces based on single responsibilities`,
|
|
164
|
-
metadata: {
|
|
165
|
-
interfaceName,
|
|
166
|
-
methodCount: methods.length,
|
|
167
|
-
maxAllowed: this.maxMethodsInInterface,
|
|
168
|
-
violationType: 'too-many-methods',
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Check if interface mixes different concerns
|
|
174
|
-
const concernAnalysis = this.analyzeInterfaceConcerns(
|
|
175
|
-
methods,
|
|
176
|
-
properties,
|
|
177
|
-
);
|
|
178
|
-
if (concernAnalysis.mixedConcerns) {
|
|
179
|
-
const startLine = interfaceDecl.getStartLineNumber();
|
|
180
|
-
const startColumn =
|
|
181
|
-
interfaceDecl.getStart() - interfaceDecl.getStartLinePos();
|
|
182
|
-
|
|
183
|
-
violations.push({
|
|
184
|
-
ruleId: this.id,
|
|
185
|
-
message: `Interface '${interfaceName}' mixes multiple concerns: ${concernAnalysis.concerns.join(', ')}. Consider segregating into focused interfaces.`,
|
|
186
|
-
filePath,
|
|
187
|
-
line: startLine,
|
|
188
|
-
column: startColumn,
|
|
189
|
-
suggestedFix: `Create separate interfaces for each concern: ${concernAnalysis.concerns.map((c) => `${interfaceName}${c.charAt(0).toUpperCase() + c.slice(1)}`).join(', ')}`,
|
|
190
|
-
metadata: {
|
|
191
|
-
interfaceName,
|
|
192
|
-
concerns: concernAnalysis.concerns,
|
|
193
|
-
violationType: 'mixed-concerns',
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
} catch (_error) {
|
|
198
|
-
// Continue with other interfaces
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return violations;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Analyze if a class implements interfaces it doesn't fully use.
|
|
206
|
-
*/
|
|
207
|
-
private analyzeClassInterfaceUsage(
|
|
208
|
-
classDecl: ClassDeclaration,
|
|
209
|
-
filePath: string,
|
|
210
|
-
): AnalysisResult[] {
|
|
211
|
-
const violations: AnalysisResult[] = [];
|
|
212
|
-
|
|
213
|
-
try {
|
|
214
|
-
const className = classDecl.getName();
|
|
215
|
-
const implementedInterfaces = classDecl.getImplements();
|
|
216
|
-
|
|
217
|
-
// Check if class implements too many interfaces
|
|
218
|
-
if (implementedInterfaces.length > this.maxInterfacesPerClass) {
|
|
219
|
-
const startLine = classDecl.getStartLineNumber();
|
|
220
|
-
const startColumn = classDecl.getStart() - classDecl.getStartLinePos();
|
|
221
|
-
|
|
222
|
-
violations.push({
|
|
223
|
-
ruleId: this.id,
|
|
224
|
-
message: `Class '${className}' implements ${implementedInterfaces.length} interfaces, exceeding the recommended maximum of ${this.maxInterfacesPerClass}. Consider segregating interfaces or using composition.`,
|
|
225
|
-
filePath,
|
|
226
|
-
line: startLine,
|
|
227
|
-
column: startColumn,
|
|
228
|
-
suggestedFix: `Split '${className}' into smaller classes or use composition instead of implementing multiple interfaces`,
|
|
229
|
-
metadata: {
|
|
230
|
-
className,
|
|
231
|
-
interfaceCount: implementedInterfaces.length,
|
|
232
|
-
maxAllowed: this.maxInterfacesPerClass,
|
|
233
|
-
violationType: 'too-many-interfaces',
|
|
234
|
-
},
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
for (const impl of implementedInterfaces) {
|
|
239
|
-
const interfaceType = impl.getType();
|
|
240
|
-
const interfaceSymbol = interfaceType.getSymbol();
|
|
241
|
-
|
|
242
|
-
if (interfaceSymbol) {
|
|
243
|
-
// This is a simplified check - in a real implementation,
|
|
244
|
-
// we'd analyze which methods are actually used vs declared
|
|
245
|
-
const interfaceName = interfaceSymbol.getName();
|
|
246
|
-
|
|
247
|
-
// For now, just check if the class has any methods at all
|
|
248
|
-
// A more sophisticated analysis would check method usage
|
|
249
|
-
const classMethods = classDecl.getMethods();
|
|
250
|
-
|
|
251
|
-
if (classMethods.length === 0) {
|
|
252
|
-
const startLine = classDecl.getStartLineNumber();
|
|
253
|
-
const startColumn =
|
|
254
|
-
classDecl.getStart() - classDecl.getStartLinePos();
|
|
255
|
-
|
|
256
|
-
violations.push({
|
|
257
|
-
ruleId: this.id,
|
|
258
|
-
message: `Class '${className}' implements interface '${interfaceName}' but may not use all its methods. Consider implementing only the methods you need.`,
|
|
259
|
-
filePath,
|
|
260
|
-
line: startLine,
|
|
261
|
-
column: startColumn,
|
|
262
|
-
suggestedFix: `Implement only the interface methods that '${className}' actually needs`,
|
|
263
|
-
metadata: {
|
|
264
|
-
className,
|
|
265
|
-
interfaceName,
|
|
266
|
-
violationType: 'unused-interface-methods',
|
|
267
|
-
},
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
} catch (_error) {
|
|
273
|
-
// Continue with other classes
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
return violations;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Analyze if an interface mixes different concerns.
|
|
281
|
-
*/
|
|
282
|
-
private analyzeInterfaceConcerns(
|
|
283
|
-
methods: (MethodDeclaration | MethodSignature)[],
|
|
284
|
-
properties: (PropertyDeclaration | PropertySignature)[],
|
|
285
|
-
): { mixedConcerns: boolean; concerns: string[] } {
|
|
286
|
-
const concerns = new Set<string>();
|
|
287
|
-
|
|
288
|
-
// Analyze method names for concerns
|
|
289
|
-
for (const method of methods) {
|
|
290
|
-
const methodName = method.getName().toLowerCase();
|
|
291
|
-
|
|
292
|
-
for (const [concern, keywords] of Object.entries(this.concernKeywords)) {
|
|
293
|
-
if (keywords.some((keyword) => methodName.includes(keyword))) {
|
|
294
|
-
concerns.add(concern);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// Analyze property names for concerns
|
|
300
|
-
for (const property of properties) {
|
|
301
|
-
const propertyName = property.getName().toLowerCase();
|
|
302
|
-
|
|
303
|
-
for (const [concern, keywords] of Object.entries(this.concernKeywords)) {
|
|
304
|
-
if (keywords.some((keyword) => propertyName.includes(keyword))) {
|
|
305
|
-
concerns.add(concern);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return {
|
|
311
|
-
mixedConcerns: concerns.size > 1,
|
|
312
|
-
concerns: Array.from(concerns),
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
}
|