devlensio 1.1.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/graph/buildLookup.d.ts +30 -0
- package/dist/graph/buildLookup.js +36 -1
- package/dist/graph/edges/aliasChain.d.ts +7 -0
- package/dist/graph/edges/aliasChain.js +63 -0
- package/dist/graph/edges/aliasResolution.test.d.ts +1 -0
- package/dist/graph/edges/aliasResolution.test.js +351 -0
- package/dist/graph/edges/callEdges.js +178 -18
- package/dist/graph/edges/callEdges.test.js +119 -0
- package/dist/graph/edges/callOrder.test.d.ts +1 -0
- package/dist/graph/edges/callOrder.test.js +227 -0
- package/dist/graph/edges/importEdges.js +124 -0
- package/dist/graph/edges/propEdges.js +6 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/parser/callFlow.d.ts +15 -0
- package/dist/parser/callFlow.js +302 -0
- package/dist/parser/callOrder.test.d.ts +1 -0
- package/dist/parser/callOrder.test.js +407 -0
- package/dist/parser/declarationIds.d.ts +23 -0
- package/dist/parser/declarationIds.js +123 -0
- package/dist/parser/exportsMap.js +1 -90
- package/dist/parser/extractors/components.js +18 -28
- package/dist/parser/extractors/functions.d.ts +21 -1
- package/dist/parser/extractors/functions.js +92 -60
- package/dist/parser/extractors/moduleScope.d.ts +8 -0
- package/dist/parser/extractors/moduleScope.js +80 -0
- package/dist/parser/index.js +12 -0
- package/dist/parser/moduleScope.test.d.ts +1 -0
- package/dist/parser/moduleScope.test.js +176 -0
- package/dist/summarizer/index.js +53 -4
- package/dist/summarizer/prompts.js +111 -50
- package/dist/summarizer/prompts.test.d.ts +1 -0
- package/dist/summarizer/prompts.test.js +112 -0
- package/dist/types.d.ts +63 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +2 -0
- package/extractors/go/bin/darwin-amd64/devlens_go_extractor +0 -0
- package/extractors/go/bin/darwin-arm64/devlens_go_extractor +0 -0
- package/extractors/go/bin/linux-amd64/devlens_go_extractor +0 -0
- package/extractors/go/bin/linux-arm64/devlens_go_extractor +0 -0
- package/extractors/go/bin/windows-amd64/devlens_go_extractor.exe +0 -0
- package/extractors/go/calls.go +131 -10
- package/extractors/go/nodes.go +29 -11
- package/extractors/go/parser.go +376 -41
- package/extractors/java/devlens_java_extractor.jar +0 -0
- package/extractors/java/src/devlens/extractor/Extractor.java +52 -12
- package/extractors/java/src/devlens/extractor/LookupMaps.java +2 -0
- package/extractors/java/src/devlens/extractor/Parser.java +420 -1
- package/extractors/java/src/devlens/extractor/edges/Calls.java +105 -5
- package/extractors/python/devlens_extractors_python/edges/calls.py +199 -36
- package/extractors/python/devlens_extractors_python/edges/imports.py +21 -1
- package/extractors/python/devlens_extractors_python/lookup.py +9 -0
- package/extractors/python/devlens_extractors_python/parser/__init__.py +11 -1
- package/extractors/python/devlens_extractors_python/parser/functions.py +199 -42
- package/extractors/rust/bin/darwin-amd64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/darwin-arm64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/linux-amd64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/linux-arm64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/windows-amd64/devlens_rust_extractor.exe +0 -0
- package/extractors/rust/src/calls.rs +114 -8
- package/extractors/rust/src/lookup.rs +66 -0
- package/extractors/rust/src/nodes.rs +29 -0
- package/extractors/rust/src/parser.rs +362 -27
- package/package.json +77 -73
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { SyntaxKind } from "ts-morph";
|
|
3
3
|
import { detectFunctionDirective } from "../directives.js";
|
|
4
4
|
import { extractReturnTypeAnnotation, extractReferencedInterfaces, } from "../typeUtils.js";
|
|
5
|
-
import {
|
|
5
|
+
import { classifyCall, createCallAccumulator, finishCalls } from "./functions.js";
|
|
6
6
|
// Generates a unique id for a node
|
|
7
7
|
function makeId(filePath, name) {
|
|
8
8
|
return `${filePath}::${name}`;
|
|
@@ -41,10 +41,13 @@ function extractContextRefs(node) {
|
|
|
41
41
|
}
|
|
42
42
|
return [...new Set(refs)];
|
|
43
43
|
}
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
// Component call sites. The legacy `uses` list keeps its exact shape (external
|
|
45
|
+
// calls only: no hooks, no React.*, no builtins, no calls to the component's own
|
|
46
|
+
// inner functions), while callSites keeps every occurrence in source order for
|
|
47
|
+
// callOrder v1. Calls made inside inner functions or JSX handlers are kept but
|
|
48
|
+
// tagged by scope, never renumbered into the render path.
|
|
49
|
+
function extractComponentCalls(node) {
|
|
50
|
+
const innerFunctionNames = new Set(["React"]);
|
|
48
51
|
for (const varDecl of node.getDescendantsOfKind(SyntaxKind.VariableDeclaration)) {
|
|
49
52
|
const name = varDecl.getName();
|
|
50
53
|
const init = varDecl.getInitializer();
|
|
@@ -54,31 +57,16 @@ function extractAllCalls(node) {
|
|
|
54
57
|
innerFunctionNames.add(name);
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
|
-
// Function declarations like function fn() {}
|
|
58
60
|
for (const fn of node.getDescendantsOfKind(SyntaxKind.FunctionDeclaration)) {
|
|
59
61
|
const name = fn.getName();
|
|
60
62
|
if (name)
|
|
61
63
|
innerFunctionNames.add(name);
|
|
62
64
|
}
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
const expr = call.getExpression();
|
|
67
|
-
const fullExpr = expr.getText();
|
|
68
|
-
const rootName = fullExpr.split(".")[0];
|
|
69
|
-
if (rootName.startsWith("use"))
|
|
70
|
-
continue; // skip hooks
|
|
71
|
-
if (rootName.startsWith("React"))
|
|
72
|
-
continue;
|
|
73
|
-
if (JS_BUILTINS.has(rootName))
|
|
74
|
-
continue;
|
|
75
|
-
if (innerFunctionNames.has(rootName))
|
|
76
|
-
continue; // skip calls to inner functions
|
|
77
|
-
// Capture the full expression (e.g. "axios.get" not just "axios") so
|
|
78
|
-
// callEdges.ts can create a per-method third-party node when needed.
|
|
79
|
-
externalCalls.add(fullExpr);
|
|
65
|
+
const acc = createCallAccumulator({ owner: node, skipRoots: innerFunctionNames });
|
|
66
|
+
for (const call of node.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
67
|
+
classifyCall(call, acc);
|
|
80
68
|
}
|
|
81
|
-
return
|
|
69
|
+
return finishCalls(acc);
|
|
82
70
|
}
|
|
83
71
|
// Checks if a component has any state (useState or useReducer)
|
|
84
72
|
function hasState(hooks) {
|
|
@@ -138,7 +126,7 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
138
126
|
if (!returnsJSX(fn))
|
|
139
127
|
continue;
|
|
140
128
|
const hooks = extractHooks(fn);
|
|
141
|
-
const
|
|
129
|
+
const callData = extractComponentCalls(fn);
|
|
142
130
|
const contextRefs = extractContextRefs(fn);
|
|
143
131
|
const renderingBoundary = detectFunctionDirective(fn.getBody()) ?? fileDirective;
|
|
144
132
|
const propTypes = extractPropTypes(fn, file);
|
|
@@ -154,7 +142,8 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
154
142
|
metadata: {
|
|
155
143
|
hooks,
|
|
156
144
|
contextRefs,
|
|
157
|
-
uses:
|
|
145
|
+
uses: callData.calls,
|
|
146
|
+
callSites: callData.callSites,
|
|
158
147
|
hasState: hasState(hooks),
|
|
159
148
|
exportType: fn.isDefaultExport() ? "default" : "named",
|
|
160
149
|
propTypes,
|
|
@@ -204,7 +193,7 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
204
193
|
if (!returnsJSX(nodeToAnalyze))
|
|
205
194
|
continue;
|
|
206
195
|
const hooks = extractHooks(nodeToAnalyze);
|
|
207
|
-
const
|
|
196
|
+
const callData = extractComponentCalls(nodeToAnalyze);
|
|
208
197
|
const contextRefs = extractContextRefs(nodeToAnalyze);
|
|
209
198
|
const renderingBoundary = detectFunctionDirective(nodeToAnalyze.getKind() === SyntaxKind.ArrowFunction
|
|
210
199
|
? nodeToAnalyze.getBody()
|
|
@@ -229,7 +218,8 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
229
218
|
metadata: {
|
|
230
219
|
hooks,
|
|
231
220
|
contextRefs,
|
|
232
|
-
uses:
|
|
221
|
+
uses: callData.calls,
|
|
222
|
+
callSites: callData.callSites,
|
|
233
223
|
hasState: hasState(hooks),
|
|
234
224
|
exportType: isDefault ? "default" : isExported ? "named" : "none",
|
|
235
225
|
isMemoized: isMemoOrForwardRef,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SourceFile } from "ts-morph";
|
|
1
|
+
import { SourceFile, type Node } from "ts-morph";
|
|
2
2
|
import type { CallSite, CodeNode } from "../../types.js";
|
|
3
3
|
import { type RenderingBoundary } from "../directives.js";
|
|
4
4
|
export declare const JS_BUILTINS: Set<string>;
|
|
@@ -10,6 +10,26 @@ export interface ExtractedCallData {
|
|
|
10
10
|
dependencyNames: string[];
|
|
11
11
|
contextRefs: string[];
|
|
12
12
|
}
|
|
13
|
+
export interface CallAccumulatorOptions {
|
|
14
|
+
owner: Node;
|
|
15
|
+
isFileOwner?: boolean;
|
|
16
|
+
skipRoots?: Set<string>;
|
|
17
|
+
}
|
|
18
|
+
export interface CallAccumulator {
|
|
19
|
+
owner: Node;
|
|
20
|
+
isFileOwner: boolean;
|
|
21
|
+
skipRoots: Set<string>;
|
|
22
|
+
sites: CallSite[];
|
|
23
|
+
hookCalls: string[];
|
|
24
|
+
apiCalls: string[];
|
|
25
|
+
dependencyNames: string[];
|
|
26
|
+
contextRefs: string[];
|
|
27
|
+
order: number;
|
|
28
|
+
ordinals: Map<Node, number>;
|
|
29
|
+
}
|
|
30
|
+
export declare function createCallAccumulator(options: CallAccumulatorOptions): CallAccumulator;
|
|
31
|
+
export declare function classifyCall(call: any, acc: CallAccumulator): void;
|
|
32
|
+
export declare function finishCalls(acc: CallAccumulator): ExtractedCallData;
|
|
13
33
|
export declare function extractCallsWithSites(node: any): ExtractedCallData;
|
|
14
34
|
export declare function extractFunctionCalls(node: any): string[];
|
|
15
35
|
export declare function hasErrorHandling(node: any): boolean;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
import { annotateCall, isIifeCallee } from "../callFlow.js";
|
|
2
3
|
import { returnsJSX } from "./components.js";
|
|
3
4
|
import { detectFunctionDirective } from "../directives.js";
|
|
4
5
|
import { extractParams, extractReturnTypeAnnotation, extractBareTypeNames, extractReferencedInterfaces, } from "../typeUtils.js";
|
|
@@ -43,77 +44,108 @@ function inferArgType(arg) {
|
|
|
43
44
|
// classifier below.
|
|
44
45
|
const API_HOOK_FAMILIES = new Set(["useQuery", "useMutation", "useInfiniteQuery", "useSuspenseQuery", "useSWR", "useSWRMutation"]);
|
|
45
46
|
const AXIOS_METHODS = new Set(["axios.get", "axios.post", "axios.put", "axios.delete", "axios.patch", "axios"]);
|
|
47
|
+
export function createCallAccumulator(options) {
|
|
48
|
+
return {
|
|
49
|
+
owner: options.owner,
|
|
50
|
+
isFileOwner: options.isFileOwner ?? false,
|
|
51
|
+
skipRoots: options.skipRoots ?? new Set(),
|
|
52
|
+
sites: [],
|
|
53
|
+
hookCalls: [],
|
|
54
|
+
apiCalls: [],
|
|
55
|
+
dependencyNames: [],
|
|
56
|
+
contextRefs: [],
|
|
57
|
+
order: 0,
|
|
58
|
+
ordinals: new Map(),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function pushSite(call, name, acc, kind) {
|
|
62
|
+
const args = call.getArguments();
|
|
63
|
+
let hasSpread = false;
|
|
64
|
+
const argTypes = [];
|
|
65
|
+
for (const arg of args) {
|
|
66
|
+
if (arg.getKind?.() === SyntaxKind.SpreadElement) {
|
|
67
|
+
hasSpread = true;
|
|
68
|
+
argTypes.push("unknown");
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
argTypes.push(inferArgType(arg) ?? "unknown");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const order = ++acc.order;
|
|
75
|
+
acc.ordinals.set(call, order);
|
|
76
|
+
const annotations = annotateCall(call, acc.owner, acc.isFileOwner, acc.ordinals);
|
|
77
|
+
acc.sites.push({
|
|
78
|
+
name,
|
|
79
|
+
argCount: args.length,
|
|
80
|
+
argTypes,
|
|
81
|
+
...(hasSpread && { hasSpread: true }),
|
|
82
|
+
kind,
|
|
83
|
+
order,
|
|
84
|
+
line: call.getStartLineNumber(),
|
|
85
|
+
...annotations,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
46
88
|
// SINGLE traversal over all call expressions producing every call-derived
|
|
47
89
|
// output at once: structured call sites + legacy bare names, custom hook
|
|
48
90
|
// calls, and API-call strings. The per-aspect extractors used to walk the
|
|
49
91
|
// same descendants tree four times per function-like; whole-repo cost is now
|
|
50
92
|
// one walk.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
dependencyNames.push(name);
|
|
68
|
-
if (name === "useContext") {
|
|
69
|
-
const ctxArg = call.getArguments()[0];
|
|
70
|
-
if (ctxArg && !contextRefs.includes(ctxArg.getText()))
|
|
71
|
-
contextRefs.push(ctxArg.getText());
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// API-call families: fetch / axios.* / React Query / SWR
|
|
75
|
-
if (name === "fetch" || AXIOS_METHODS.has(name) || API_HOOK_FAMILIES.has(name)) {
|
|
76
|
-
const args = call.getArguments();
|
|
77
|
-
if (args.length > 0)
|
|
78
|
-
apiCalls.push(`${name}(${args[0].getText()})`);
|
|
79
|
-
}
|
|
80
|
-
// Plain calls/sites skip hooks and builtin noise
|
|
81
|
-
if (name.startsWith("use"))
|
|
82
|
-
continue;
|
|
83
|
-
const rootName = name.split(".")[0];
|
|
84
|
-
if (JS_BUILTINS.has(rootName))
|
|
85
|
-
continue;
|
|
93
|
+
//
|
|
94
|
+
// callOrder v1: every occurrence is kept (no shape de-duplication), hooks are
|
|
95
|
+
// recorded with kind "hook" so their position is known, and metadata.calls is
|
|
96
|
+
// derived as the ordered, name-deduped, non-hook subset.
|
|
97
|
+
export function classifyCall(call, acc) {
|
|
98
|
+
const expression = call.getExpression?.();
|
|
99
|
+
if (!expression)
|
|
100
|
+
return;
|
|
101
|
+
if (isIifeCallee(expression))
|
|
102
|
+
return;
|
|
103
|
+
const name = expression.getText();
|
|
104
|
+
const rootName = name.split(".")[0];
|
|
105
|
+
if (acc.skipRoots.has(rootName))
|
|
106
|
+
return;
|
|
107
|
+
// API-call families: fetch / axios.* / React Query / SWR
|
|
108
|
+
if (name === "fetch" || AXIOS_METHODS.has(name) || API_HOOK_FAMILIES.has(name)) {
|
|
86
109
|
const args = call.getArguments();
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
110
|
+
if (args.length > 0)
|
|
111
|
+
acc.apiCalls.push(`${name}(${args[0].getText()})`);
|
|
112
|
+
}
|
|
113
|
+
// use* family: custom hooks (^use[A-Z]) plus ALL use* names (incl.
|
|
114
|
+
// built-ins like useState) for hook dependency tracking
|
|
115
|
+
if (name.startsWith("use")) {
|
|
116
|
+
if (/^use[A-Z]/.test(name) && !acc.hookCalls.includes(name))
|
|
117
|
+
acc.hookCalls.push(name);
|
|
118
|
+
if (!acc.dependencyNames.includes(name))
|
|
119
|
+
acc.dependencyNames.push(name);
|
|
120
|
+
if (name === "useContext") {
|
|
121
|
+
const ctxArg = call.getArguments()[0];
|
|
122
|
+
if (ctxArg && !acc.contextRefs.includes(ctxArg.getText()))
|
|
123
|
+
acc.contextRefs.push(ctxArg.getText());
|
|
97
124
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// signals (same arity + different literal types can hit different
|
|
101
|
-
// overloads, and each deserves its own edge).
|
|
102
|
-
const key = `${name}/${args.length}/${argTypes.join(",")}`;
|
|
103
|
-
if (seenSites.has(key))
|
|
104
|
-
continue;
|
|
105
|
-
seenSites.add(key);
|
|
106
|
-
sites.push({ name, argCount: args.length, argTypes, ...(hasSpread && { hasSpread: true }) });
|
|
125
|
+
pushSite(call, name, acc, "hook");
|
|
126
|
+
return;
|
|
107
127
|
}
|
|
128
|
+
if (JS_BUILTINS.has(rootName))
|
|
129
|
+
return;
|
|
130
|
+
pushSite(call, name, acc, "call");
|
|
131
|
+
}
|
|
132
|
+
export function finishCalls(acc) {
|
|
108
133
|
return {
|
|
109
|
-
calls: [...new Set(sites.map((cs) => cs.name))],
|
|
110
|
-
callSites: sites,
|
|
111
|
-
hookCalls,
|
|
112
|
-
apiCalls,
|
|
113
|
-
dependencyNames,
|
|
114
|
-
contextRefs,
|
|
134
|
+
calls: [...new Set(acc.sites.filter((s) => s.kind !== "hook").map((cs) => cs.name))],
|
|
135
|
+
callSites: acc.sites,
|
|
136
|
+
hookCalls: acc.hookCalls,
|
|
137
|
+
apiCalls: acc.apiCalls,
|
|
138
|
+
dependencyNames: acc.dependencyNames,
|
|
139
|
+
contextRefs: acc.contextRefs,
|
|
115
140
|
};
|
|
116
141
|
}
|
|
142
|
+
export function extractCallsWithSites(node) {
|
|
143
|
+
const acc = createCallAccumulator({ owner: node });
|
|
144
|
+
for (const call of node.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
145
|
+
classifyCall(call, acc);
|
|
146
|
+
}
|
|
147
|
+
return finishCalls(acc);
|
|
148
|
+
}
|
|
117
149
|
export function extractFunctionCalls(node) {
|
|
118
150
|
return extractCallsWithSites(node).calls;
|
|
119
151
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SourceFile } from "ts-morph";
|
|
2
|
+
import { type ExtractedCallData } from "./functions.js";
|
|
3
|
+
/**
|
|
4
|
+
* Collect every call executed at module scope, classified exactly like calls
|
|
5
|
+
* inside a function (same builtin filtering, hook/api tracking, call sites).
|
|
6
|
+
* Returns EMPTY data for files with no module-scope calls.
|
|
7
|
+
*/
|
|
8
|
+
export declare function extractModuleScopeCalls(file: SourceFile): ExtractedCallData;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
import { classifyCall, createCallAccumulator, finishCalls, } from "./functions.js";
|
|
3
|
+
// ─── Module-scope call extraction ─────────────────────────────────────────────
|
|
4
|
+
//
|
|
5
|
+
// A FILE node represents the module itself. Calls executed at module scope
|
|
6
|
+
// (`const cleanup = startAll()`, `const app = express()`, top-level `if`
|
|
7
|
+
// blocks) belong to that FILE node, not to any child function. Entry files
|
|
8
|
+
// (main.ts, server.ts, workers.ts) do most of their work here, so without this
|
|
9
|
+
// pass the graph shows them importing things but never calling anything.
|
|
10
|
+
//
|
|
11
|
+
// Scope rule: a call is module scope iff its nearest function-like/class
|
|
12
|
+
// ancestor is absent. Everything nested in a function body, arrow initializer,
|
|
13
|
+
// class field, or static block belongs to that owner and is extracted by the
|
|
14
|
+
// regular per-declaration extractors.
|
|
15
|
+
const NON_MODULE_ANCESTORS = new Set([
|
|
16
|
+
SyntaxKind.FunctionDeclaration,
|
|
17
|
+
SyntaxKind.FunctionExpression,
|
|
18
|
+
SyntaxKind.ArrowFunction,
|
|
19
|
+
SyntaxKind.MethodDeclaration,
|
|
20
|
+
SyntaxKind.Constructor,
|
|
21
|
+
SyntaxKind.GetAccessor,
|
|
22
|
+
SyntaxKind.SetAccessor,
|
|
23
|
+
SyntaxKind.ClassDeclaration,
|
|
24
|
+
SyntaxKind.ClassExpression,
|
|
25
|
+
SyntaxKind.ClassStaticBlockDeclaration,
|
|
26
|
+
]);
|
|
27
|
+
// Top-level statements that cannot contain module-scope calls. Skipping them
|
|
28
|
+
// before descending is the perf win: a 3k-line file of function declarations
|
|
29
|
+
// costs nothing here (correctness is still guaranteed by the ancestor guard).
|
|
30
|
+
const SKIP_TOP_LEVEL = new Set([
|
|
31
|
+
SyntaxKind.FunctionDeclaration,
|
|
32
|
+
SyntaxKind.ClassDeclaration,
|
|
33
|
+
SyntaxKind.InterfaceDeclaration,
|
|
34
|
+
SyntaxKind.TypeAliasDeclaration,
|
|
35
|
+
SyntaxKind.EnumDeclaration,
|
|
36
|
+
SyntaxKind.ModuleDeclaration,
|
|
37
|
+
SyntaxKind.ImportDeclaration,
|
|
38
|
+
SyntaxKind.ImportEqualsDeclaration,
|
|
39
|
+
SyntaxKind.ExportDeclaration,
|
|
40
|
+
]);
|
|
41
|
+
/**
|
|
42
|
+
* Collect every call executed at module scope, classified exactly like calls
|
|
43
|
+
* inside a function (same builtin filtering, hook/api tracking, call sites).
|
|
44
|
+
* Returns EMPTY data for files with no module-scope calls.
|
|
45
|
+
*/
|
|
46
|
+
export function extractModuleScopeCalls(file) {
|
|
47
|
+
const acc = createCallAccumulator({ owner: file, isFileOwner: true });
|
|
48
|
+
for (const stmt of file.getStatements()) {
|
|
49
|
+
if (SKIP_TOP_LEVEL.has(stmt.getKind()))
|
|
50
|
+
continue;
|
|
51
|
+
for (const call of stmt.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
52
|
+
if (isOwnedByDeclaration(call))
|
|
53
|
+
continue;
|
|
54
|
+
// Dynamic import() is a CallExpression whose callee is ImportKeyword —
|
|
55
|
+
// not a function call; recording it as `import(...)` is pure noise.
|
|
56
|
+
if (call.getExpression().getKind() === SyntaxKind.ImportKeyword)
|
|
57
|
+
continue;
|
|
58
|
+
// IIFE: the callee is an inline function expression, so the "name" would
|
|
59
|
+
// be the whole function body text. Its body is already excluded by the
|
|
60
|
+
// ancestor rule; the call itself carries no resolvable name.
|
|
61
|
+
if (isIifeCallee(call.getExpression()))
|
|
62
|
+
continue;
|
|
63
|
+
classifyCall(call, acc);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return finishCalls(acc);
|
|
67
|
+
}
|
|
68
|
+
function isOwnedByDeclaration(call) {
|
|
69
|
+
return Boolean(call.getFirstAncestor((ancestor) => NON_MODULE_ANCESTORS.has(ancestor.getKind())));
|
|
70
|
+
}
|
|
71
|
+
// `(async () => { ... })()` / `(function () { ... })()` — unwrap parentheses
|
|
72
|
+
// and detect a function-expression callee.
|
|
73
|
+
function isIifeCallee(callee) {
|
|
74
|
+
let expr = callee;
|
|
75
|
+
while (expr.getKind() === SyntaxKind.ParenthesizedExpression) {
|
|
76
|
+
expr = expr.getExpression();
|
|
77
|
+
}
|
|
78
|
+
return (expr.getKind() === SyntaxKind.ArrowFunction ||
|
|
79
|
+
expr.getKind() === SyntaxKind.FunctionExpression);
|
|
80
|
+
}
|
package/dist/parser/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import { extractFunctions } from "./extractors/functions.js";
|
|
|
7
7
|
import { extractStores } from "./extractors/stores.js";
|
|
8
8
|
import { extractObjectMethods } from "./extractors/objectMethods.js";
|
|
9
9
|
import { extractClasses } from "./extractors/classes.js";
|
|
10
|
+
import { extractModuleScopeCalls } from "./extractors/moduleScope.js";
|
|
10
11
|
import { detectFileDirective } from "./directives.js";
|
|
11
12
|
import { applyOverloadDisambiguation } from "./overloads.js";
|
|
12
13
|
import { buildExportsMap, validateExportsMap, exportsMapStats } from "./exportsMap.js";
|
|
@@ -107,6 +108,9 @@ export function parseRepo(repoPath) {
|
|
|
107
108
|
},
|
|
108
109
|
};
|
|
109
110
|
const fileDirective = detectFileDirective(file);
|
|
111
|
+
// Module scope — calls executed at file load belong to the FILE node.
|
|
112
|
+
// TEST/STORY files are leaves and never carry them (same rule as children).
|
|
113
|
+
const moduleScope = fileType === "FILE" ? extractModuleScopeCalls(file) : undefined;
|
|
110
114
|
const components = extractComponents(file, fileDirective);
|
|
111
115
|
const hooks = extractHooks(file, fileDirective);
|
|
112
116
|
const functions = extractFunctions(file, fileDirective);
|
|
@@ -145,6 +149,14 @@ export function parseRepo(repoPath) {
|
|
|
145
149
|
else {
|
|
146
150
|
fileNode.metadata.nodeCount = overloads.length;
|
|
147
151
|
fileNode.metadata.childNodeIds = overloads.map(n => n.id);
|
|
152
|
+
// Module-scope call facts — only when non-empty so files with no
|
|
153
|
+
// top-level work don't gain four meaningless metadata keys.
|
|
154
|
+
if (moduleScope && moduleScope.callSites.length > 0) {
|
|
155
|
+
fileNode.metadata.calls = moduleScope.calls;
|
|
156
|
+
fileNode.metadata.callSites = moduleScope.callSites;
|
|
157
|
+
fileNode.metadata.hookCalls = moduleScope.hookCalls;
|
|
158
|
+
fileNode.metadata.apiCalls = moduleScope.apiCalls;
|
|
159
|
+
}
|
|
148
160
|
// File node hash — based on all child code combined
|
|
149
161
|
const fileRawCode = overloads.map(n => n.rawCode ?? "").join("\n");
|
|
150
162
|
if (fileRawCode.trim()) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import { Project } from "ts-morph";
|
|
5
|
+
import { extractModuleScopeCalls } from "./extractors/moduleScope.js";
|
|
6
|
+
import { parseRepo } from "./index.js";
|
|
7
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
8
|
+
function parse(source) {
|
|
9
|
+
const project = new Project({ useInMemoryFileSystem: true });
|
|
10
|
+
const file = project.createSourceFile("main.ts", source);
|
|
11
|
+
return extractModuleScopeCalls(file);
|
|
12
|
+
}
|
|
13
|
+
function createFakeRepo(files) {
|
|
14
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "devlens-module-scope-test-"));
|
|
15
|
+
for (const [filePath, content] of Object.entries(files)) {
|
|
16
|
+
const fullPath = path.join(tmpDir, filePath);
|
|
17
|
+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
18
|
+
fs.writeFileSync(fullPath, content);
|
|
19
|
+
}
|
|
20
|
+
return tmpDir;
|
|
21
|
+
}
|
|
22
|
+
function deleteFakeRepo(repoPath) {
|
|
23
|
+
fs.rmSync(repoPath, { recursive: true, force: true });
|
|
24
|
+
}
|
|
25
|
+
// ─── Scope rule ───────────────────────────────────────────────────────────────
|
|
26
|
+
describe("extractModuleScopeCalls — scope rule", () => {
|
|
27
|
+
it("collects a call in a top-level variable initializer", () => {
|
|
28
|
+
const data = parse(`
|
|
29
|
+
import { startAll } from "./lib/startAll";
|
|
30
|
+
const cleanup = startAll();
|
|
31
|
+
`);
|
|
32
|
+
expect(data.calls).toEqual(["startAll"]);
|
|
33
|
+
expect(data.callSites).toEqual([
|
|
34
|
+
expect.objectContaining({ name: "startAll", argCount: 0, order: 1, scope: "module", line: 3 }),
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
it("collects calls inside a top-level if block", () => {
|
|
38
|
+
const data = parse(`
|
|
39
|
+
if (process.env.NODE_ENV === "production") {
|
|
40
|
+
startAll();
|
|
41
|
+
}
|
|
42
|
+
`);
|
|
43
|
+
expect(data.calls).toEqual(["startAll"]);
|
|
44
|
+
});
|
|
45
|
+
it("collects exported top-level initializers", () => {
|
|
46
|
+
const data = parse(`export const app = createApp();`);
|
|
47
|
+
expect(data.calls).toEqual(["createApp"]);
|
|
48
|
+
});
|
|
49
|
+
it("does NOT collect calls inside a function body", () => {
|
|
50
|
+
const data = parse(`
|
|
51
|
+
function boot() {
|
|
52
|
+
startAll();
|
|
53
|
+
}
|
|
54
|
+
`);
|
|
55
|
+
expect(data.calls).toEqual([]);
|
|
56
|
+
});
|
|
57
|
+
it("does NOT collect calls inside an arrow-function initializer", () => {
|
|
58
|
+
const data = parse(`const boot = () => startAll();`);
|
|
59
|
+
expect(data.calls).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
it("does NOT collect calls inside a class method", () => {
|
|
62
|
+
const data = parse(`
|
|
63
|
+
class Boot {
|
|
64
|
+
run() { startAll(); }
|
|
65
|
+
}
|
|
66
|
+
`);
|
|
67
|
+
expect(data.calls).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
it("does NOT collect calls inside a class field initializer", () => {
|
|
70
|
+
const data = parse(`
|
|
71
|
+
class Boot {
|
|
72
|
+
cleanup = startAll();
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
expect(data.calls).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
it("does NOT collect calls inside a class static block", () => {
|
|
78
|
+
const data = parse(`
|
|
79
|
+
class Boot {
|
|
80
|
+
static { startAll(); }
|
|
81
|
+
}
|
|
82
|
+
`);
|
|
83
|
+
expect(data.calls).toEqual([]);
|
|
84
|
+
});
|
|
85
|
+
it("does NOT collect an IIFE body (the IIFE is a function scope)", () => {
|
|
86
|
+
const data = parse(`
|
|
87
|
+
(async () => { startAll(); })();
|
|
88
|
+
`);
|
|
89
|
+
expect(data.calls).toEqual([]);
|
|
90
|
+
});
|
|
91
|
+
it("does NOT collect dynamic import()", () => {
|
|
92
|
+
const data = parse(`const mod = await import("./lib/startAll");`);
|
|
93
|
+
expect(data.calls).toEqual([]);
|
|
94
|
+
});
|
|
95
|
+
it("returns empty data for a file with only declarations", () => {
|
|
96
|
+
const data = parse(`export function a() {} export class B {}`);
|
|
97
|
+
expect(data.calls).toEqual([]);
|
|
98
|
+
expect(data.callSites).toEqual([]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
// ─── Builtin noise (same rule as function bodies) ─────────────────────────────
|
|
102
|
+
describe("extractModuleScopeCalls — builtin filtering", () => {
|
|
103
|
+
it("drops console.* and Math.* at module scope", () => {
|
|
104
|
+
const data = parse(`
|
|
105
|
+
console.log("booting");
|
|
106
|
+
const n = Math.min(1, 2);
|
|
107
|
+
startAll();
|
|
108
|
+
`);
|
|
109
|
+
expect(data.calls).toEqual(["startAll"]);
|
|
110
|
+
});
|
|
111
|
+
it("drops builtin member calls but keeps user calls with the same shape", () => {
|
|
112
|
+
const data = parse(`
|
|
113
|
+
Object.keys({});
|
|
114
|
+
JSON.stringify({});
|
|
115
|
+
app.listen(3000);
|
|
116
|
+
`);
|
|
117
|
+
expect(data.calls).toEqual(["app.listen"]);
|
|
118
|
+
});
|
|
119
|
+
it("keeps use* names in hookCalls but not in calls (hook rule)", () => {
|
|
120
|
+
const data = parse(`
|
|
121
|
+
const theme = useTheme();
|
|
122
|
+
startAll();
|
|
123
|
+
`);
|
|
124
|
+
expect(data.calls).toEqual(["startAll"]);
|
|
125
|
+
expect(data.hookCalls).toEqual(["useTheme"]);
|
|
126
|
+
});
|
|
127
|
+
it("captures fetch at module scope as an api call", () => {
|
|
128
|
+
const data = parse(`const cfg = fetch("/config");`);
|
|
129
|
+
expect(data.apiCalls).toEqual(['fetch("/config")']);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
// ─── parseRepo wiring ─────────────────────────────────────────────────────────
|
|
133
|
+
describe("parseRepo — module-scope metadata on FILE nodes", () => {
|
|
134
|
+
it("attaches calls/callSites to the FILE node", () => {
|
|
135
|
+
const repo = createFakeRepo({
|
|
136
|
+
"lib/startAll.ts": `export function startAll() { return () => {}; }`,
|
|
137
|
+
"main.ts": `
|
|
138
|
+
import { startAll } from "./lib/startAll";
|
|
139
|
+
const cleanup = startAll();
|
|
140
|
+
process.on("SIGTERM", cleanup);
|
|
141
|
+
`,
|
|
142
|
+
});
|
|
143
|
+
const { nodes } = parseRepo(repo);
|
|
144
|
+
const file = nodes.find((n) => n.id === "file::main.ts");
|
|
145
|
+
expect(file.type).toBe("FILE");
|
|
146
|
+
expect(file.metadata.calls).toEqual(["startAll", "process.on"]);
|
|
147
|
+
expect(file.metadata.callSites).toEqual([
|
|
148
|
+
expect.objectContaining({ name: "startAll", argCount: 0, order: 1, scope: "module" }),
|
|
149
|
+
expect.objectContaining({ name: "process.on", argCount: 2, argTypes: ["string", "unknown"], order: 2, scope: "module" }),
|
|
150
|
+
]);
|
|
151
|
+
deleteFakeRepo(repo);
|
|
152
|
+
});
|
|
153
|
+
it("does not attach module-scope keys to files with no top-level calls", () => {
|
|
154
|
+
const repo = createFakeRepo({
|
|
155
|
+
"lib/startAll.ts": `export function startAll() { return 1; }`,
|
|
156
|
+
});
|
|
157
|
+
const { nodes } = parseRepo(repo);
|
|
158
|
+
const file = nodes.find((n) => n.id === "file::lib/startAll.ts");
|
|
159
|
+
expect(file.metadata.calls).toBeUndefined();
|
|
160
|
+
expect(file.metadata.callSites).toBeUndefined();
|
|
161
|
+
deleteFakeRepo(repo);
|
|
162
|
+
});
|
|
163
|
+
it("does not attach module-scope facts to TEST file nodes", () => {
|
|
164
|
+
const repo = createFakeRepo({
|
|
165
|
+
"main.test.ts": `
|
|
166
|
+
import { startAll } from "./lib/startAll";
|
|
167
|
+
const cleanup = startAll();
|
|
168
|
+
`,
|
|
169
|
+
});
|
|
170
|
+
const { nodes } = parseRepo(repo);
|
|
171
|
+
const file = nodes.find((n) => n.id === "file::main.test.ts");
|
|
172
|
+
expect(file.type).toBe("TEST");
|
|
173
|
+
expect(file.metadata.calls).toBeUndefined();
|
|
174
|
+
deleteFakeRepo(repo);
|
|
175
|
+
});
|
|
176
|
+
});
|