@angular-modernizer/plugin-solid 1.2.0 → 2.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/CHANGELOG.md +270 -0
- package/README.md +103 -99
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/rules/dip-violation.rule.d.ts +30 -31
- package/dist/rules/dip-violation.rule.js +246 -112
- package/dist/rules/god-switch-analysis.rule.d.ts +44 -33
- package/dist/rules/god-switch-analysis.rule.js +146 -109
- package/dist/rules/isp-violation.rule.d.ts +35 -33
- package/dist/rules/isp-violation.rule.js +83 -202
- package/dist/rules/lsp-violation.rule.d.ts +15 -39
- package/dist/rules/lsp-violation.rule.js +17 -300
- package/dist/rules/ocp-violation.rule.d.ts +52 -36
- package/dist/rules/ocp-violation.rule.js +227 -145
- package/dist/rules/service-dip-violation.analysis.rule.d.ts +32 -56
- package/dist/rules/service-dip-violation.analysis.rule.js +21 -192
- package/dist/rules/srp-violation.rule.d.ts +36 -71
- package/dist/rules/srp-violation.rule.js +78 -384
- package/dist/solid-plugin.d.ts +13 -8
- package/dist/solid-plugin.js +17 -11
- package/package.json +7 -6
|
@@ -2,33 +2,58 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DIPViolationRule = void 0;
|
|
4
4
|
const ts_morph_1 = require("ts-morph");
|
|
5
|
+
const api_1 = require("@angular-modernizer/api");
|
|
6
|
+
/** Module that exports `Injectable`, `Injector` and `inject`. */
|
|
7
|
+
const ANGULAR_CORE = '@angular/core';
|
|
8
|
+
/** First Angular major with `inject()` in field initializers and constructors. */
|
|
9
|
+
const INJECT_FUNCTION_MAJOR = 14;
|
|
10
|
+
/** `@angular/core` functions whose function argument runs as an initializer, not as a class member. */
|
|
11
|
+
const INITIALIZER_FUNCTIONS = new Set([
|
|
12
|
+
'provideAppInitializer',
|
|
13
|
+
'provideEnvironmentInitializer',
|
|
14
|
+
'providePlatformInitializer',
|
|
15
|
+
]);
|
|
5
16
|
/**
|
|
6
|
-
* DIP Violation Rule (`solid:dip-violation-direct-instantiation`) -
|
|
7
|
-
*
|
|
17
|
+
* DIP Violation Rule (`solid:dip-violation-direct-instantiation`) - reports
|
|
18
|
+
* dependencies a class creates or looks up itself instead of declaring them
|
|
19
|
+
* for Angular DI. Severity warning per finding.
|
|
8
20
|
*
|
|
9
|
-
*
|
|
10
|
-
* - `new X()`
|
|
11
|
-
* (
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* -
|
|
15
|
-
*
|
|
21
|
+
* Violation types (`violationType` in the metadata):
|
|
22
|
+
* - `direct-instantiation`: `new X()` where `X` resolves by symbol to a class
|
|
23
|
+
* declared in the project (not in a `.d.ts` file) that has an `Injectable`
|
|
24
|
+
* decorator from `@angular/core`. Library classes (`new HttpClient(handler)`)
|
|
25
|
+
* and undecorated helpers or models are not reported, whatever their name.
|
|
26
|
+
* - `service-locator-pattern`: `.get(...)` on a receiver whose type is
|
|
27
|
+
* Angular's `Injector` (type checker), any receiver expression
|
|
28
|
+
* (`this.injector`, a parameter, `inject(Injector)`).
|
|
16
29
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
30
|
+
* Both are reported only inside class members (methods, constructors,
|
|
31
|
+
* accessors, property initializers), not inside a `useFactory` function, a
|
|
32
|
+
* function passed to `provideAppInitializer`, `provideEnvironmentInitializer`
|
|
33
|
+
* or `providePlatformInitializer`, a decorator or any function outside a class:
|
|
34
|
+
* these build providers and are DI configuration.
|
|
35
|
+
*
|
|
36
|
+
* `suggestedFix` proposes `inject(X)` when the project uses Angular 14 or later
|
|
37
|
+
* (api `angularMajorVersion`) and the call site is an injection context (an
|
|
38
|
+
* instance field initializer or the constructor body, not a nested function);
|
|
39
|
+
* otherwise a constructor parameter. Metadata: `violationType`, `principle`,
|
|
40
|
+
* `instantiatedType` or `receiver` and `token`, `injectionContext`,
|
|
41
|
+
* `angularMajorVersion`.
|
|
19
42
|
*
|
|
20
43
|
* @example
|
|
21
44
|
* ```typescript
|
|
22
|
-
*
|
|
23
|
-
* class
|
|
24
|
-
*
|
|
45
|
+
* @Injectable({ providedIn: 'root' })
|
|
46
|
+
* export class UserService {}
|
|
47
|
+
*
|
|
48
|
+
* export class OrderComponent {
|
|
49
|
+
* private users = new UserService(); // reported: bypasses Angular DI
|
|
25
50
|
* }
|
|
26
51
|
* ```
|
|
27
52
|
*/
|
|
28
53
|
class DIPViolationRule {
|
|
29
54
|
id = 'solid:dip-violation-direct-instantiation';
|
|
30
55
|
name = 'Dependency Inversion Principle Violation';
|
|
31
|
-
description = 'Detects
|
|
56
|
+
description = 'Detects @Injectable classes created with new and Injector.get() lookups inside class members';
|
|
32
57
|
severity = 'warning';
|
|
33
58
|
category = 'solid-violation';
|
|
34
59
|
tags = [
|
|
@@ -37,117 +62,226 @@ class DIPViolationRule {
|
|
|
37
62
|
'dependency-injection',
|
|
38
63
|
'architecture',
|
|
39
64
|
];
|
|
40
|
-
|
|
41
|
-
* Service class suffixes to detect.
|
|
42
|
-
* Aligned with ServicePatternRecognizer patterns for consistency.
|
|
43
|
-
*/
|
|
44
|
-
defaultServiceSuffixes = [
|
|
45
|
-
'Service',
|
|
46
|
-
'Repository',
|
|
47
|
-
'Store',
|
|
48
|
-
'Manager',
|
|
49
|
-
'Api',
|
|
50
|
-
'Client',
|
|
51
|
-
'Provider',
|
|
52
|
-
'Facade',
|
|
53
|
-
'Helper',
|
|
54
|
-
'Util',
|
|
55
|
-
'Utils',
|
|
56
|
-
];
|
|
57
|
-
async analyze(context) {
|
|
58
|
-
const violations = [];
|
|
65
|
+
analyze(context) {
|
|
59
66
|
const sourceFile = context.sourceFile;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
const violations = [];
|
|
68
|
+
let major = null;
|
|
69
|
+
const version = () => {
|
|
70
|
+
if (major === null) {
|
|
71
|
+
major = (0, api_1.angularMajorVersion)(sourceFile.getProject(), sourceFile.getFilePath());
|
|
72
|
+
}
|
|
73
|
+
return major;
|
|
74
|
+
};
|
|
75
|
+
for (const newExpr of sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.NewExpression)) {
|
|
76
|
+
const violation = this.checkNewExpression(newExpr, sourceFile, version);
|
|
77
|
+
if (violation) {
|
|
78
|
+
violations.push(violation);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const call of sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression)) {
|
|
82
|
+
const violation = this.checkInjectorGet(call, sourceFile, version);
|
|
64
83
|
if (violation) {
|
|
65
84
|
violations.push(violation);
|
|
66
85
|
}
|
|
67
86
|
}
|
|
68
|
-
// Check for service locator pattern (Cat 6)
|
|
69
|
-
violations.push(...this.analyzeServiceLocator(sourceFile, sourceFile.getFilePath()));
|
|
70
87
|
return Promise.resolve(violations);
|
|
71
88
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
89
|
+
checkNewExpression(newExpr, sourceFile, version) {
|
|
90
|
+
const cls = injectableProjectClass(newExpr.getExpression());
|
|
91
|
+
if (cls === undefined || !isInClassMember(newExpr)) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
const typeName = cls.getName() ?? 'class';
|
|
95
|
+
const major = version();
|
|
96
|
+
const injectionContext = isInjectionContext(newExpr);
|
|
97
|
+
return {
|
|
98
|
+
ruleId: this.id,
|
|
99
|
+
severity: 'warning',
|
|
100
|
+
message: `new ${typeName}() creates an @Injectable class outside Angular DI (Dependency Inversion Principle): its own dependencies are not injected, providers and test doubles for ${typeName} are bypassed.`,
|
|
101
|
+
filePath: sourceFile.getFilePath(),
|
|
102
|
+
line: newExpr.getStartLineNumber(),
|
|
103
|
+
column: newExpr.getStart() - newExpr.getStartLinePos(),
|
|
104
|
+
suggestedFix: injectionAdvice(typeName, `new ${typeName}()`, major, injectionContext),
|
|
105
|
+
metadata: {
|
|
106
|
+
instantiatedType: typeName,
|
|
107
|
+
violationType: 'direct-instantiation',
|
|
108
|
+
principle: 'DIP',
|
|
109
|
+
injectionContext,
|
|
110
|
+
angularMajorVersion: major,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
checkInjectorGet(call, sourceFile, version) {
|
|
115
|
+
const callee = call.getExpression();
|
|
116
|
+
if (!ts_morph_1.Node.isPropertyAccessExpression(callee) || callee.getName() !== 'get') {
|
|
98
117
|
return null;
|
|
99
118
|
}
|
|
100
|
-
|
|
101
|
-
|
|
119
|
+
const receiverNode = callee.getExpression();
|
|
120
|
+
if (!isAngularInjectorType(receiverNode) || !isInClassMember(call)) {
|
|
102
121
|
return null;
|
|
103
122
|
}
|
|
123
|
+
const receiver = receiverNode.getText();
|
|
124
|
+
const token = call.getArguments()[0]?.getText() ?? 'token';
|
|
125
|
+
const major = version();
|
|
126
|
+
const injectionContext = isInjectionContext(call);
|
|
127
|
+
return {
|
|
128
|
+
ruleId: this.id,
|
|
129
|
+
severity: 'warning',
|
|
130
|
+
message: `Service locator: ${receiver}.get(${token}) looks up ${token} at runtime instead of declaring it as a dependency (Dependency Inversion Principle); the class's dependencies are hidden from its signature.`,
|
|
131
|
+
filePath: sourceFile.getFilePath(),
|
|
132
|
+
line: call.getStartLineNumber(),
|
|
133
|
+
column: call.getStart() - call.getStartLinePos(),
|
|
134
|
+
suggestedFix: injectionAdvice(token, `${receiver}.get(${token})`, major, injectionContext),
|
|
135
|
+
metadata: {
|
|
136
|
+
violationType: 'service-locator-pattern',
|
|
137
|
+
principle: 'DIP',
|
|
138
|
+
receiver,
|
|
139
|
+
token,
|
|
140
|
+
injectionContext,
|
|
141
|
+
angularMajorVersion: major,
|
|
142
|
+
},
|
|
143
|
+
};
|
|
104
144
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
violations.push({
|
|
127
|
-
ruleId: this.id,
|
|
128
|
-
message: `Service locator pattern: ${receiver}.get() bypasses Angular DI and makes testing difficult. Inject dependencies directly.`,
|
|
129
|
-
filePath,
|
|
130
|
-
line: call.getStartLineNumber(),
|
|
131
|
-
column: call.getStart() - call.getStartLinePos(),
|
|
132
|
-
suggestedFix: `Replace '${receiver}.get(SomeService)' with 'inject(SomeService)' or add the dependency to the constructor.`,
|
|
133
|
-
metadata: {
|
|
134
|
-
violationType: 'service-locator-pattern',
|
|
135
|
-
principle: 'DIP',
|
|
136
|
-
receiver,
|
|
137
|
-
},
|
|
138
|
-
});
|
|
145
|
+
}
|
|
146
|
+
exports.DIPViolationRule = DIPViolationRule;
|
|
147
|
+
/** Fix text: `inject(X)` from Angular 14 in an injection context, else a constructor parameter. */
|
|
148
|
+
function injectionAdvice(dependency, current, major, injectionContext) {
|
|
149
|
+
if (injectionContext && (major ?? 0) >= INJECT_FUNCTION_MAJOR) {
|
|
150
|
+
return `Replace '${current}' with 'inject(${dependency})'.`;
|
|
151
|
+
}
|
|
152
|
+
return `Declare ${dependency} as a constructor parameter and use it instead of '${current}'.`;
|
|
153
|
+
}
|
|
154
|
+
/** The project class decorated with `@Injectable` from `@angular/core` that `expression` names, if any. */
|
|
155
|
+
function injectableProjectClass(expression) {
|
|
156
|
+
const symbol = expression.getSymbol();
|
|
157
|
+
const target = symbol?.isAlias() ? symbol.getAliasedSymbol() : symbol;
|
|
158
|
+
for (const declaration of target?.getDeclarations() ?? []) {
|
|
159
|
+
if (ts_morph_1.Node.isClassDeclaration(declaration) &&
|
|
160
|
+
!declaration.getSourceFile().isDeclarationFile() &&
|
|
161
|
+
!declaration.getSourceFile().isInNodeModules() &&
|
|
162
|
+
hasAngularInjectable(declaration)) {
|
|
163
|
+
return declaration;
|
|
139
164
|
}
|
|
140
|
-
return violations;
|
|
141
165
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
return
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
/** `true` when the class has an `Injectable` decorator that resolves to `@angular/core`. */
|
|
169
|
+
function hasAngularInjectable(cls) {
|
|
170
|
+
return cls.getDecorators().some((decorator) => {
|
|
171
|
+
const call = decorator.getCallExpression();
|
|
172
|
+
const name = call ? call.getExpression() : decorator.getExpression();
|
|
173
|
+
return isAngularCoreReference(name, 'Injectable');
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** `true` when the type of `receiver` is the `Injector` class of `@angular/core`. */
|
|
177
|
+
function isAngularInjectorType(receiver) {
|
|
178
|
+
const type = receiver.getType();
|
|
179
|
+
const symbol = type.getSymbol() ?? type.getAliasSymbol();
|
|
180
|
+
if (symbol?.getName() !== 'Injector') {
|
|
181
|
+
return false;
|
|
150
182
|
}
|
|
183
|
+
return symbol.getDeclarations().some((declaration) => isInAngularCore(declaration));
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* `true` when `name` (an identifier or `ns.member`) refers to the export
|
|
187
|
+
* `exported` of `@angular/core`: named or aliased import, namespace import, or
|
|
188
|
+
* a symbol declared in the package.
|
|
189
|
+
*/
|
|
190
|
+
function isAngularCoreReference(name, exported) {
|
|
191
|
+
if (ts_morph_1.Node.isIdentifier(name)) {
|
|
192
|
+
return isAngularCoreExport(name, exported);
|
|
193
|
+
}
|
|
194
|
+
if (!ts_morph_1.Node.isPropertyAccessExpression(name) || name.getName() !== exported) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
const namespace = name.getExpression();
|
|
198
|
+
return ts_morph_1.Node.isIdentifier(namespace) && isAngularCoreNamespace(namespace);
|
|
199
|
+
}
|
|
200
|
+
/** `true` when the identifier's symbol is the export `exported` of `@angular/core`. */
|
|
201
|
+
function isAngularCoreExport(identifier, exported) {
|
|
202
|
+
const symbol = identifier.getSymbol();
|
|
203
|
+
if (symbol === undefined) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
const imported = symbol
|
|
207
|
+
.getDeclarations()
|
|
208
|
+
.some((declaration) => ts_morph_1.Node.isImportSpecifier(declaration) &&
|
|
209
|
+
declaration.getName() === exported &&
|
|
210
|
+
declaration.getImportDeclaration().getModuleSpecifierValue() === ANGULAR_CORE);
|
|
211
|
+
if (imported) {
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
const target = symbol.isAlias() ? symbol.getAliasedSymbol() : undefined;
|
|
215
|
+
return (target?.getName() === exported &&
|
|
216
|
+
target.getDeclarations().some((declaration) => isInAngularCore(declaration)));
|
|
217
|
+
}
|
|
218
|
+
/** `true` when the identifier is a namespace import of `@angular/core`. */
|
|
219
|
+
function isAngularCoreNamespace(identifier) {
|
|
220
|
+
return (identifier.getSymbol()?.getDeclarations() ?? []).some((declaration) => ts_morph_1.Node.isNamespaceImport(declaration) &&
|
|
221
|
+
declaration
|
|
222
|
+
.getFirstAncestorByKind(ts_morph_1.SyntaxKind.ImportDeclaration)
|
|
223
|
+
?.getModuleSpecifierValue() === ANGULAR_CORE);
|
|
224
|
+
}
|
|
225
|
+
/** `true` for a declaration inside the `@angular/core` package. */
|
|
226
|
+
function isInAngularCore(declaration) {
|
|
227
|
+
return declaration
|
|
228
|
+
.getSourceFile()
|
|
229
|
+
.getFilePath()
|
|
230
|
+
.includes(`/node_modules/${ANGULAR_CORE}/`);
|
|
231
|
+
}
|
|
232
|
+
/** `true` for a function that builds a provider: a `useFactory` value or an argument of an Angular initializer function. */
|
|
233
|
+
function isProviderFactory(fn) {
|
|
234
|
+
const parent = fn.getParent();
|
|
235
|
+
if (ts_morph_1.Node.isPropertyAssignment(parent) && parent.getName() === 'useFactory') {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
if (!ts_morph_1.Node.isCallExpression(parent) || !parent.getArguments().includes(fn)) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
const callee = parent.getExpression();
|
|
242
|
+
return [...INITIALIZER_FUNCTIONS].some((name) => isAngularCoreReference(callee, name));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* `true` when `node` is inside a member of a class (method, constructor,
|
|
246
|
+
* accessor, property initializer) and inside no provider factory between it
|
|
247
|
+
* and that member. Decorators and functions outside classes are not members.
|
|
248
|
+
*/
|
|
249
|
+
function isInClassMember(node) {
|
|
250
|
+
for (let current = node.getParent(); current !== undefined; current = current.getParent()) {
|
|
251
|
+
if (ts_morph_1.Node.isDecorator(current) || ts_morph_1.Node.isSourceFile(current)) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
if (ts_morph_1.Node.isFunctionLikeDeclaration(current) && isProviderFactory(current)) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (ts_morph_1.Node.isMethodDeclaration(current) ||
|
|
258
|
+
ts_morph_1.Node.isConstructorDeclaration(current) ||
|
|
259
|
+
ts_morph_1.Node.isGetAccessorDeclaration(current) ||
|
|
260
|
+
ts_morph_1.Node.isSetAccessorDeclaration(current) ||
|
|
261
|
+
ts_morph_1.Node.isPropertyDeclaration(current) ||
|
|
262
|
+
ts_morph_1.Node.isClassStaticBlockDeclaration(current)) {
|
|
263
|
+
return ts_morph_1.Node.isClassDeclaration(current.getParent()) || ts_morph_1.Node.isClassExpression(current.getParent());
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* `true` when `node` runs in Angular's injection context: directly in an
|
|
270
|
+
* instance field initializer or the constructor body, with no function in
|
|
271
|
+
* between (a callback runs later, outside the context).
|
|
272
|
+
*/
|
|
273
|
+
function isInjectionContext(node) {
|
|
274
|
+
for (let current = node.getParent(); current !== undefined; current = current.getParent()) {
|
|
275
|
+
if (ts_morph_1.Node.isConstructorDeclaration(current)) {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
if (ts_morph_1.Node.isPropertyDeclaration(current)) {
|
|
279
|
+
return !current.isStatic();
|
|
280
|
+
}
|
|
281
|
+
if (ts_morph_1.Node.isFunctionLikeDeclaration(current) || ts_morph_1.Node.isClassStaticBlockDeclaration(current)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return false;
|
|
151
286
|
}
|
|
152
|
-
exports.DIPViolationRule = DIPViolationRule;
|
|
153
287
|
//# sourceMappingURL=dip-violation.rule.js.map
|
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
import type { AnalysisRule, AnalysisContext, AnalysisResult } from '@angular-modernizer/plugin-system';
|
|
2
2
|
/**
|
|
3
|
-
* God Switch Analysis Rule (`solid:god-switch`) -
|
|
4
|
-
*
|
|
3
|
+
* God Switch Analysis Rule (`solid:god-switch`) - reports switch statements
|
|
4
|
+
* that dispatch to too many different behaviors (Open/Closed Principle).
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
6
|
+
* Clauses are counted by body: labels that fall through without statements
|
|
7
|
+
* form one clause with the next label that has statements, and `default`
|
|
8
|
+
* counts like a case clause when it has a body. A clause is a lookup when
|
|
9
|
+
* its body, ignoring `break` and unwrapping blocks, is a single `return` or
|
|
10
|
+
* a single assignment expression statement whose value contains no call or
|
|
11
|
+
* `new` expression or is one call whose arguments are all constants
|
|
12
|
+
* (literals, enum members, `const` variables; `translate.instant('KEY')`);
|
|
13
|
+
* a clause whose body is only `break` is not counted. One
|
|
14
|
+
* finding per switch (severity warning, at the switch) when the number of
|
|
15
|
+
* non-lookup clauses is above the threshold, read from
|
|
16
|
+
* `rules['solid:god-switch'].maxCases` of `.angular-modernizer.json`
|
|
17
|
+
* (`context.config['solid:god-switch'].maxCases`, default: 10). Switches
|
|
18
|
+
* are checked everywhere in the file. A pure value mapping is never
|
|
19
|
+
* reported; {@link GodSwitchTransformRule} applies a factory refactoring.
|
|
13
20
|
*
|
|
14
21
|
* @example
|
|
15
22
|
* ```typescript
|
|
16
|
-
* //
|
|
23
|
+
* // Reported: 11 clauses that call a handler
|
|
17
24
|
* function handleAction(action: string) {
|
|
18
25
|
* switch (action) {
|
|
19
26
|
* case 'create': return onCreate();
|
|
20
27
|
* case 'read': return onRead();
|
|
21
|
-
* // 9
|
|
28
|
+
* // 9 more clauses that call a handler
|
|
22
29
|
* }
|
|
23
30
|
* }
|
|
24
31
|
* ```
|
|
@@ -26,35 +33,39 @@ import type { AnalysisRule, AnalysisContext, AnalysisResult } from '@angular-mod
|
|
|
26
33
|
export declare class GodSwitchAnalysisRule implements AnalysisRule {
|
|
27
34
|
readonly id = "solid:god-switch";
|
|
28
35
|
readonly name = "God Switch Detection";
|
|
29
|
-
readonly description = "Detects switch statements with too many
|
|
36
|
+
readonly description = "Detects switch statements with too many clauses that do more than return or assign a value (configurable threshold, default: 10)";
|
|
30
37
|
readonly severity = "warning";
|
|
31
38
|
readonly category = "solid-violation";
|
|
32
39
|
readonly tags: string[];
|
|
33
40
|
analyze(context: AnalysisContext): Promise<AnalysisResult[]>;
|
|
41
|
+
private checkSwitch;
|
|
34
42
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* @param switchStmt - The switch statement to analyze
|
|
38
|
-
* @param filePath - Path to the file containing the switch statement
|
|
39
|
-
* @param maxSwitchCases - Maximum allowed case clauses (configurable threshold)
|
|
43
|
+
* Counts clauses with a body of their own; grouped labels without
|
|
44
|
+
* statements belong to the next clause with statements.
|
|
40
45
|
*/
|
|
41
|
-
private
|
|
46
|
+
private countClauses;
|
|
47
|
+
/** Statements without `break`, with a single block unwrapped. */
|
|
48
|
+
private withoutBreaks;
|
|
42
49
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* - type-based: Use inheritance/polymorphism
|
|
47
|
-
* - status-based: Use State Pattern
|
|
48
|
-
* - action-based: Use Command Pattern
|
|
49
|
-
* - event-based: Use Observer Pattern
|
|
50
|
-
* - command-based: Use Command Pattern
|
|
51
|
-
* - state-based: Use State Pattern
|
|
52
|
-
* - kind-based: Use Factory Pattern
|
|
53
|
-
* - category-based: Use Strategy Pattern
|
|
54
|
-
* - discriminated-union: Use type-safe discriminated unions
|
|
55
|
-
*
|
|
56
|
-
* Note: Order matters! Check more specific patterns (event, command) before generic ones (type).
|
|
50
|
+
* True when the body is one `return` or one assignment expression
|
|
51
|
+
* statement whose value has no call or `new` expression in it, or is one
|
|
52
|
+
* call with constant arguments.
|
|
57
53
|
*/
|
|
58
|
-
private
|
|
54
|
+
private isLookup;
|
|
55
|
+
/**
|
|
56
|
+
* A value without a call or `new` expression, or a single call whose
|
|
57
|
+
* arguments (at least one) are all constants: `translate.instant('KEY')`
|
|
58
|
+
* is a key lookup, `this.compute()` and `handle(input)` are not.
|
|
59
|
+
*/
|
|
60
|
+
private isLookupValue;
|
|
61
|
+
/**
|
|
62
|
+
* A string, number, boolean or `null` literal, a template without
|
|
63
|
+
* substitutions, or a reference to an enum member or a `const` variable.
|
|
64
|
+
*/
|
|
65
|
+
private isConstant;
|
|
66
|
+
/** Strips parentheses. */
|
|
67
|
+
private unwrap;
|
|
68
|
+
/** True when the node is or contains a call or `new` expression. */
|
|
69
|
+
private containsCall;
|
|
59
70
|
}
|
|
60
71
|
//# sourceMappingURL=god-switch-analysis.rule.d.ts.map
|