-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathget-dependencies.ts
More file actions
40 lines (38 loc) · 1.16 KB
/
get-dependencies.ts
File metadata and controls
40 lines (38 loc) · 1.16 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
import { error } from "../../../deno_ral/log.ts";
import { walk } from "./ast-utils.ts";
// import { assert } from "jsr:@std/assert";
// deno-lint-ignore no-explicit-any
const assert = (condition: any) => {
if (!condition) {
throw new Error("Assertion failed");
}
};
export const getVariableDependencies = (declarations: Map<string, any>) => {
const dependencies = new Map<string, {
// deno-lint-ignore no-explicit-any
node: any;
dependencies: Set<string>;
}>();
for (const [name, node] of declarations) {
assert(node?.type === "declaration");
const varName = node?.property?.variable?.value;
assert(varName === name);
if (!dependencies.has(varName)) {
dependencies.set(varName, { node: node, dependencies: new Set() });
}
const varValue = node?.value;
// deno-lint-ignore no-explicit-any
walk(varValue, (inner: any) => {
if (inner?.type === "variable") {
const innerName = inner?.value;
if (!innerName) {
error(inner);
throw new Error("stop");
}
dependencies.get(varName)!.dependencies.add(innerName);
}
return true;
});
}
return dependencies;
};