@krisada-m/ksd-intelligence 0.0.0-stage → 1.0.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 +167 -2
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1002 -0
- package/dist/adapters/cli/policy-bin.js +10 -0
- package/dist/adapters/cli/review-verdict.js +3 -0
- package/dist/adapters/omp/extension.js +893 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +318 -0
- package/dist/adapters/ui/tree.js +460 -0
- package/dist/core/audit/index.js +2 -0
- package/dist/core/audit/log.js +505 -0
- package/dist/core/audit/summary.js +188 -0
- package/dist/core/changes/changes.js +591 -0
- package/dist/core/changes/git.js +266 -0
- package/dist/core/changes/plan.js +109 -0
- package/dist/core/changes/render.js +356 -0
- package/dist/core/engine/build.js +178 -0
- package/dist/core/engine/child.js +389 -0
- package/dist/core/engine/host.js +206 -0
- package/dist/core/engine/lock.js +133 -0
- package/dist/core/engine/protocol.js +43 -0
- package/dist/core/engine/runtime.js +44 -0
- package/dist/core/engine/status.js +381 -0
- package/dist/core/engine/typescript.js +194 -0
- package/dist/core/engine/workspace.js +72 -0
- package/dist/core/graph/graph.js +127 -0
- package/dist/core/graph/limits.js +16 -0
- package/dist/core/impact/query.js +1292 -0
- package/dist/core/impact/render.js +590 -0
- package/dist/core/impact/session.js +694 -0
- package/dist/core/indexing/declarations.js +455 -0
- package/dist/core/indexing/differential.js +345 -0
- package/dist/core/indexing/doctor.js +120 -0
- package/dist/core/indexing/indexer.js +792 -0
- package/dist/core/indexing/keys.js +451 -0
- package/dist/core/indexing/lexical.js +212 -0
- package/dist/core/indexing/packages.js +57 -0
- package/dist/core/indexing/program.js +171 -0
- package/dist/core/indexing/resolve.js +1004 -0
- package/dist/core/indexing/stamp.js +52 -0
- package/dist/core/indexing/surface.js +335 -0
- package/dist/core/indexing/ui-progress.js +37 -0
- package/dist/core/indexing/verify.js +76 -0
- package/dist/core/kernel/config.js +383 -0
- package/dist/core/kernel/glob.js +105 -0
- package/dist/core/kernel/guards.js +4 -0
- package/dist/core/kernel/types.js +3 -0
- package/dist/core/kernel/verdict.js +142 -0
- package/dist/core/knowledge/anchors.js +139 -0
- package/dist/core/knowledge/knowledge.js +95 -0
- package/dist/core/knowledge/learning.js +102 -0
- package/dist/core/knowledge/render.js +76 -0
- package/dist/core/knowledge/store.js +109 -0
- package/dist/core/knowledge/yaml.js +87 -0
- package/dist/core/search/lexical.js +382 -0
- package/dist/core/search/render.js +98 -0
- package/dist/core/search/segments.js +118 -0
- package/dist/core/verify/parse.js +127 -0
- package/dist/core/verify/render.js +67 -0
- package/dist/core/verify/report.js +40 -0
- package/dist/core/verify/revision.js +79 -0
- package/dist/core/verify/run.js +117 -0
- package/dist/core/verify/store.js +66 -0
- package/dist/core/verify/verify.js +173 -0
- package/dist/core/workspace/init.js +267 -0
- package/dist/core/workspace/probe.js +56 -0
- package/dist/core/workspace/scan.js +420 -0
- package/dist/index.js +20 -0
- package/dist/policy/change-scope.js +500 -0
- package/dist/policy/check-changed.js +40 -0
- package/dist/policy/config.js +106 -0
- package/dist/policy/dist/policy.d.ts +17 -0
- package/dist/policy/dist/policy.js +147 -0
- package/dist/policy/dist/runtime.d.ts +40 -0
- package/dist/policy/dist/runtime.js +75 -0
- package/dist/policy/dist/sdlc.d.ts +7 -0
- package/dist/policy/dist/sdlc.js +27 -0
- package/dist/policy/eslint-rules/index.js +107 -0
- package/dist/policy/index.d.ts +2 -0
- package/dist/policy/index.js +241 -0
- package/dist/policy/package.json +33 -0
- package/dist/policy/policy.d.ts +2 -0
- package/dist/policy/policy.js +2 -0
- package/dist/policy/review-verdict.js +262 -0
- package/dist/policy/sdlc.js +2 -0
- package/dist/storage/records.js +245 -0
- package/dist/storage/sqlite.js +314 -0
- package/dist/workflow/catalog.js +69 -0
- package/dist/workflow/classify.js +55 -0
- package/dist/workflow/loader.js +140 -0
- package/dist/workflow/packs.js +469 -0
- package/dist/workflow/registry.js +359 -0
- package/dist/workflow/render.js +61 -0
- package/dist/workflow/router.js +141 -0
- package/dist/workflow/sources.js +229 -0
- package/dist/workflow/types.js +44 -0
- package/dist/workflow/updates.js +532 -0
- package/package.json +74 -4
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +451 -0
- package/skills/ksd-intelligence/SKILL.md +54 -0
- package/skills/matt-sdlc/SKILL.md +42 -0
- package/skills/osmani-quality/SKILL.md +38 -0
- package/skills/poteto-mode/SKILL.md +40 -0
- package/skills/pstack-analysis/SKILL.md +36 -0
- package/skills.lock.json +71 -0
- package/skills.registry.json +181 -0
|
@@ -0,0 +1,1004 @@
|
|
|
1
|
+
import { isAbsolute, posix, relative, sep } from 'node:path';
|
|
2
|
+
import { MODULE_NAME, enclosingKey, fileDeclarations, } from './declarations.js';
|
|
3
|
+
import { KeyAccess } from './keys.js';
|
|
4
|
+
import { surfaceOf } from './surface.js';
|
|
5
|
+
const TEST_FILE = /(^|\/)(__tests__|tests?)\/|\.(test|spec)\.[cm]?[jt]sx?$/;
|
|
6
|
+
export function isTestFile(path) {
|
|
7
|
+
return TEST_FILE.test(path);
|
|
8
|
+
}
|
|
9
|
+
/** Scripts (no module symbol), ambient modules and global augmentations affect every file. */
|
|
10
|
+
export function isGlobalFile(ts, checker, sf) {
|
|
11
|
+
if (checker.getSymbolAtLocation(sf) === undefined)
|
|
12
|
+
return true;
|
|
13
|
+
return sf.statements.some((statement) => ts.isModuleDeclaration(statement) &&
|
|
14
|
+
(!ts.isIdentifier(statement.name) ||
|
|
15
|
+
(statement.flags & ts.NodeFlags.GlobalAugmentation) !== 0));
|
|
16
|
+
}
|
|
17
|
+
/** Resolves the files of one program (phase 3). Caches per-file facts it touches. */
|
|
18
|
+
export class Resolver {
|
|
19
|
+
ts;
|
|
20
|
+
program;
|
|
21
|
+
scope;
|
|
22
|
+
declarationCache = new Map();
|
|
23
|
+
fileCache = new Map();
|
|
24
|
+
checkerInstance;
|
|
25
|
+
keyAccessInstance;
|
|
26
|
+
declarationKinds;
|
|
27
|
+
constructor(ts, program, scope) {
|
|
28
|
+
this.ts = ts;
|
|
29
|
+
this.program = program;
|
|
30
|
+
this.scope = scope;
|
|
31
|
+
const k = ts.SyntaxKind;
|
|
32
|
+
// Nodes whose `name` is a name being declared, not a use.
|
|
33
|
+
this.declarationKinds = new Set([
|
|
34
|
+
k.VariableDeclaration,
|
|
35
|
+
k.FunctionDeclaration,
|
|
36
|
+
k.FunctionExpression,
|
|
37
|
+
k.ClassDeclaration,
|
|
38
|
+
k.ClassExpression,
|
|
39
|
+
k.InterfaceDeclaration,
|
|
40
|
+
k.TypeAliasDeclaration,
|
|
41
|
+
k.EnumDeclaration,
|
|
42
|
+
k.EnumMember,
|
|
43
|
+
k.ModuleDeclaration,
|
|
44
|
+
k.Parameter,
|
|
45
|
+
k.PropertyDeclaration,
|
|
46
|
+
k.PropertySignature,
|
|
47
|
+
k.MethodDeclaration,
|
|
48
|
+
k.MethodSignature,
|
|
49
|
+
k.GetAccessor,
|
|
50
|
+
k.SetAccessor,
|
|
51
|
+
k.PropertyAssignment,
|
|
52
|
+
k.BindingElement,
|
|
53
|
+
k.TypeParameter,
|
|
54
|
+
k.ImportEqualsDeclaration,
|
|
55
|
+
k.NamespaceExportDeclaration,
|
|
56
|
+
k.ImportSpecifier,
|
|
57
|
+
k.ImportClause,
|
|
58
|
+
k.NamespaceImport,
|
|
59
|
+
k.NamespaceExport,
|
|
60
|
+
k.ExportSpecifier,
|
|
61
|
+
k.JsxAttribute,
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
// Created on first use: the declaration pass needs syntax only, and a checker is the bulk of a
|
|
65
|
+
// program's memory.
|
|
66
|
+
get checker() {
|
|
67
|
+
this.checkerInstance ??= this.program.getTypeChecker();
|
|
68
|
+
return this.checkerInstance;
|
|
69
|
+
}
|
|
70
|
+
get keys() {
|
|
71
|
+
this.keyAccessInstance ??= new KeyAccess(this.ts, this.checker, {
|
|
72
|
+
indexedTargets: (property) => {
|
|
73
|
+
const keys = new Set();
|
|
74
|
+
for (const node of this.targetNodes(property)) {
|
|
75
|
+
const target = this.targetOfDeclaration(node);
|
|
76
|
+
if (typeof target === 'object')
|
|
77
|
+
keys.add(target.key);
|
|
78
|
+
}
|
|
79
|
+
return [...keys];
|
|
80
|
+
},
|
|
81
|
+
ownerKey: (declaration) => {
|
|
82
|
+
// Anonymous types have no key of their own (D51); scope the gap to their indexed parent.
|
|
83
|
+
// This also covers `keyof typeof obj`, whose literal belongs to its variable declaration.
|
|
84
|
+
let node = declaration;
|
|
85
|
+
while (node !== undefined) {
|
|
86
|
+
const target = this.targetOfDeclaration(node);
|
|
87
|
+
if (typeof target === 'object')
|
|
88
|
+
return target.key;
|
|
89
|
+
if (target === 'outside' || target === 'external')
|
|
90
|
+
return null;
|
|
91
|
+
node = node.parent;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return this.keyAccessInstance;
|
|
97
|
+
}
|
|
98
|
+
repoPath(fileName) {
|
|
99
|
+
const rel = relative(this.scope.root, fileName);
|
|
100
|
+
const posixRel = sep === '/' ? rel : rel.split(sep).join('/');
|
|
101
|
+
if (posixRel.startsWith('..') || isAbsolute(posixRel))
|
|
102
|
+
return null;
|
|
103
|
+
if (posixRel.startsWith('node_modules/') || posixRel.includes('/node_modules/'))
|
|
104
|
+
return null;
|
|
105
|
+
return posixRel;
|
|
106
|
+
}
|
|
107
|
+
infoOf(sf) {
|
|
108
|
+
let info = this.fileCache.get(sf);
|
|
109
|
+
if (info === undefined) {
|
|
110
|
+
const path = this.repoPath(sf.fileName);
|
|
111
|
+
info = {
|
|
112
|
+
path,
|
|
113
|
+
inScope: path !== null && this.scope.indexable.has(path),
|
|
114
|
+
library: path === null || this.program.isSourceFileDefaultLibrary(sf),
|
|
115
|
+
};
|
|
116
|
+
this.fileCache.set(sf, info);
|
|
117
|
+
}
|
|
118
|
+
return info;
|
|
119
|
+
}
|
|
120
|
+
declarationsOf(sf) {
|
|
121
|
+
let found = this.declarationCache.get(sf);
|
|
122
|
+
if (found === undefined) {
|
|
123
|
+
found = fileDeclarations(this.ts, sf, this.repoPath(sf.fileName) ?? sf.fileName);
|
|
124
|
+
this.declarationCache.set(sf, found);
|
|
125
|
+
}
|
|
126
|
+
return found;
|
|
127
|
+
}
|
|
128
|
+
targetOfDeclaration(node) {
|
|
129
|
+
const sf = node.getSourceFile();
|
|
130
|
+
const info = this.infoOf(sf);
|
|
131
|
+
if (info.library)
|
|
132
|
+
return 'external';
|
|
133
|
+
if (!info.inScope || info.path === null)
|
|
134
|
+
return 'outside';
|
|
135
|
+
const key = this.declarationsOf(sf).keyOf.get(node);
|
|
136
|
+
return key === undefined ? 'local' : { key, file: info.path };
|
|
137
|
+
}
|
|
138
|
+
resolveAliases(symbol) {
|
|
139
|
+
return symbol.flags & this.ts.SymbolFlags.Alias
|
|
140
|
+
? this.checker.getAliasedSymbol(symbol)
|
|
141
|
+
: symbol;
|
|
142
|
+
}
|
|
143
|
+
/** Declarations of a symbol: root symbols of union/mapped properties, aliases followed. */
|
|
144
|
+
targetNodes(symbol) {
|
|
145
|
+
const resolved = this.resolveAliases(symbol);
|
|
146
|
+
const roots = resolved.flags & this.ts.SymbolFlags.Transient
|
|
147
|
+
? this.checker.getRootSymbols(resolved)
|
|
148
|
+
: [resolved];
|
|
149
|
+
// One use of a declaration is one target, however many instantiations reach it.
|
|
150
|
+
const nodes = new Set();
|
|
151
|
+
for (const root of roots) {
|
|
152
|
+
const first = this.resolveAliases(root).declarations?.[0];
|
|
153
|
+
if (first !== undefined)
|
|
154
|
+
nodes.add(first);
|
|
155
|
+
}
|
|
156
|
+
return [...nodes];
|
|
157
|
+
}
|
|
158
|
+
moduleTarget(spec, sf) {
|
|
159
|
+
const ts = this.ts;
|
|
160
|
+
const declaration = this.checker.getSymbolAtLocation(spec)?.declarations?.[0];
|
|
161
|
+
let fileName;
|
|
162
|
+
if (declaration !== undefined && ts.isSourceFile(declaration))
|
|
163
|
+
fileName = declaration.fileName;
|
|
164
|
+
else if (declaration !== undefined)
|
|
165
|
+
return { kind: 'external' }; // `declare module "x"`
|
|
166
|
+
else {
|
|
167
|
+
const resolved = ts.resolveModuleName(spec.text, sf.fileName, this.program.getCompilerOptions(), ts.sys, undefined, undefined, this.program.getModeForUsageLocation(sf, spec));
|
|
168
|
+
fileName = resolved.resolvedModule?.resolvedFileName;
|
|
169
|
+
}
|
|
170
|
+
if (fileName === undefined) {
|
|
171
|
+
return { kind: this.isInRepoSpecifier(spec.text) ? 'unresolved' : 'bare' };
|
|
172
|
+
}
|
|
173
|
+
const path = this.repoPath(fileName);
|
|
174
|
+
if (path === null)
|
|
175
|
+
return { kind: 'external' };
|
|
176
|
+
return this.scope.indexable.has(path) ? { kind: 'file', path } : { kind: 'outside' };
|
|
177
|
+
}
|
|
178
|
+
/** Relative, `paths` alias or workspace package specifiers point into the repository. */
|
|
179
|
+
isInRepoSpecifier(text) {
|
|
180
|
+
if (text.startsWith('.') || text.startsWith('/'))
|
|
181
|
+
return true;
|
|
182
|
+
for (const pattern of Object.keys(this.program.getCompilerOptions().paths ?? {})) {
|
|
183
|
+
const star = pattern.indexOf('*');
|
|
184
|
+
if (star === -1 ? text === pattern : text.startsWith(pattern.slice(0, star)))
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
for (const name of this.scope.workspacePackages) {
|
|
188
|
+
if (text === name || text.startsWith(`${name}/`))
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
resolveFile(sf) {
|
|
194
|
+
const ts = this.ts;
|
|
195
|
+
const checker = this.checker;
|
|
196
|
+
const file = this.repoPath(sf.fileName) ?? sf.fileName;
|
|
197
|
+
const declarations = this.declarationsOf(sf);
|
|
198
|
+
const edges = new Map();
|
|
199
|
+
const unresolved = new Map();
|
|
200
|
+
const gaps = new Map();
|
|
201
|
+
const externalImports = new Map();
|
|
202
|
+
const counts = {
|
|
203
|
+
indexed: 0,
|
|
204
|
+
local: 0,
|
|
205
|
+
external: 0,
|
|
206
|
+
externalUnresolved: 0,
|
|
207
|
+
unresolvedOther: 0,
|
|
208
|
+
outsideScope: 0,
|
|
209
|
+
};
|
|
210
|
+
const test = isTestFile(file);
|
|
211
|
+
const line = (node) => sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
|
|
212
|
+
const addEdge = (source, target, kind, at) => {
|
|
213
|
+
const id = `${source}\u0000${target}\u0000${kind}`;
|
|
214
|
+
const existing = edges.get(id);
|
|
215
|
+
if (existing === undefined) {
|
|
216
|
+
edges.set(id, { source, target, kind, strength: 'compiler', lines: [at] });
|
|
217
|
+
}
|
|
218
|
+
else if (!existing.lines.includes(at)) {
|
|
219
|
+
existing.lines.push(at);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const addUnresolved = (record) => {
|
|
223
|
+
unresolved.set(`${record.line}\u0000${record.name}\u0000${record.reason}`, record);
|
|
224
|
+
};
|
|
225
|
+
const addGap = (record) => {
|
|
226
|
+
gaps.set(`${record.line}\u0000${record.code}\u0000${record.detail}`, record);
|
|
227
|
+
};
|
|
228
|
+
const markers = new Map();
|
|
229
|
+
const addMarker = (kind, at) => {
|
|
230
|
+
const record = { file, line: line(at), kind };
|
|
231
|
+
markers.set(`${record.line}\u0000${kind}`, record);
|
|
232
|
+
};
|
|
233
|
+
const edgeTo = (source, node, kind, at) => {
|
|
234
|
+
const target = this.targetOfDeclaration(node);
|
|
235
|
+
if (target === 'external')
|
|
236
|
+
counts.external++;
|
|
237
|
+
else if (target === 'outside')
|
|
238
|
+
counts.outsideScope++;
|
|
239
|
+
else if (target === 'local')
|
|
240
|
+
counts.local++;
|
|
241
|
+
else {
|
|
242
|
+
counts.indexed++;
|
|
243
|
+
addEdge(source, target.key, kind, at);
|
|
244
|
+
}
|
|
245
|
+
return target;
|
|
246
|
+
};
|
|
247
|
+
/** File-level IMPORTS (and TESTS) edge, or a record of why there is none. */
|
|
248
|
+
const fileImport = (spec, at) => {
|
|
249
|
+
const target = this.moduleTarget(spec, sf);
|
|
250
|
+
if (target.kind === 'file') {
|
|
251
|
+
const moduleKey = `${target.path}#${MODULE_NAME}`;
|
|
252
|
+
counts.indexed++;
|
|
253
|
+
addEdge(declarations.moduleKey, moduleKey, 'IMPORTS', line(at));
|
|
254
|
+
if (test)
|
|
255
|
+
addEdge(declarations.moduleKey, moduleKey, 'TESTS', line(at));
|
|
256
|
+
return target.path;
|
|
257
|
+
}
|
|
258
|
+
if (target.kind === 'unresolved') {
|
|
259
|
+
addUnresolved({
|
|
260
|
+
file,
|
|
261
|
+
line: line(at),
|
|
262
|
+
name: spec.text,
|
|
263
|
+
reason: 'unresolved-import',
|
|
264
|
+
source: enclosingKey(at, declarations),
|
|
265
|
+
});
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
if (target.kind === 'outside')
|
|
269
|
+
counts.outsideScope++;
|
|
270
|
+
else if (target.kind === 'external')
|
|
271
|
+
counts.external++;
|
|
272
|
+
else
|
|
273
|
+
counts.externalUnresolved++;
|
|
274
|
+
if (target.kind !== 'outside' && !spec.text.startsWith('.')) {
|
|
275
|
+
externalImports.set(`${spec.text}\u0000${line(at)}`, {
|
|
276
|
+
file,
|
|
277
|
+
specifier: spec.text,
|
|
278
|
+
line: line(at),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
};
|
|
283
|
+
const staticImport = (spec, at) => {
|
|
284
|
+
const targetKind = this.moduleTarget(spec, sf).kind;
|
|
285
|
+
const trackBindings = targetKind === 'file' || targetKind === 'unresolved';
|
|
286
|
+
const target = fileImport(spec, at);
|
|
287
|
+
if (target !== null)
|
|
288
|
+
addMarker(`static-import:${target}`, at);
|
|
289
|
+
return { path: target, trackBindings };
|
|
290
|
+
};
|
|
291
|
+
const brokenImports = new Map();
|
|
292
|
+
/** An import or re-export binding is unresolved when the module or export fails to resolve. */
|
|
293
|
+
const binding = (name, names, at) => {
|
|
294
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
295
|
+
if (symbol === undefined)
|
|
296
|
+
return;
|
|
297
|
+
if ((this.resolveAliases(symbol).declarations ?? []).length > 0)
|
|
298
|
+
return;
|
|
299
|
+
const filtered = [...new Set(names.filter((entry) => entry !== 'default' && entry !== ''))];
|
|
300
|
+
if (filtered.length === 0)
|
|
301
|
+
return;
|
|
302
|
+
brokenImports.set(symbol, filtered);
|
|
303
|
+
for (const imported of filtered) {
|
|
304
|
+
addUnresolved({
|
|
305
|
+
file,
|
|
306
|
+
line: line(at),
|
|
307
|
+
name: imported,
|
|
308
|
+
reason: 'unresolved-import',
|
|
309
|
+
source: declarations.moduleKey,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
const kindOfUse = (id, targetIsClass) => {
|
|
314
|
+
let expr = id;
|
|
315
|
+
const parent = id.parent;
|
|
316
|
+
if ((ts.isPropertyAccessExpression(parent) && parent.name === id) ||
|
|
317
|
+
(ts.isQualifiedName(parent) && parent.right === id) ||
|
|
318
|
+
(ts.isElementAccessExpression(parent) && parent.argumentExpression === id)) {
|
|
319
|
+
expr = parent;
|
|
320
|
+
}
|
|
321
|
+
while (ts.isParenthesizedExpression(expr.parent) ||
|
|
322
|
+
ts.isNonNullExpression(expr.parent) ||
|
|
323
|
+
ts.isAsExpression(expr.parent) ||
|
|
324
|
+
ts.isSatisfiesExpression(expr.parent)) {
|
|
325
|
+
expr = expr.parent;
|
|
326
|
+
}
|
|
327
|
+
const user = expr.parent;
|
|
328
|
+
if (ts.isNewExpression(user) && user.expression === expr) {
|
|
329
|
+
return targetIsClass ? 'CONSTRUCTS' : 'CALLS';
|
|
330
|
+
}
|
|
331
|
+
if (ts.isCallExpression(user) && user.expression === expr)
|
|
332
|
+
return 'CALLS';
|
|
333
|
+
if (ts.isTaggedTemplateExpression(user) && user.tag === expr)
|
|
334
|
+
return 'CALLS';
|
|
335
|
+
if (ts.isDecorator(user) && user.expression === expr)
|
|
336
|
+
return 'CALLS';
|
|
337
|
+
return 'REFERENCES';
|
|
338
|
+
};
|
|
339
|
+
const isAbstractOrInterfaceMember = (node) => ((ts.isMethodSignature(node) || ts.isPropertySignature(node)) &&
|
|
340
|
+
ts.isInterfaceDeclaration(node.parent)) ||
|
|
341
|
+
((ts.isMethodDeclaration(node) || ts.isPropertyDeclaration(node)) &&
|
|
342
|
+
(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Abstract) !== 0);
|
|
343
|
+
/** Names being declared, labels and syntax are not uses. */
|
|
344
|
+
const isNotUse = (id) => {
|
|
345
|
+
const parent = id.parent;
|
|
346
|
+
if (ts.isShorthandPropertyAssignment(parent))
|
|
347
|
+
return false;
|
|
348
|
+
if (ts.isMetaProperty(parent) || ts.isLabeledStatement(parent))
|
|
349
|
+
return true;
|
|
350
|
+
if (ts.isBreakOrContinueStatement(parent) || ts.isTypePredicateNode(parent))
|
|
351
|
+
return true;
|
|
352
|
+
if (ts.isNamedTupleMember(parent) || ts.isJSDocParameterTag(parent))
|
|
353
|
+
return true;
|
|
354
|
+
// `exports.x = …`, `module.exports.x = …`: the CommonJS declaration's own name
|
|
355
|
+
if (ts.isPropertyAccessExpression(parent) && parent.name === id) {
|
|
356
|
+
return declarations.keyOf.has(parent);
|
|
357
|
+
}
|
|
358
|
+
return (this.declarationKinds.has(parent.kind) &&
|
|
359
|
+
'name' in parent &&
|
|
360
|
+
parent.name === id);
|
|
361
|
+
};
|
|
362
|
+
const use = (id, symbol) => {
|
|
363
|
+
const at = line(id);
|
|
364
|
+
if ((this.resolveAliases(symbol).declarations ?? []).length === 0) {
|
|
365
|
+
const names = brokenImports.get(symbol);
|
|
366
|
+
if (names === undefined)
|
|
367
|
+
return;
|
|
368
|
+
const source = enclosingKey(id, declarations);
|
|
369
|
+
if (ts.isPropertyAccessExpression(id.parent) && id.parent.expression === id) {
|
|
370
|
+
const prop = id.parent.name;
|
|
371
|
+
const propReason = kindOfUse(prop, false) === 'CALLS' ? 'unresolved-name-match' : 'unresolved-import';
|
|
372
|
+
addUnresolved({ file, line: line(prop), name: prop.text, reason: propReason, source });
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const reason = kindOfUse(id, false) === 'CALLS' ? 'unresolved-name-match' : 'unresolved-import';
|
|
376
|
+
for (const imported of names) {
|
|
377
|
+
addUnresolved({ file, line: at, name: imported, reason, source });
|
|
378
|
+
}
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const source = enclosingKey(id, declarations);
|
|
382
|
+
const isClass = (this.resolveAliases(symbol).flags & ts.SymbolFlags.Class) !== 0;
|
|
383
|
+
const kind = kindOfUse(id, isClass);
|
|
384
|
+
for (const node of this.targetNodes(symbol)) {
|
|
385
|
+
const target = edgeTo(source, node, kind, at);
|
|
386
|
+
if (typeof target === 'object' && kind === 'CALLS' && isAbstractOrInterfaceMember(node)) {
|
|
387
|
+
addGap({ file, line: at, code: 'interface-dispatch', detail: target.key, source });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
// One contextual type per literal, not per property (it was 28% of resolve time on oh-my-pi).
|
|
392
|
+
const contextual = new Map();
|
|
393
|
+
const contextualTypeOf = (literal) => {
|
|
394
|
+
if (!contextual.has(literal))
|
|
395
|
+
contextual.set(literal, checker.getContextualType(literal));
|
|
396
|
+
return contextual.get(literal);
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* The property `name` of a type; of each member when the type is a union (`A | B`, or
|
|
400
|
+
* `A | undefined` for an optional parameter), where a union-wide property may not exist.
|
|
401
|
+
*/
|
|
402
|
+
const propertiesOfType = (type, name) => {
|
|
403
|
+
if (type === undefined)
|
|
404
|
+
return [];
|
|
405
|
+
const found = new Set();
|
|
406
|
+
for (const part of type.isUnion() ? type.types : [type]) {
|
|
407
|
+
const property = checker.getPropertyOfType(checker.getApparentType(part), name);
|
|
408
|
+
if (property !== undefined)
|
|
409
|
+
found.add(property);
|
|
410
|
+
}
|
|
411
|
+
return [...found];
|
|
412
|
+
};
|
|
413
|
+
const identifier = (id) => {
|
|
414
|
+
const parent = id.parent;
|
|
415
|
+
// `const { a } = obj`: the binding names a property of obj's type as well as a local.
|
|
416
|
+
if (ts.isBindingElement(parent) &&
|
|
417
|
+
parent.name === id &&
|
|
418
|
+
parent.propertyName === undefined &&
|
|
419
|
+
ts.isObjectBindingPattern(parent.parent)) {
|
|
420
|
+
for (const property of propertiesOfType(checker.getTypeAtLocation(parent.parent), id.text)) {
|
|
421
|
+
use(id, property);
|
|
422
|
+
}
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
// `{ a: 1 }`, `{ a() {} }`, `{ get a() {} }`, `{ a }` typed by context: the literal's property
|
|
426
|
+
// implements the contextual one. A shorthand also uses the value it names, below.
|
|
427
|
+
if ((ts.isPropertyAssignment(parent) ||
|
|
428
|
+
ts.isMethodDeclaration(parent) ||
|
|
429
|
+
ts.isGetAccessorDeclaration(parent) ||
|
|
430
|
+
ts.isSetAccessorDeclaration(parent) ||
|
|
431
|
+
ts.isShorthandPropertyAssignment(parent)) &&
|
|
432
|
+
parent.name === id &&
|
|
433
|
+
ts.isObjectLiteralExpression(parent.parent)) {
|
|
434
|
+
for (const property of propertiesOfType(contextualTypeOf(parent.parent), id.text)) {
|
|
435
|
+
if (!(property.declarations ?? []).includes(parent))
|
|
436
|
+
use(id, property);
|
|
437
|
+
}
|
|
438
|
+
if (!ts.isShorthandPropertyAssignment(parent))
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (isNotUse(id))
|
|
442
|
+
return;
|
|
443
|
+
const symbol = ts.isShorthandPropertyAssignment(parent) && parent.name === id
|
|
444
|
+
? checker.getShorthandAssignmentValueSymbol(parent)
|
|
445
|
+
: checker.getSymbolAtLocation(id);
|
|
446
|
+
if (symbol !== undefined) {
|
|
447
|
+
use(id, symbol);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const name = id.text;
|
|
451
|
+
if (name === 'exports' || name === 'module' || name === 'require' || name === 'undefined') {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (this.scope.declarationNames.has(name)) {
|
|
455
|
+
addUnresolved({
|
|
456
|
+
file,
|
|
457
|
+
line: line(id),
|
|
458
|
+
name,
|
|
459
|
+
reason: 'unresolved-name-match',
|
|
460
|
+
source: enclosingKey(id, declarations),
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
counts.unresolvedOther++;
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
// A literal that names a property is a use of it: `T["k"]`, and the key of `{ "k": v }` or
|
|
468
|
+
// `{ 11: v }` typed by context, like the identifier forms above.
|
|
469
|
+
const propertyNameLiteral = (node) => {
|
|
470
|
+
const parent = node.parent;
|
|
471
|
+
if (ts.isLiteralTypeNode(parent) &&
|
|
472
|
+
ts.isIndexedAccessTypeNode(parent.parent) &&
|
|
473
|
+
parent.parent.indexType === parent) {
|
|
474
|
+
for (const property of propertiesOfType(checker.getTypeFromTypeNode(parent.parent.objectType), node.text)) {
|
|
475
|
+
use(node, property);
|
|
476
|
+
}
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if ((ts.isPropertyAssignment(parent) ||
|
|
480
|
+
ts.isMethodDeclaration(parent) ||
|
|
481
|
+
ts.isGetAccessorDeclaration(parent) ||
|
|
482
|
+
ts.isSetAccessorDeclaration(parent)) &&
|
|
483
|
+
parent.name === node &&
|
|
484
|
+
ts.isObjectLiteralExpression(parent.parent)) {
|
|
485
|
+
for (const property of propertiesOfType(contextualTypeOf(parent.parent), node.text)) {
|
|
486
|
+
if (!(property.declarations ?? []).includes(parent))
|
|
487
|
+
use(node, property);
|
|
488
|
+
}
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (ts.isStringLiteralLike(node) && isKeyPosition(node))
|
|
492
|
+
keyLiteral(node);
|
|
493
|
+
};
|
|
494
|
+
// Declared names, `T["k"]` and `obj["k"]` are handled above and in elementAccess.
|
|
495
|
+
// Do not filter by indexed names: an unindexed anonymous member still needs an owner gap.
|
|
496
|
+
const isKeyPosition = (node) => {
|
|
497
|
+
const parent = node.parent;
|
|
498
|
+
if (ts.isLiteralTypeNode(parent) || ts.isExternalModuleReference(parent))
|
|
499
|
+
return false;
|
|
500
|
+
if (ts.isElementAccessExpression(parent) && parent.argumentExpression === node)
|
|
501
|
+
return false;
|
|
502
|
+
return !('name' in parent && parent.name === node);
|
|
503
|
+
};
|
|
504
|
+
/** `get(u, "cost")` with `K extends keyof Usage`: an edge to `Usage.cost`, or gaps. */
|
|
505
|
+
const keyLiteral = (node) => {
|
|
506
|
+
const found = this.keys.literal(node);
|
|
507
|
+
if (found === null)
|
|
508
|
+
return;
|
|
509
|
+
if (found.kind === 'property') {
|
|
510
|
+
use(node, found.property);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
keyGaps(node, found.owners);
|
|
514
|
+
};
|
|
515
|
+
/** A key that may name any member of each owner: every member's picture is partial. */
|
|
516
|
+
const keyGaps = (node, owners) => {
|
|
517
|
+
const source = enclosingKey(node, declarations);
|
|
518
|
+
for (const owner of owners) {
|
|
519
|
+
addGap({ file, line: line(node), code: 'computed-member-access', detail: owner, source });
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
/** `get(u, k)` with `k: keyof Usage` passed where a key is declared: a gap on Usage. */
|
|
523
|
+
const keyArguments = (node) => {
|
|
524
|
+
if (node.expression.kind === ts.SyntaxKind.ImportKeyword)
|
|
525
|
+
return;
|
|
526
|
+
for (const argument of node.arguments ?? []) {
|
|
527
|
+
// Literals are handled separately; containers and callbacks are not scalar keys.
|
|
528
|
+
if (ts.isStringLiteralLike(argument) ||
|
|
529
|
+
ts.isNumericLiteral(argument) ||
|
|
530
|
+
ts.isArrayLiteralExpression(argument) ||
|
|
531
|
+
ts.isObjectLiteralExpression(argument) ||
|
|
532
|
+
ts.isArrowFunction(argument) ||
|
|
533
|
+
ts.isFunctionExpression(argument)) {
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const owners = this.keys.expression(argument);
|
|
537
|
+
if (owners.length > 0 && this.keys.takesKey(node, argument))
|
|
538
|
+
keyGaps(argument, owners);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
const heritage = (node) => {
|
|
542
|
+
const ownerKey = declarations.keyOf.get(node);
|
|
543
|
+
if (ownerKey === undefined)
|
|
544
|
+
return;
|
|
545
|
+
for (const clause of node.heritageClauses ?? []) {
|
|
546
|
+
const isImplements = clause.token === ts.SyntaxKind.ImplementsKeyword;
|
|
547
|
+
// Type-level `IMPLEMENTS` applies to `implements` and interface extension, not class `extends`.
|
|
548
|
+
const addTypeLevel = !ts.isClassLike(node) || isImplements;
|
|
549
|
+
for (const typeNode of clause.types) {
|
|
550
|
+
if (addTypeLevel) {
|
|
551
|
+
const expression = typeNode.expression;
|
|
552
|
+
const name = ts.isPropertyAccessExpression(expression) ? expression.name : expression;
|
|
553
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
554
|
+
if (symbol !== undefined) {
|
|
555
|
+
for (const declaration of this.targetNodes(symbol)) {
|
|
556
|
+
const target = this.targetOfDeclaration(declaration);
|
|
557
|
+
if (typeof target === 'object') {
|
|
558
|
+
addEdge(ownerKey, target.key, 'IMPLEMENTS', line(typeNode));
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
const inherited = checker.getTypeAtLocation(typeNode);
|
|
564
|
+
for (const member of node.members) {
|
|
565
|
+
if (ts.isClassLike(node) &&
|
|
566
|
+
(ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static) !== 0) {
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
const memberKey = declarations.keyOf.get(member);
|
|
570
|
+
const memberName = member.name;
|
|
571
|
+
if (memberKey === undefined || memberName === undefined)
|
|
572
|
+
continue;
|
|
573
|
+
if (!ts.isIdentifier(memberName) && !ts.isStringLiteral(memberName))
|
|
574
|
+
continue;
|
|
575
|
+
const property = checker.getPropertyOfType(inherited, memberName.text);
|
|
576
|
+
for (const declaration of property?.declarations ?? []) {
|
|
577
|
+
const target = this.targetOfDeclaration(declaration);
|
|
578
|
+
if (typeof target === 'object') {
|
|
579
|
+
addEdge(memberKey, target.key, 'IMPLEMENTS', line(memberName));
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const unwrapExpression = (expr) => {
|
|
587
|
+
let current = expr;
|
|
588
|
+
while (true) {
|
|
589
|
+
if (ts.isParenthesizedExpression(current) ||
|
|
590
|
+
ts.isAsExpression(current) ||
|
|
591
|
+
ts.isSatisfiesExpression(current) ||
|
|
592
|
+
ts.isTypeAssertionExpression(current) ||
|
|
593
|
+
ts.isNonNullExpression(current)) {
|
|
594
|
+
current = current.expression;
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
return current;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
const extractHead = (expr) => {
|
|
601
|
+
const current = unwrapExpression(expr);
|
|
602
|
+
if (ts.isTemplateExpression(current))
|
|
603
|
+
return current.head.text;
|
|
604
|
+
if (ts.isStringLiteralLike(current))
|
|
605
|
+
return current.text;
|
|
606
|
+
if (ts.isBinaryExpression(current) &&
|
|
607
|
+
current.operatorToken.kind === ts.SyntaxKind.PlusToken) {
|
|
608
|
+
const leftUnwrapped = unwrapExpression(current.left);
|
|
609
|
+
const rightUnwrapped = unwrapExpression(current.right);
|
|
610
|
+
const leftHead = extractHead(leftUnwrapped);
|
|
611
|
+
if (ts.isStringLiteralLike(leftUnwrapped) && ts.isStringLiteralLike(rightUnwrapped)) {
|
|
612
|
+
return leftHead + rightUnwrapped.text;
|
|
613
|
+
}
|
|
614
|
+
return leftHead;
|
|
615
|
+
}
|
|
616
|
+
return '';
|
|
617
|
+
};
|
|
618
|
+
const staticPrefix = (arg) => {
|
|
619
|
+
const head = extractHead(arg);
|
|
620
|
+
if (!head.startsWith('.'))
|
|
621
|
+
return head === '' ? '*' : head;
|
|
622
|
+
// normalize keeps a trailing slash, so `./handlers/` stays a directory prefix
|
|
623
|
+
return posix.normalize(posix.join(posix.dirname(file), head));
|
|
624
|
+
};
|
|
625
|
+
const call = (node) => {
|
|
626
|
+
const [arg] = node.arguments;
|
|
627
|
+
if (arg === undefined)
|
|
628
|
+
return;
|
|
629
|
+
if (node.expression.kind === ts.SyntaxKind.ImportKeyword) {
|
|
630
|
+
if (ts.isStringLiteralLike(arg)) {
|
|
631
|
+
fileImport(arg, node);
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
addGap({
|
|
635
|
+
file,
|
|
636
|
+
line: line(node),
|
|
637
|
+
code: 'dynamic-import',
|
|
638
|
+
detail: staticPrefix(arg),
|
|
639
|
+
source: enclosingKey(node, declarations),
|
|
640
|
+
});
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
const isRequire = ts.isIdentifier(node.expression) &&
|
|
644
|
+
node.expression.text === 'require' &&
|
|
645
|
+
node.arguments.length === 1;
|
|
646
|
+
if (!isRequire)
|
|
647
|
+
return;
|
|
648
|
+
const source = enclosingKey(node, declarations);
|
|
649
|
+
if (!ts.isStringLiteralLike(arg)) {
|
|
650
|
+
addGap({
|
|
651
|
+
file,
|
|
652
|
+
line: line(node),
|
|
653
|
+
code: 'cjs-require',
|
|
654
|
+
detail: staticPrefix(arg),
|
|
655
|
+
source,
|
|
656
|
+
});
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
// TypeScript binds `require` only in JavaScript files; in TypeScript its result is untyped.
|
|
660
|
+
const bound = checker.getSymbolAtLocation(arg) !== undefined;
|
|
661
|
+
const path = fileImport(arg, node);
|
|
662
|
+
if (!bound) {
|
|
663
|
+
addGap({ file, line: line(node), code: 'cjs-require', detail: path ?? arg.text, source });
|
|
664
|
+
}
|
|
665
|
+
};
|
|
666
|
+
const propertyNameText = (name) => {
|
|
667
|
+
if (ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)) {
|
|
668
|
+
return name.text;
|
|
669
|
+
}
|
|
670
|
+
return null;
|
|
671
|
+
};
|
|
672
|
+
const emitComputedOwner = (declaration, atNode, source) => {
|
|
673
|
+
if (ts.isSourceFile(declaration)) {
|
|
674
|
+
const info = this.infoOf(declaration);
|
|
675
|
+
if (info.inScope && info.path !== null) {
|
|
676
|
+
addGap({
|
|
677
|
+
file,
|
|
678
|
+
line: line(atNode),
|
|
679
|
+
code: 'computed-member-access',
|
|
680
|
+
detail: `${info.path}#${MODULE_NAME}`,
|
|
681
|
+
source,
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const initializer = (ts.isVariableDeclaration(declaration) ||
|
|
687
|
+
ts.isPropertyDeclaration(declaration) ||
|
|
688
|
+
ts.isPropertyAssignment(declaration)) &&
|
|
689
|
+
declaration.initializer !== undefined
|
|
690
|
+
? unwrapExpression(declaration.initializer)
|
|
691
|
+
: undefined;
|
|
692
|
+
const holdsMembers = ts.isClassLike(declaration) ||
|
|
693
|
+
ts.isEnumDeclaration(declaration) ||
|
|
694
|
+
ts.isModuleDeclaration(declaration) ||
|
|
695
|
+
(initializer !== undefined && ts.isObjectLiteralExpression(initializer));
|
|
696
|
+
if (!holdsMembers)
|
|
697
|
+
return;
|
|
698
|
+
const target = this.targetOfDeclaration(declaration);
|
|
699
|
+
if (typeof target === 'object') {
|
|
700
|
+
addGap({
|
|
701
|
+
file,
|
|
702
|
+
line: line(atNode),
|
|
703
|
+
code: 'computed-member-access',
|
|
704
|
+
detail: target.key,
|
|
705
|
+
source,
|
|
706
|
+
});
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (target === 'local') {
|
|
710
|
+
const ownerSf = declaration.getSourceFile();
|
|
711
|
+
const info = this.infoOf(ownerSf);
|
|
712
|
+
if (info.inScope && info.path !== null) {
|
|
713
|
+
addGap({
|
|
714
|
+
file,
|
|
715
|
+
line: line(atNode),
|
|
716
|
+
code: 'computed-member-access',
|
|
717
|
+
detail: enclosingKey(declaration, this.declarationsOf(ownerSf)),
|
|
718
|
+
source,
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
const elementAccess = (node) => {
|
|
724
|
+
const argument = node.argumentExpression;
|
|
725
|
+
if (ts.isStringLiteralLike(argument) || ts.isNumericLiteral(argument)) {
|
|
726
|
+
// `obj['literal']` is a use of that member.
|
|
727
|
+
const symbol = checker.getSymbolAtLocation(argument);
|
|
728
|
+
if (symbol !== undefined)
|
|
729
|
+
use(argument, symbol);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
// `u[k]` with `k: keyof Usage`: any member of Usage may be read.
|
|
733
|
+
keyGaps(node, this.keys.expression(argument));
|
|
734
|
+
const source = enclosingKey(node, declarations);
|
|
735
|
+
const unwrapped = unwrapExpression(node.expression);
|
|
736
|
+
if (ts.isObjectLiteralExpression(unwrapped)) {
|
|
737
|
+
const ownerSf = unwrapped.getSourceFile();
|
|
738
|
+
const info = this.infoOf(ownerSf);
|
|
739
|
+
if (info.inScope && info.path !== null) {
|
|
740
|
+
addGap({
|
|
741
|
+
file,
|
|
742
|
+
line: line(node),
|
|
743
|
+
code: 'computed-member-access',
|
|
744
|
+
detail: enclosingKey(unwrapped, this.declarationsOf(ownerSf)),
|
|
745
|
+
source,
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const chainNames = [];
|
|
751
|
+
const chainNodes = [];
|
|
752
|
+
let root = unwrapped;
|
|
753
|
+
while (true) {
|
|
754
|
+
const current = unwrapExpression(root);
|
|
755
|
+
if (ts.isPropertyAccessExpression(current)) {
|
|
756
|
+
chainNames.push(current.name.text);
|
|
757
|
+
chainNodes.push(current.name);
|
|
758
|
+
root = current.expression;
|
|
759
|
+
}
|
|
760
|
+
else if (ts.isElementAccessExpression(current) &&
|
|
761
|
+
(ts.isStringLiteralLike(current.argumentExpression) ||
|
|
762
|
+
ts.isNumericLiteral(current.argumentExpression))) {
|
|
763
|
+
chainNames.push(current.argumentExpression.text);
|
|
764
|
+
chainNodes.push(current.argumentExpression);
|
|
765
|
+
root = current.expression;
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
root = current;
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
chainNames.reverse();
|
|
773
|
+
chainNodes.reverse();
|
|
774
|
+
const leaf = chainNodes.length > 0 ? chainNodes[chainNodes.length - 1] : root;
|
|
775
|
+
const leafSymbol = checker.getSymbolAtLocation(leaf);
|
|
776
|
+
if (leafSymbol !== undefined) {
|
|
777
|
+
for (const declaration of this.targetNodes(leafSymbol)) {
|
|
778
|
+
emitComputedOwner(declaration, node, source);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
if (chainNames.length > 0) {
|
|
782
|
+
const startNode = ts.isThisTypeNode(root) || root.kind === ts.SyntaxKind.ThisKeyword ? chainNodes[0] : root;
|
|
783
|
+
const stepOffset = startNode === root ? 0 : 1;
|
|
784
|
+
const startSymbol = startNode !== undefined ? checker.getSymbolAtLocation(startNode) : undefined;
|
|
785
|
+
if (startSymbol !== undefined) {
|
|
786
|
+
const walkChain = (decl, names) => {
|
|
787
|
+
if (!('initializer' in decl) || decl.initializer === undefined)
|
|
788
|
+
return;
|
|
789
|
+
let currentExpr = unwrapExpression(decl.initializer);
|
|
790
|
+
let ownerDecl;
|
|
791
|
+
for (const propName of names) {
|
|
792
|
+
if (!ts.isObjectLiteralExpression(currentExpr))
|
|
793
|
+
return;
|
|
794
|
+
const match = currentExpr.properties.find((prop) => prop.name !== undefined && propertyNameText(prop.name) === propName);
|
|
795
|
+
if (match === undefined)
|
|
796
|
+
return;
|
|
797
|
+
if (ts.isPropertyAssignment(match)) {
|
|
798
|
+
ownerDecl = match;
|
|
799
|
+
currentExpr = unwrapExpression(match.initializer);
|
|
800
|
+
}
|
|
801
|
+
else if (ts.isShorthandPropertyAssignment(match)) {
|
|
802
|
+
const sym = checker.getSymbolAtLocation(match.name);
|
|
803
|
+
const valDecl = sym !== undefined ? this.targetNodes(sym)[0] : undefined;
|
|
804
|
+
if (valDecl === undefined ||
|
|
805
|
+
!('initializer' in valDecl) ||
|
|
806
|
+
valDecl.initializer === undefined) {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
ownerDecl = valDecl;
|
|
810
|
+
currentExpr = unwrapExpression(valDecl.initializer);
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
if (ownerDecl !== undefined && ts.isObjectLiteralExpression(currentExpr)) {
|
|
817
|
+
emitComputedOwner(ownerDecl, node, source);
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
for (const decl of this.targetNodes(startSymbol)) {
|
|
821
|
+
walkChain(decl, chainNames.slice(stepOffset));
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
// docs/RISK.md §3.2: `NativeModules` or `TurboModuleRegistry` imported from react-native.
|
|
827
|
+
const nativeImport = (node) => {
|
|
828
|
+
const bindings = node.importClause?.namedBindings;
|
|
829
|
+
if (bindings === undefined || !ts.isNamedImports(bindings))
|
|
830
|
+
return;
|
|
831
|
+
const names = bindings.elements.map((element) => (element.propertyName ?? element.name).text);
|
|
832
|
+
if (names.includes('NativeModules') || names.includes('TurboModuleRegistry')) {
|
|
833
|
+
addMarker('boundary:native', node);
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
const visit = (node) => {
|
|
837
|
+
// JSDoc types (JavaScript) and {@link} references are uses too; each comment once, from its host.
|
|
838
|
+
if ('jsDoc' in node) {
|
|
839
|
+
for (const doc of ts.getJSDocCommentsAndTags(node)) {
|
|
840
|
+
const host = doc.parent;
|
|
841
|
+
if (ts.isJSDoc(doc) && host === node)
|
|
842
|
+
ts.forEachChild(doc, visit);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (ts.isIdentifier(node) || ts.isPrivateIdentifier(node)) {
|
|
846
|
+
identifier(node);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteralLike(node.moduleSpecifier)) {
|
|
850
|
+
const { trackBindings } = staticImport(node.moduleSpecifier, node);
|
|
851
|
+
const clause = node.importClause;
|
|
852
|
+
if (trackBindings && clause !== undefined) {
|
|
853
|
+
if (clause.name !== undefined)
|
|
854
|
+
binding(clause.name, [clause.name.text], node);
|
|
855
|
+
const bindings = clause.namedBindings;
|
|
856
|
+
if (bindings !== undefined && ts.isNamedImports(bindings)) {
|
|
857
|
+
for (const element of bindings.elements) {
|
|
858
|
+
const local = element.name.text;
|
|
859
|
+
const imported = (element.propertyName ?? element.name).text;
|
|
860
|
+
binding(element.name, [imported, local], element);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
else if (bindings !== undefined && ts.isNamespaceImport(bindings)) {
|
|
864
|
+
binding(bindings.name, [bindings.name.text], node);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
if (node.moduleSpecifier.text === 'react-native')
|
|
868
|
+
nativeImport(node);
|
|
869
|
+
return; // import bindings are not uses; the file-level IMPORTS edge records them
|
|
870
|
+
}
|
|
871
|
+
if (ts.isExportDeclaration(node)) {
|
|
872
|
+
const spec = node.moduleSpecifier;
|
|
873
|
+
const clause = node.exportClause;
|
|
874
|
+
// `export { a }` names local declarations: their export status, not a use
|
|
875
|
+
if (spec === undefined || !ts.isStringLiteralLike(spec))
|
|
876
|
+
return;
|
|
877
|
+
const { trackBindings } = staticImport(spec, node);
|
|
878
|
+
if (!trackBindings || clause === undefined)
|
|
879
|
+
return;
|
|
880
|
+
if (ts.isNamedExports(clause)) {
|
|
881
|
+
for (const element of clause.elements) {
|
|
882
|
+
const exported = element.name.text;
|
|
883
|
+
const imported = (element.propertyName ?? element.name).text;
|
|
884
|
+
binding(element.name, [imported, exported], element);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
else if (ts.isNamespaceExport(clause)) {
|
|
888
|
+
binding(clause.name, [clause.name.text], node);
|
|
889
|
+
}
|
|
890
|
+
return; // re-exports are recorded in the exports table
|
|
891
|
+
}
|
|
892
|
+
if (ts.isImportEqualsDeclaration(node) &&
|
|
893
|
+
ts.isExternalModuleReference(node.moduleReference) &&
|
|
894
|
+
ts.isStringLiteralLike(node.moduleReference.expression)) {
|
|
895
|
+
const { trackBindings } = staticImport(node.moduleReference.expression, node);
|
|
896
|
+
if (trackBindings)
|
|
897
|
+
binding(node.name, [node.name.text], node);
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
if (ts.isImportTypeNode(node) &&
|
|
901
|
+
ts.isLiteralTypeNode(node.argument) &&
|
|
902
|
+
ts.isStringLiteralLike(node.argument.literal)) {
|
|
903
|
+
staticImport(node.argument.literal, node);
|
|
904
|
+
}
|
|
905
|
+
if (ts.isCallExpression(node))
|
|
906
|
+
call(node);
|
|
907
|
+
if (ts.isCallExpression(node) || ts.isNewExpression(node))
|
|
908
|
+
keyArguments(node);
|
|
909
|
+
if (ts.isElementAccessExpression(node))
|
|
910
|
+
elementAccess(node);
|
|
911
|
+
// docs/RISK.md §3.2: a `sql` tagged template (`sql\`…\``, `db.sql\`…\``) talks to a database.
|
|
912
|
+
if (ts.isTaggedTemplateExpression(node) &&
|
|
913
|
+
((ts.isIdentifier(node.tag) && node.tag.text === 'sql') ||
|
|
914
|
+
(ts.isPropertyAccessExpression(node.tag) && node.tag.name.text === 'sql'))) {
|
|
915
|
+
addMarker('boundary:sql', node);
|
|
916
|
+
}
|
|
917
|
+
if (ts.isStringLiteralLike(node) || ts.isNumericLiteral(node))
|
|
918
|
+
propertyNameLiteral(node);
|
|
919
|
+
if (ts.isClassLike(node) || ts.isInterfaceDeclaration(node))
|
|
920
|
+
heritage(node);
|
|
921
|
+
ts.forEachChild(node, visit);
|
|
922
|
+
};
|
|
923
|
+
const syntaxError = this.program.getSyntacticDiagnostics(sf)[0];
|
|
924
|
+
if (syntaxError !== undefined) {
|
|
925
|
+
const at = sf.getLineAndCharacterOfPosition(syntaxError.start ?? 0).line + 1;
|
|
926
|
+
addGap({ file, line: at, code: 'parse-error', detail: file, source: declarations.moduleKey });
|
|
927
|
+
}
|
|
928
|
+
ts.forEachChild(sf, visit);
|
|
929
|
+
const exports = this.exportsOf(sf, file);
|
|
930
|
+
const surface = surfaceOf(ts, sf, declarations);
|
|
931
|
+
return {
|
|
932
|
+
file,
|
|
933
|
+
edges: [...edges.values()].map((edge) => ({
|
|
934
|
+
...edge,
|
|
935
|
+
lines: [...edge.lines].sort((a, b) => a - b),
|
|
936
|
+
})),
|
|
937
|
+
unresolved: [...unresolved.values()],
|
|
938
|
+
gaps: [...gaps.values()],
|
|
939
|
+
tokens: this.tokensOf(sf, file),
|
|
940
|
+
externalImports: [...externalImports.values()],
|
|
941
|
+
exports,
|
|
942
|
+
markers: [...markers.values()],
|
|
943
|
+
surface: surface.hash,
|
|
944
|
+
observableKeys: surface.observableKeys,
|
|
945
|
+
global: isGlobalFile(this.ts, this.checker, sf),
|
|
946
|
+
counts,
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
exportsOf(sf, file) {
|
|
950
|
+
const moduleSymbol = this.checker.getSymbolAtLocation(sf);
|
|
951
|
+
if (moduleSymbol === undefined)
|
|
952
|
+
return [];
|
|
953
|
+
const records = [];
|
|
954
|
+
for (const exported of this.checker.getExportsOfModule(moduleSymbol)) {
|
|
955
|
+
for (const node of this.targetNodes(exported)) {
|
|
956
|
+
const target = this.targetOfDeclaration(node);
|
|
957
|
+
if (typeof target === 'object')
|
|
958
|
+
records.push({ file, name: exported.name, target: target.key });
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return records;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* String literals whose text is an indexed declaration's simple name, from a position that
|
|
965
|
+
* could name a symbol: no contextual type (an untyped const, golden C23) or one of
|
|
966
|
+
* `string`/`any`/`unknown`. A discriminant, enum-like or keyof-typed literal is resolved by
|
|
967
|
+
* the checker and is not a match (D55, G14).
|
|
968
|
+
*/
|
|
969
|
+
tokensOf(sf, file) {
|
|
970
|
+
const ts = this.ts;
|
|
971
|
+
const checker = this.checker;
|
|
972
|
+
const names = this.scope.declarationNames;
|
|
973
|
+
const canNameSymbol = (type) => {
|
|
974
|
+
if ((type.flags & (ts.TypeFlags.String | ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0) {
|
|
975
|
+
return true;
|
|
976
|
+
}
|
|
977
|
+
return type.isUnion() && type.types.some(canNameSymbol);
|
|
978
|
+
};
|
|
979
|
+
const couldName = (node) => {
|
|
980
|
+
if (ts.isLiteralTypeNode(node.parent))
|
|
981
|
+
return false; // a type position declares a shape
|
|
982
|
+
const contextual = checker.getContextualType(node);
|
|
983
|
+
if (contextual === undefined)
|
|
984
|
+
return true;
|
|
985
|
+
return canNameSymbol(contextual);
|
|
986
|
+
};
|
|
987
|
+
const found = new Map();
|
|
988
|
+
const walk = (node) => {
|
|
989
|
+
if ((ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) &&
|
|
990
|
+
names.has(node.text) &&
|
|
991
|
+
couldName(node)) {
|
|
992
|
+
const at = sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
|
|
993
|
+
const token = found.get(node.text);
|
|
994
|
+
if (token === undefined)
|
|
995
|
+
found.set(node.text, { file, name: node.text, kind: 'str', lines: [at] });
|
|
996
|
+
else if (token.lines[token.lines.length - 1] !== at)
|
|
997
|
+
token.lines.push(at);
|
|
998
|
+
}
|
|
999
|
+
ts.forEachChild(node, walk);
|
|
1000
|
+
};
|
|
1001
|
+
ts.forEachChild(sf, walk);
|
|
1002
|
+
return [...found.values()];
|
|
1003
|
+
}
|
|
1004
|
+
}
|