-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourcecode.ts
More file actions
215 lines (193 loc) · 5.51 KB
/
sourcecode.ts
File metadata and controls
215 lines (193 loc) · 5.51 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { readFileSync } from "node:fs";
import { basename } from "node:path";
import { ScriptTarget, type SourceFile, createSourceFile } from "typescript";
import type { Call, Callable } from "./call";
import type { Abstract } from "./definition";
import { Parser } from "./parser";
import type * as dep from "./protocol/dep";
import type * as file from "./protocol/file";
import type * as pkg from "./protocol/pkg";
import { caculateHashID, getNameFromPath, normalizePath } from "./utils";
export type LookupDepFn = (name: string) => Dep | undefined;
// export type LookupDirFn = (name: string) => Dir | undefined;
export class Dep {
ident: string;
name: string;
typ: string;
filePtr: File;
constructor(file: File) {
this.ident = `dep-${file.name}`;
this.name = file.name;
this.typ = "file";
this.filePtr = file;
}
getID(): string {
return caculateHashID(this.ident);
}
dump(): dep.SourceDep {
const result: dep.SourceDep = {
id: this.getID(),
name: this.name,
type: this.typ,
ref: this.filePtr ? this.filePtr.getID() : "",
};
return result;
}
}
export class Dir {
ident: string;
path: string;
files: number;
pkg: boolean;
imps: Map<string, Dir>;
constructor(path: string) {
this.path = path;
this.files = 0;
this.pkg = false;
this.ident = path;
this.imps = new Map();
}
getID(): string {
return caculateHashID(this.ident);
}
dump(): pkg.SourcePkg {
const dumpPath = normalizePath(this.path);
const result: pkg.SourcePkg = {
id: this.getID(),
name: getNameFromPath(dumpPath),
fullName: dumpPath,
path: dumpPath,
imports: [],
};
for (const i of this.imps.values()) {
result.imports.push(i.getID());
}
return result;
}
}
export class File {
ident: string;
path: string;
name: string;
dir: Dir;
ts: boolean;
js: boolean;
json: boolean;
test: boolean;
error: string;
fns: Map<string, Callable>;
abs: Map<string, Abstract>;
imps: Map<string, File>;
calls: Call[];
deps: Map<string, Dep>;
lookupDep: LookupDepFn;
registry: Map<string, string>;
private source?: SourceFile;
constructor(
path: string,
dir: Dir,
lookupDep: LookupDepFn,
registry: Map<string, string>,
) {
this.ident = path;
this.path = path;
this.name = basename(path);
this.dir = dir;
this.ts = false;
this.js = false;
this.json = false;
this.test = false;
this.error = "";
this.fns = new Map();
this.abs = new Map();
this.imps = new Map();
this.calls = [];
this.source = undefined;
this.lookupDep = lookupDep;
this.registry = registry;
this.deps = new Map();
if (
this.name.endsWith(".ts") &&
!this.name.endsWith(".d.ts") &&
!this.name.endsWith(".dts")
) {
this.ts = true;
} else if (this.name.endsWith(".js")) {
this.js = true;
} else if (this.name.endsWith(".json")) {
this.json = true;
}
}
parse() {
const sourceData = this.loadFromDisk();
this.source = createSourceFile(
this.path,
sourceData,
ScriptTarget.ES2015,
);
const parser = new Parser(this.dir.path, this.source, this.registry);
parser.parseSource();
for (const name of parser.imports.values()) {
const dep = this.lookupDep(name);
if (dep) this.deps.set(name, dep);
}
this.fns = parser.fns;
for (const fn of parser.fns.values()) {
this.completeFunction(fn);
}
for (const dep of this.deps.values()) {
this.connectDependencies(dep);
}
}
connectDependencies(dep: Dep) {
if (dep.typ === "file") {
const target = dep.filePtr;
this.imps.set(target.ident, target);
const srcDir = this.dir;
const targetDir = target.dir;
if (srcDir && targetDir && srcDir !== targetDir) {
srcDir.imps.set(targetDir.ident, targetDir);
}
}
}
completeFunction(fn: Callable) {
fn.file = this.getID();
fn.dir = this.getDirID();
fn.fileIdent = this.ident;
}
private loadFromDisk(): string {
return readFileSync(this.path).toString();
}
isSource(): boolean {
return this.ts || this.js;
}
getID(): string {
return caculateHashID(this.ident);
}
getDirID(): string {
if (this.dir) return this.dir.getID();
return "";
}
dump(): file.SourceFile {
const dumpPath = normalizePath(this.dir.path);
const pkg = this.dir.pkg ? this.dir.getID() : "";
const result: file.SourceFile = {
id: this.getID(),
name: this.name,
path: dumpPath,
source: this.isSource(),
parsed: !!this.source,
error: this.error,
pkg,
imports: [],
deps: [],
};
for (const dep of this.deps.values()) {
result.deps.push(dep.getID());
}
for (const i of this.imps.values()) {
result.imports.push(i.getID());
}
return result;
}
}