seance-cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/commands/init.d.ts +29 -0
- package/dist/commands/init.js +310 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/mcp.d.ts +19 -0
- package/dist/commands/mcp.js +38 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/quiz.d.ts +23 -0
- package/dist/commands/quiz.js +187 -0
- package/dist/commands/quiz.js.map +1 -0
- package/dist/commands/review.d.ts +4 -0
- package/dist/commands/review.js +115 -0
- package/dist/commands/review.js.map +1 -0
- package/dist/commands/sync.d.ts +17 -0
- package/dist/commands/sync.js +367 -0
- package/dist/commands/sync.js.map +1 -0
- package/dist/git.d.ts +33 -0
- package/dist/git.js +103 -0
- package/dist/git.js.map +1 -0
- package/dist/hosted-api.d.ts +24 -0
- package/dist/hosted-api.js +85 -0
- package/dist/hosted-api.js.map +1 -0
- package/dist/hosted-consent.d.ts +8 -0
- package/dist/hosted-consent.js +37 -0
- package/dist/hosted-consent.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/dist/init-fs.d.ts +51 -0
- package/dist/init-fs.js +220 -0
- package/dist/init-fs.js.map +1 -0
- package/dist/init-questions.d.ts +21 -0
- package/dist/init-questions.js +161 -0
- package/dist/init-questions.js.map +1 -0
- package/dist/mcp-handlers.d.ts +22 -0
- package/dist/mcp-handlers.js +185 -0
- package/dist/mcp-handlers.js.map +1 -0
- package/dist/mcp-server.d.ts +4 -0
- package/dist/mcp-server.js +39 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/prompter.d.ts +29 -0
- package/dist/prompter.js +42 -0
- package/dist/prompter.js.map +1 -0
- package/dist/quiz-backend.d.ts +40 -0
- package/dist/quiz-backend.js +116 -0
- package/dist/quiz-backend.js.map +1 -0
- package/dist/quiz-input.d.ts +12 -0
- package/dist/quiz-input.js +52 -0
- package/dist/quiz-input.js.map +1 -0
- package/dist/quiz-io.d.ts +30 -0
- package/dist/quiz-io.js +83 -0
- package/dist/quiz-io.js.map +1 -0
- package/dist/quiz-ui.d.ts +18 -0
- package/dist/quiz-ui.js +51 -0
- package/dist/quiz-ui.js.map +1 -0
- package/node_modules/@seance/ai/LICENSE +21 -0
- package/node_modules/@seance/ai/dist/chain.d.ts +31 -0
- package/node_modules/@seance/ai/dist/chain.js +114 -0
- package/node_modules/@seance/ai/dist/chain.js.map +1 -0
- package/node_modules/@seance/ai/dist/grade.d.ts +14 -0
- package/node_modules/@seance/ai/dist/grade.js +44 -0
- package/node_modules/@seance/ai/dist/grade.js.map +1 -0
- package/node_modules/@seance/ai/dist/hosted-config.d.ts +9 -0
- package/node_modules/@seance/ai/dist/hosted-config.js +27 -0
- package/node_modules/@seance/ai/dist/hosted-config.js.map +1 -0
- package/node_modules/@seance/ai/dist/index.d.ts +9 -0
- package/node_modules/@seance/ai/dist/index.js +9 -0
- package/node_modules/@seance/ai/dist/index.js.map +1 -0
- package/node_modules/@seance/ai/dist/providers.d.ts +54 -0
- package/node_modules/@seance/ai/dist/providers.js +149 -0
- package/node_modules/@seance/ai/dist/providers.js.map +1 -0
- package/node_modules/@seance/ai/dist/quiz.d.ts +26 -0
- package/node_modules/@seance/ai/dist/quiz.js +41 -0
- package/node_modules/@seance/ai/dist/quiz.js.map +1 -0
- package/node_modules/@seance/ai/package.json +25 -0
- package/node_modules/@seance/core/LICENSE +21 -0
- package/node_modules/@seance/core/dist/ai-check.d.ts +58 -0
- package/node_modules/@seance/core/dist/ai-check.js +460 -0
- package/node_modules/@seance/core/dist/ai-check.js.map +1 -0
- package/node_modules/@seance/core/dist/ai-prompts.d.ts +29 -0
- package/node_modules/@seance/core/dist/ai-prompts.js +221 -0
- package/node_modules/@seance/core/dist/ai-prompts.js.map +1 -0
- package/node_modules/@seance/core/dist/ai-schemas.d.ts +155 -0
- package/node_modules/@seance/core/dist/ai-schemas.js +92 -0
- package/node_modules/@seance/core/dist/ai-schemas.js.map +1 -0
- package/node_modules/@seance/core/dist/api-client.d.ts +26 -0
- package/node_modules/@seance/core/dist/api-client.js +71 -0
- package/node_modules/@seance/core/dist/api-client.js.map +1 -0
- package/node_modules/@seance/core/dist/api-errors.d.ts +37 -0
- package/node_modules/@seance/core/dist/api-errors.js +66 -0
- package/node_modules/@seance/core/dist/api-errors.js.map +1 -0
- package/node_modules/@seance/core/dist/api-schemas.d.ts +284 -0
- package/node_modules/@seance/core/dist/api-schemas.js +67 -0
- package/node_modules/@seance/core/dist/api-schemas.js.map +1 -0
- package/node_modules/@seance/core/dist/decision-log.d.ts +73 -0
- package/node_modules/@seance/core/dist/decision-log.js +239 -0
- package/node_modules/@seance/core/dist/decision-log.js.map +1 -0
- package/node_modules/@seance/core/dist/dependency-guard.d.ts +20 -0
- package/node_modules/@seance/core/dist/dependency-guard.js +732 -0
- package/node_modules/@seance/core/dist/dependency-guard.js.map +1 -0
- package/node_modules/@seance/core/dist/gate.d.ts +11 -0
- package/node_modules/@seance/core/dist/gate.js +26 -0
- package/node_modules/@seance/core/dist/gate.js.map +1 -0
- package/node_modules/@seance/core/dist/git-parse.d.ts +11 -0
- package/node_modules/@seance/core/dist/git-parse.js +23 -0
- package/node_modules/@seance/core/dist/git-parse.js.map +1 -0
- package/node_modules/@seance/core/dist/index.d.ts +65 -0
- package/node_modules/@seance/core/dist/index.js +47 -0
- package/node_modules/@seance/core/dist/index.js.map +1 -0
- package/node_modules/@seance/core/dist/init-checklist.d.ts +153 -0
- package/node_modules/@seance/core/dist/init-checklist.js +200 -0
- package/node_modules/@seance/core/dist/init-checklist.js.map +1 -0
- package/node_modules/@seance/core/dist/init-detect.d.ts +9 -0
- package/node_modules/@seance/core/dist/init-detect.js +84 -0
- package/node_modules/@seance/core/dist/init-detect.js.map +1 -0
- package/node_modules/@seance/core/dist/init-templates.d.ts +36 -0
- package/node_modules/@seance/core/dist/init-templates.js +257 -0
- package/node_modules/@seance/core/dist/init-templates.js.map +1 -0
- package/node_modules/@seance/core/dist/map-basics.d.ts +13 -0
- package/node_modules/@seance/core/dist/map-basics.js +19 -0
- package/node_modules/@seance/core/dist/map-basics.js.map +1 -0
- package/node_modules/@seance/core/dist/map-summary.d.ts +5 -0
- package/node_modules/@seance/core/dist/map-summary.js +50 -0
- package/node_modules/@seance/core/dist/map-summary.js.map +1 -0
- package/node_modules/@seance/core/dist/markers.d.ts +51 -0
- package/node_modules/@seance/core/dist/markers.js +84 -0
- package/node_modules/@seance/core/dist/markers.js.map +1 -0
- package/node_modules/@seance/core/dist/mcp-quiz.d.ts +94 -0
- package/node_modules/@seance/core/dist/mcp-quiz.js +130 -0
- package/node_modules/@seance/core/dist/mcp-quiz.js.map +1 -0
- package/node_modules/@seance/core/dist/mcp-tools.d.ts +82 -0
- package/node_modules/@seance/core/dist/mcp-tools.js +136 -0
- package/node_modules/@seance/core/dist/mcp-tools.js.map +1 -0
- package/node_modules/@seance/core/dist/mcp-views.d.ts +21 -0
- package/node_modules/@seance/core/dist/mcp-views.js +83 -0
- package/node_modules/@seance/core/dist/mcp-views.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-assembly.d.ts +21 -0
- package/node_modules/@seance/core/dist/quiz-assembly.js +43 -0
- package/node_modules/@seance/core/dist/quiz-assembly.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-cards.d.ts +183 -0
- package/node_modules/@seance/core/dist/quiz-cards.js +215 -0
- package/node_modules/@seance/core/dist/quiz-cards.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-session.d.ts +67 -0
- package/node_modules/@seance/core/dist/quiz-session.js +141 -0
- package/node_modules/@seance/core/dist/quiz-session.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-subjects.d.ts +13 -0
- package/node_modules/@seance/core/dist/quiz-subjects.js +63 -0
- package/node_modules/@seance/core/dist/quiz-subjects.js.map +1 -0
- package/node_modules/@seance/core/dist/rate-limit.d.ts +33 -0
- package/node_modules/@seance/core/dist/rate-limit.js +114 -0
- package/node_modules/@seance/core/dist/rate-limit.js.map +1 -0
- package/node_modules/@seance/core/dist/scoring.d.ts +48 -0
- package/node_modules/@seance/core/dist/scoring.js +95 -0
- package/node_modules/@seance/core/dist/scoring.js.map +1 -0
- package/node_modules/@seance/core/dist/seance-config.d.ts +177 -0
- package/node_modules/@seance/core/dist/seance-config.js +43 -0
- package/node_modules/@seance/core/dist/seance-config.js.map +1 -0
- package/node_modules/@seance/core/dist/seance-state.d.ts +125 -0
- package/node_modules/@seance/core/dist/seance-state.js +61 -0
- package/node_modules/@seance/core/dist/seance-state.js.map +1 -0
- package/node_modules/@seance/core/dist/sm2.d.ts +18 -0
- package/node_modules/@seance/core/dist/sm2.js +87 -0
- package/node_modules/@seance/core/dist/sm2.js.map +1 -0
- package/node_modules/@seance/core/dist/stored-schemas.d.ts +29 -0
- package/node_modules/@seance/core/dist/stored-schemas.js +29 -0
- package/node_modules/@seance/core/dist/stored-schemas.js.map +1 -0
- package/node_modules/@seance/core/dist/structural-questions.d.ts +31 -0
- package/node_modules/@seance/core/dist/structural-questions.js +575 -0
- package/node_modules/@seance/core/dist/structural-questions.js.map +1 -0
- package/node_modules/@seance/core/dist/structure-map-schema.d.ts +84 -0
- package/node_modules/@seance/core/dist/structure-map-schema.js +73 -0
- package/node_modules/@seance/core/dist/structure-map-schema.js.map +1 -0
- package/node_modules/@seance/core/dist/structure-map.d.ts +78 -0
- package/node_modules/@seance/core/dist/structure-map.js +750 -0
- package/node_modules/@seance/core/dist/structure-map.js.map +1 -0
- package/node_modules/@seance/core/dist/sync-changes.d.ts +38 -0
- package/node_modules/@seance/core/dist/sync-changes.js +89 -0
- package/node_modules/@seance/core/dist/sync-changes.js.map +1 -0
- package/node_modules/@seance/core/dist/sync-render.d.ts +28 -0
- package/node_modules/@seance/core/dist/sync-render.js +113 -0
- package/node_modules/@seance/core/dist/sync-render.js.map +1 -0
- package/node_modules/@seance/core/dist/terminal-text.d.ts +8 -0
- package/node_modules/@seance/core/dist/terminal-text.js +33 -0
- package/node_modules/@seance/core/dist/terminal-text.js.map +1 -0
- package/node_modules/@seance/core/dist/write-plan.d.ts +51 -0
- package/node_modules/@seance/core/dist/write-plan.js +173 -0
- package/node_modules/@seance/core/dist/write-plan.js.map +1 -0
- package/node_modules/@seance/core/package.json +26 -0
- package/package.json +42 -6
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
// Structure map: reads JS/TS source text with Babel and lists each file's imports, exports,
|
|
2
|
+
// functions, classes and calls. Pure function: no disk, clock, network or randomness.
|
|
3
|
+
import { parse } from "@babel/parser";
|
|
4
|
+
import { VISITOR_KEYS } from "@babel/types";
|
|
5
|
+
import { MAP_LIMITS, MAX_MENTIONED_NAMES, UNKNOWN_SUPERCLASS, normalisePath } from "./map-basics.js";
|
|
6
|
+
// Re-exported so existing imports from "./structure-map.js" keep working.
|
|
7
|
+
export { MAP_LIMITS, MAX_MENTIONED_NAMES, UNKNOWN_SUPERCLASS, normalisePath };
|
|
8
|
+
const MAX_RECORDED_ERRORS = 10;
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Small helpers
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
/** Compares two strings by UTF-16 code unit, so the order never depends on the locale. */
|
|
13
|
+
function compareText(a, b) {
|
|
14
|
+
if (a < b)
|
|
15
|
+
return -1;
|
|
16
|
+
return a > b ? 1 : 0;
|
|
17
|
+
}
|
|
18
|
+
/** Sorted copy without duplicates. */
|
|
19
|
+
function sortedUnique(items) {
|
|
20
|
+
return [...new Set(items)].sort(compareText);
|
|
21
|
+
}
|
|
22
|
+
function lineOf(node) {
|
|
23
|
+
return node.loc?.start.line ?? 0;
|
|
24
|
+
}
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Input checks (the only place that throws)
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
/** Checks the limits and returns the files with normalised paths. Throws RangeError. */
|
|
29
|
+
function checkInput(files) {
|
|
30
|
+
if (!Array.isArray(files))
|
|
31
|
+
throw new RangeError("files must be an array");
|
|
32
|
+
if (files.length > MAP_LIMITS.maxFiles) {
|
|
33
|
+
throw new RangeError(`got ${files.length} files, limit ${MAP_LIMITS.maxFiles}`);
|
|
34
|
+
}
|
|
35
|
+
const encoder = new TextEncoder();
|
|
36
|
+
const seen = new Set();
|
|
37
|
+
let totalChars = 0;
|
|
38
|
+
let totalBytes = 0;
|
|
39
|
+
const result = [];
|
|
40
|
+
for (const file of files) {
|
|
41
|
+
if (typeof file?.path !== "string" || file.path === "") {
|
|
42
|
+
throw new RangeError("every file needs a non-empty string path");
|
|
43
|
+
}
|
|
44
|
+
if (typeof file.content !== "string") {
|
|
45
|
+
throw new RangeError(`content of ${file.path} must be a string`);
|
|
46
|
+
}
|
|
47
|
+
const path = normalisePath(file.path);
|
|
48
|
+
if (path === "")
|
|
49
|
+
throw new RangeError(`path ${file.path} is empty after normalising`);
|
|
50
|
+
if (seen.has(path))
|
|
51
|
+
throw new RangeError(`duplicate path after normalising: ${path}`);
|
|
52
|
+
seen.add(path);
|
|
53
|
+
// A string never has fewer UTF-8 bytes than characters, so reject before encoding anything huge.
|
|
54
|
+
totalChars += file.content.length;
|
|
55
|
+
if (totalChars > MAP_LIMITS.maxTotalBytes) {
|
|
56
|
+
throw new RangeError(`total content is at least ${totalChars} bytes, limit ${MAP_LIMITS.maxTotalBytes}`);
|
|
57
|
+
}
|
|
58
|
+
totalBytes += encoder.encode(file.content).length;
|
|
59
|
+
result.push({ path, content: file.content });
|
|
60
|
+
}
|
|
61
|
+
if (totalBytes > MAP_LIMITS.maxTotalBytes) {
|
|
62
|
+
throw new RangeError(`total content is ${totalBytes} bytes, limit ${MAP_LIMITS.maxTotalBytes}`);
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// File-level facts: language and line count
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
const LANGUAGE_BY_EXTENSION = new Map([
|
|
70
|
+
["js", "javascript"],
|
|
71
|
+
["jsx", "javascript"],
|
|
72
|
+
["mjs", "javascript"],
|
|
73
|
+
["cjs", "javascript"],
|
|
74
|
+
["ts", "typescript"],
|
|
75
|
+
["tsx", "typescript"],
|
|
76
|
+
["mts", "typescript"],
|
|
77
|
+
["cts", "typescript"],
|
|
78
|
+
["py", "python"],
|
|
79
|
+
["json", "json"],
|
|
80
|
+
["md", "markdown"],
|
|
81
|
+
["css", "css"],
|
|
82
|
+
["html", "html"],
|
|
83
|
+
]);
|
|
84
|
+
function extensionOf(path) {
|
|
85
|
+
const base = path.slice(path.lastIndexOf("/") + 1);
|
|
86
|
+
const dot = base.lastIndexOf(".");
|
|
87
|
+
return dot === -1 ? "" : base.slice(dot + 1).toLowerCase();
|
|
88
|
+
}
|
|
89
|
+
function languageOf(path) {
|
|
90
|
+
return LANGUAGE_BY_EXTENSION.get(extensionOf(path)) ?? "other";
|
|
91
|
+
}
|
|
92
|
+
/** Counts lines: `""` is 0, `"a\n"` is 1, `"a\nb"` is 2. Handles \r\n, \r and \n. */
|
|
93
|
+
function countLines(content) {
|
|
94
|
+
const pieces = content.split(/\r\n|\r|\n/);
|
|
95
|
+
if (pieces[pieces.length - 1] === "")
|
|
96
|
+
pieces.pop();
|
|
97
|
+
return pieces.length;
|
|
98
|
+
}
|
|
99
|
+
function emptyEntry(path, content) {
|
|
100
|
+
return {
|
|
101
|
+
path,
|
|
102
|
+
language: languageOf(path),
|
|
103
|
+
lines: countLines(content),
|
|
104
|
+
parsed: false,
|
|
105
|
+
errors: [],
|
|
106
|
+
imports: [],
|
|
107
|
+
exports: [],
|
|
108
|
+
functions: [],
|
|
109
|
+
classes: [],
|
|
110
|
+
commonjsExports: [],
|
|
111
|
+
hasUnknownExports: false,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Parsing
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
/**
|
|
118
|
+
* Babel plugins by file extension: TypeScript syntax for TS files, JSX for JS and TSX, and
|
|
119
|
+
* `decorators-legacy` for all of them (Angular and NestJS style `@Component({...}) export class`).
|
|
120
|
+
*/
|
|
121
|
+
function pluginsFor(path) {
|
|
122
|
+
const lower = path.toLowerCase();
|
|
123
|
+
if (lower.endsWith(".d.ts") || lower.endsWith(".d.mts") || lower.endsWith(".d.cts")) {
|
|
124
|
+
return [["typescript", { dts: true }], "decorators-legacy"];
|
|
125
|
+
}
|
|
126
|
+
const ext = extensionOf(path);
|
|
127
|
+
if (ext === "tsx")
|
|
128
|
+
return ["typescript", "jsx", "decorators-legacy"];
|
|
129
|
+
if (ext === "ts" || ext === "mts" || ext === "cts")
|
|
130
|
+
return ["typescript", "decorators-legacy"];
|
|
131
|
+
return ["jsx", "decorators-legacy"];
|
|
132
|
+
}
|
|
133
|
+
const STACK_OVERFLOW_TEXT = "Maximum call stack size exceeded";
|
|
134
|
+
/** Turns anything that was thrown or recorded by Babel into a ParseIssue. */
|
|
135
|
+
function toIssue(error) {
|
|
136
|
+
let message = "unknown parse error";
|
|
137
|
+
let line = 0;
|
|
138
|
+
try {
|
|
139
|
+
if (error instanceof Error)
|
|
140
|
+
message = error.message;
|
|
141
|
+
else
|
|
142
|
+
message = String(error);
|
|
143
|
+
// Babel is recursive and runs out of stack at about 500 nested levels; say so in plain words.
|
|
144
|
+
if (error instanceof Error && message.includes(STACK_OVERFLOW_TEXT)) {
|
|
145
|
+
return { message: "nested too deeply to parse", line: 0 };
|
|
146
|
+
}
|
|
147
|
+
const loc = error?.loc;
|
|
148
|
+
if (typeof loc?.line === "number")
|
|
149
|
+
line = loc.line;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// An object that cannot be turned into text keeps the default message.
|
|
153
|
+
}
|
|
154
|
+
return { message, line };
|
|
155
|
+
}
|
|
156
|
+
/** One parse attempt. Catches anything that is thrown. */
|
|
157
|
+
function tryParse(path, content, sourceType) {
|
|
158
|
+
try {
|
|
159
|
+
const ast = parse(content, { sourceType, errorRecovery: true, plugins: pluginsFor(path) });
|
|
160
|
+
return { ok: true, ast, errorCount: ast.errors?.length ?? 0 };
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
return { ok: false, issue: toIssue(error) };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Parses as a module first. When that throws or records errors, also parses as a script and keeps
|
|
168
|
+
* the result with fewer errors (a tie keeps the module). Old CommonJS code with `with` or legacy
|
|
169
|
+
* octal numbers only parses cleanly as a script. If both attempts throw, the module issue is reported.
|
|
170
|
+
*/
|
|
171
|
+
function parseSource(path, content) {
|
|
172
|
+
const asModule = tryParse(path, content, "module");
|
|
173
|
+
if (asModule.ok && asModule.errorCount === 0)
|
|
174
|
+
return asModule;
|
|
175
|
+
const asScript = tryParse(path, content, "script");
|
|
176
|
+
if (!asScript.ok)
|
|
177
|
+
return asModule; // the module attempt: errors recorded, or the module issue
|
|
178
|
+
if (!asModule.ok)
|
|
179
|
+
return asScript;
|
|
180
|
+
return asScript.errorCount < asModule.errorCount ? asScript : asModule;
|
|
181
|
+
}
|
|
182
|
+
// ---------------------------------------------------------------------------
|
|
183
|
+
// Call collection
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
function isNode(value) {
|
|
186
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
187
|
+
}
|
|
188
|
+
/** Adds the child nodes of `node` to the stack, using Babel's list of child keys. */
|
|
189
|
+
function pushChildren(stack, node) {
|
|
190
|
+
const keys = VISITOR_KEYS[node.type] ?? [];
|
|
191
|
+
for (const key of keys) {
|
|
192
|
+
const value = node[key];
|
|
193
|
+
if (Array.isArray(value)) {
|
|
194
|
+
for (const item of value)
|
|
195
|
+
if (isNode(item))
|
|
196
|
+
stack.push(item);
|
|
197
|
+
}
|
|
198
|
+
else if (isNode(value)) {
|
|
199
|
+
stack.push(value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/** `foo`, `obj.foo` or `this.foo`. Any other callee (`a.b.c()`, `fn()()`) gives undefined. */
|
|
204
|
+
function calleeName(callee) {
|
|
205
|
+
if (callee.type === "Identifier")
|
|
206
|
+
return callee.name;
|
|
207
|
+
if (callee.type !== "MemberExpression" && callee.type !== "OptionalMemberExpression")
|
|
208
|
+
return undefined;
|
|
209
|
+
if (callee.computed || callee.property.type !== "Identifier")
|
|
210
|
+
return undefined;
|
|
211
|
+
if (callee.object.type === "Identifier")
|
|
212
|
+
return `${callee.object.name}.${callee.property.name}`;
|
|
213
|
+
if (callee.object.type === "ThisExpression")
|
|
214
|
+
return `this.${callee.property.name}`;
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
/** Last name of any call: `foo()`, `a.b.foo()` and `a["foo"]()` give `foo`. Other callees give undefined. */
|
|
218
|
+
function lastCalledName(callee) {
|
|
219
|
+
if (callee.type === "Identifier")
|
|
220
|
+
return callee.name;
|
|
221
|
+
if (callee.type !== "MemberExpression" && callee.type !== "OptionalMemberExpression")
|
|
222
|
+
return undefined;
|
|
223
|
+
const property = callee.property;
|
|
224
|
+
if (!callee.computed)
|
|
225
|
+
return property.type === "Identifier" ? property.name : undefined;
|
|
226
|
+
return property.type === "StringLiteral" ? property.value : undefined;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Lists what a function does with names, sorted and without duplicates: `calls` in the short forms
|
|
230
|
+
* of req 1.9, `calledNames` with the last name of every call, and `mentionedNames` with every
|
|
231
|
+
* identifier it contains (so `helper.call(null)`, `map(helper)` and `const h = helper` all count).
|
|
232
|
+
* It walks the parameters as well as the body, so a default value like `a = helper()` is seen.
|
|
233
|
+
* It uses its own stack instead of recursion, so a deeply nested body cannot overflow our own code,
|
|
234
|
+
* and Sets, so a long function costs one pass.
|
|
235
|
+
*/
|
|
236
|
+
function collectUsage(fn) {
|
|
237
|
+
const calls = new Set();
|
|
238
|
+
const calledNames = new Set();
|
|
239
|
+
const mentioned = new Set();
|
|
240
|
+
const stack = [...fn.params, fn.body];
|
|
241
|
+
for (let node = stack.pop(); node !== undefined; node = stack.pop()) {
|
|
242
|
+
if (node.type === "CallExpression" || node.type === "OptionalCallExpression") {
|
|
243
|
+
const name = calleeName(node.callee);
|
|
244
|
+
if (name !== undefined)
|
|
245
|
+
calls.add(name);
|
|
246
|
+
const last = lastCalledName(node.callee);
|
|
247
|
+
if (last !== undefined)
|
|
248
|
+
calledNames.add(last);
|
|
249
|
+
}
|
|
250
|
+
else if (node.type === "Identifier" || node.type === "JSXIdentifier") {
|
|
251
|
+
mentioned.add(node.name);
|
|
252
|
+
}
|
|
253
|
+
else if ((node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && node.computed) {
|
|
254
|
+
// `obj["helper"]` mentions `helper` with a string instead of an identifier.
|
|
255
|
+
if (node.property.type === "StringLiteral")
|
|
256
|
+
mentioned.add(node.property.value);
|
|
257
|
+
}
|
|
258
|
+
pushChildren(stack, node);
|
|
259
|
+
}
|
|
260
|
+
return {
|
|
261
|
+
calls: sortedUnique(calls),
|
|
262
|
+
calledNames: sortedUnique(calledNames),
|
|
263
|
+
mentionedNames: sortedUnique(mentioned).slice(0, MAX_MENTIONED_NAMES),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
/** Name of an identifier or string literal (`export { a as "b-c" }`). */
|
|
267
|
+
function nameOf(node) {
|
|
268
|
+
return node.type === "Identifier" ? node.name : node.value;
|
|
269
|
+
}
|
|
270
|
+
/** Parameters without a TypeScript `this` parameter, which does not count. */
|
|
271
|
+
function realParams(params) {
|
|
272
|
+
return params.filter((p) => !(p.type === "Identifier" && p.name === "this"));
|
|
273
|
+
}
|
|
274
|
+
/** What the map records about the parameters of a function. */
|
|
275
|
+
function readParams(params) {
|
|
276
|
+
const real = realParams(params);
|
|
277
|
+
return { params: real.length, plainParams: real.every((p) => p.type === "Identifier") };
|
|
278
|
+
}
|
|
279
|
+
/** Records an export. The same (name, kind, from) is kept once, with the lowest line. */
|
|
280
|
+
function addExport(c, name, kind, node, from) {
|
|
281
|
+
const key = `${kind}|${from ?? ""}|${name}`;
|
|
282
|
+
const line = lineOf(node);
|
|
283
|
+
const existing = c.exports.get(key);
|
|
284
|
+
if (existing) {
|
|
285
|
+
existing.line = Math.min(existing.line, line);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const entry = { name, kind, line };
|
|
289
|
+
if (from !== undefined)
|
|
290
|
+
entry.from = from;
|
|
291
|
+
c.exports.set(key, entry);
|
|
292
|
+
}
|
|
293
|
+
function addFunction(c, name, fn, exported, lineNode) {
|
|
294
|
+
c.functions.push({
|
|
295
|
+
name,
|
|
296
|
+
...readParams(fn.params),
|
|
297
|
+
async: fn.async === true,
|
|
298
|
+
exported,
|
|
299
|
+
line: lineOf(lineNode),
|
|
300
|
+
...collectUsage(fn),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/** Method name from a class member key; computed and private names are skipped. */
|
|
304
|
+
function memberName(key, computed) {
|
|
305
|
+
if (computed)
|
|
306
|
+
return undefined;
|
|
307
|
+
if (key.type === "Identifier")
|
|
308
|
+
return key.name;
|
|
309
|
+
if (key.type === "StringLiteral")
|
|
310
|
+
return key.value;
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
function isFunctionExpression(node) {
|
|
314
|
+
return node?.type === "FunctionExpression" || node?.type === "ArrowFunctionExpression";
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Class methods and arrow-function properties, plus the names of getters and setters.
|
|
318
|
+
* Constructors are skipped; getters and setters go into `accessors`, not `methods`.
|
|
319
|
+
*/
|
|
320
|
+
function readMembers(body) {
|
|
321
|
+
const methods = [];
|
|
322
|
+
const accessors = new Set();
|
|
323
|
+
const add = (member, fn) => {
|
|
324
|
+
const name = memberName(member.key, member.computed === true);
|
|
325
|
+
if (name === undefined)
|
|
326
|
+
return;
|
|
327
|
+
methods.push({
|
|
328
|
+
name,
|
|
329
|
+
...readParams(fn.params),
|
|
330
|
+
async: fn.async === true,
|
|
331
|
+
static: member.static === true,
|
|
332
|
+
line: lineOf(member),
|
|
333
|
+
...collectUsage(fn),
|
|
334
|
+
});
|
|
335
|
+
};
|
|
336
|
+
for (const member of body.body) {
|
|
337
|
+
if (member.type === "ClassMethod" && member.kind === "method") {
|
|
338
|
+
add(member, member);
|
|
339
|
+
}
|
|
340
|
+
else if (member.type === "ClassMethod" && (member.kind === "get" || member.kind === "set")) {
|
|
341
|
+
const name = memberName(member.key, member.computed === true);
|
|
342
|
+
if (name !== undefined)
|
|
343
|
+
accessors.add(name);
|
|
344
|
+
}
|
|
345
|
+
else if (member.type === "ClassProperty" && isFunctionExpression(member.value)) {
|
|
346
|
+
add(member, member.value);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
methods: sortWith(methods, byText((m) => m.name), byNumber((m) => Number(m.static)), byNumber((m) => m.line)),
|
|
351
|
+
accessors: sortedUnique(accessors),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Text of an `extends` expression: `Base`, or `React.Component` for a chain of plain members.
|
|
356
|
+
* Anything else (`mixin(Base)`, `a[b]`) gives UNKNOWN_SUPERCLASS, which never equals a class name.
|
|
357
|
+
*/
|
|
358
|
+
function superClassText(node) {
|
|
359
|
+
const parts = [];
|
|
360
|
+
let current = node;
|
|
361
|
+
while (current.type === "MemberExpression" && !current.computed && current.property.type === "Identifier") {
|
|
362
|
+
parts.push(current.property.name);
|
|
363
|
+
current = current.object;
|
|
364
|
+
}
|
|
365
|
+
if (current.type !== "Identifier")
|
|
366
|
+
return UNKNOWN_SUPERCLASS;
|
|
367
|
+
parts.push(current.name);
|
|
368
|
+
return parts.reverse().join(".");
|
|
369
|
+
}
|
|
370
|
+
function addClass(c, name, node, exported) {
|
|
371
|
+
const { methods, accessors } = readMembers(node.body);
|
|
372
|
+
const entry = { name, exported, line: lineOf(node), accessors, methods };
|
|
373
|
+
if (node.superClass)
|
|
374
|
+
entry.superClass = superClassText(node.superClass);
|
|
375
|
+
c.classes.push(entry);
|
|
376
|
+
}
|
|
377
|
+
/** `const x = require("m")` and `const { a, b } = require("m")`. */
|
|
378
|
+
function readRequire(c, declarator) {
|
|
379
|
+
const init = declarator.init;
|
|
380
|
+
if (init?.type !== "CallExpression")
|
|
381
|
+
return;
|
|
382
|
+
if (init.callee.type !== "Identifier" || init.callee.name !== "require")
|
|
383
|
+
return;
|
|
384
|
+
const arg = init.arguments[0];
|
|
385
|
+
if (init.arguments.length !== 1 || arg?.type !== "StringLiteral")
|
|
386
|
+
return;
|
|
387
|
+
const entry = { source: arg.value, kind: "require", names: [], typeOnly: false, line: lineOf(declarator) };
|
|
388
|
+
if (declarator.id.type === "Identifier")
|
|
389
|
+
entry.defaultName = declarator.id.name;
|
|
390
|
+
if (declarator.id.type === "ObjectPattern") {
|
|
391
|
+
const names = [];
|
|
392
|
+
for (const prop of declarator.id.properties) {
|
|
393
|
+
if (prop.type !== "ObjectProperty")
|
|
394
|
+
continue;
|
|
395
|
+
const name = memberName(prop.key, prop.computed);
|
|
396
|
+
if (name !== undefined)
|
|
397
|
+
names.push(name);
|
|
398
|
+
}
|
|
399
|
+
entry.names = sortedUnique(names);
|
|
400
|
+
}
|
|
401
|
+
c.imports.push(entry);
|
|
402
|
+
}
|
|
403
|
+
/** Every name bound by an identifier or a destructuring pattern, found with an explicit stack. */
|
|
404
|
+
function boundNames(pattern) {
|
|
405
|
+
const names = [];
|
|
406
|
+
const stack = [pattern];
|
|
407
|
+
for (let node = stack.pop(); node !== undefined; node = stack.pop()) {
|
|
408
|
+
if (node.type === "Identifier") {
|
|
409
|
+
names.push(node.name);
|
|
410
|
+
}
|
|
411
|
+
else if (node.type === "ObjectPattern") {
|
|
412
|
+
for (const prop of node.properties)
|
|
413
|
+
stack.push(prop.type === "RestElement" ? prop : prop.value);
|
|
414
|
+
}
|
|
415
|
+
else if (node.type === "ArrayPattern") {
|
|
416
|
+
for (const element of node.elements)
|
|
417
|
+
if (element)
|
|
418
|
+
stack.push(element);
|
|
419
|
+
}
|
|
420
|
+
else if (node.type === "AssignmentPattern") {
|
|
421
|
+
stack.push(node.left);
|
|
422
|
+
}
|
|
423
|
+
else if (node.type === "RestElement") {
|
|
424
|
+
stack.push(node.argument);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return names;
|
|
428
|
+
}
|
|
429
|
+
/** Handles `const` / `let` / `var` at top level. Returns the declared names (for `export const`). */
|
|
430
|
+
function addVariables(c, node, exported) {
|
|
431
|
+
const names = [];
|
|
432
|
+
for (const declarator of node.declarations) {
|
|
433
|
+
const id = declarator.id;
|
|
434
|
+
names.push(...boundNames(id));
|
|
435
|
+
if (node.kind !== "const")
|
|
436
|
+
continue;
|
|
437
|
+
readRequire(c, declarator);
|
|
438
|
+
if (id.type === "Identifier" && isFunctionExpression(declarator.init)) {
|
|
439
|
+
addFunction(c, id.name, declarator.init, exported, declarator);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return names;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Records a top-level declaration (function, class, variable, TS type) and returns its names.
|
|
446
|
+
* `exported` is true when the declaration sits inside `export ...`.
|
|
447
|
+
*/
|
|
448
|
+
function addDeclaration(c, node, exported) {
|
|
449
|
+
switch (node.type) {
|
|
450
|
+
case "FunctionDeclaration": {
|
|
451
|
+
const name = node.id?.name ?? "default";
|
|
452
|
+
addFunction(c, name, node, exported, node);
|
|
453
|
+
return [name];
|
|
454
|
+
}
|
|
455
|
+
case "TSDeclareFunction":
|
|
456
|
+
// An overload signature or `export declare function f(): void;` has no body: the name is
|
|
457
|
+
// exported (the caller records that) but there is no function entry.
|
|
458
|
+
return node.id ? [node.id.name] : [];
|
|
459
|
+
case "TSModuleDeclaration":
|
|
460
|
+
// `namespace A.B.C {}` is named `A`; `declare module "x"` and `declare global` bind no name.
|
|
461
|
+
return node.id.type === "Identifier" && node.global !== true ? [node.id.name] : [];
|
|
462
|
+
case "ClassDeclaration": {
|
|
463
|
+
const name = node.id?.name ?? "default";
|
|
464
|
+
addClass(c, name, node, exported);
|
|
465
|
+
return [name];
|
|
466
|
+
}
|
|
467
|
+
case "VariableDeclaration":
|
|
468
|
+
return addVariables(c, node, exported);
|
|
469
|
+
case "TSInterfaceDeclaration":
|
|
470
|
+
case "TSTypeAliasDeclaration":
|
|
471
|
+
case "TSEnumDeclaration":
|
|
472
|
+
return [node.id.name];
|
|
473
|
+
default:
|
|
474
|
+
return [];
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
function readImport(c, node) {
|
|
478
|
+
const entry = { source: node.source.value, kind: "import", names: [], typeOnly: false, line: lineOf(node) };
|
|
479
|
+
const names = [];
|
|
480
|
+
let allTypeSpecifiers = node.specifiers.length > 0;
|
|
481
|
+
for (const spec of node.specifiers) {
|
|
482
|
+
if (spec.type === "ImportDefaultSpecifier")
|
|
483
|
+
entry.defaultName = spec.local.name;
|
|
484
|
+
if (spec.type === "ImportNamespaceSpecifier")
|
|
485
|
+
entry.namespace = spec.local.name;
|
|
486
|
+
if (spec.type === "ImportSpecifier")
|
|
487
|
+
names.push(nameOf(spec.imported));
|
|
488
|
+
if (spec.type !== "ImportSpecifier" || spec.importKind !== "type")
|
|
489
|
+
allTypeSpecifiers = false;
|
|
490
|
+
}
|
|
491
|
+
entry.names = sortedUnique(names);
|
|
492
|
+
entry.typeOnly = node.importKind === "type" || allTypeSpecifiers;
|
|
493
|
+
c.imports.push(entry);
|
|
494
|
+
}
|
|
495
|
+
/** `export const a`, `export { a }`, `export { a } from "m"`, `export * as ns from "m"`. */
|
|
496
|
+
function readNamedExport(c, node) {
|
|
497
|
+
if (node.declaration) {
|
|
498
|
+
for (const name of addDeclaration(c, node.declaration, true))
|
|
499
|
+
addExport(c, name, "named", node);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const from = node.source?.value;
|
|
503
|
+
for (const spec of node.specifiers) {
|
|
504
|
+
const exportedName = nameOf(spec.exported);
|
|
505
|
+
if (from !== undefined) {
|
|
506
|
+
addExport(c, exportedName, "reexport", node, from);
|
|
507
|
+
}
|
|
508
|
+
else if (spec.type === "ExportSpecifier") {
|
|
509
|
+
c.localExports.add(spec.local.name);
|
|
510
|
+
if (exportedName === "default") {
|
|
511
|
+
// `export { a as default }` is a default export of `a`.
|
|
512
|
+
addExport(c, spec.local.name, "default", node);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
addExport(c, exportedName, "named", node);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
/** `export default function main() {}`, `export default class {}`, `export default value`. */
|
|
521
|
+
function readDefaultExport(c, node) {
|
|
522
|
+
const decl = node.declaration;
|
|
523
|
+
let name = "default";
|
|
524
|
+
if (decl.type === "FunctionDeclaration") {
|
|
525
|
+
name = decl.id?.name ?? "default";
|
|
526
|
+
addFunction(c, name, decl, true, decl);
|
|
527
|
+
}
|
|
528
|
+
else if (decl.type === "ClassDeclaration") {
|
|
529
|
+
name = decl.id?.name ?? "default";
|
|
530
|
+
addClass(c, name, decl, true);
|
|
531
|
+
}
|
|
532
|
+
else if (decl.type === "TSDeclareFunction") {
|
|
533
|
+
name = decl.id?.name ?? "default";
|
|
534
|
+
}
|
|
535
|
+
else if (isFunctionExpression(decl)) {
|
|
536
|
+
addFunction(c, "default", decl, true, decl);
|
|
537
|
+
}
|
|
538
|
+
else if (decl.type === "Identifier") {
|
|
539
|
+
name = decl.name;
|
|
540
|
+
c.localExports.add(decl.name);
|
|
541
|
+
}
|
|
542
|
+
// Anything else, such as `export default interface I {}` or `export default 42`, keeps the name
|
|
543
|
+
// "default": it is recorded as the export `default:default` and has no function or class entry.
|
|
544
|
+
addExport(c, name, "default", node);
|
|
545
|
+
}
|
|
546
|
+
/** `export = foo;` (TypeScript, CommonJS style) is a default export of `foo`. */
|
|
547
|
+
function readExportAssignment(c, node) {
|
|
548
|
+
const expression = node.expression;
|
|
549
|
+
if (expression.type === "Identifier") {
|
|
550
|
+
c.localExports.add(expression.name);
|
|
551
|
+
addExport(c, expression.name, "default", node);
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
addExport(c, "default", "default", node);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
// --- CommonJS exports: `exports.x = ...`, `module.exports = ...` (top-level statements only) ---
|
|
558
|
+
/** `module.exports` */
|
|
559
|
+
function isModuleExports(node) {
|
|
560
|
+
return (node.type === "MemberExpression" &&
|
|
561
|
+
!node.computed &&
|
|
562
|
+
node.object.type === "Identifier" &&
|
|
563
|
+
node.object.name === "module" &&
|
|
564
|
+
node.property.type === "Identifier" &&
|
|
565
|
+
node.property.name === "exports");
|
|
566
|
+
}
|
|
567
|
+
/** `exports` or `module.exports`: the objects whose properties are the exports. */
|
|
568
|
+
function isExportsObject(node) {
|
|
569
|
+
return (node.type === "Identifier" && node.name === "exports") || isModuleExports(node);
|
|
570
|
+
}
|
|
571
|
+
function commonjsTarget(left) {
|
|
572
|
+
if (isModuleExports(left))
|
|
573
|
+
return { kind: "whole" };
|
|
574
|
+
if (left.type !== "MemberExpression" || !isExportsObject(left.object))
|
|
575
|
+
return undefined;
|
|
576
|
+
const key = left.property;
|
|
577
|
+
if (left.computed)
|
|
578
|
+
return { kind: "property", name: key.type === "StringLiteral" ? key.value : undefined };
|
|
579
|
+
return { kind: "property", name: key.type === "Identifier" ? key.name : undefined };
|
|
580
|
+
}
|
|
581
|
+
/** Adds the keys of `{ a, b: c, d() {} }`. A spread or a computed key means a name we cannot read. */
|
|
582
|
+
function readExportedObject(c, object) {
|
|
583
|
+
for (const prop of object.properties) {
|
|
584
|
+
const name = prop.type === "SpreadElement" ? undefined : memberName(prop.key, prop.computed);
|
|
585
|
+
if (name === undefined)
|
|
586
|
+
c.unknownExports = true;
|
|
587
|
+
else
|
|
588
|
+
c.commonjs.add(name);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
/** The value given to all of `module.exports`: an object literal gives names, anything else gives `default`. */
|
|
592
|
+
function readWholeExports(c, value) {
|
|
593
|
+
if (value.type === "ObjectExpression")
|
|
594
|
+
return readExportedObject(c, value);
|
|
595
|
+
c.commonjs.add("default");
|
|
596
|
+
c.unknownExports = true;
|
|
597
|
+
}
|
|
598
|
+
/** `Object.assign(exports, { a })` and `Object.defineProperty(exports, "x", ...)`. */
|
|
599
|
+
function readCommonjsCall(c, call) {
|
|
600
|
+
const callee = call.callee;
|
|
601
|
+
const [target, second] = call.arguments;
|
|
602
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.object.type !== "Identifier")
|
|
603
|
+
return;
|
|
604
|
+
if (callee.object.name !== "Object" || callee.property.type !== "Identifier" || !target || !isExportsObject(target))
|
|
605
|
+
return;
|
|
606
|
+
if (callee.property.name === "defineProperty") {
|
|
607
|
+
if (second?.type === "StringLiteral")
|
|
608
|
+
c.commonjs.add(second.value);
|
|
609
|
+
else
|
|
610
|
+
c.unknownExports = true;
|
|
611
|
+
}
|
|
612
|
+
else if (callee.property.name === "assign") {
|
|
613
|
+
for (const source of call.arguments.slice(1)) {
|
|
614
|
+
if (source.type === "ObjectExpression")
|
|
615
|
+
readExportedObject(c, source);
|
|
616
|
+
else
|
|
617
|
+
c.unknownExports = true;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
/** Reads one top-level expression statement; most of them have nothing to do with exports. */
|
|
622
|
+
function readCommonjs(c, expression) {
|
|
623
|
+
if (expression.type === "CallExpression")
|
|
624
|
+
return readCommonjsCall(c, expression);
|
|
625
|
+
// `a = b = value`: every left side receives the final value.
|
|
626
|
+
const targets = [];
|
|
627
|
+
let value = expression;
|
|
628
|
+
while (value.type === "AssignmentExpression" && value.operator === "=") {
|
|
629
|
+
const target = commonjsTarget(value.left);
|
|
630
|
+
if (target)
|
|
631
|
+
targets.push(target);
|
|
632
|
+
value = value.right;
|
|
633
|
+
}
|
|
634
|
+
for (const target of targets) {
|
|
635
|
+
if (target.kind === "whole")
|
|
636
|
+
readWholeExports(c, value);
|
|
637
|
+
else if (target.name === undefined)
|
|
638
|
+
c.unknownExports = true;
|
|
639
|
+
else
|
|
640
|
+
c.commonjs.add(target.name);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
function readStatement(c, node) {
|
|
644
|
+
if (node.type === "ExpressionStatement")
|
|
645
|
+
return readCommonjs(c, node.expression);
|
|
646
|
+
if (node.type === "ImportDeclaration")
|
|
647
|
+
return readImport(c, node);
|
|
648
|
+
if (node.type === "ExportNamedDeclaration")
|
|
649
|
+
return readNamedExport(c, node);
|
|
650
|
+
if (node.type === "ExportDefaultDeclaration")
|
|
651
|
+
return readDefaultExport(c, node);
|
|
652
|
+
if (node.type === "ExportAllDeclaration")
|
|
653
|
+
return addExport(c, "*", "reexport", node, node.source.value);
|
|
654
|
+
if (node.type === "TSExportAssignment")
|
|
655
|
+
return readExportAssignment(c, node);
|
|
656
|
+
// `export import X = require("m")`; a plain `import X = require("m")` is not recorded.
|
|
657
|
+
if (node.type === "TSImportEqualsDeclaration") {
|
|
658
|
+
if (node.isExport)
|
|
659
|
+
addExport(c, node.id.name, "named", node);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
addDeclaration(c, node, false);
|
|
663
|
+
}
|
|
664
|
+
/** Compares two entries by a text field. */
|
|
665
|
+
function byText(get) {
|
|
666
|
+
return (a, b) => compareText(get(a), get(b));
|
|
667
|
+
}
|
|
668
|
+
/** Compares two entries by a number field. */
|
|
669
|
+
function byNumber(get) {
|
|
670
|
+
return (a, b) => get(a) - get(b);
|
|
671
|
+
}
|
|
672
|
+
/** Sorts by the comparators in order, then by JSON text so that two different entries never tie. */
|
|
673
|
+
function sortWith(list, ...order) {
|
|
674
|
+
return list.sort((a, b) => {
|
|
675
|
+
for (const compare of order) {
|
|
676
|
+
const result = compare(a, b);
|
|
677
|
+
if (result !== 0)
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
return compareText(JSON.stringify(a), JSON.stringify(b));
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
function sortImports(list) {
|
|
684
|
+
return sortWith(list, byText((i) => i.source), byNumber((i) => i.line));
|
|
685
|
+
}
|
|
686
|
+
function sortExports(list) {
|
|
687
|
+
return sortWith(list, byText((e) => e.name), byText((e) => e.from ?? ""), byNumber((e) => e.line));
|
|
688
|
+
}
|
|
689
|
+
function sortFunctions(list) {
|
|
690
|
+
return sortWith(list, byText((f) => f.name), byNumber((f) => f.line));
|
|
691
|
+
}
|
|
692
|
+
function sortClasses(list) {
|
|
693
|
+
return sortWith(list, byText((c) => c.name), byNumber((c) => c.line));
|
|
694
|
+
}
|
|
695
|
+
// ---------------------------------------------------------------------------
|
|
696
|
+
// One file
|
|
697
|
+
// ---------------------------------------------------------------------------
|
|
698
|
+
/** Parses a JS/TS file and fills the lists. Throws only for bugs; the caller catches them. */
|
|
699
|
+
function fillFromSource(entry, content) {
|
|
700
|
+
const outcome = parseSource(entry.path, content);
|
|
701
|
+
if (!outcome.ok) {
|
|
702
|
+
entry.errors = [outcome.issue];
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
const collected = {
|
|
706
|
+
imports: [],
|
|
707
|
+
exports: new Map(),
|
|
708
|
+
functions: [],
|
|
709
|
+
classes: [],
|
|
710
|
+
localExports: new Set(),
|
|
711
|
+
commonjs: new Set(),
|
|
712
|
+
unknownExports: false,
|
|
713
|
+
};
|
|
714
|
+
for (const statement of outcome.ast.program.body)
|
|
715
|
+
readStatement(collected, statement);
|
|
716
|
+
for (const fn of collected.functions)
|
|
717
|
+
if (collected.localExports.has(fn.name))
|
|
718
|
+
fn.exported = true;
|
|
719
|
+
for (const cls of collected.classes)
|
|
720
|
+
if (collected.localExports.has(cls.name))
|
|
721
|
+
cls.exported = true;
|
|
722
|
+
entry.parsed = true;
|
|
723
|
+
entry.errors = (outcome.ast.errors ?? []).slice(0, MAX_RECORDED_ERRORS).map(toIssue);
|
|
724
|
+
entry.imports = sortImports(collected.imports);
|
|
725
|
+
entry.exports = sortExports([...collected.exports.values()]);
|
|
726
|
+
entry.functions = sortFunctions(collected.functions);
|
|
727
|
+
entry.classes = sortClasses(collected.classes);
|
|
728
|
+
entry.commonjsExports = sortedUnique(collected.commonjs);
|
|
729
|
+
entry.hasUnknownExports = collected.unknownExports;
|
|
730
|
+
}
|
|
731
|
+
function buildFileEntry(file) {
|
|
732
|
+
const entry = emptyEntry(file.path, file.content);
|
|
733
|
+
if (entry.language !== "javascript" && entry.language !== "typescript")
|
|
734
|
+
return entry;
|
|
735
|
+
try {
|
|
736
|
+
fillFromSource(entry, file.content);
|
|
737
|
+
return entry;
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
// Requirement 1.15: file content must never make the whole map throw.
|
|
741
|
+
return { ...emptyEntry(file.path, file.content), errors: [toIssue(error)] };
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
/** Builds the structure map of up to 50 files (100 KB in total). Throws RangeError for bad input only. */
|
|
745
|
+
export function buildStructureMap(files) {
|
|
746
|
+
const entries = checkInput(files).map(buildFileEntry);
|
|
747
|
+
entries.sort((a, b) => compareText(a.path, b.path));
|
|
748
|
+
return { schemaVersion: 1, files: entries };
|
|
749
|
+
}
|
|
750
|
+
//# sourceMappingURL=structure-map.js.map
|