@angular-modernizer/plugin-system 0.3.0 → 1.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/README.md +127 -203
- package/dist/contexts.d.ts +5 -12
- package/dist/contexts.js +0 -12
- package/dist/index.d.ts +11 -6
- package/dist/index.js +11 -6
- package/dist/plugin-interface.d.ts +14 -12
- package/dist/plugin-interface.js +0 -12
- package/package.json +8 -13
- package/dist/contexts.d.ts.map +0 -1
- package/dist/contexts.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/plugin-interface.d.ts.map +0 -1
- package/dist/plugin-interface.js.map +0 -1
- package/src/contexts.ts +0 -223
- package/src/index.ts +0 -33
- package/src/plugin-interface.ts +0 -359
package/README.md
CHANGED
|
@@ -6,14 +6,14 @@ Plugin contracts and context-based dependency injection for the Angular Moderniz
|
|
|
6
6
|
|
|
7
7
|
The `@angular-modernizer/plugin-system` package defines the interfaces and context types that enable the API-driven plugin architecture. It provides the context objects and dependency injection patterns that keep plugins stateless and composable.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
The package contains interfaces plus one class, `ContextFactory`. Its only runtime dependency is `ts-morph` (types of `SourceFile` and `Project`).
|
|
10
10
|
|
|
11
11
|
## Architecture
|
|
12
12
|
|
|
13
13
|
The plugin system uses a context-driven pattern where all dependencies are passed through context objects rather than constructor injection:
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
// Constructor injection
|
|
16
|
+
// Constructor injection - tight coupling, avoid this
|
|
17
17
|
class OldStylePlugin {
|
|
18
18
|
constructor(
|
|
19
19
|
private api: PublicApi,
|
|
@@ -24,7 +24,7 @@ class OldStylePlugin {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
// Context injection
|
|
27
|
+
// Context injection - loose coupling, use this
|
|
28
28
|
class CorrectPlugin {
|
|
29
29
|
constructor() {} // Stateless
|
|
30
30
|
|
|
@@ -54,46 +54,35 @@ interface Plugin {
|
|
|
54
54
|
}
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
`IKernel` is intentionally minimal
|
|
57
|
+
`IKernel` is intentionally minimal: it only exposes `getProjectRoot()`. Plugins that need project-level access must defer that work until `analyze()` or `transform()` is called, where they receive the full context.
|
|
58
58
|
|
|
59
59
|
### Rule Interfaces
|
|
60
60
|
|
|
61
61
|
#### AnalysisRule
|
|
62
62
|
|
|
63
|
-
Analyzes
|
|
63
|
+
Analyzes a file and returns one result per finding.
|
|
64
64
|
|
|
65
65
|
```typescript
|
|
66
66
|
interface AnalysisRule {
|
|
67
67
|
readonly id: string;
|
|
68
68
|
readonly name: string;
|
|
69
69
|
readonly description: string;
|
|
70
|
+
readonly severity: 'error' | 'warning' | 'info';
|
|
71
|
+
readonly category: string;
|
|
72
|
+
readonly tags?: string[];
|
|
70
73
|
|
|
71
|
-
analyze(context: AnalysisContext): Promise<AnalysisResult>;
|
|
74
|
+
analyze(context: AnalysisContext): Promise<AnalysisResult[]>;
|
|
72
75
|
}
|
|
73
76
|
|
|
74
77
|
interface AnalysisResult {
|
|
75
78
|
ruleId: string;
|
|
76
|
-
violations: Violation[];
|
|
77
|
-
metadata?: Record<string, unknown>;
|
|
78
|
-
suggestions?: Suggestion[];
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
interface Violation {
|
|
82
|
-
id: string;
|
|
83
|
-
severity: 'low' | 'medium' | 'high' | 'critical';
|
|
84
79
|
message: string;
|
|
85
|
-
location: SourceLocation;
|
|
86
|
-
category: string;
|
|
87
|
-
ruleId: string;
|
|
88
|
-
autoFix?: AutoFix;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
interface SourceLocation {
|
|
92
80
|
filePath: string;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
81
|
+
line?: number;
|
|
82
|
+
column?: number;
|
|
83
|
+
suggestedFix?: string;
|
|
84
|
+
severity?: 'error' | 'warning' | 'info';
|
|
85
|
+
metadata?: Record<string, unknown>;
|
|
97
86
|
}
|
|
98
87
|
```
|
|
99
88
|
|
|
@@ -106,26 +95,19 @@ interface TransformRule {
|
|
|
106
95
|
readonly id: string;
|
|
107
96
|
readonly name: string;
|
|
108
97
|
readonly description: string;
|
|
98
|
+
readonly category: string;
|
|
99
|
+
readonly tags?: string[];
|
|
109
100
|
|
|
110
|
-
canTransform(context: TransformContext): Promise<boolean>;
|
|
111
101
|
transform(context: TransformContext): Promise<TransformResult>;
|
|
112
102
|
}
|
|
113
103
|
|
|
114
104
|
interface TransformResult {
|
|
115
105
|
ruleId: string;
|
|
116
106
|
modified: boolean;
|
|
117
|
-
message
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
interface Change {
|
|
124
|
-
type: 'add' | 'remove' | 'modify';
|
|
125
|
-
description: string;
|
|
126
|
-
location: SourceLocation;
|
|
127
|
-
before?: string;
|
|
128
|
-
after?: string;
|
|
107
|
+
message: string;
|
|
108
|
+
filePath: string;
|
|
109
|
+
changeCount?: number;
|
|
110
|
+
metadata?: Record<string, unknown>;
|
|
129
111
|
}
|
|
130
112
|
```
|
|
131
113
|
|
|
@@ -138,51 +120,58 @@ interface ValidationRule {
|
|
|
138
120
|
readonly id: string;
|
|
139
121
|
readonly name: string;
|
|
140
122
|
readonly description: string;
|
|
123
|
+
readonly category: string;
|
|
141
124
|
|
|
142
125
|
validate(context: ValidationContext): Promise<ValidationResult>;
|
|
143
126
|
}
|
|
144
127
|
|
|
145
128
|
interface ValidationResult {
|
|
146
129
|
ruleId: string;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
130
|
+
passed: boolean;
|
|
131
|
+
message: string;
|
|
132
|
+
filePath: string;
|
|
133
|
+
details?: string;
|
|
134
|
+
metadata?: Record<string, unknown>;
|
|
151
135
|
}
|
|
152
136
|
```
|
|
153
137
|
|
|
154
|
-
|
|
138
|
+
#### PluginLifecycle
|
|
155
139
|
|
|
156
|
-
|
|
140
|
+
Optional hooks: `onBeforeAnalysis`, `onAfterAnalysis`, `onBeforeTransform`, `onAfterTransform`, `onShutdown` (all `() => Promise<void>`).
|
|
157
141
|
|
|
158
|
-
|
|
142
|
+
## Context Objects
|
|
159
143
|
|
|
160
|
-
|
|
144
|
+
Context objects provide scoped access to dependencies. Always construct them via `ContextFactory`, never by hand. All contexts share `BaseContext`; `api` is typed by a generic parameter `TApi` (pass `PublicApi` from `@angular-modernizer/api`).
|
|
161
145
|
|
|
162
146
|
```typescript
|
|
163
|
-
interface
|
|
147
|
+
interface BaseContext<TApi> {
|
|
164
148
|
readonly sourceFile: SourceFile;
|
|
165
149
|
readonly project: Project;
|
|
166
|
-
readonly api:
|
|
150
|
+
readonly api: TApi;
|
|
167
151
|
readonly config: Record<string, unknown>;
|
|
168
152
|
readonly filePath: string;
|
|
169
|
-
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### AnalysisContext
|
|
157
|
+
|
|
158
|
+
Read-only access for analysis operations.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
interface AnalysisContext<TApi> extends BaseContext<TApi> {
|
|
162
|
+
readonly type: 'analysis';
|
|
163
|
+
readonly options?: { deep?: boolean; maxDepth?: number };
|
|
170
164
|
}
|
|
171
165
|
```
|
|
172
166
|
|
|
173
167
|
### TransformContext
|
|
174
168
|
|
|
175
|
-
Read-write access for transformation operations.
|
|
169
|
+
Read-write access for transformation operations.
|
|
176
170
|
|
|
177
171
|
```typescript
|
|
178
|
-
interface TransformContext {
|
|
179
|
-
readonly
|
|
180
|
-
readonly
|
|
181
|
-
readonly api: PublicApi;
|
|
182
|
-
readonly config: Record<string, unknown>;
|
|
183
|
-
readonly filePath: string;
|
|
184
|
-
readonly dryRun?: boolean;
|
|
185
|
-
readonly ruleConfig?: Record<string, unknown>;
|
|
172
|
+
interface TransformContext<TApi> extends BaseContext<TApi> {
|
|
173
|
+
readonly type: 'transform';
|
|
174
|
+
readonly options?: { dryRun?: boolean; autoSave?: boolean; format?: boolean };
|
|
186
175
|
}
|
|
187
176
|
```
|
|
188
177
|
|
|
@@ -191,36 +180,31 @@ interface TransformContext {
|
|
|
191
180
|
Access for validation operations after transformation.
|
|
192
181
|
|
|
193
182
|
```typescript
|
|
194
|
-
interface ValidationContext {
|
|
195
|
-
readonly
|
|
196
|
-
readonly
|
|
197
|
-
readonly api: PublicApi;
|
|
198
|
-
readonly config: Record<string, unknown>;
|
|
199
|
-
readonly previousResult?: TransformResult;
|
|
183
|
+
interface ValidationContext<TApi> extends BaseContext<TApi> {
|
|
184
|
+
readonly type: 'validation';
|
|
185
|
+
readonly options?: { failFast?: boolean };
|
|
200
186
|
}
|
|
201
187
|
```
|
|
202
188
|
|
|
203
189
|
## Context Factory
|
|
204
190
|
|
|
205
|
-
The `ContextFactory` creates properly typed context objects. Always use it
|
|
191
|
+
The `ContextFactory` creates properly typed context objects and fills `type`, `filePath` (from `sourceFile`) and `config` (default `{}`). Always use it; never construct context objects directly.
|
|
206
192
|
|
|
207
193
|
```typescript
|
|
208
194
|
class ContextFactory {
|
|
209
|
-
static createAnalysisContext(params:
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
config?: Record<string, unknown>;
|
|
219
|
-
ruleConfig?: Record<string, unknown>;
|
|
195
|
+
static createAnalysisContext<TApi>(params: {
|
|
196
|
+
sourceFile: SourceFile;
|
|
197
|
+
project: Project;
|
|
198
|
+
api: TApi;
|
|
199
|
+
config?: Record<string, unknown>;
|
|
200
|
+
options?: AnalysisContext<TApi>['options'];
|
|
201
|
+
}): AnalysisContext<TApi>;
|
|
202
|
+
static createTransformContext<TApi>(params: { /* same, with TransformContext options */ }): TransformContext<TApi>;
|
|
203
|
+
static createValidationContext<TApi>(params: { /* same, with ValidationContext options */ }): ValidationContext<TApi>;
|
|
220
204
|
}
|
|
221
205
|
```
|
|
222
206
|
|
|
223
|
-
`AnalysisContext` requires
|
|
207
|
+
`AnalysisContext` requires `type`, `sourceFile`, `project`, `api`, `config` and `filePath`. Tests that pass only `{ sourceFile }` may compile under ts-jest's lenient inline tsconfig but will fail in `pnpm run type-check`.
|
|
224
208
|
|
|
225
209
|
## Usage Examples
|
|
226
210
|
|
|
@@ -229,8 +213,10 @@ interface AnalysisContextParams {
|
|
|
229
213
|
```typescript
|
|
230
214
|
import {
|
|
231
215
|
type Plugin,
|
|
216
|
+
type IKernel,
|
|
232
217
|
type AnalysisRule,
|
|
233
218
|
type TransformRule,
|
|
219
|
+
type ValidationRule,
|
|
234
220
|
} from '@angular-modernizer/plugin-system';
|
|
235
221
|
|
|
236
222
|
class MyPlugin implements Plugin {
|
|
@@ -259,49 +245,39 @@ class MyPlugin implements Plugin {
|
|
|
259
245
|
### Implementing an Analysis Rule
|
|
260
246
|
|
|
261
247
|
```typescript
|
|
248
|
+
import { Node } from 'ts-morph';
|
|
262
249
|
import {
|
|
263
250
|
type AnalysisRule,
|
|
264
251
|
type AnalysisContext,
|
|
252
|
+
type AnalysisResult,
|
|
265
253
|
} from '@angular-modernizer/plugin-system';
|
|
266
254
|
|
|
267
255
|
class DirectInstantiationRule implements AnalysisRule {
|
|
268
|
-
readonly id = 'direct-instantiation';
|
|
256
|
+
readonly id = 'my-plugin:direct-instantiation';
|
|
269
257
|
readonly name = 'Direct Service Instantiation';
|
|
270
258
|
readonly description = 'Detects direct instantiation of services (DIP violation)';
|
|
259
|
+
readonly severity = 'warning' as const;
|
|
260
|
+
readonly category = 'solid';
|
|
271
261
|
|
|
272
|
-
async analyze(context: AnalysisContext): Promise<AnalysisResult> {
|
|
273
|
-
const { sourceFile,
|
|
274
|
-
const
|
|
262
|
+
async analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
263
|
+
const { sourceFile, filePath } = context;
|
|
264
|
+
const results: AnalysisResult[] = [];
|
|
275
265
|
|
|
276
266
|
sourceFile.forEachDescendant((node) => {
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (this.isServiceClass(className)) {
|
|
281
|
-
violations.push({
|
|
282
|
-
id: `direct-instantiation-${violations.length}`,
|
|
283
|
-
severity: 'high',
|
|
284
|
-
message: `Direct instantiation of ${className} violates Dependency Inversion Principle`,
|
|
285
|
-
location: this.getLocation(node),
|
|
286
|
-
category: 'solid-violation',
|
|
287
|
-
ruleId: this.id,
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
});
|
|
267
|
+
if (!Node.isNewExpression(node)) return;
|
|
268
|
+
const className = node.getExpression().getText();
|
|
269
|
+
if (!className.endsWith('Service')) return;
|
|
292
270
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
}
|
|
271
|
+
results.push({
|
|
272
|
+
ruleId: this.id,
|
|
273
|
+
message: `Direct instantiation of ${className} violates Dependency Inversion Principle`,
|
|
274
|
+
filePath,
|
|
275
|
+
line: node.getStartLineNumber(),
|
|
276
|
+
severity: this.severity,
|
|
277
|
+
});
|
|
278
|
+
});
|
|
302
279
|
|
|
303
|
-
|
|
304
|
-
return className.endsWith('Service') || className.endsWith('Repository');
|
|
280
|
+
return results;
|
|
305
281
|
}
|
|
306
282
|
}
|
|
307
283
|
```
|
|
@@ -312,51 +288,24 @@ class DirectInstantiationRule implements AnalysisRule {
|
|
|
312
288
|
import {
|
|
313
289
|
type TransformRule,
|
|
314
290
|
type TransformContext,
|
|
291
|
+
type TransformResult,
|
|
315
292
|
} from '@angular-modernizer/plugin-system';
|
|
316
293
|
|
|
317
|
-
class
|
|
318
|
-
readonly id = '
|
|
319
|
-
readonly name = '
|
|
320
|
-
readonly description = '
|
|
321
|
-
|
|
322
|
-
async canTransform(context: TransformContext): Promise<boolean> {
|
|
323
|
-
const text = context.sourceFile.getText();
|
|
324
|
-
return text.includes('constructor(') && text.includes('standalone: true');
|
|
325
|
-
}
|
|
294
|
+
class AddInjectImportRule implements TransformRule {
|
|
295
|
+
readonly id = 'my-plugin:add-inject-import';
|
|
296
|
+
readonly name = 'Add inject import';
|
|
297
|
+
readonly description = 'Adds the inject import from @angular/core';
|
|
298
|
+
readonly category = 'angular';
|
|
326
299
|
|
|
327
300
|
async transform(context: TransformContext): Promise<TransformResult> {
|
|
328
|
-
const { sourceFile, api } = context;
|
|
329
|
-
|
|
330
|
-
try {
|
|
331
|
-
const constructorMatch = this.findConstructor(sourceFile);
|
|
332
|
-
if (!constructorMatch) {
|
|
333
|
-
return { ruleId: this.id, modified: false, message: 'No constructor found' };
|
|
334
|
-
}
|
|
301
|
+
const { sourceFile, filePath, api } = context;
|
|
335
302
|
|
|
336
|
-
|
|
337
|
-
this.
|
|
338
|
-
|
|
339
|
-
api.transformation.importManager.addImport(sourceFile, 'inject', '@angular/core');
|
|
340
|
-
|
|
341
|
-
return {
|
|
342
|
-
ruleId: this.id,
|
|
343
|
-
modified: true,
|
|
344
|
-
message: 'Successfully converted constructor to inject()',
|
|
345
|
-
changes: [
|
|
346
|
-
{
|
|
347
|
-
type: 'modify',
|
|
348
|
-
description: 'Replaced constructor with inject() statements',
|
|
349
|
-
location: this.getLocation(constructorMatch.node),
|
|
350
|
-
},
|
|
351
|
-
],
|
|
352
|
-
};
|
|
353
|
-
} catch (error) {
|
|
354
|
-
return {
|
|
355
|
-
ruleId: this.id,
|
|
356
|
-
modified: false,
|
|
357
|
-
errors: [`Transformation failed: ${String(error)}`],
|
|
358
|
-
};
|
|
303
|
+
if (api.transformation.importManager.hasImport(sourceFile, 'inject', '@angular/core')) {
|
|
304
|
+
return { ruleId: this.id, modified: false, message: 'Import already present', filePath };
|
|
359
305
|
}
|
|
306
|
+
|
|
307
|
+
api.transformation.importManager.addNamedImport(sourceFile, '@angular/core', 'inject');
|
|
308
|
+
return { ruleId: this.id, modified: true, message: 'Added inject import', filePath, changeCount: 1 };
|
|
360
309
|
}
|
|
361
310
|
}
|
|
362
311
|
```
|
|
@@ -370,22 +319,17 @@ import { createPublicApi } from '@angular-modernizer/api';
|
|
|
370
319
|
const analysisContext = ContextFactory.createAnalysisContext({
|
|
371
320
|
sourceFile,
|
|
372
321
|
project,
|
|
373
|
-
api: createPublicApi(),
|
|
322
|
+
api: createPublicApi(project),
|
|
374
323
|
config: {
|
|
375
|
-
|
|
376
|
-
},
|
|
377
|
-
ruleConfig: {
|
|
378
|
-
serviceSuffixes: ['Service', 'Repository', 'Store'],
|
|
324
|
+
solid: { godSwitch: { maxCases: 10 } },
|
|
379
325
|
},
|
|
380
326
|
});
|
|
381
327
|
|
|
382
328
|
const transformContext = ContextFactory.createTransformContext({
|
|
383
329
|
sourceFile,
|
|
384
330
|
project,
|
|
385
|
-
api: createPublicApi(),
|
|
386
|
-
|
|
387
|
-
preserveExistingImports: false,
|
|
388
|
-
},
|
|
331
|
+
api: createPublicApi(project),
|
|
332
|
+
options: { dryRun: true },
|
|
389
333
|
});
|
|
390
334
|
```
|
|
391
335
|
|
|
@@ -417,7 +361,7 @@ Rules must not hold internal state between invocations:
|
|
|
417
361
|
```typescript
|
|
418
362
|
// Correct: stateless
|
|
419
363
|
class MyRule implements AnalysisRule {
|
|
420
|
-
analyze(context: AnalysisContext): Promise<AnalysisResult> {
|
|
364
|
+
analyze(context: AnalysisContext): Promise<AnalysisResult[]> {
|
|
421
365
|
// Same context always produces same result
|
|
422
366
|
}
|
|
423
367
|
}
|
|
@@ -457,20 +401,9 @@ Plugins must not manipulate the ts-morph AST directly. All analysis and transfor
|
|
|
457
401
|
```
|
|
458
402
|
packages/plugin-system/
|
|
459
403
|
src/
|
|
460
|
-
interfaces
|
|
461
|
-
|
|
462
|
-
rules.ts
|
|
463
|
-
contexts.ts
|
|
464
|
-
context-factory.ts
|
|
404
|
+
plugin-interface.ts # IKernel, Plugin, rule and result interfaces, PluginLifecycle
|
|
405
|
+
contexts.ts # BaseContext, AnalysisContext, TransformContext, ValidationContext, ContextFactory
|
|
465
406
|
index.ts
|
|
466
|
-
types.ts
|
|
467
|
-
__tests__/
|
|
468
|
-
interfaces/
|
|
469
|
-
plugin.test.ts
|
|
470
|
-
rules.test.ts
|
|
471
|
-
contexts.test.ts
|
|
472
|
-
context-factory.test.ts
|
|
473
|
-
integration.test.ts
|
|
474
407
|
package.json
|
|
475
408
|
tsconfig.json
|
|
476
409
|
jest.config.js
|
|
@@ -479,63 +412,54 @@ packages/plugin-system/
|
|
|
479
412
|
|
|
480
413
|
## Testing
|
|
481
414
|
|
|
482
|
-
The plugin
|
|
415
|
+
The package has no tests of its own (`jest --passWithNoTests`). Rule tests in the plugin packages build contexts with `ContextFactory`:
|
|
483
416
|
|
|
484
417
|
```typescript
|
|
485
|
-
import {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
} from '@angular-modernizer/plugin-system';
|
|
489
|
-
|
|
490
|
-
describe('MyAnalysisRule', () => {
|
|
491
|
-
let rule: MyAnalysisRule;
|
|
492
|
-
let context: AnalysisContext;
|
|
493
|
-
|
|
494
|
-
beforeEach(() => {
|
|
495
|
-
rule = new MyAnalysisRule();
|
|
496
|
-
context = createMockAnalysisContext({
|
|
497
|
-
sourceFile: mockSourceFile,
|
|
498
|
-
config: { customSetting: true },
|
|
499
|
-
});
|
|
500
|
-
});
|
|
418
|
+
import { ContextFactory } from '@angular-modernizer/plugin-system';
|
|
419
|
+
import { createPublicApi } from '@angular-modernizer/api';
|
|
420
|
+
import { Project } from 'ts-morph';
|
|
501
421
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
422
|
+
const project = new Project({ useInMemoryFileSystem: true });
|
|
423
|
+
const sourceFile = project.createSourceFile('/test/user.service.ts', 'export class UserService {}');
|
|
424
|
+
const context = ContextFactory.createAnalysisContext({
|
|
425
|
+
sourceFile,
|
|
426
|
+
project,
|
|
427
|
+
api: createPublicApi(project),
|
|
506
428
|
});
|
|
429
|
+
|
|
430
|
+
const results = await new MyAnalysisRule().analyze(context);
|
|
507
431
|
```
|
|
508
432
|
|
|
509
433
|
## Best Practices
|
|
510
434
|
|
|
511
435
|
### Rule Development
|
|
512
436
|
|
|
513
|
-
1. Keep rules focused
|
|
514
|
-
2. Use descriptive IDs
|
|
515
|
-
3. Provide
|
|
516
|
-
4. Handle errors gracefully
|
|
437
|
+
1. Keep rules focused: one rule, one responsibility
|
|
438
|
+
2. Use descriptive IDs: `solid:dip-violation-direct-instantiation`, not `rule1`
|
|
439
|
+
3. Provide `suggestedFix` when possible
|
|
440
|
+
4. Handle errors gracefully: do not throw on malformed code
|
|
517
441
|
5. Write comprehensive tests covering edge cases
|
|
518
442
|
|
|
519
443
|
### Plugin Development
|
|
520
444
|
|
|
521
|
-
1. Group related rules
|
|
445
|
+
1. Group related rules: one plugin per domain (SOLID, standalone, etc.)
|
|
522
446
|
2. Use semantic versioning for API compatibility
|
|
523
|
-
3. Initialize efficiently
|
|
447
|
+
3. Initialize efficiently: defer I/O until `analyze()` or `transform()`
|
|
524
448
|
|
|
525
449
|
### Context Usage
|
|
526
450
|
|
|
527
451
|
1. Destructure contexts: `const { api, sourceFile } = context;`
|
|
528
|
-
2.
|
|
452
|
+
2. Read rule-specific settings from `config`
|
|
529
453
|
3. Validate required context properties
|
|
530
|
-
4. Return meaningful results including metadata and
|
|
454
|
+
4. Return meaningful results including metadata and suggested fixes
|
|
531
455
|
|
|
532
456
|
## Contributing
|
|
533
457
|
|
|
534
|
-
1. Maintain backward compatibility
|
|
535
|
-
2. Add comprehensive types
|
|
536
|
-
3. Write integration tests
|
|
537
|
-
4. Document with examples
|
|
538
|
-
5. Consider extensibility
|
|
458
|
+
1. Maintain backward compatibility: do not break existing interfaces
|
|
459
|
+
2. Add comprehensive types: strong typing for all interfaces
|
|
460
|
+
3. Write integration tests: test rule composition and context usage
|
|
461
|
+
4. Document with examples: show real usage patterns
|
|
462
|
+
5. Consider extensibility: design for future enhancements
|
|
539
463
|
|
|
540
464
|
### Adding New Rule Types
|
|
541
465
|
|
package/dist/contexts.d.ts
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-system - Context Objects
|
|
3
|
-
*
|
|
4
|
-
* Context objects are the mechanism by which we pass capabilities to plugins.
|
|
5
|
-
* They contain the PublicApi and provide scoped access to the codebase.
|
|
6
|
-
*
|
|
7
|
-
* Design Philosophy: "Context is the API Gateway"
|
|
8
|
-
* - Plugins receive a context object, not raw infrastructure
|
|
9
|
-
* - Context contains the PublicApi with battle-tested helpers
|
|
10
|
-
* - Context is immutable and scoped to a single operation
|
|
11
|
-
* - This enforces plugins to use standardized, high-quality tools
|
|
12
|
-
*/
|
|
13
1
|
import type { Project, SourceFile } from 'ts-morph';
|
|
14
2
|
/**
|
|
15
3
|
* Generic type for the Public API.
|
|
@@ -20,6 +8,11 @@ export type PublicApiType = any;
|
|
|
20
8
|
/**
|
|
21
9
|
* Base context shared by all plugin operations.
|
|
22
10
|
* Contains common elements needed across analysis, transformation, and validation.
|
|
11
|
+
*
|
|
12
|
+
* Context objects are how capabilities reach plugins: a rule receives a
|
|
13
|
+
* context, not raw infrastructure. The context carries the public API
|
|
14
|
+
* (`api`) and is scoped to one operation on one source file; all fields
|
|
15
|
+
* are read-only.
|
|
23
16
|
*/
|
|
24
17
|
export interface BaseContext<TApi = PublicApiType> {
|
|
25
18
|
/**
|
package/dist/contexts.js
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/plugin-system - Context Objects
|
|
3
|
-
*
|
|
4
|
-
* Context objects are the mechanism by which we pass capabilities to plugins.
|
|
5
|
-
* They contain the PublicApi and provide scoped access to the codebase.
|
|
6
|
-
*
|
|
7
|
-
* Design Philosophy: "Context is the API Gateway"
|
|
8
|
-
* - Plugins receive a context object, not raw infrastructure
|
|
9
|
-
* - Context contains the PublicApi with battle-tested helpers
|
|
10
|
-
* - Context is immutable and scoped to a single operation
|
|
11
|
-
* - This enforces plugins to use standardized, high-quality tools
|
|
12
|
-
*/
|
|
13
1
|
/**
|
|
14
2
|
* Factory for creating context objects.
|
|
15
3
|
* This is used internally by the orchestration layer.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Plugin contracts for the Angular Modernizer.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Defines what a plugin is ({@link Plugin}), the three rule kinds it can
|
|
5
|
+
* contribute ({@link AnalysisRule}, {@link TransformRule},
|
|
6
|
+
* {@link ValidationRule}) and the context objects the kernel passes to them
|
|
7
|
+
* ({@link AnalysisContext}, {@link TransformContext},
|
|
8
|
+
* {@link ValidationContext}). {@link ContextFactory} builds those contexts.
|
|
9
|
+
* {@link IKernel} is the minimal kernel view a plugin sees during
|
|
10
|
+
* {@link Plugin.initialize}.
|
|
6
11
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
12
|
+
* Plugin authors import only from `@angular-modernizer/plugin-system`.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
10
15
|
*/
|
|
11
16
|
export type { Plugin, AnalysisRule, AnalysisResult, TransformRule, TransformResult, ValidationRule, ValidationResult, PluginLifecycle, IKernel, } from './plugin-interface.js';
|
|
12
17
|
export type { BaseContext, AnalysisContext, TransformContext, ValidationContext, } from './contexts.js';
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Plugin contracts for the Angular Modernizer.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Defines what a plugin is ({@link Plugin}), the three rule kinds it can
|
|
5
|
+
* contribute ({@link AnalysisRule}, {@link TransformRule},
|
|
6
|
+
* {@link ValidationRule}) and the context objects the kernel passes to them
|
|
7
|
+
* ({@link AnalysisContext}, {@link TransformContext},
|
|
8
|
+
* {@link ValidationContext}). {@link ContextFactory} builds those contexts.
|
|
9
|
+
* {@link IKernel} is the minimal kernel view a plugin sees during
|
|
10
|
+
* {@link Plugin.initialize}.
|
|
6
11
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
12
|
+
* Plugin authors import only from `@angular-modernizer/plugin-system`.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
10
15
|
*/
|
|
11
16
|
export { ContextFactory } from './contexts.js';
|
|
12
17
|
//# sourceMappingURL=index.js.map
|