@dep-radius/core 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 +48 -0
- package/dist/analyze/brief.d.ts +18 -0
- package/dist/analyze/brief.d.ts.map +1 -0
- package/dist/analyze/brief.js +89 -0
- package/dist/analyze/verdict.d.ts +16 -0
- package/dist/analyze/verdict.d.ts.map +1 -0
- package/dist/analyze/verdict.js +134 -0
- package/dist/context.d.ts +4 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +36 -0
- package/dist/debug.d.ts +2 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +130 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/infra/cache.d.ts +18 -0
- package/dist/infra/cache.d.ts.map +1 -0
- package/dist/infra/cache.js +66 -0
- package/dist/infra/hash.d.ts +4 -0
- package/dist/infra/hash.d.ts.map +1 -0
- package/dist/infra/hash.js +18 -0
- package/dist/infra/http.d.ts +24 -0
- package/dist/infra/http.d.ts.map +1 -0
- package/dist/infra/http.js +44 -0
- package/dist/infra/limit.d.ts +4 -0
- package/dist/infra/limit.d.ts.map +1 -0
- package/dist/infra/limit.js +34 -0
- package/dist/infra/log.d.ts +8 -0
- package/dist/infra/log.d.ts.map +1 -0
- package/dist/infra/log.js +13 -0
- package/dist/inventory/installed.d.ts +15 -0
- package/dist/inventory/installed.d.ts.map +1 -0
- package/dist/inventory/installed.js +241 -0
- package/dist/inventory/lockfiles/bun.d.ts +3 -0
- package/dist/inventory/lockfiles/bun.d.ts.map +1 -0
- package/dist/inventory/lockfiles/bun.js +24 -0
- package/dist/inventory/lockfiles/npm.d.ts +3 -0
- package/dist/inventory/lockfiles/npm.d.ts.map +1 -0
- package/dist/inventory/lockfiles/npm.js +29 -0
- package/dist/inventory/lockfiles/pnpm.d.ts +3 -0
- package/dist/inventory/lockfiles/pnpm.d.ts.map +1 -0
- package/dist/inventory/lockfiles/pnpm.js +84 -0
- package/dist/inventory/lockfiles/types.d.ts +9 -0
- package/dist/inventory/lockfiles/types.d.ts.map +1 -0
- package/dist/inventory/lockfiles/types.js +1 -0
- package/dist/inventory/lockfiles/yarn.d.ts +3 -0
- package/dist/inventory/lockfiles/yarn.d.ts.map +1 -0
- package/dist/inventory/lockfiles/yarn.js +46 -0
- package/dist/inventory/manifests.d.ts +9 -0
- package/dist/inventory/manifests.d.ts.map +1 -0
- package/dist/inventory/manifests.js +135 -0
- package/dist/inventory/specifiers.d.ts +8 -0
- package/dist/inventory/specifiers.d.ts.map +1 -0
- package/dist/inventory/specifiers.js +53 -0
- package/dist/labels.d.ts +2 -0
- package/dist/labels.d.ts.map +1 -0
- package/dist/labels.js +18 -0
- package/dist/model.d.ts +276 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +2 -0
- package/dist/notes/changelog.d.ts +11 -0
- package/dist/notes/changelog.d.ts.map +1 -0
- package/dist/notes/changelog.js +60 -0
- package/dist/notes/collect.d.ts +13 -0
- package/dist/notes/collect.d.ts.map +1 -0
- package/dist/notes/collect.js +225 -0
- package/dist/notes/entries.d.ts +3 -0
- package/dist/notes/entries.d.ts.map +1 -0
- package/dist/notes/entries.js +209 -0
- package/dist/notes/github.d.ts +25 -0
- package/dist/notes/github.d.ts.map +1 -0
- package/dist/notes/github.js +132 -0
- package/dist/notes/match.d.ts +13 -0
- package/dist/notes/match.d.ts.map +1 -0
- package/dist/notes/match.js +75 -0
- package/dist/notes/repo-url.d.ts +9 -0
- package/dist/notes/repo-url.d.ts.map +1 -0
- package/dist/notes/repo-url.js +46 -0
- package/dist/options.d.ts +53 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +27 -0
- package/dist/registry/candidates.d.ts +24 -0
- package/dist/registry/candidates.d.ts.map +1 -0
- package/dist/registry/candidates.js +139 -0
- package/dist/registry/npmrc.d.ts +13 -0
- package/dist/registry/npmrc.d.ts.map +1 -0
- package/dist/registry/npmrc.js +99 -0
- package/dist/registry/packument.d.ts +34 -0
- package/dist/registry/packument.d.ts.map +1 -0
- package/dist/registry/packument.js +85 -0
- package/dist/registry/tar.d.ts +2 -0
- package/dist/registry/tar.d.ts.map +1 -0
- package/dist/registry/tar.js +75 -0
- package/dist/registry/tarball.d.ts +16 -0
- package/dist/registry/tarball.d.ts.map +1 -0
- package/dist/registry/tarball.js +59 -0
- package/dist/render/groups.d.ts +16 -0
- package/dist/render/groups.d.ts.map +1 -0
- package/dist/render/groups.js +105 -0
- package/dist/render/index.d.ts +2 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +3 -0
- package/dist/render/json.d.ts +119 -0
- package/dist/render/json.d.ts.map +1 -0
- package/dist/render/json.js +130 -0
- package/dist/render/limits.d.ts +3 -0
- package/dist/render/limits.d.ts.map +1 -0
- package/dist/render/limits.js +25 -0
- package/dist/render/markdown.d.ts +3 -0
- package/dist/render/markdown.d.ts.map +1 -0
- package/dist/render/markdown.js +62 -0
- package/dist/run.d.ts +10 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +218 -0
- package/dist/surface/analyze.d.ts +13 -0
- package/dist/surface/analyze.d.ts.map +1 -0
- package/dist/surface/analyze.js +166 -0
- package/dist/surface/delta.d.ts +5 -0
- package/dist/surface/delta.d.ts.map +1 -0
- package/dist/surface/delta.js +173 -0
- package/dist/surface/entries.d.ts +16 -0
- package/dist/surface/entries.d.ts.map +1 -0
- package/dist/surface/entries.js +187 -0
- package/dist/surface/extract.d.ts +15 -0
- package/dist/surface/extract.d.ts.map +1 -0
- package/dist/surface/extract.js +460 -0
- package/dist/surface/signature.d.ts +6 -0
- package/dist/surface/signature.d.ts.map +1 -0
- package/dist/surface/signature.js +229 -0
- package/dist/surface/vfs-host.d.ts +5 -0
- package/dist/surface/vfs-host.d.ts.map +1 -0
- package/dist/surface/vfs-host.js +54 -0
- package/dist/symbol-path.d.ts +28 -0
- package/dist/symbol-path.d.ts.map +1 -0
- package/dist/symbol-path.js +223 -0
- package/dist/usage/files.d.ts +18 -0
- package/dist/usage/files.d.ts.map +1 -0
- package/dist/usage/files.js +83 -0
- package/dist/usage/link.d.ts +23 -0
- package/dist/usage/link.d.ts.map +1 -0
- package/dist/usage/link.js +588 -0
- package/dist/usage/opaque.d.ts +7 -0
- package/dist/usage/opaque.d.ts.map +1 -0
- package/dist/usage/opaque.js +193 -0
- package/dist/usage/parse.d.ts +73 -0
- package/dist/usage/parse.d.ts.map +1 -0
- package/dist/usage/parse.js +608 -0
- package/dist/usage/resolve-local.d.ts +35 -0
- package/dist/usage/resolve-local.d.ts.map +1 -0
- package/dist/usage/resolve-local.js +340 -0
- package/dist/usage/scan.d.ts +4 -0
- package/dist/usage/scan.d.ts.map +1 -0
- package/dist/usage/scan.js +92 -0
- package/package.json +79 -0
- package/schema/brief-v1.schema.json +319 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
export const SCANNER_VERSION = 2;
|
|
3
|
+
export function parseSource(src) {
|
|
4
|
+
const facts = {
|
|
5
|
+
imports: [],
|
|
6
|
+
sideEffects: [],
|
|
7
|
+
reExports: [],
|
|
8
|
+
exportedLocals: [],
|
|
9
|
+
declaredExports: [],
|
|
10
|
+
references: [],
|
|
11
|
+
inline: [],
|
|
12
|
+
nonLiteralRequire: [],
|
|
13
|
+
nonLiteralImport: [],
|
|
14
|
+
prefixImports: [],
|
|
15
|
+
stringLiterals: [],
|
|
16
|
+
unparseable: false,
|
|
17
|
+
};
|
|
18
|
+
for (const block of src.blocks)
|
|
19
|
+
parseBlock(src.rel, block, facts);
|
|
20
|
+
return facts;
|
|
21
|
+
}
|
|
22
|
+
function scriptKind(kind) {
|
|
23
|
+
return kind === "ts"
|
|
24
|
+
? ts.ScriptKind.TS
|
|
25
|
+
: kind === "tsx"
|
|
26
|
+
? ts.ScriptKind.TSX
|
|
27
|
+
: kind === "jsx"
|
|
28
|
+
? ts.ScriptKind.JSX
|
|
29
|
+
: ts.ScriptKind.JS;
|
|
30
|
+
}
|
|
31
|
+
function parseBlock(rel, block, facts) {
|
|
32
|
+
// .js files routinely hold JSX; parsing them as JSX costs nothing when there is none
|
|
33
|
+
const kind = block.kind === "js" ? "jsx" : block.kind;
|
|
34
|
+
const sf = ts.createSourceFile(rel, block.text, ts.ScriptTarget.Latest, true, scriptKind(kind));
|
|
35
|
+
// parseDiagnostics is internal; the pinned TypeScript version keeps it stable (see test/unit/parse.test.ts)
|
|
36
|
+
const diags = sf
|
|
37
|
+
.parseDiagnostics;
|
|
38
|
+
if (diags && diags.length > 0)
|
|
39
|
+
facts.unparseable = true;
|
|
40
|
+
const lines = block.text.split("\n");
|
|
41
|
+
const pos = (node) => {
|
|
42
|
+
const start = node.getStart(sf);
|
|
43
|
+
const lc = sf.getLineAndCharacterOfPosition(start);
|
|
44
|
+
const lineText = lines[lc.line] ?? "";
|
|
45
|
+
return {
|
|
46
|
+
line: lc.line + 1 + block.lineOffset,
|
|
47
|
+
col: lc.character + 1,
|
|
48
|
+
text: lineText.trim().slice(0, 160),
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const bindings = new Map();
|
|
52
|
+
const add = (b) => {
|
|
53
|
+
facts.imports.push(b);
|
|
54
|
+
bindings.set(b.local, b);
|
|
55
|
+
};
|
|
56
|
+
const handledCalls = new Set();
|
|
57
|
+
// ---------------------------------------------------------------- pass 1: declarations
|
|
58
|
+
const visitDecl = (node) => {
|
|
59
|
+
if (ts.isImportDeclaration(node) &&
|
|
60
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
61
|
+
const specifier = node.moduleSpecifier.text;
|
|
62
|
+
const clause = node.importClause;
|
|
63
|
+
if (!clause) {
|
|
64
|
+
facts.sideEffects.push({ specifier, pos: pos(node) });
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const typeOnly = clause.isTypeOnly;
|
|
68
|
+
if (clause.name)
|
|
69
|
+
add({
|
|
70
|
+
local: clause.name.text,
|
|
71
|
+
specifier,
|
|
72
|
+
binding: { kind: "default" },
|
|
73
|
+
typeOnly,
|
|
74
|
+
pos: pos(clause.name),
|
|
75
|
+
});
|
|
76
|
+
const nb = clause.namedBindings;
|
|
77
|
+
if (nb && ts.isNamespaceImport(nb)) {
|
|
78
|
+
add({
|
|
79
|
+
local: nb.name.text,
|
|
80
|
+
specifier,
|
|
81
|
+
binding: { kind: "namespace" },
|
|
82
|
+
typeOnly,
|
|
83
|
+
pos: pos(nb),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (nb && ts.isNamedImports(nb)) {
|
|
87
|
+
for (const el of nb.elements) {
|
|
88
|
+
const imported = el.propertyName
|
|
89
|
+
? moduleExportName(el.propertyName)
|
|
90
|
+
: el.name.text;
|
|
91
|
+
add({
|
|
92
|
+
local: el.name.text,
|
|
93
|
+
specifier,
|
|
94
|
+
binding: imported === "default"
|
|
95
|
+
? { kind: "default" }
|
|
96
|
+
: { kind: "named", imported },
|
|
97
|
+
typeOnly: typeOnly || el.isTypeOnly,
|
|
98
|
+
pos: pos(el),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (ts.isImportEqualsDeclaration(node) &&
|
|
105
|
+
ts.isExternalModuleReference(node.moduleReference)) {
|
|
106
|
+
const expr = node.moduleReference.expression;
|
|
107
|
+
if (ts.isStringLiteral(expr)) {
|
|
108
|
+
add({
|
|
109
|
+
local: node.name.text,
|
|
110
|
+
specifier: expr.text,
|
|
111
|
+
binding: { kind: "cjs" },
|
|
112
|
+
typeOnly: node.isTypeOnly,
|
|
113
|
+
pos: pos(node),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else if (ts.isExportDeclaration(node)) {
|
|
118
|
+
const spec = node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)
|
|
119
|
+
? node.moduleSpecifier.text
|
|
120
|
+
: undefined;
|
|
121
|
+
const clause = node.exportClause;
|
|
122
|
+
if (spec) {
|
|
123
|
+
if (!clause)
|
|
124
|
+
facts.reExports.push({
|
|
125
|
+
specifier: spec,
|
|
126
|
+
kind: "star",
|
|
127
|
+
names: [],
|
|
128
|
+
pos: pos(node),
|
|
129
|
+
});
|
|
130
|
+
else if (ts.isNamespaceExport(clause))
|
|
131
|
+
facts.reExports.push({
|
|
132
|
+
specifier: spec,
|
|
133
|
+
kind: "star-as",
|
|
134
|
+
names: [{ imported: "*", exported: moduleExportName(clause.name) }],
|
|
135
|
+
pos: pos(node),
|
|
136
|
+
});
|
|
137
|
+
else
|
|
138
|
+
facts.reExports.push({
|
|
139
|
+
specifier: spec,
|
|
140
|
+
kind: "named",
|
|
141
|
+
names: clause.elements.map((el) => ({
|
|
142
|
+
imported: el.propertyName
|
|
143
|
+
? moduleExportName(el.propertyName)
|
|
144
|
+
: moduleExportName(el.name),
|
|
145
|
+
exported: moduleExportName(el.name),
|
|
146
|
+
})),
|
|
147
|
+
pos: pos(node),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else if (clause && ts.isNamedExports(clause)) {
|
|
151
|
+
for (const el of clause.elements) {
|
|
152
|
+
facts.exportedLocals.push({
|
|
153
|
+
local: el.propertyName
|
|
154
|
+
? moduleExportName(el.propertyName)
|
|
155
|
+
: moduleExportName(el.name),
|
|
156
|
+
exported: moduleExportName(el.name),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (ts.isExportAssignment(node) &&
|
|
162
|
+
ts.isIdentifier(node.expression)) {
|
|
163
|
+
facts.exportedLocals.push({
|
|
164
|
+
local: node.expression.text,
|
|
165
|
+
exported: "default",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
else if (ts.isVariableStatement(node) || isNamedDeclaration(node)) {
|
|
169
|
+
const exported = hasExport(node);
|
|
170
|
+
if (ts.isVariableStatement(node)) {
|
|
171
|
+
if (exported)
|
|
172
|
+
for (const d of node.declarationList.declarations)
|
|
173
|
+
for (const n of bindingNames(d.name))
|
|
174
|
+
facts.declaredExports.push(n);
|
|
175
|
+
}
|
|
176
|
+
else if (exported) {
|
|
177
|
+
const name = node.name;
|
|
178
|
+
if (hasDefault(node))
|
|
179
|
+
facts.declaredExports.push("default");
|
|
180
|
+
else if (name && ts.isIdentifier(name))
|
|
181
|
+
facts.declaredExports.push(name.text);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (ts.isExportAssignment(node)) {
|
|
185
|
+
facts.declaredExports.push("default");
|
|
186
|
+
}
|
|
187
|
+
if (ts.isSourceFile(node) || ts.isModuleBlock(node))
|
|
188
|
+
node.forEachChild(visitDecl);
|
|
189
|
+
};
|
|
190
|
+
sf.forEachChild(visitDecl);
|
|
191
|
+
// CommonJS and awaited dynamic imports bound to a name, at any depth: `const { a } = require("p")`
|
|
192
|
+
const visitRequires = (node) => {
|
|
193
|
+
if (ts.isVariableDeclaration(node)) {
|
|
194
|
+
const req = requireOrImportCall(node.initializer);
|
|
195
|
+
if (req && req.specifier !== undefined) {
|
|
196
|
+
const d = node;
|
|
197
|
+
if (ts.isIdentifier(d.name)) {
|
|
198
|
+
handledCalls.add(req.call);
|
|
199
|
+
add({
|
|
200
|
+
local: d.name.text,
|
|
201
|
+
specifier: req.specifier,
|
|
202
|
+
binding: req.dynamic ? { kind: "namespace" } : { kind: "cjs" },
|
|
203
|
+
typeOnly: false,
|
|
204
|
+
pos: pos(d),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
else if (ts.isObjectBindingPattern(d.name)) {
|
|
208
|
+
handledCalls.add(req.call);
|
|
209
|
+
for (const el of d.name.elements) {
|
|
210
|
+
if (!ts.isIdentifier(el.name) || el.dotDotDotToken)
|
|
211
|
+
continue;
|
|
212
|
+
const imported = el.propertyName && ts.isIdentifier(el.propertyName)
|
|
213
|
+
? el.propertyName.text
|
|
214
|
+
: el.name.text;
|
|
215
|
+
add({
|
|
216
|
+
local: el.name.text,
|
|
217
|
+
specifier: req.specifier,
|
|
218
|
+
binding: imported === "default"
|
|
219
|
+
? { kind: "default" }
|
|
220
|
+
: { kind: "named", imported },
|
|
221
|
+
typeOnly: false,
|
|
222
|
+
pos: pos(el),
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
node.forEachChild(visitRequires);
|
|
229
|
+
};
|
|
230
|
+
sf.forEachChild(visitRequires);
|
|
231
|
+
// ---------------------------------------------------------------- pass 2: references and inline uses
|
|
232
|
+
const derived = new Map();
|
|
233
|
+
const visit = (node) => {
|
|
234
|
+
if (ts.isIdentifier(node) &&
|
|
235
|
+
bindings.has(node.text) &&
|
|
236
|
+
isReferencePosition(node)) {
|
|
237
|
+
const ref = climb(node);
|
|
238
|
+
facts.references.push({
|
|
239
|
+
local: node.text,
|
|
240
|
+
...withoutBenign(ref),
|
|
241
|
+
pos: pos(node),
|
|
242
|
+
});
|
|
243
|
+
if (ref.derivedInto)
|
|
244
|
+
derived.set(ref.derivedInto, true);
|
|
245
|
+
for (const cb of ref.callbackInto ?? [])
|
|
246
|
+
derived.set(cb, true);
|
|
247
|
+
}
|
|
248
|
+
else if (ts.isCallExpression(node)) {
|
|
249
|
+
handleCall(node);
|
|
250
|
+
}
|
|
251
|
+
else if (ts.isStringLiteral(node) ||
|
|
252
|
+
ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
253
|
+
if (!isModuleSpecifierPosition(node) &&
|
|
254
|
+
node.text.length > 0 &&
|
|
255
|
+
node.text.length < 200) {
|
|
256
|
+
facts.stringLiterals.push({ value: node.text, pos: pos(node) });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
node.forEachChild(visit);
|
|
260
|
+
};
|
|
261
|
+
const handleCall = (node) => {
|
|
262
|
+
const isRequire = ts.isIdentifier(node.expression) && node.expression.text === "require";
|
|
263
|
+
const isImport = node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
264
|
+
if (!isRequire && !isImport)
|
|
265
|
+
return;
|
|
266
|
+
const arg = node.arguments[0];
|
|
267
|
+
if (!arg)
|
|
268
|
+
return;
|
|
269
|
+
const specifier = ts.isStringLiteral(arg) || ts.isNoSubstitutionTemplateLiteral(arg)
|
|
270
|
+
? arg.text
|
|
271
|
+
: undefined;
|
|
272
|
+
if (specifier === undefined) {
|
|
273
|
+
if (ts.isTemplateExpression(arg) &&
|
|
274
|
+
arg.head.text &&
|
|
275
|
+
!arg.head.text.startsWith(".")) {
|
|
276
|
+
facts.prefixImports.push({ prefix: arg.head.text, pos: pos(node) });
|
|
277
|
+
}
|
|
278
|
+
else if (isRequire)
|
|
279
|
+
facts.nonLiteralRequire.push(pos(node));
|
|
280
|
+
else
|
|
281
|
+
facts.nonLiteralImport.push(pos(node));
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (handledCalls.has(node))
|
|
285
|
+
return;
|
|
286
|
+
// climb from the call: require("p").a.b() / (await import("p")).a
|
|
287
|
+
let cur = node;
|
|
288
|
+
if (isImport && ts.isAwaitExpression(cur.parent))
|
|
289
|
+
cur = cur.parent;
|
|
290
|
+
while (ts.isParenthesizedExpression(cur.parent))
|
|
291
|
+
cur = cur.parent;
|
|
292
|
+
const r = climbFrom(cur);
|
|
293
|
+
const binding = isImport ? { kind: "namespace" } : { kind: "cjs" };
|
|
294
|
+
const parent = cur.parent;
|
|
295
|
+
if (r.chain.length === 0 &&
|
|
296
|
+
!r.callSelf &&
|
|
297
|
+
parent &&
|
|
298
|
+
ts.isExpressionStatement(parent)) {
|
|
299
|
+
facts.sideEffects.push({ specifier, pos: pos(node) });
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
facts.inline.push({
|
|
303
|
+
specifier,
|
|
304
|
+
binding,
|
|
305
|
+
chain: r.chain,
|
|
306
|
+
callSelf: r.callSelf,
|
|
307
|
+
pos: pos(node),
|
|
308
|
+
escape: r.chain.length === 0 && !r.callSelf && !r.computed,
|
|
309
|
+
...(r.derivedInto ? { derivedInto: r.derivedInto } : {}),
|
|
310
|
+
...(r.callbackInto ? { callbackInto: r.callbackInto } : {}),
|
|
311
|
+
});
|
|
312
|
+
if (r.derivedInto)
|
|
313
|
+
derived.set(r.derivedInto, true);
|
|
314
|
+
for (const cb of r.callbackInto ?? [])
|
|
315
|
+
derived.set(cb, true);
|
|
316
|
+
};
|
|
317
|
+
const climb = (id) => {
|
|
318
|
+
const r = climbFrom(id);
|
|
319
|
+
const b = bindings.get(id.text);
|
|
320
|
+
const bare = r.chain.length === 0 && !r.callSelf && !r.computed;
|
|
321
|
+
const typeOnly = r.typeOnly || b.typeOnly;
|
|
322
|
+
return {
|
|
323
|
+
...r,
|
|
324
|
+
typeOnly,
|
|
325
|
+
escape: bare && b.binding.kind !== "named" && !r.benign,
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
sf.forEachChild(visit);
|
|
329
|
+
// derived bindings: `const app = createApp()`, then `app.use(...)`. Two more rounds reach `const r = app.route()`.
|
|
330
|
+
let frontier = new Set(derived.keys());
|
|
331
|
+
const seen = new Set();
|
|
332
|
+
for (let round = 0; round < 3 && frontier.size > 0; round++) {
|
|
333
|
+
const names = new Set([...frontier].filter((n) => !bindings.has(n) && !seen.has(n)));
|
|
334
|
+
for (const n of names)
|
|
335
|
+
seen.add(n);
|
|
336
|
+
const next = new Set();
|
|
337
|
+
const visitDerived = (node) => {
|
|
338
|
+
if (ts.isIdentifier(node) &&
|
|
339
|
+
names.has(node.text) &&
|
|
340
|
+
isReferencePosition(node)) {
|
|
341
|
+
const r = climbFrom(node);
|
|
342
|
+
if (r.chain.length > 0 || r.callSelf) {
|
|
343
|
+
facts.references.push({
|
|
344
|
+
local: node.text,
|
|
345
|
+
chain: r.chain,
|
|
346
|
+
callSelf: r.callSelf,
|
|
347
|
+
typeOnly: r.typeOnly,
|
|
348
|
+
pos: pos(node),
|
|
349
|
+
escape: false,
|
|
350
|
+
computed: r.computed,
|
|
351
|
+
...(r.derivedInto ? { derivedInto: r.derivedInto } : {}),
|
|
352
|
+
...(r.callbackInto ? { callbackInto: r.callbackInto } : {}),
|
|
353
|
+
});
|
|
354
|
+
if (r.derivedInto)
|
|
355
|
+
next.add(r.derivedInto);
|
|
356
|
+
for (const cb of r.callbackInto ?? [])
|
|
357
|
+
next.add(cb);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
node.forEachChild(visitDerived);
|
|
361
|
+
};
|
|
362
|
+
if (names.size > 0)
|
|
363
|
+
sf.forEachChild(visitDerived);
|
|
364
|
+
frontier = next;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function climbFrom(start) {
|
|
368
|
+
const chain = [];
|
|
369
|
+
let callSelf = false;
|
|
370
|
+
let typeOnly = false;
|
|
371
|
+
let computed = false;
|
|
372
|
+
let benign = false;
|
|
373
|
+
const callbacks = [];
|
|
374
|
+
let cur = start;
|
|
375
|
+
for (;;) {
|
|
376
|
+
const p = cur.parent;
|
|
377
|
+
if (!p)
|
|
378
|
+
break;
|
|
379
|
+
if ((ts.isParenthesizedExpression(p) ||
|
|
380
|
+
ts.isNonNullExpression(p) ||
|
|
381
|
+
ts.isAsExpression(p) ||
|
|
382
|
+
ts.isSatisfiesExpression(p)) &&
|
|
383
|
+
p.expression === cur) {
|
|
384
|
+
cur = p;
|
|
385
|
+
}
|
|
386
|
+
else if (ts.isAwaitExpression(p)) {
|
|
387
|
+
cur = p;
|
|
388
|
+
}
|
|
389
|
+
else if (ts.isPropertyAccessExpression(p) && p.expression === cur) {
|
|
390
|
+
if (ts.isIdentifier(p.name))
|
|
391
|
+
chain.push({ name: p.name.text, call: false });
|
|
392
|
+
cur = p;
|
|
393
|
+
}
|
|
394
|
+
else if (ts.isElementAccessExpression(p) && p.expression === cur) {
|
|
395
|
+
const a = p.argumentExpression;
|
|
396
|
+
if (ts.isStringLiteral(a) || ts.isNoSubstitutionTemplateLiteral(a)) {
|
|
397
|
+
chain.push({ name: a.text, call: false });
|
|
398
|
+
cur = p;
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
computed = true;
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
else if ((ts.isCallExpression(p) || ts.isNewExpression(p)) &&
|
|
406
|
+
p.expression === cur) {
|
|
407
|
+
for (const arg of p.arguments ?? [])
|
|
408
|
+
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg))
|
|
409
|
+
for (const param of arg.parameters)
|
|
410
|
+
callbacks.push(...bindingNames(param.name));
|
|
411
|
+
const last = chain[chain.length - 1];
|
|
412
|
+
const construct = ts.isNewExpression(p);
|
|
413
|
+
if (last) {
|
|
414
|
+
last.call = true;
|
|
415
|
+
if (construct)
|
|
416
|
+
last.construct = true;
|
|
417
|
+
}
|
|
418
|
+
else
|
|
419
|
+
callSelf = true;
|
|
420
|
+
cur = p;
|
|
421
|
+
}
|
|
422
|
+
else if (ts.isTaggedTemplateExpression(p) && p.tag === cur) {
|
|
423
|
+
const last = chain[chain.length - 1];
|
|
424
|
+
if (last)
|
|
425
|
+
last.call = true;
|
|
426
|
+
else
|
|
427
|
+
callSelf = true;
|
|
428
|
+
cur = p;
|
|
429
|
+
}
|
|
430
|
+
else if (ts.isQualifiedName(p) && p.left === cur) {
|
|
431
|
+
chain.push({ name: p.right.text, call: false });
|
|
432
|
+
typeOnly = true;
|
|
433
|
+
cur = p;
|
|
434
|
+
}
|
|
435
|
+
else if (ts.isTypeReferenceNode(p) ||
|
|
436
|
+
ts.isExpressionWithTypeArguments(p)) {
|
|
437
|
+
if (ts.isTypeReferenceNode(p))
|
|
438
|
+
typeOnly = true;
|
|
439
|
+
if (ts.isExpressionWithTypeArguments(p) && ts.isHeritageClause(p.parent))
|
|
440
|
+
benign = true;
|
|
441
|
+
if (ts.isTypeReferenceNode(p))
|
|
442
|
+
benign = true;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
else if (ts.isTypeQueryNode(p)) {
|
|
446
|
+
typeOnly = true;
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
else if ((ts.isJsxOpeningElement(p) ||
|
|
450
|
+
ts.isJsxSelfClosingElement(p) ||
|
|
451
|
+
ts.isJsxClosingElement(p)) &&
|
|
452
|
+
p.tagName === cur) {
|
|
453
|
+
if (chain.length === 0)
|
|
454
|
+
callSelf = true;
|
|
455
|
+
else
|
|
456
|
+
chain[chain.length - 1].call = true;
|
|
457
|
+
benign = true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
else if (ts.isExportSpecifier(p) || ts.isExportAssignment(p)) {
|
|
461
|
+
benign = true;
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
else if (ts.isHeritageClause(p)) {
|
|
465
|
+
benign = true;
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
let derivedInto;
|
|
473
|
+
if ((chain.length > 0 || callSelf) && !typeOnly) {
|
|
474
|
+
const p = cur.parent;
|
|
475
|
+
if (p &&
|
|
476
|
+
ts.isVariableDeclaration(p) &&
|
|
477
|
+
p.initializer === cur &&
|
|
478
|
+
ts.isIdentifier(p.name))
|
|
479
|
+
derivedInto = p.name.text;
|
|
480
|
+
}
|
|
481
|
+
return {
|
|
482
|
+
chain,
|
|
483
|
+
callSelf,
|
|
484
|
+
typeOnly,
|
|
485
|
+
computed,
|
|
486
|
+
benign,
|
|
487
|
+
...(derivedInto ? { derivedInto } : {}),
|
|
488
|
+
...(callbacks.length > 0 ? { callbackInto: [...new Set(callbacks)] } : {}),
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
function withoutBenign(r) {
|
|
492
|
+
const { benign: _b, ...rest } = r;
|
|
493
|
+
return rest;
|
|
494
|
+
}
|
|
495
|
+
function isReferencePosition(node) {
|
|
496
|
+
const p = node.parent;
|
|
497
|
+
if (!p)
|
|
498
|
+
return false;
|
|
499
|
+
if (ts.isPropertyAccessExpression(p) && p.name === node)
|
|
500
|
+
return false;
|
|
501
|
+
if (ts.isQualifiedName(p) && p.right === node)
|
|
502
|
+
return false;
|
|
503
|
+
if ((ts.isPropertyAssignment(p) ||
|
|
504
|
+
ts.isPropertyDeclaration(p) ||
|
|
505
|
+
ts.isPropertySignature(p) ||
|
|
506
|
+
ts.isMethodDeclaration(p) ||
|
|
507
|
+
ts.isMethodSignature(p) ||
|
|
508
|
+
ts.isGetAccessorDeclaration(p) ||
|
|
509
|
+
ts.isSetAccessorDeclaration(p) ||
|
|
510
|
+
ts.isEnumMember(p) ||
|
|
511
|
+
ts.isVariableDeclaration(p) ||
|
|
512
|
+
ts.isFunctionDeclaration(p) ||
|
|
513
|
+
ts.isFunctionExpression(p) ||
|
|
514
|
+
ts.isClassDeclaration(p) ||
|
|
515
|
+
ts.isClassExpression(p) ||
|
|
516
|
+
ts.isInterfaceDeclaration(p) ||
|
|
517
|
+
ts.isTypeAliasDeclaration(p) ||
|
|
518
|
+
ts.isEnumDeclaration(p) ||
|
|
519
|
+
ts.isModuleDeclaration(p) ||
|
|
520
|
+
ts.isParameter(p) ||
|
|
521
|
+
ts.isTypeParameterDeclaration(p) ||
|
|
522
|
+
ts.isJsxAttribute(p) ||
|
|
523
|
+
ts.isLabeledStatement(p) ||
|
|
524
|
+
ts.isBreakOrContinueStatement(p) ||
|
|
525
|
+
ts.isPropertySignature(p)) &&
|
|
526
|
+
p.name === node)
|
|
527
|
+
return false;
|
|
528
|
+
if (ts.isBindingElement(p) && (p.name === node || p.propertyName === node))
|
|
529
|
+
return false;
|
|
530
|
+
if (ts.isImportSpecifier(p) ||
|
|
531
|
+
ts.isImportClause(p) ||
|
|
532
|
+
ts.isNamespaceImport(p) ||
|
|
533
|
+
ts.isImportEqualsDeclaration(p) ||
|
|
534
|
+
ts.isNamespaceExport(p))
|
|
535
|
+
return false;
|
|
536
|
+
if (ts.isExportSpecifier(p) && p.parent.parent.moduleSpecifier)
|
|
537
|
+
return false;
|
|
538
|
+
if (ts.isImportTypeNode(p))
|
|
539
|
+
return false;
|
|
540
|
+
return true;
|
|
541
|
+
}
|
|
542
|
+
function isModuleSpecifierPosition(node) {
|
|
543
|
+
const p = node.parent;
|
|
544
|
+
if (!p)
|
|
545
|
+
return false;
|
|
546
|
+
if ((ts.isImportDeclaration(p) || ts.isExportDeclaration(p)) &&
|
|
547
|
+
p.moduleSpecifier === node)
|
|
548
|
+
return true;
|
|
549
|
+
if (ts.isExternalModuleReference(p))
|
|
550
|
+
return true;
|
|
551
|
+
if (ts.isCallExpression(p) &&
|
|
552
|
+
(p.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
553
|
+
(ts.isIdentifier(p.expression) && p.expression.text === "require")))
|
|
554
|
+
return true;
|
|
555
|
+
if (ts.isLiteralTypeNode(p) && ts.isImportTypeNode(p.parent))
|
|
556
|
+
return true;
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
559
|
+
function requireOrImportCall(init) {
|
|
560
|
+
let e = init;
|
|
561
|
+
let awaited = false;
|
|
562
|
+
while (e && (ts.isParenthesizedExpression(e) || ts.isAwaitExpression(e))) {
|
|
563
|
+
if (ts.isAwaitExpression(e))
|
|
564
|
+
awaited = true;
|
|
565
|
+
e = e.expression;
|
|
566
|
+
}
|
|
567
|
+
if (!e || !ts.isCallExpression(e))
|
|
568
|
+
return undefined;
|
|
569
|
+
const isRequire = ts.isIdentifier(e.expression) && e.expression.text === "require";
|
|
570
|
+
const isImport = e.expression.kind === ts.SyntaxKind.ImportKeyword && awaited;
|
|
571
|
+
if (!isRequire && !isImport)
|
|
572
|
+
return undefined;
|
|
573
|
+
const arg = e.arguments[0];
|
|
574
|
+
const specifier = arg && (ts.isStringLiteral(arg) || ts.isNoSubstitutionTemplateLiteral(arg))
|
|
575
|
+
? arg.text
|
|
576
|
+
: undefined;
|
|
577
|
+
return { call: e, specifier, dynamic: isImport };
|
|
578
|
+
}
|
|
579
|
+
function moduleExportName(n) {
|
|
580
|
+
return n.text;
|
|
581
|
+
}
|
|
582
|
+
function isNamedDeclaration(node) {
|
|
583
|
+
return (ts.isFunctionDeclaration(node) ||
|
|
584
|
+
ts.isClassDeclaration(node) ||
|
|
585
|
+
ts.isInterfaceDeclaration(node) ||
|
|
586
|
+
ts.isTypeAliasDeclaration(node) ||
|
|
587
|
+
ts.isEnumDeclaration(node) ||
|
|
588
|
+
ts.isModuleDeclaration(node));
|
|
589
|
+
}
|
|
590
|
+
function hasExport(node) {
|
|
591
|
+
return (ts.canHaveModifiers(node) &&
|
|
592
|
+
!!ts.getModifiers(node)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword));
|
|
593
|
+
}
|
|
594
|
+
function hasDefault(node) {
|
|
595
|
+
return (ts.canHaveModifiers(node) &&
|
|
596
|
+
!!ts
|
|
597
|
+
.getModifiers(node)
|
|
598
|
+
?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword));
|
|
599
|
+
}
|
|
600
|
+
function bindingNames(name) {
|
|
601
|
+
if (ts.isIdentifier(name))
|
|
602
|
+
return [name.text];
|
|
603
|
+
const out = [];
|
|
604
|
+
for (const el of name.elements)
|
|
605
|
+
if (!ts.isOmittedExpression(el))
|
|
606
|
+
out.push(...bindingNames(el.name));
|
|
607
|
+
return out;
|
|
608
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Inventory } from "../model.ts";
|
|
2
|
+
export type Target = {
|
|
3
|
+
kind: "file";
|
|
4
|
+
rel: string;
|
|
5
|
+
} | {
|
|
6
|
+
kind: "pkg";
|
|
7
|
+
installedId: string;
|
|
8
|
+
pkg: string;
|
|
9
|
+
entry: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "builtin";
|
|
12
|
+
} | {
|
|
13
|
+
kind: "unresolved-local";
|
|
14
|
+
} | {
|
|
15
|
+
kind: "unknown";
|
|
16
|
+
};
|
|
17
|
+
export declare class LocalResolver {
|
|
18
|
+
private readonly fileSet;
|
|
19
|
+
private readonly boundary;
|
|
20
|
+
private readonly installCache;
|
|
21
|
+
private readonly configCache;
|
|
22
|
+
private readonly byName;
|
|
23
|
+
private readonly root;
|
|
24
|
+
private readonly inventory;
|
|
25
|
+
constructor(root: string, files: string[], inventory: Inventory);
|
|
26
|
+
resolve(fromRel: string, specifier: string): Promise<Target>;
|
|
27
|
+
private nearestDeclared;
|
|
28
|
+
tryFile(rel: string): string | undefined;
|
|
29
|
+
private workspaceEntry;
|
|
30
|
+
private fromTsconfigPaths;
|
|
31
|
+
private pathsConfigFor;
|
|
32
|
+
private readPathsConfig;
|
|
33
|
+
}
|
|
34
|
+
export declare function exportsTarget(exp: unknown, subpath: string): string | undefined;
|
|
35
|
+
//# sourceMappingURL=resolve-local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-local.d.ts","sourceRoot":"","sources":["../../src/usage/resolve-local.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAgB,SAAS,EAAE,MAAM,aAAa,CAAA;AAI1D,MAAM,MAAM,MAAM,GACd;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAC7B;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAChE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,kBAAkB,CAAA;CAAE,GAC5B;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAA;AAwBvB,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAa;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAG1B;IACH,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA6C;IACzE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;IAC3D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;gBAEzB,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,SAAS;IAmBzD,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAuElE,OAAO,CAAC,eAAe;IAgBvB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAiBxC,OAAO,CAAC,cAAc;IAsCtB,OAAO,CAAC,iBAAiB;IA2CzB,OAAO,CAAC,cAAc;IAkBtB,OAAO,CAAC,eAAe;CAsCxB;AAMD,wBAAgB,aAAa,CAC3B,GAAG,EAAE,OAAO,EACZ,OAAO,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CAuDpB"}
|