@krisada-m/ksd-intelligence 0.0.0-stage → 1.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/.antigravity-plugin/plugin.json +20 -0
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/agy-skills/erosiano-review/SKILL.md +17 -0
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1013 -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 +884 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +389 -0
- package/dist/adapters/ui/tree.js +471 -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 +452 -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 +811 -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/engineering/baseline.js +337 -0
- package/dist/engineering/change-scope.js +500 -0
- package/dist/engineering/check-changed.js +40 -0
- package/dist/engineering/config.js +129 -0
- package/dist/engineering/dist/policy.d.ts +17 -0
- package/dist/engineering/dist/policy.js +150 -0
- package/dist/engineering/dist/runtime.d.ts +40 -0
- package/dist/engineering/dist/runtime.js +77 -0
- package/dist/engineering/dist/sdlc.d.ts +7 -0
- package/dist/engineering/dist/sdlc.js +27 -0
- package/dist/engineering/eslint-rules/index.js +107 -0
- package/dist/engineering/index.d.ts +7 -0
- package/dist/engineering/index.js +319 -0
- package/dist/engineering/intelligence.js +44 -0
- package/dist/engineering/package.json +36 -0
- package/dist/engineering/policy.js +2 -0
- package/dist/engineering/practice-tool.js +72 -0
- package/dist/engineering/practices.js +283 -0
- package/dist/engineering/practices.json +2453 -0
- package/dist/engineering/review-verdict.js +262 -0
- package/dist/engineering/sdlc.js +2 -0
- package/dist/index.js +20 -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/engineering.lock.json +94 -0
- package/package.json +81 -4
- package/rules/engineering.md +20 -0
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/engineering.mjs +91 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +491 -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,451 @@
|
|
|
1
|
+
function isIndexType(ts, type) {
|
|
2
|
+
return (type.flags & ts.TypeFlags.Index) !== 0;
|
|
3
|
+
}
|
|
4
|
+
function isType(value) {
|
|
5
|
+
return (typeof value === 'object' &&
|
|
6
|
+
value !== null &&
|
|
7
|
+
'flags' in value &&
|
|
8
|
+
typeof value.flags === 'number' &&
|
|
9
|
+
'checker' in value);
|
|
10
|
+
}
|
|
11
|
+
function isTypeReference(ts, type) {
|
|
12
|
+
return ('objectFlags' in type &&
|
|
13
|
+
typeof type.objectFlags === 'number' &&
|
|
14
|
+
(type.objectFlags & ts.ObjectFlags.Reference) !== 0);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The `keyof X` a resolved key union was built from. `origin` is internal to TypeScript (kept
|
|
18
|
+
* since 4.2 for display); the pinned compiler and the key tests catch a change of it.
|
|
19
|
+
*/
|
|
20
|
+
function unionOrigin(type) {
|
|
21
|
+
if (!type.isUnion() || !('origin' in type))
|
|
22
|
+
return undefined;
|
|
23
|
+
const origin = type.origin;
|
|
24
|
+
return isType(origin) ? origin : undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Key access through `keyof` (D52): a string literal used as a key of a type with declared
|
|
28
|
+
* properties, and a non-literal key expression typed by `keyof T`. Owners come from written
|
|
29
|
+
* types (`keyof X`, a type parameter constrained to one, a type alias of one) or from a key type
|
|
30
|
+
* TypeScript built from `keyof X`; an ordinary `string` never yields an owner.
|
|
31
|
+
*/
|
|
32
|
+
export class KeyAccess {
|
|
33
|
+
ts;
|
|
34
|
+
checker;
|
|
35
|
+
scope;
|
|
36
|
+
constructor(ts, checker, scope) {
|
|
37
|
+
this.ts = ts;
|
|
38
|
+
this.checker = checker;
|
|
39
|
+
this.scope = scope;
|
|
40
|
+
}
|
|
41
|
+
/** A string literal in a key position, or null when it names no indexed declaration. */
|
|
42
|
+
literal(node) {
|
|
43
|
+
const targets = new Map();
|
|
44
|
+
const holders = new Set();
|
|
45
|
+
let unindexed = false;
|
|
46
|
+
for (const owner of this.ownersOfLiteral(node)) {
|
|
47
|
+
const apparent = this.checker.getApparentType(owner);
|
|
48
|
+
const property = this.checker.getPropertyOfType(apparent, node.text);
|
|
49
|
+
if (property === undefined)
|
|
50
|
+
continue;
|
|
51
|
+
const keys = this.scope.indexedTargets(property);
|
|
52
|
+
for (const key of keys)
|
|
53
|
+
targets.set(key, property);
|
|
54
|
+
// Mapped Record keys have no declaration at all; anonymous type members do, but their
|
|
55
|
+
// declarations are outside ADR 0003. Only the latter need an owner-scoped gap.
|
|
56
|
+
if (keys.length === 0) {
|
|
57
|
+
if ((property.declarations?.length ?? 0) === 0)
|
|
58
|
+
continue;
|
|
59
|
+
unindexed = true;
|
|
60
|
+
}
|
|
61
|
+
for (const key of this.ownerKeys(owner))
|
|
62
|
+
holders.add(key);
|
|
63
|
+
}
|
|
64
|
+
const [single] = targets.values();
|
|
65
|
+
if (!unindexed && targets.size === 1 && single !== undefined) {
|
|
66
|
+
return { kind: 'property', property: single };
|
|
67
|
+
}
|
|
68
|
+
if (holders.size === 0)
|
|
69
|
+
return null;
|
|
70
|
+
return { kind: 'owners', owners: [...holders].sort() };
|
|
71
|
+
}
|
|
72
|
+
/** Owner keys of a non-literal key expression typed by `keyof T` (empty for any other type). */
|
|
73
|
+
expression(node) {
|
|
74
|
+
const keys = new Set();
|
|
75
|
+
for (const owner of this.ownersOfExpression(node)) {
|
|
76
|
+
for (const key of this.ownerKeys(owner))
|
|
77
|
+
keys.add(key);
|
|
78
|
+
}
|
|
79
|
+
return [...keys].sort();
|
|
80
|
+
}
|
|
81
|
+
/** Whether a call passes `argument` to a parameter declared as a key (`keyof T`, `K`). */
|
|
82
|
+
takesKey(call, argument) {
|
|
83
|
+
const index = call.arguments?.indexOf(argument) ?? -1;
|
|
84
|
+
const parameter = this.parameterAt(call, index);
|
|
85
|
+
return parameter?.type !== undefined && this.writtenOwners(parameter.type).length > 0;
|
|
86
|
+
}
|
|
87
|
+
ownerKeys(owner) {
|
|
88
|
+
const keys = [];
|
|
89
|
+
for (const part of owner.isUnionOrIntersection() ? owner.types : [owner]) {
|
|
90
|
+
const symbol = part.aliasSymbol ?? part.getSymbol();
|
|
91
|
+
const declaration = symbol?.declarations?.[0];
|
|
92
|
+
const key = declaration === undefined ? null : this.scope.ownerKey(declaration);
|
|
93
|
+
if (key !== null)
|
|
94
|
+
keys.push(key);
|
|
95
|
+
}
|
|
96
|
+
return keys;
|
|
97
|
+
}
|
|
98
|
+
ownersOfLiteral(node) {
|
|
99
|
+
const ts = this.ts;
|
|
100
|
+
let argument = node;
|
|
101
|
+
let inArray = false;
|
|
102
|
+
if (ts.isArrayLiteralExpression(node.parent)) {
|
|
103
|
+
argument = node.parent;
|
|
104
|
+
inArray = true;
|
|
105
|
+
}
|
|
106
|
+
const call = argument.parent;
|
|
107
|
+
if ((ts.isCallExpression(call) || ts.isNewExpression(call)) && call.expression !== argument) {
|
|
108
|
+
// A generic call has already inferred `K` as the literal itself: read the declaration.
|
|
109
|
+
const index = call.arguments?.indexOf(argument) ?? -1;
|
|
110
|
+
const parameter = this.parameterAt(call, index);
|
|
111
|
+
if (parameter?.type === undefined)
|
|
112
|
+
return [];
|
|
113
|
+
const rest = parameter.dotDotDotToken !== undefined;
|
|
114
|
+
if (inArray && rest)
|
|
115
|
+
return [];
|
|
116
|
+
const declared = inArray || rest ? this.elementTypeNodes(parameter.type) : [parameter.type];
|
|
117
|
+
if (declared.length === 0)
|
|
118
|
+
return [];
|
|
119
|
+
return declared
|
|
120
|
+
.flatMap((d) => this.writtenOwners(d))
|
|
121
|
+
.flatMap((owner) => this.instantiate(owner, call, parameter.parent));
|
|
122
|
+
}
|
|
123
|
+
const parent = argument.parent;
|
|
124
|
+
if ((ts.isVariableDeclaration(parent) ||
|
|
125
|
+
ts.isPropertyDeclaration(parent) ||
|
|
126
|
+
ts.isParameter(parent)) &&
|
|
127
|
+
parent.initializer === argument &&
|
|
128
|
+
parent.type !== undefined) {
|
|
129
|
+
const nodes = inArray ? this.elementTypeNodes(parent.type) : [parent.type];
|
|
130
|
+
return this.concrete(nodes.flatMap((t) => this.writtenOwners(t)));
|
|
131
|
+
}
|
|
132
|
+
if ((ts.isAsExpression(parent) ||
|
|
133
|
+
ts.isSatisfiesExpression(parent) ||
|
|
134
|
+
ts.isTypeAssertionExpression(parent)) &&
|
|
135
|
+
parent.expression === argument) {
|
|
136
|
+
const nodes = inArray ? this.elementTypeNodes(parent.type) : [parent.type];
|
|
137
|
+
return this.concrete(nodes.flatMap((t) => this.writtenOwners(t)));
|
|
138
|
+
}
|
|
139
|
+
if (ts.isPropertyAssignment(parent) &&
|
|
140
|
+
parent.initializer === argument &&
|
|
141
|
+
ts.isObjectLiteralExpression(parent.parent)) {
|
|
142
|
+
const fromProp = this.ownersOfPropertyAssignment(parent, inArray);
|
|
143
|
+
if (fromProp.length > 0)
|
|
144
|
+
return fromProp;
|
|
145
|
+
}
|
|
146
|
+
const contextual = this.checker.getContextualType(node);
|
|
147
|
+
if (contextual !== undefined) {
|
|
148
|
+
const fromContext = this.concrete(this.ownersOfType(contextual));
|
|
149
|
+
if (fromContext.length > 0)
|
|
150
|
+
return fromContext;
|
|
151
|
+
}
|
|
152
|
+
if (inArray) {
|
|
153
|
+
const arrCtx = this.checker.getContextualType(argument);
|
|
154
|
+
if (arrCtx !== undefined) {
|
|
155
|
+
const elementTypes = (arrCtx.isUnion() ? arrCtx.types : [arrCtx])
|
|
156
|
+
.map((t) => this.checker.getIndexTypeOfType(t, ts.IndexKind.Number))
|
|
157
|
+
.filter((t) => t !== undefined);
|
|
158
|
+
return this.concrete(elementTypes.flatMap((t) => this.ownersOfType(t)));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
ownersOfPropertyAssignment(prop, inArray) {
|
|
164
|
+
const ts = this.ts;
|
|
165
|
+
if (!ts.isObjectLiteralExpression(prop.parent))
|
|
166
|
+
return [];
|
|
167
|
+
const propName = prop.name;
|
|
168
|
+
if (!ts.isIdentifier(propName) &&
|
|
169
|
+
!ts.isStringLiteralLike(propName) &&
|
|
170
|
+
!ts.isNumericLiteral(propName)) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
const objType = this.checker.getContextualType(prop.parent);
|
|
174
|
+
if (objType === undefined)
|
|
175
|
+
return [];
|
|
176
|
+
const owners = [];
|
|
177
|
+
for (const part of objType.isUnion() ? objType.types : [objType]) {
|
|
178
|
+
const symbol = this.checker.getPropertyOfType(part, propName.text);
|
|
179
|
+
for (const decl of symbol?.declarations ?? []) {
|
|
180
|
+
if ('type' in decl && decl.type !== undefined) {
|
|
181
|
+
const typeNode = decl.type;
|
|
182
|
+
const nodes = inArray ? this.elementTypeNodes(typeNode) : [typeNode];
|
|
183
|
+
owners.push(...this.concrete(nodes.flatMap((t) => this.writtenOwners(t))));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return owners;
|
|
188
|
+
}
|
|
189
|
+
isBroadStringLike(type) {
|
|
190
|
+
if ((type.flags &
|
|
191
|
+
(this.ts.TypeFlags.String | this.ts.TypeFlags.Any | this.ts.TypeFlags.Unknown)) !==
|
|
192
|
+
0) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
return type.isUnion() && type.types.some((t) => this.isBroadStringLike(t));
|
|
196
|
+
}
|
|
197
|
+
ownersOfExpression(node) {
|
|
198
|
+
const ts = this.ts;
|
|
199
|
+
if (ts.isParenthesizedExpression(node) || ts.isNonNullExpression(node)) {
|
|
200
|
+
return this.ownersOfExpression(node.expression);
|
|
201
|
+
}
|
|
202
|
+
if (ts.isAsExpression(node) ||
|
|
203
|
+
ts.isSatisfiesExpression(node) ||
|
|
204
|
+
ts.isTypeAssertionExpression(node)) {
|
|
205
|
+
return this.concrete(this.writtenOwners(node.type));
|
|
206
|
+
}
|
|
207
|
+
const exprType = this.checker.getTypeAtLocation(node);
|
|
208
|
+
if (this.isBroadStringLike(this.checker.getBaseConstraintOfType(exprType) ?? exprType)) {
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
if (ts.isConditionalExpression(node)) {
|
|
212
|
+
return [
|
|
213
|
+
...this.ownersOfExpression(node.whenTrue),
|
|
214
|
+
...this.ownersOfExpression(node.whenFalse),
|
|
215
|
+
];
|
|
216
|
+
}
|
|
217
|
+
if (ts.isBinaryExpression(node) &&
|
|
218
|
+
(node.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken ||
|
|
219
|
+
node.operatorToken.kind === ts.SyntaxKind.BarBarToken)) {
|
|
220
|
+
return [...this.ownersOfExpression(node.left), ...this.ownersOfExpression(node.right)];
|
|
221
|
+
}
|
|
222
|
+
const name = ts.isPropertyAccessExpression(node) ? node.name : node;
|
|
223
|
+
if (!ts.isIdentifier(name)) {
|
|
224
|
+
return this.concrete(this.ownersOfType(exprType));
|
|
225
|
+
}
|
|
226
|
+
let symbol = this.checker.getSymbolAtLocation(name);
|
|
227
|
+
if (symbol !== undefined && (symbol.flags & ts.SymbolFlags.Alias) !== 0) {
|
|
228
|
+
symbol = this.checker.getAliasedSymbol(symbol);
|
|
229
|
+
}
|
|
230
|
+
const declaration = symbol?.valueDeclaration;
|
|
231
|
+
if (symbol === undefined || declaration === undefined)
|
|
232
|
+
return [];
|
|
233
|
+
const holdsValue = ts.isVariableDeclaration(declaration) ||
|
|
234
|
+
ts.isParameter(declaration) ||
|
|
235
|
+
ts.isPropertyDeclaration(declaration) ||
|
|
236
|
+
ts.isPropertySignature(declaration) ||
|
|
237
|
+
ts.isBindingElement(declaration);
|
|
238
|
+
if (!holdsValue)
|
|
239
|
+
return [];
|
|
240
|
+
// A written type decides; only an inferred one asks the checker.
|
|
241
|
+
if ('type' in declaration && declaration.type !== undefined) {
|
|
242
|
+
return this.concrete(this.writtenOwners(declaration.type));
|
|
243
|
+
}
|
|
244
|
+
return this.concrete(this.ownersOfType(this.checker.getTypeOfSymbol(symbol)));
|
|
245
|
+
}
|
|
246
|
+
writtenOwners(node, seen = new Set()) {
|
|
247
|
+
if (seen.has(node))
|
|
248
|
+
return [];
|
|
249
|
+
seen.add(node);
|
|
250
|
+
const ts = this.ts;
|
|
251
|
+
const type = this.checker.getTypeFromTypeNode(node);
|
|
252
|
+
const constraint = this.checker.getBaseConstraintOfType(type) ?? type;
|
|
253
|
+
// `keyof Usage | string` is just string: its syntax must not turn log("cost") into a read.
|
|
254
|
+
if ((constraint.flags & (ts.TypeFlags.String | ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0 ||
|
|
255
|
+
this.isBroadStringLike(type)) {
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
if (ts.isParenthesizedTypeNode(node)) {
|
|
259
|
+
return this.writtenOwners(node.type, seen);
|
|
260
|
+
}
|
|
261
|
+
if (ts.isUnionTypeNode(node)) {
|
|
262
|
+
return node.types.flatMap((part) => this.writtenOwners(part, seen));
|
|
263
|
+
}
|
|
264
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
265
|
+
let symbol = this.checker.getSymbolAtLocation(node.typeName);
|
|
266
|
+
if (symbol !== undefined && (symbol.flags & ts.SymbolFlags.Alias) !== 0) {
|
|
267
|
+
symbol = this.checker.getAliasedSymbol(symbol);
|
|
268
|
+
}
|
|
269
|
+
const decl = symbol?.declarations?.[0];
|
|
270
|
+
if (decl !== undefined && ts.isTypeAliasDeclaration(decl)) {
|
|
271
|
+
if (decl.typeParameters === undefined) {
|
|
272
|
+
return this.writtenOwners(decl.type, seen);
|
|
273
|
+
}
|
|
274
|
+
// Single-property interfaces collapse `keyof Single` to a string literal; map `type Keys<T> = keyof T` directly.
|
|
275
|
+
if (ts.isTypeOperatorNode(decl.type) &&
|
|
276
|
+
decl.type.operator === ts.SyntaxKind.KeyOfKeyword &&
|
|
277
|
+
ts.isTypeReferenceNode(decl.type.type) &&
|
|
278
|
+
ts.isIdentifier(decl.type.type.typeName)) {
|
|
279
|
+
const paramName = decl.type.type.typeName.text;
|
|
280
|
+
const idx = decl.typeParameters.findIndex((p) => p.name.text === paramName);
|
|
281
|
+
const arg = idx >= 0 ? node.typeArguments?.[idx] : undefined;
|
|
282
|
+
if (arg !== undefined)
|
|
283
|
+
return [this.checker.getTypeFromTypeNode(arg)];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const targets = this.keyofTargets(node);
|
|
288
|
+
// Resolving the type preserves instantiated generic aliases such as `Keys<Usage>`.
|
|
289
|
+
return targets.length > 0
|
|
290
|
+
? targets.map((target) => this.checker.getTypeFromTypeNode(target))
|
|
291
|
+
: this.ownersOfType(type);
|
|
292
|
+
}
|
|
293
|
+
/** Owners of a key type: `keyof X`, a key union built from one, or a constrained `K`. */
|
|
294
|
+
ownersOfType(type, depth = 0) {
|
|
295
|
+
if (type === undefined || depth > 6)
|
|
296
|
+
return [];
|
|
297
|
+
if (this.isBroadStringLike(type))
|
|
298
|
+
return [];
|
|
299
|
+
if (isIndexType(this.ts, type))
|
|
300
|
+
return [type.type];
|
|
301
|
+
const origin = unionOrigin(type);
|
|
302
|
+
if (origin !== undefined) {
|
|
303
|
+
const fromOrigin = this.ownersOfType(origin, depth + 1);
|
|
304
|
+
if (fromOrigin.length > 0)
|
|
305
|
+
return fromOrigin;
|
|
306
|
+
}
|
|
307
|
+
if (type.isUnion()) {
|
|
308
|
+
return type.types.flatMap((part) => this.ownersOfType(part, depth + 1));
|
|
309
|
+
}
|
|
310
|
+
if ((type.flags & this.ts.TypeFlags.TypeParameter) !== 0) {
|
|
311
|
+
return this.ownersOfType(type.getConstraint(), depth + 1);
|
|
312
|
+
}
|
|
313
|
+
return [];
|
|
314
|
+
}
|
|
315
|
+
/** The `X` of every `keyof X` a written key type names, through `K extends …` and aliases. */
|
|
316
|
+
keyofTargets(node, seen = new Set()) {
|
|
317
|
+
const ts = this.ts;
|
|
318
|
+
if (seen.has(node))
|
|
319
|
+
return [];
|
|
320
|
+
seen.add(node);
|
|
321
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
322
|
+
return this.keyofTargets(node.type, seen);
|
|
323
|
+
if (ts.isUnionTypeNode(node))
|
|
324
|
+
return node.types.flatMap((t) => this.keyofTargets(t, seen));
|
|
325
|
+
if (ts.isTypeOperatorNode(node) && node.operator === ts.SyntaxKind.KeyOfKeyword) {
|
|
326
|
+
return [node.type];
|
|
327
|
+
}
|
|
328
|
+
if (!ts.isTypeReferenceNode(node))
|
|
329
|
+
return [];
|
|
330
|
+
let symbol = this.checker.getSymbolAtLocation(node.typeName);
|
|
331
|
+
if (symbol !== undefined && (symbol.flags & ts.SymbolFlags.Alias) !== 0) {
|
|
332
|
+
symbol = this.checker.getAliasedSymbol(symbol);
|
|
333
|
+
}
|
|
334
|
+
const declaration = symbol?.declarations?.[0];
|
|
335
|
+
if (declaration === undefined)
|
|
336
|
+
return [];
|
|
337
|
+
if (ts.isTypeParameterDeclaration(declaration) && declaration.constraint !== undefined) {
|
|
338
|
+
return this.keyofTargets(declaration.constraint, seen);
|
|
339
|
+
}
|
|
340
|
+
if (ts.isTypeAliasDeclaration(declaration) && declaration.typeParameters === undefined) {
|
|
341
|
+
return this.keyofTargets(declaration.type, seen);
|
|
342
|
+
}
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
345
|
+
elementTypeNodes(node) {
|
|
346
|
+
const ts = this.ts;
|
|
347
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
348
|
+
return this.elementTypeNodes(node.type);
|
|
349
|
+
if (ts.isUnionTypeNode(node))
|
|
350
|
+
return node.types.flatMap((t) => this.elementTypeNodes(t));
|
|
351
|
+
if (ts.isArrayTypeNode(node))
|
|
352
|
+
return [node.elementType];
|
|
353
|
+
if (ts.isTypeOperatorNode(node) && node.operator === ts.SyntaxKind.ReadonlyKeyword) {
|
|
354
|
+
return this.elementTypeNodes(node.type);
|
|
355
|
+
}
|
|
356
|
+
if (ts.isTypeReferenceNode(node) &&
|
|
357
|
+
ts.isIdentifier(node.typeName) &&
|
|
358
|
+
(node.typeName.text === 'Array' || node.typeName.text === 'ReadonlyArray')) {
|
|
359
|
+
return node.typeArguments?.[0] !== undefined ? [node.typeArguments[0]] : [];
|
|
360
|
+
}
|
|
361
|
+
return [];
|
|
362
|
+
}
|
|
363
|
+
parameterAt(call, index) {
|
|
364
|
+
if (index < 0)
|
|
365
|
+
return undefined;
|
|
366
|
+
// An untyped or unresolved call has no declaration (TypeScript returns a synthetic signature).
|
|
367
|
+
const declaration = this.checker
|
|
368
|
+
.getResolvedSignature(call)
|
|
369
|
+
?.getDeclaration();
|
|
370
|
+
const parameters = declaration?.parameters ?? [];
|
|
371
|
+
const last = parameters[parameters.length - 1];
|
|
372
|
+
if (index < parameters.length)
|
|
373
|
+
return parameters[index];
|
|
374
|
+
return last?.dotDotDotToken !== undefined ? last : undefined;
|
|
375
|
+
}
|
|
376
|
+
/** A type parameter owner, instantiated at this call where the call shows it. */
|
|
377
|
+
instantiate(type, call, signature, seenDefaults) {
|
|
378
|
+
const ts = this.ts;
|
|
379
|
+
if ((type.flags & ts.TypeFlags.TypeParameter) === 0)
|
|
380
|
+
return [type];
|
|
381
|
+
if (seenDefaults?.has(type))
|
|
382
|
+
return [];
|
|
383
|
+
const declaration = type.getSymbol()?.declarations?.[0];
|
|
384
|
+
const holder = declaration?.parent;
|
|
385
|
+
// Explicit arguments select T even when an object argument has a narrower subtype.
|
|
386
|
+
// On `new C<T>(...)`, the type parameters belong to the class, not its constructor.
|
|
387
|
+
if (declaration !== undefined &&
|
|
388
|
+
ts.isTypeParameterDeclaration(declaration) &&
|
|
389
|
+
holder !== undefined) {
|
|
390
|
+
let parameters;
|
|
391
|
+
if (holder === signature && ts.isFunctionLike(holder))
|
|
392
|
+
parameters = holder.typeParameters;
|
|
393
|
+
if (ts.isNewExpression(call) && ts.isClassLike(holder))
|
|
394
|
+
parameters = holder.typeParameters;
|
|
395
|
+
const index = parameters?.indexOf(declaration) ?? -1;
|
|
396
|
+
const argument = index < 0 ? undefined : call.typeArguments?.[index];
|
|
397
|
+
if (argument !== undefined)
|
|
398
|
+
return [this.checker.getTypeFromTypeNode(argument)];
|
|
399
|
+
}
|
|
400
|
+
// Inferred from an argument declared with exactly this type parameter: `get(o: T, k: K)`.
|
|
401
|
+
if (ts.isFunctionLike(signature)) {
|
|
402
|
+
for (const [index, parameter] of signature.parameters.entries()) {
|
|
403
|
+
const argument = call.arguments?.[index];
|
|
404
|
+
if (parameter.type === undefined || argument === undefined)
|
|
405
|
+
continue;
|
|
406
|
+
if (this.checker.getTypeFromTypeNode(parameter.type) === type) {
|
|
407
|
+
return [this.checker.getTypeAtLocation(argument)];
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
// A type parameter of the receiver's class or interface: `store.get('k')` on `Store<Usage>`.
|
|
412
|
+
const callee = call.expression;
|
|
413
|
+
if (declaration !== undefined &&
|
|
414
|
+
ts.isTypeParameterDeclaration(declaration) &&
|
|
415
|
+
holder !== undefined &&
|
|
416
|
+
(ts.isClassLike(holder) || ts.isInterfaceDeclaration(holder)) &&
|
|
417
|
+
(ts.isPropertyAccessExpression(callee) || ts.isElementAccessExpression(callee))) {
|
|
418
|
+
const receiver = this.checker.getTypeAtLocation(callee.expression);
|
|
419
|
+
const index = holder.typeParameters?.indexOf(declaration) ?? -1;
|
|
420
|
+
if (isTypeReference(ts, receiver) && index >= 0) {
|
|
421
|
+
const holderSymbol = holder.name === undefined ? undefined : this.checker.getSymbolAtLocation(holder.name);
|
|
422
|
+
const argument = this.checker.getTypeArguments(receiver)[index];
|
|
423
|
+
if (receiver.target.getSymbol() === holderSymbol && argument !== undefined) {
|
|
424
|
+
return [argument];
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// Defaults can refer to earlier type parameters; follow their bindings rather than treating
|
|
429
|
+
// a constraint as the selected owner. Invalid cyclic defaults must not recurse indefinitely.
|
|
430
|
+
if (declaration !== undefined &&
|
|
431
|
+
ts.isTypeParameterDeclaration(declaration) &&
|
|
432
|
+
declaration.default !== undefined) {
|
|
433
|
+
const seen = new Set(seenDefaults);
|
|
434
|
+
seen.add(type);
|
|
435
|
+
return this.instantiate(this.checker.getTypeFromTypeNode(declaration.default), call, signature, seen);
|
|
436
|
+
}
|
|
437
|
+
return this.concrete([type]);
|
|
438
|
+
}
|
|
439
|
+
/** Owners with type parameters replaced by their constraints; unconstrained ones dropped. */
|
|
440
|
+
concrete(owners) {
|
|
441
|
+
const ts = this.ts;
|
|
442
|
+
return owners.flatMap((owner) => {
|
|
443
|
+
if ((owner.flags & ts.TypeFlags.TypeParameter) === 0)
|
|
444
|
+
return [owner];
|
|
445
|
+
const constraint = owner.getConstraint();
|
|
446
|
+
return constraint === undefined || (constraint.flags & ts.TypeFlags.TypeParameter) !== 0
|
|
447
|
+
? []
|
|
448
|
+
: [constraint];
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
const regexPrefixCache = new WeakMap();
|
|
2
|
+
function regexPrefixTokens(ts) {
|
|
3
|
+
let cached = regexPrefixCache.get(ts);
|
|
4
|
+
if (cached === undefined) {
|
|
5
|
+
const k = ts.SyntaxKind;
|
|
6
|
+
cached = new Set([
|
|
7
|
+
k.Unknown,
|
|
8
|
+
k.EqualsToken,
|
|
9
|
+
k.OpenParenToken,
|
|
10
|
+
k.SemicolonToken,
|
|
11
|
+
k.BarBarToken,
|
|
12
|
+
k.AmpersandAmpersandToken,
|
|
13
|
+
k.QuestionQuestionToken,
|
|
14
|
+
k.ExclamationToken,
|
|
15
|
+
k.PlusToken,
|
|
16
|
+
k.MinusToken,
|
|
17
|
+
k.AsteriskToken,
|
|
18
|
+
k.AsteriskAsteriskToken,
|
|
19
|
+
k.SlashToken,
|
|
20
|
+
k.SlashEqualsToken,
|
|
21
|
+
k.PercentToken,
|
|
22
|
+
k.TildeToken,
|
|
23
|
+
k.DotDotDotToken,
|
|
24
|
+
k.PlusEqualsToken,
|
|
25
|
+
k.MinusEqualsToken,
|
|
26
|
+
k.AsteriskEqualsToken,
|
|
27
|
+
k.AsteriskAsteriskEqualsToken,
|
|
28
|
+
k.PercentEqualsToken,
|
|
29
|
+
k.LessThanLessThanToken,
|
|
30
|
+
k.LessThanLessThanEqualsToken,
|
|
31
|
+
k.GreaterThanGreaterThanToken,
|
|
32
|
+
k.GreaterThanGreaterThanEqualsToken,
|
|
33
|
+
k.GreaterThanGreaterThanGreaterThanToken,
|
|
34
|
+
k.GreaterThanGreaterThanGreaterThanEqualsToken,
|
|
35
|
+
k.AmpersandToken,
|
|
36
|
+
k.AmpersandEqualsToken,
|
|
37
|
+
k.BarToken,
|
|
38
|
+
k.BarEqualsToken,
|
|
39
|
+
k.CaretToken,
|
|
40
|
+
k.CaretEqualsToken,
|
|
41
|
+
k.BarBarEqualsToken,
|
|
42
|
+
k.AmpersandAmpersandEqualsToken,
|
|
43
|
+
k.QuestionQuestionEqualsToken,
|
|
44
|
+
k.EqualsEqualsToken,
|
|
45
|
+
k.EqualsEqualsEqualsToken,
|
|
46
|
+
k.ExclamationEqualsToken,
|
|
47
|
+
k.ExclamationEqualsEqualsToken,
|
|
48
|
+
k.LessThanToken,
|
|
49
|
+
k.LessThanEqualsToken,
|
|
50
|
+
k.GreaterThanToken,
|
|
51
|
+
k.GreaterThanEqualsToken,
|
|
52
|
+
k.OpenBracketToken,
|
|
53
|
+
k.OpenBraceToken,
|
|
54
|
+
k.CommaToken,
|
|
55
|
+
k.ColonToken,
|
|
56
|
+
k.QuestionToken,
|
|
57
|
+
k.EqualsGreaterThanToken,
|
|
58
|
+
k.TemplateHead,
|
|
59
|
+
k.TemplateMiddle,
|
|
60
|
+
k.ReturnKeyword,
|
|
61
|
+
k.ThrowKeyword,
|
|
62
|
+
k.CaseKeyword,
|
|
63
|
+
k.ElseKeyword,
|
|
64
|
+
k.AwaitKeyword,
|
|
65
|
+
k.YieldKeyword,
|
|
66
|
+
k.TypeOfKeyword,
|
|
67
|
+
k.VoidKeyword,
|
|
68
|
+
k.DeleteKeyword,
|
|
69
|
+
k.DoKeyword,
|
|
70
|
+
k.TryKeyword,
|
|
71
|
+
k.FinallyKeyword,
|
|
72
|
+
k.InKeyword,
|
|
73
|
+
k.InstanceOfKeyword,
|
|
74
|
+
k.NewKeyword,
|
|
75
|
+
k.OfKeyword,
|
|
76
|
+
k.ExportKeyword,
|
|
77
|
+
k.DefaultKeyword,
|
|
78
|
+
]);
|
|
79
|
+
regexPrefixCache.set(ts, cached);
|
|
80
|
+
}
|
|
81
|
+
return cached;
|
|
82
|
+
}
|
|
83
|
+
/** The line of a byte offset in `content`, from precomputed line starts. */
|
|
84
|
+
function lineOf(lineStarts, offset) {
|
|
85
|
+
let low = 0;
|
|
86
|
+
let high = lineStarts.length - 1;
|
|
87
|
+
while (low < high) {
|
|
88
|
+
const mid = (low + high + 1) >> 1;
|
|
89
|
+
if ((lineStarts[mid] ?? 0) <= offset)
|
|
90
|
+
low = mid;
|
|
91
|
+
else
|
|
92
|
+
high = mid - 1;
|
|
93
|
+
}
|
|
94
|
+
return low + 1;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A code file no tsconfig owns (census `orphan`): no program covers it, so its names can hide
|
|
98
|
+
* references. The TypeScript scanner alone reads it (D55); identifiers that equal an indexed
|
|
99
|
+
* declaration's simple name become `unresolved-name-match` records and its string literals
|
|
100
|
+
* become name tokens, so the query-level cross-check covers these files too. Secret and
|
|
101
|
+
* excluded files never reach this function (RISK §6; they are `unindexed`, not `orphan`).
|
|
102
|
+
*/
|
|
103
|
+
export function scanLexically(ts, file, content, names) {
|
|
104
|
+
const lineStarts = [0];
|
|
105
|
+
for (let at = content.indexOf('\n'); at !== -1; at = content.indexOf('\n', at + 1))
|
|
106
|
+
lineStarts.push(at + 1);
|
|
107
|
+
const identifiers = new Map();
|
|
108
|
+
const strings = new Map();
|
|
109
|
+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true, ts.LanguageVariant.Standard, content);
|
|
110
|
+
const templateBraces = [];
|
|
111
|
+
let previous = ts.SyntaxKind.Unknown;
|
|
112
|
+
const controlParens = [];
|
|
113
|
+
const regexPrefixes = regexPrefixTokens(ts);
|
|
114
|
+
let regexOffsets;
|
|
115
|
+
const regexStartsHere = (offset) => {
|
|
116
|
+
if (regexOffsets === undefined) {
|
|
117
|
+
// Only parse a full AST on the rare `}` + `/` boundary so `{ a: 1 } / x` and
|
|
118
|
+
// `function() {} / x` remain division while `if (b) {} \n /regex/` stays a regex.
|
|
119
|
+
const collected = new Set();
|
|
120
|
+
const sf = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, false);
|
|
121
|
+
const visit = (node) => {
|
|
122
|
+
if (ts.isRegularExpressionLiteral(node))
|
|
123
|
+
collected.add(node.getStart(sf));
|
|
124
|
+
ts.forEachChild(node, visit);
|
|
125
|
+
};
|
|
126
|
+
visit(sf);
|
|
127
|
+
regexOffsets = collected;
|
|
128
|
+
}
|
|
129
|
+
return regexOffsets.has(offset);
|
|
130
|
+
};
|
|
131
|
+
for (let token = scanner.scan(); token !== ts.SyntaxKind.EndOfFileToken; token = scanner.scan()) {
|
|
132
|
+
if (token === ts.SyntaxKind.OpenParenToken) {
|
|
133
|
+
controlParens.push(previous === ts.SyntaxKind.IfKeyword ||
|
|
134
|
+
previous === ts.SyntaxKind.WhileKeyword ||
|
|
135
|
+
previous === ts.SyntaxKind.ForKeyword ||
|
|
136
|
+
previous === ts.SyntaxKind.WithKeyword ||
|
|
137
|
+
previous === ts.SyntaxKind.SwitchKeyword ||
|
|
138
|
+
previous === ts.SyntaxKind.CatchKeyword);
|
|
139
|
+
}
|
|
140
|
+
if (token === ts.SyntaxKind.CloseBraceToken && templateBraces.at(-1) === 0) {
|
|
141
|
+
templateBraces.pop();
|
|
142
|
+
token = scanner.reScanTemplateToken(false);
|
|
143
|
+
}
|
|
144
|
+
else if (token === ts.SyntaxKind.OpenBraceToken && templateBraces.length > 0) {
|
|
145
|
+
templateBraces[templateBraces.length - 1] = (templateBraces.at(-1) ?? 0) + 1;
|
|
146
|
+
}
|
|
147
|
+
else if (token === ts.SyntaxKind.CloseBraceToken && templateBraces.length > 0) {
|
|
148
|
+
templateBraces[templateBraces.length - 1] = (templateBraces.at(-1) ?? 0) - 1;
|
|
149
|
+
}
|
|
150
|
+
if (token === ts.SyntaxKind.TemplateHead || token === ts.SyntaxKind.TemplateMiddle) {
|
|
151
|
+
templateBraces.push(0);
|
|
152
|
+
}
|
|
153
|
+
if ((token === ts.SyntaxKind.SlashToken || token === ts.SyntaxKind.SlashEqualsToken) &&
|
|
154
|
+
(regexPrefixes.has(previous) ||
|
|
155
|
+
(previous === ts.SyntaxKind.CloseBraceToken && regexStartsHere(scanner.getTokenStart())))) {
|
|
156
|
+
token = scanner.reScanSlashToken();
|
|
157
|
+
}
|
|
158
|
+
const isString = token === ts.SyntaxKind.StringLiteral ||
|
|
159
|
+
token === ts.SyntaxKind.NoSubstitutionTemplateLiteral;
|
|
160
|
+
const isIdentifier = token === ts.SyntaxKind.Identifier || token === ts.SyntaxKind.PrivateIdentifier;
|
|
161
|
+
if (isIdentifier || isString) {
|
|
162
|
+
const text = scanner.getTokenValue();
|
|
163
|
+
if (names.has(text)) {
|
|
164
|
+
const at = lineOf(lineStarts, scanner.getTokenStart());
|
|
165
|
+
const target = isString ? strings : identifiers;
|
|
166
|
+
const lines = target.get(text) ?? [];
|
|
167
|
+
if (lines[lines.length - 1] !== at)
|
|
168
|
+
lines.push(at);
|
|
169
|
+
target.set(text, lines);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
previous =
|
|
173
|
+
previous === ts.SyntaxKind.DotToken || previous === ts.SyntaxKind.QuestionDotToken
|
|
174
|
+
? ts.SyntaxKind.Identifier
|
|
175
|
+
: token === ts.SyntaxKind.CloseParenToken && controlParens.pop()
|
|
176
|
+
? ts.SyntaxKind.Unknown
|
|
177
|
+
: token;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
index: {
|
|
181
|
+
file,
|
|
182
|
+
extraction: 'lexical',
|
|
183
|
+
lines: lineStarts.length,
|
|
184
|
+
surface: '',
|
|
185
|
+
global: false,
|
|
186
|
+
counts: {
|
|
187
|
+
indexed: 0,
|
|
188
|
+
local: 0,
|
|
189
|
+
external: 0,
|
|
190
|
+
externalUnresolved: 0,
|
|
191
|
+
unresolvedOther: 0,
|
|
192
|
+
outsideScope: 0,
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
declarations: [],
|
|
196
|
+
edges: [],
|
|
197
|
+
// No checker can resolve these uses: every matching identifier is a potential reference.
|
|
198
|
+
unresolved: [...identifiers].flatMap(([name, lines]) => lines.map((line) => ({
|
|
199
|
+
file,
|
|
200
|
+
line,
|
|
201
|
+
name,
|
|
202
|
+
reason: 'unresolved-name-match',
|
|
203
|
+
source: `${file}#<module>`,
|
|
204
|
+
}))),
|
|
205
|
+
gaps: [],
|
|
206
|
+
tokens: [...strings].map(([name, lines]) => ({ file, name, kind: 'str', lines })),
|
|
207
|
+
externalImports: [],
|
|
208
|
+
exports: [],
|
|
209
|
+
markers: [],
|
|
210
|
+
search: [],
|
|
211
|
+
};
|
|
212
|
+
}
|