-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathplugin-registry.ts
More file actions
85 lines (70 loc) · 2.23 KB
/
Copy pathplugin-registry.ts
File metadata and controls
85 lines (70 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { logger } from '@veridion/logger';
import { UncheckedReturnPlugin } from '@veridion/plugin-unchecked-return';
import type { AnalysisContext, IRulePlugin, PluginMetadata } from '@veridion/scanner-types';
export const defaultPlugins: IRulePlugin[] = [new UncheckedReturnPlugin()];
export class PluginRegistry {
private plugins = new Map<string, IRulePlugin>();
register(plugin: IRulePlugin): void {
if (this.plugins.has(plugin.metadata.id)) {
logger.warn({ pluginId: plugin.metadata.id }, 'Plugin already registered, overwriting');
}
this.plugins.set(plugin.metadata.id, plugin);
logger.info(
{ pluginId: plugin.metadata.id, version: plugin.metadata.version },
'Plugin registered',
);
}
registerAll(plugins: IRulePlugin[]): void {
for (const plugin of plugins) {
this.register(plugin);
}
}
registerDefaultPlugins(): void {
this.registerAll(defaultPlugins);
}
unregister(pluginId: string): boolean {
return this.plugins.delete(pluginId);
}
get(pluginId: string): IRulePlugin | undefined {
return this.plugins.get(pluginId);
}
getAll(): IRulePlugin[] {
return Array.from(this.plugins.values());
}
getByCategory(category: string): IRulePlugin[] {
return this.getAll().filter(
(p) => p.metadata.category === (category as PluginMetadata['category']),
);
}
getBySeverity(severity: string): IRulePlugin[] {
return this.getAll().filter(
(p) => p.metadata.severity === (severity as PluginMetadata['severity']),
);
}
getByChain(chain: string): IRulePlugin[] {
return this.getAll().filter((p) =>
p.supportsContext({
contractName: '',
sourceCode: '',
chain,
language: '',
compilerVersion: null,
metadata: {},
}),
);
}
getMatchingPlugins(context: AnalysisContext): IRulePlugin[] {
return this.getAll().filter((p) => p.supportsContext(context));
}
getAllMetadata(): PluginMetadata[] {
return this.getAll().map((p) => ({ ...p.metadata }));
}
get size(): number {
return this.plugins.size;
}
}
export function createDefaultRegistry(): PluginRegistry {
const registry = new PluginRegistry();
registry.registerDefaultPlugins();
return registry;
}