-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathast-node-pipeline.ts
More file actions
52 lines (43 loc) · 1.62 KB
/
ast-node-pipeline.ts
File metadata and controls
52 lines (43 loc) · 1.62 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
import { extractAstNodesFromXml, type AstNode } from "./extract-ast-nodes.js";
import { type ApexAstNodeMetadata } from "./metadata/apex-ast-reference.js";
import { getEngineStrategy } from "../engines/engine-strategies.js";
// Template Method pipeline for AST XML -> nodes -> metadata.
export type AstPipelineInput = {
code: string;
language: string;
};
export type AstPipelineOutput = {
nodes: AstNode[];
metadata: ApexAstNodeMetadata[];
};
export abstract class AstNodePipeline {
public async run(input: AstPipelineInput): Promise<AstPipelineOutput> {
const astXml = await this.generateAstXml(input);
const nodes = this.extractNodes(astXml);
const metadata = await this.enrichMetadata(input, nodes);
return { nodes, metadata };
}
protected abstract generateAstXml(input: AstPipelineInput): Promise<string>;
protected extractNodes(astXml: string): AstNode[] {
return extractAstNodesFromXml(astXml);
}
protected async enrichMetadata(
_input: AstPipelineInput,
_nodes: AstNode[]
): Promise<ApexAstNodeMetadata[]> {
return [];
}
}
export class PmdAstNodePipeline extends AstNodePipeline {
private readonly strategy = getEngineStrategy("pmd");
protected async generateAstXml(input: AstPipelineInput): Promise<string> {
return this.strategy.astGenerator.generateAstXml(input.code, input.language);
}
protected async enrichMetadata(
input: AstPipelineInput,
nodes: AstNode[]
): Promise<ApexAstNodeMetadata[]> {
const nodeNames = Array.from(new Set(nodes.map((node) => node.nodeName)));
return this.strategy.metadataProvider.getMetadata(input.language, nodeNames);
}
}