-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
78 lines (72 loc) · 2.1 KB
/
index.ts
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
import { EngineKeywords } from "@c11/engine.types";
import {
type Node,
SyntaxKind,
type VariableDeclaration,
ts,
type Project,
} from "ts-morph";
import { instrumentProducer } from "./instrumentProducer.js";
import { instrumentView } from "./instrumentView.js";
type engineCompilerProps = {
project: Project;
viewLibrary?: string;
};
const canProcess = (v: VariableDeclaration) => {
const id = v
.getNameNode()
.asKindOrThrow(
SyntaxKind.Identifier,
() => `Cannot prpcess "${v.getNameNode().getKindName()}`,
);
// const fn = v
// .getInitializerOrThrow()
// .asKindOrThrow(
// SyntaxKind.ArrowFunction,
// `Expect Assignment to be Arrow Function got: '${v.getText()}'`,
// );
return true;
};
export const engineCompiler = ({
project,
viewLibrary = "@c11/engine.react",
}: engineCompilerProps) => {
const typeFiles = project.createSourceFile(
"lib/.exclude/EngineTypes.ts",
`let v: ${EngineKeywords.VIEW}; let p: ${EngineKeywords.PRODUCER};`,
);
const [ViewIdentifier, ProducerIdentifier] = typeFiles
.getVariableDeclarations()
.map((d) =>
//@ts-ignore
d
.getTypeNode()
.getFirstDescendantByKindOrThrow(ts.SyntaxKind.Identifier),
);
const viewProcessor = instrumentView({ viewLibrary });
const producerProcessor = instrumentProducer();
return {
name: "engine-compiler",
process: async (project: Project): Promise<void> => {
const views = ViewIdentifier.findReferencesAsNodes()
.filter(excludeTypesAndGenerated)
.map(getVariableDeclaration);
const producers = ProducerIdentifier.findReferencesAsNodes()
.filter(excludeTypesAndGenerated)
.map(getVariableDeclaration);
for (const view of views) {
if (canProcess(view)) viewProcessor(view);
}
for (const producer of producers) {
if (canProcess(producer)) producerProcessor(producer);
}
},
};
};
function getVariableDeclaration(n: Node<ts.Node>) {
return n.getFirstAncestorByKindOrThrow(ts.SyntaxKind.VariableDeclaration);
}
function excludeTypesAndGenerated(n: Node<ts.Node>) {
const path = n.getSourceFile().getFilePath();
return !path.match(/(\/\.exclude\/)|(\.d\.ts$)/);
}