veoflow 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/veo.mjs
CHANGED
|
@@ -3420,7 +3420,7 @@ var init_catalog = __esm({
|
|
|
3420
3420
|
init_src();
|
|
3421
3421
|
init_project_store();
|
|
3422
3422
|
init_sqlite();
|
|
3423
|
-
DEFAULT_PROJECT_SETTINGS = { maxFileMB: 10 };
|
|
3423
|
+
DEFAULT_PROJECT_SETTINGS = { maxFileMB: 10, closedWorld: false };
|
|
3424
3424
|
SCHEMA2 = `
|
|
3425
3425
|
CREATE TABLE IF NOT EXISTS projects (id TEXT PRIMARY KEY, name TEXT NOT NULL, created_at TEXT NOT NULL);
|
|
3426
3426
|
CREATE TABLE IF NOT EXISTS sources (
|
|
@@ -4446,6 +4446,7 @@ async function runNow(request) {
|
|
|
4446
4446
|
view: run2.view().withStages(request.stages.slice(0, index + 1).map((s) => s.name)),
|
|
4447
4447
|
evidence: request.evidence,
|
|
4448
4448
|
entitlements,
|
|
4449
|
+
settings: request.settings ?? {},
|
|
4449
4450
|
progress
|
|
4450
4451
|
});
|
|
4451
4452
|
const facts = write2.facts.length;
|
|
@@ -5665,10 +5666,18 @@ var init_engine = __esm({
|
|
|
5665
5666
|
throw new VeoError("invalid-arguments", `maxFileMB must be a number between 0 and ${MAX_FILE_MB_CAP}`, {
|
|
5666
5667
|
fields: ["maxFileMB"]
|
|
5667
5668
|
});
|
|
5669
|
+
const closedWorld = args.settings?.closedWorld;
|
|
5670
|
+
if (closedWorld !== void 0 && typeof closedWorld !== "boolean")
|
|
5671
|
+
throw new VeoError("invalid-arguments", "closedWorld must be true or false", {
|
|
5672
|
+
fields: ["closedWorld"]
|
|
5673
|
+
});
|
|
5668
5674
|
const project2 = this.resolveProject(args, cwd);
|
|
5669
5675
|
return {
|
|
5670
5676
|
project: project2.id,
|
|
5671
|
-
settings: this.catalog.setProjectSettings(project2.id,
|
|
5677
|
+
settings: this.catalog.setProjectSettings(project2.id, {
|
|
5678
|
+
...mb === void 0 ? {} : { maxFileMB: mb },
|
|
5679
|
+
...closedWorld === void 0 ? {} : { closedWorld }
|
|
5680
|
+
})
|
|
5672
5681
|
};
|
|
5673
5682
|
}
|
|
5674
5683
|
/**
|
|
@@ -5738,6 +5747,7 @@ var init_engine = __esm({
|
|
|
5738
5747
|
...options2.coalesce ? { coalesce: options2.coalesce } : {},
|
|
5739
5748
|
...options2.ref ? { ref: options2.ref } : {},
|
|
5740
5749
|
planOptions: { maxFileBytes: this.catalog.projectSettings(project2.id).maxFileMB * MB },
|
|
5750
|
+
settings: { closedWorld: this.catalog.projectSettings(project2.id).closedWorld },
|
|
5741
5751
|
signal: controller.signal,
|
|
5742
5752
|
...this.killableRuns ? { killable: true } : {},
|
|
5743
5753
|
onEvent: (e) => {
|
|
@@ -8978,7 +8988,7 @@ function explainSite(reader, s, r, stage22) {
|
|
|
8978
8988
|
return {
|
|
8979
8989
|
...at,
|
|
8980
8990
|
outcome: "linked",
|
|
8981
|
-
summary: `${s.expression} calls ${target2.name} in ${target2.path}${r.hops?.length ? `, through ${r.hops.length} re-export${r.hops.length > 1 ? "s" : ""}` : ""}.`,
|
|
8991
|
+
summary: `${s.expression} calls ${target2.name} in ${target2.path}${r.hops?.length ? `, through ${r.hops.length} re-export${r.hops.length > 1 ? "s" : ""}` : ""}${r.via === "injection" ? ", the class the framework injects" : r.via === "flow" ? ", the only function its receiver's value can run" : r.via === "closed-world" ? ", assuming the analyzed code is the whole program (closed world)" : ""}.`,
|
|
8982
8992
|
steps: r.steps,
|
|
8983
8993
|
target: target2
|
|
8984
8994
|
};
|
|
@@ -9003,14 +9013,15 @@ function explainSite(reader, s, r, stage22) {
|
|
|
9003
9013
|
...pkg ? { target: pkg } : {},
|
|
9004
9014
|
...exit
|
|
9005
9015
|
};
|
|
9016
|
+
const injection = r.candidates?.length && last?.kind === "injection" ? last.detail : null;
|
|
9006
9017
|
if (r.candidates?.length)
|
|
9007
9018
|
return {
|
|
9008
9019
|
...at,
|
|
9009
9020
|
outcome: "ambiguous",
|
|
9010
|
-
summary: typed ? `${s.expression}: ${typed}; ${last?.detail ?? ""}, so none is linked.` : r.exhaustive === false ? `${s.expression} could call ${r.candidates.length === 1 ? "the 1 known function" : `any of ${r.candidates.length} known functions`}, or a method of any other object of the right shape, so none is linked.` : `${s.expression} could call ${r.candidates.length === 1 ? "only 1 function" : `any of ${r.candidates.length} functions`}, so none is linked.`,
|
|
9021
|
+
summary: injection ? `${s.expression} could call ${r.candidates.length === 1 ? "the 1 known function" : `any of ${r.candidates.length} known functions`}: it is injected, but ${injection}, so none is linked.` : typed ? `${s.expression}: ${typed}; ${last?.detail ?? ""}, so none is linked.` : r.exhaustive === false ? `${s.expression} could call ${r.candidates.length === 1 ? "the 1 known function" : `any of ${r.candidates.length} known functions`}, or a method of any other object of the right shape, so none is linked.` : `${s.expression} could call ${r.candidates.length === 1 ? "only 1 function" : `any of ${r.candidates.length} functions`}, so none is linked.`,
|
|
9011
9022
|
steps: r.steps,
|
|
9012
9023
|
candidates: r.candidates.map((c) => end(reader, c.function)),
|
|
9013
|
-
toResolve: CALL_FIXES.candidates,
|
|
9024
|
+
toResolve: injection ? CALL_FIXES.injection : CALL_FIXES.candidates,
|
|
9014
9025
|
...r.exhaustive === false ? { exhaustive: false } : {}
|
|
9015
9026
|
};
|
|
9016
9027
|
const stop = r.steps.at(-1);
|
|
@@ -9282,6 +9293,7 @@ var init_explain = __esm({
|
|
|
9282
9293
|
"no-body": "It calls whatever function is passed in: see what reaches this function, and what it passes.",
|
|
9283
9294
|
"bodies-unknown": "A class reaching the type isn't in the project: add the source that holds it.",
|
|
9284
9295
|
default: "Callers pass what runs: see what calls this function.",
|
|
9296
|
+
injection: "Let the framework's wiring fix the class: provide the token as one class, set the field only from the constructor, and construct the class only through the framework.",
|
|
9285
9297
|
candidates: "One of the known functions runs, and nothing in the code fixes which (a type doesn't, D72): calling the function itself would.",
|
|
9286
9298
|
"dynamic-dispatch": "Write the receiver's type, or call the function by name.",
|
|
9287
9299
|
"computed-member": "Call the member by name, or narrow the key to known names.",
|
|
@@ -14764,6 +14776,2821 @@ var init_candidates = __esm({
|
|
|
14764
14776
|
}
|
|
14765
14777
|
});
|
|
14766
14778
|
|
|
14779
|
+
// packages/stage2/src/di.ts
|
|
14780
|
+
function makeInjection(graph, s, together = () => true) {
|
|
14781
|
+
if (!s?.checker || !s.program) return null;
|
|
14782
|
+
const { ts, checker, program, fileIdOf: fileIdOf2 } = s;
|
|
14783
|
+
const pathOf9 = (file2) => file2.slice(file2.indexOf(":") + 1);
|
|
14784
|
+
const where = (node2) => {
|
|
14785
|
+
const sf = node2.getSourceFile();
|
|
14786
|
+
const file2 = fileIdOf2(sf);
|
|
14787
|
+
return `${file2 ? pathOf9(file2) : sf.fileName}:${sf.getLineAndCharacterOfPosition(node2.getStart(sf)).line + 1}`;
|
|
14788
|
+
};
|
|
14789
|
+
const resolved = (node2) => {
|
|
14790
|
+
const sym = checker.getSymbolAtLocation(node2);
|
|
14791
|
+
return sym && sym.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(sym) : sym;
|
|
14792
|
+
};
|
|
14793
|
+
const declOfClass = (node2) => {
|
|
14794
|
+
if (!node2 || !ts.isClassDeclaration(node2) || !node2.name || !ts.isSourceFile(node2.parent)) return null;
|
|
14795
|
+
const file2 = fileIdOf2(node2.getSourceFile());
|
|
14796
|
+
const d = file2 ? { file: file2, name: node2.name.text } : null;
|
|
14797
|
+
return d && graph.declared(d) ? d : null;
|
|
14798
|
+
};
|
|
14799
|
+
const classOf = (node2) => {
|
|
14800
|
+
while (ts.isParenthesizedExpression(node2) || ts.isAsExpression(node2) || ts.isSatisfiesExpression(node2) || ts.isNonNullExpression(node2) || ts.isTypeAssertionExpression(node2))
|
|
14801
|
+
node2 = node2.expression;
|
|
14802
|
+
const sym = resolved(node2);
|
|
14803
|
+
const d = sym?.valueDeclaration ?? sym?.declarations?.find((x) => ts.isClassDeclaration(x));
|
|
14804
|
+
let decl = declOfClass(d);
|
|
14805
|
+
if (!decl && ts.isExpression(node2)) {
|
|
14806
|
+
const made = checker.getTypeAtLocation(node2).getConstructSignatures()[0]?.getReturnType().getSymbol();
|
|
14807
|
+
const md = made?.valueDeclaration ?? made?.declarations?.[0];
|
|
14808
|
+
decl = declOfClass(md);
|
|
14809
|
+
if (decl) return { decl, node: md };
|
|
14810
|
+
}
|
|
14811
|
+
return decl ? { decl, node: d } : null;
|
|
14812
|
+
};
|
|
14813
|
+
const isAbstract = (c) => (ts.getCombinedModifierFlags(c) & ts.ModifierFlags.Abstract) !== 0;
|
|
14814
|
+
const classNode = (d) => s.sourceFile(d.file)?.statements.find((x) => ts.isClassDeclaration(x) && x.name?.text === d.name);
|
|
14815
|
+
const importedAs = (callee) => {
|
|
14816
|
+
if (!ts.isIdentifier(callee)) return null;
|
|
14817
|
+
const sym = checker.getSymbolAtLocation(callee);
|
|
14818
|
+
const spec = sym?.declarations?.[0];
|
|
14819
|
+
if (!spec || !ts.isImportSpecifier(spec)) return null;
|
|
14820
|
+
const decl = spec.parent.parent.parent;
|
|
14821
|
+
if (!ts.isImportDeclaration(decl) || !ts.isStringLiteral(decl.moduleSpecifier)) return null;
|
|
14822
|
+
return { pkg: decl.moduleSpecifier.text, name: (spec.propertyName ?? spec.name).text };
|
|
14823
|
+
};
|
|
14824
|
+
const fromPackage = (node2) => {
|
|
14825
|
+
let root = node2;
|
|
14826
|
+
while (ts.isPropertyAccessExpression(root)) root = root.expression;
|
|
14827
|
+
if (!ts.isIdentifier(root)) return false;
|
|
14828
|
+
const from = importedAs(root);
|
|
14829
|
+
if (from) return !from.pkg.startsWith(".") && !from.pkg.startsWith("/");
|
|
14830
|
+
const d = checker.getSymbolAtLocation(root)?.declarations?.[0];
|
|
14831
|
+
const imp = d && ts.findAncestor(d, ts.isImportDeclaration);
|
|
14832
|
+
if (imp) return ts.isStringLiteral(imp.moduleSpecifier) && !/^[./]/.test(imp.moduleSpecifier.text);
|
|
14833
|
+
const v = d && ts.findAncestor(d, ts.isVariableDeclaration);
|
|
14834
|
+
let init = v?.initializer;
|
|
14835
|
+
while (init && (ts.isPropertyAccessExpression(init) || ts.isParenthesizedExpression(init)))
|
|
14836
|
+
init = init.expression;
|
|
14837
|
+
return !!init && ts.isCallExpression(init) && ts.isIdentifier(init.expression) && init.expression.text === "require" && !!init.arguments[0] && ts.isStringLiteral(init.arguments[0]) && !/^[./]/.test(init.arguments[0].text);
|
|
14838
|
+
};
|
|
14839
|
+
const fromWriterLibrary = (callee) => {
|
|
14840
|
+
let root = callee;
|
|
14841
|
+
while (ts.isPropertyAccessExpression(root)) root = root.expression;
|
|
14842
|
+
if (!ts.isIdentifier(root)) return false;
|
|
14843
|
+
const d = checker.getSymbolAtLocation(root)?.declarations?.[0];
|
|
14844
|
+
const imp = d && ts.findAncestor(d, ts.isImportDeclaration);
|
|
14845
|
+
const spec = imp && ts.isStringLiteral(imp.moduleSpecifier) ? imp.moduleSpecifier.text : null;
|
|
14846
|
+
return !!spec && /^(lodash(-es)?|lodash[./].*|underscore|es-toolkit(\/.*)?)$/.test(spec);
|
|
14847
|
+
};
|
|
14848
|
+
const readableToken = (node2) => {
|
|
14849
|
+
if (ts.isStringLiteralLike(node2) || ts.isNumericLiteral(node2)) return true;
|
|
14850
|
+
if (!ts.isIdentifier(node2) && !ts.isPropertyAccessExpression(node2)) return false;
|
|
14851
|
+
let root = node2;
|
|
14852
|
+
while (ts.isPropertyAccessExpression(root)) root = root.expression;
|
|
14853
|
+
const d = resolved(node2)?.valueDeclaration;
|
|
14854
|
+
if (!d) {
|
|
14855
|
+
const from = importedAs(root);
|
|
14856
|
+
return !!from && !from.pkg.startsWith(".") && !from.pkg.startsWith("/");
|
|
14857
|
+
}
|
|
14858
|
+
if (ts.isEnumMember(d)) return true;
|
|
14859
|
+
const init = ts.isVariableDeclaration(d) && ts.getCombinedNodeFlags(d) & ts.NodeFlags.Const ? d.initializer : ts.isPropertyAssignment(d) ? d.initializer : void 0;
|
|
14860
|
+
return !!init && (ts.isStringLiteralLike(init) || ts.isNumericLiteral(init) || ts.isTemplateExpression(init) || ts.isNewExpression(init) || ts.isCallExpression(init) && ts.isIdentifier(init.expression) && init.expression.text === "Symbol");
|
|
14861
|
+
};
|
|
14862
|
+
const decorators = (node2) => (ts.getDecorators(node2) ?? []).map((d) => {
|
|
14863
|
+
const call = ts.isCallExpression(d.expression) ? d.expression : null;
|
|
14864
|
+
return { call, from: importedAs(call ? call.expression : d.expression) };
|
|
14865
|
+
});
|
|
14866
|
+
const diDecorator = (c) => {
|
|
14867
|
+
for (const { from } of decorators(c))
|
|
14868
|
+
if (from && DI_DECORATORS[from.pkg]?.includes(from.name)) return `@${from.name} from ${from.pkg}`;
|
|
14869
|
+
return null;
|
|
14870
|
+
};
|
|
14871
|
+
const onlyDiDecorators = (c) => decorators(c).every(({ from }) => !!from && !!DI_DECORATORS[from.pkg]?.includes(from.name));
|
|
14872
|
+
let wiring;
|
|
14873
|
+
const nameOf = (n2) => n2 && (ts.isIdentifier(n2) || ts.isStringLiteralLike(n2)) ? n2.text : null;
|
|
14874
|
+
const enclosingClass = (node2) => declOfClass(ts.findAncestor(node2.parent, ts.isClassDeclaration));
|
|
14875
|
+
const wired = () => {
|
|
14876
|
+
if (wiring) return wiring;
|
|
14877
|
+
const w = {
|
|
14878
|
+
provides: /* @__PURE__ */ new Map(),
|
|
14879
|
+
listed: /* @__PURE__ */ new Set(),
|
|
14880
|
+
unreadable: [],
|
|
14881
|
+
news: /* @__PURE__ */ new Map(),
|
|
14882
|
+
mixins: /* @__PURE__ */ new Map(),
|
|
14883
|
+
writes: /* @__PURE__ */ new Map(),
|
|
14884
|
+
wholesale: [],
|
|
14885
|
+
borrowed: /* @__PURE__ */ new Map(),
|
|
14886
|
+
givenNews: [],
|
|
14887
|
+
handed: /* @__PURE__ */ new Map()
|
|
14888
|
+
};
|
|
14889
|
+
const push = (m, k, v) => {
|
|
14890
|
+
const list = m.get(k);
|
|
14891
|
+
if (list) list.push(v);
|
|
14892
|
+
else m.set(k, [v]);
|
|
14893
|
+
};
|
|
14894
|
+
const valueOf = (p) => ts.isPropertyAssignment(p) ? p.initializer : ts.isShorthandPropertyAssignment(p) ? p.name : void 0;
|
|
14895
|
+
const providerOf = (o, token) => {
|
|
14896
|
+
for (const p of o.properties) {
|
|
14897
|
+
const kind = PROVIDER_KINDS.find((k) => k === nameOf(p.name));
|
|
14898
|
+
if (!kind) continue;
|
|
14899
|
+
const v = valueOf(p);
|
|
14900
|
+
const target2 = kind === "useClass" && v ? classOf(v) : null;
|
|
14901
|
+
if (target2 && declKey(target2.decl) === declKey(token)) w.listed.add(declKey(token));
|
|
14902
|
+
else push(w.provides, declKey(token), { kind, target: target2?.decl ?? null, at: p });
|
|
14903
|
+
}
|
|
14904
|
+
};
|
|
14905
|
+
const written2 = (node2) => {
|
|
14906
|
+
let at = node2;
|
|
14907
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isPropertyAssignment(at.parent) || ts.isShorthandPropertyAssignment(at.parent) || ts.isSpreadAssignment(at.parent) || ts.isSpreadElement(at.parent) || ts.isObjectLiteralExpression(at.parent) || ts.isArrayLiteralExpression(at.parent)) {
|
|
14908
|
+
if (ts.isPropertyAssignment(at.parent) && at.parent.initializer !== at) return false;
|
|
14909
|
+
at = at.parent;
|
|
14910
|
+
}
|
|
14911
|
+
const parent = at.parent;
|
|
14912
|
+
if (ts.isDeleteExpression(parent) || ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent))
|
|
14913
|
+
return ts.isDeleteExpression(parent) || parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken;
|
|
14914
|
+
if (ts.isBinaryExpression(parent))
|
|
14915
|
+
return parent.left === at && parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
|
|
14916
|
+
return (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) && parent.initializer === at;
|
|
14917
|
+
};
|
|
14918
|
+
const handedOut = (node2) => {
|
|
14919
|
+
let at = node2;
|
|
14920
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isNonNullExpression(at.parent) || ts.isConditionalExpression(at.parent) && at.parent.condition !== at || ts.isBinaryExpression(at.parent) && [
|
|
14921
|
+
ts.SyntaxKind.BarBarToken,
|
|
14922
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
14923
|
+
ts.SyntaxKind.AmpersandAmpersandToken
|
|
14924
|
+
].includes(at.parent.operatorToken.kind))
|
|
14925
|
+
at = at.parent;
|
|
14926
|
+
const p = at.parent;
|
|
14927
|
+
if ((ts.isCallExpression(p) || ts.isNewExpression(p)) && p.expression === at) return false;
|
|
14928
|
+
if (ts.isTaggedTemplateExpression(p) && p.tag === at) return false;
|
|
14929
|
+
if ((ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p)) && p.expression === at) {
|
|
14930
|
+
const key = ts.isPropertyAccessExpression(p) ? p.name.text : ts.isStringLiteralLike(p.argumentExpression) ? p.argumentExpression.text : null;
|
|
14931
|
+
return key === null || ["call", "apply", "bind"].includes(key);
|
|
14932
|
+
}
|
|
14933
|
+
if (ts.isTypeOfExpression(p) || ts.isVoidExpression(p) || ts.isDeleteExpression(p)) return false;
|
|
14934
|
+
if (ts.isPrefixUnaryExpression(p) && p.operator === ts.SyntaxKind.ExclamationToken) return false;
|
|
14935
|
+
if (ts.isBinaryExpression(p) && [
|
|
14936
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
14937
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
14938
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
14939
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
14940
|
+
ts.SyntaxKind.InstanceOfKeyword,
|
|
14941
|
+
ts.SyntaxKind.InKeyword
|
|
14942
|
+
].includes(p.operatorToken.kind))
|
|
14943
|
+
return false;
|
|
14944
|
+
if ((ts.isIfStatement(p) || ts.isWhileStatement(p) || ts.isDoStatement(p)) && p.expression === at)
|
|
14945
|
+
return false;
|
|
14946
|
+
if (ts.isConditionalExpression(p) && p.condition === at) return false;
|
|
14947
|
+
if (ts.isExpressionStatement(p)) return false;
|
|
14948
|
+
return true;
|
|
14949
|
+
};
|
|
14950
|
+
const descriptorHandedOut = (call) => {
|
|
14951
|
+
const fnHandedOut = (d) => {
|
|
14952
|
+
let at = d;
|
|
14953
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isNonNullExpression(at.parent)) at = at.parent;
|
|
14954
|
+
const p2 = at.parent;
|
|
14955
|
+
if (ts.isPropertyAccessExpression(p2) && p2.expression === at)
|
|
14956
|
+
return ["value", "get", "set"].includes(p2.name.text) ? handedOut(p2) || calledOn(p2) : false;
|
|
14957
|
+
if (ts.isElementAccessExpression(p2) && p2.expression === at) return true;
|
|
14958
|
+
return handedOut(d);
|
|
14959
|
+
};
|
|
14960
|
+
const calledOn = (m) => ts.isCallExpression(m.parent) && m.parent.expression === m;
|
|
14961
|
+
const p = call.parent;
|
|
14962
|
+
if (ts.isVariableDeclaration(p) && p.initializer === call && ts.isIdentifier(p.name)) {
|
|
14963
|
+
const sym = checker.getSymbolAtLocation(p.name);
|
|
14964
|
+
const scope2 = ts.findAncestor(p, (n2) => ts.isFunctionLike(n2) || ts.isSourceFile(n2));
|
|
14965
|
+
if (!sym || !scope2) return true;
|
|
14966
|
+
let out = false;
|
|
14967
|
+
const visit2 = (n2) => {
|
|
14968
|
+
if (out) return;
|
|
14969
|
+
if (ts.isIdentifier(n2) && n2 !== p.name && checker.getSymbolAtLocation(n2) === sym)
|
|
14970
|
+
out = fnHandedOut(n2);
|
|
14971
|
+
else ts.forEachChild(n2, visit2);
|
|
14972
|
+
};
|
|
14973
|
+
visit2(scope2);
|
|
14974
|
+
return out;
|
|
14975
|
+
}
|
|
14976
|
+
return fnHandedOut(call);
|
|
14977
|
+
};
|
|
14978
|
+
const projectFiles = program.getSourceFiles().filter((sf) => !!fileIdOf2(sf));
|
|
14979
|
+
const methodNames = /* @__PURE__ */ new Set();
|
|
14980
|
+
for (const sf of projectFiles) {
|
|
14981
|
+
const look = (node2) => {
|
|
14982
|
+
if ((ts.isMethodDeclaration(node2) || ts.isGetAccessorDeclaration(node2) || ts.isSetAccessorDeclaration(node2)) && ts.isClassLike(node2.parent)) {
|
|
14983
|
+
const n2 = nameOf(node2.name);
|
|
14984
|
+
if (n2) methodNames.add(n2);
|
|
14985
|
+
}
|
|
14986
|
+
ts.forEachChild(node2, look);
|
|
14987
|
+
};
|
|
14988
|
+
look(sf);
|
|
14989
|
+
}
|
|
14990
|
+
for (const sf of projectFiles) {
|
|
14991
|
+
const test = isTestPath(pathOf9(fileIdOf2(sf)));
|
|
14992
|
+
const file2 = fileIdOf2(sf);
|
|
14993
|
+
const visit2 = (node2) => {
|
|
14994
|
+
if (test) {
|
|
14995
|
+
} else if (ts.isObjectLiteralExpression(node2)) {
|
|
14996
|
+
const provide = node2.properties.find((p) => nameOf(p.name) === "provide");
|
|
14997
|
+
const uses = node2.properties.some((p) => PROVIDER_KINDS.some((k) => k === nameOf(p.name)));
|
|
14998
|
+
const value = provide && valueOf(provide);
|
|
14999
|
+
const token = value && classOf(value);
|
|
15000
|
+
const hidden = node2.properties.some(
|
|
15001
|
+
(p) => ts.isSpreadAssignment(p) || p.name !== void 0 && ts.isComputedPropertyName(p.name)
|
|
15002
|
+
);
|
|
15003
|
+
if ((provide || uses) && hidden) w.unreadable.push(node2);
|
|
15004
|
+
else if (token) providerOf(node2, token.decl);
|
|
15005
|
+
else if (value && !readableToken(value)) w.unreadable.push(node2);
|
|
15006
|
+
} else if (ts.isArrayLiteralExpression(node2)) {
|
|
15007
|
+
for (const e of node2.elements) {
|
|
15008
|
+
const c = ts.isIdentifier(e) || ts.isPropertyAccessExpression(e) ? classOf(e) : null;
|
|
15009
|
+
if (c) w.listed.add(declKey(c.decl));
|
|
15010
|
+
}
|
|
15011
|
+
} else if (ts.isClassDeclaration(node2) || ts.isClassExpression(node2)) {
|
|
15012
|
+
const self2 = ts.isClassDeclaration(node2) ? declOfClass(node2) : null;
|
|
15013
|
+
for (const { call, from } of ts.isClassDeclaration(node2) ? decorators(node2) : []) {
|
|
15014
|
+
const arg = call?.arguments[0];
|
|
15015
|
+
if (!self2 || !arg || !ts.isObjectLiteralExpression(arg) || from?.name !== "Injectable") continue;
|
|
15016
|
+
if (arg.properties.some((p) => nameOf(p.name) === "providedIn")) w.listed.add(declKey(self2));
|
|
15017
|
+
providerOf(arg, self2);
|
|
15018
|
+
}
|
|
15019
|
+
for (const h of node2.heritageClauses ?? [])
|
|
15020
|
+
for (const t of h.types)
|
|
15021
|
+
if (ts.isCallExpression(t.expression))
|
|
15022
|
+
for (const a of t.expression.arguments) {
|
|
15023
|
+
const c = classOf(a);
|
|
15024
|
+
if (c) w.mixins.set(declKey(c.decl), t);
|
|
15025
|
+
}
|
|
15026
|
+
} else if (ts.isNewExpression(node2)) {
|
|
15027
|
+
const c = classOf(node2.expression);
|
|
15028
|
+
if (c)
|
|
15029
|
+
push(w.news, declKey(c.decl), { at: node2, args: node2.arguments ?? ts.factory.createNodeArray() });
|
|
15030
|
+
else {
|
|
15031
|
+
const d = resolved(node2.expression)?.valueDeclaration;
|
|
15032
|
+
if (d && ts.isParameter(d) || checker.getTypeAtLocation(node2).flags & ts.TypeFlags.TypeParameter || checker.getTypeAtLocation(node2.expression).flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown) && !fromPackage(node2.expression))
|
|
15033
|
+
w.givenNews.push(node2);
|
|
15034
|
+
}
|
|
15035
|
+
} else if (ts.isCallExpression(node2) && ts.isPropertyAccessExpression(node2.expression) && ts.isIdentifier(node2.expression.expression)) {
|
|
15036
|
+
const target2 = `${node2.expression.expression.text}.${node2.expression.name.text}`;
|
|
15037
|
+
const first = node2.arguments[0];
|
|
15038
|
+
if (target2 === "Reflect.construct" && first) {
|
|
15039
|
+
const c = classOf(first);
|
|
15040
|
+
if (c) push(w.news, declKey(c.decl), { at: node2, args: "all" });
|
|
15041
|
+
}
|
|
15042
|
+
}
|
|
15043
|
+
if (ts.isCallExpression(node2)) {
|
|
15044
|
+
const callee = node2.expression;
|
|
15045
|
+
const [first, second] = node2.arguments;
|
|
15046
|
+
const target2 = ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression) ? `${callee.expression.text}.${callee.name.text}` : null;
|
|
15047
|
+
if (target2 === "Object.assign" && first) {
|
|
15048
|
+
for (const src of node2.arguments.slice(1)) {
|
|
15049
|
+
if (ts.isObjectLiteralExpression(src) && !src.properties.some((p) => !nameOf(p.name)))
|
|
15050
|
+
for (const p of src.properties)
|
|
15051
|
+
push(w.writes, nameOf(p.name), { at: p, obj: first, test, file: file2 });
|
|
15052
|
+
else w.wholesale.push({ at: node2, obj: first, test, file: file2, whole: true });
|
|
15053
|
+
}
|
|
15054
|
+
} else if ((target2 === "Reflect.set" || target2 === "Object.defineProperty" || target2 === "Reflect.defineProperty") && first && second) {
|
|
15055
|
+
if (ts.isStringLiteralLike(second))
|
|
15056
|
+
push(w.writes, second.text, { at: node2, obj: first, test, file: file2 });
|
|
15057
|
+
else w.wholesale.push({ at: node2, obj: first, test, file: file2, computed: true });
|
|
15058
|
+
} else if ((target2 === "Object.getOwnPropertyDescriptor" || target2 === "Reflect.getOwnPropertyDescriptor") && first) {
|
|
15059
|
+
if (!descriptorHandedOut(node2)) {
|
|
15060
|
+
} else if (second && ts.isStringLiteralLike(second))
|
|
15061
|
+
push(w.borrowed, second.text, { at: node2, obj: first, test, file: file2, borrow: true });
|
|
15062
|
+
else push(w.borrowed, "", { at: node2, obj: first, test, file: file2, borrow: true, computed: true });
|
|
15063
|
+
} else if (target2 === "Object.getOwnPropertyDescriptors" && first) {
|
|
15064
|
+
push(w.borrowed, "", { at: node2, obj: first, test, file: file2, borrow: true, computed: true });
|
|
15065
|
+
} else if ((target2 === "Object.defineProperties" || target2 === "Object.setPrototypeOf" || target2 === "Reflect.setPrototypeOf") && first) {
|
|
15066
|
+
w.wholesale.push({ at: node2, obj: first, test, file: file2, whole: true });
|
|
15067
|
+
} else if (first) {
|
|
15068
|
+
const name = ts.isPropertyAccessExpression(callee) ? callee.name.text : ts.isIdentifier(callee) ? callee.text : null;
|
|
15069
|
+
if (name && LIBRARY_WRITERS.has(name) && fromWriterLibrary(callee)) {
|
|
15070
|
+
const byPath = ["set", "setWith", "update", "updateWith", "unset"].includes(name);
|
|
15071
|
+
const path = byPath && second && ts.isStringLiteralLike(second) ? second.text.split(/[.[]/)[0] : null;
|
|
15072
|
+
if (path) push(w.writes, path, { at: node2, obj: first, test, file: file2 });
|
|
15073
|
+
else
|
|
15074
|
+
w.wholesale.push({
|
|
15075
|
+
at: node2,
|
|
15076
|
+
obj: first,
|
|
15077
|
+
test,
|
|
15078
|
+
file: file2,
|
|
15079
|
+
...byPath ? { computed: true } : { whole: true }
|
|
15080
|
+
});
|
|
15081
|
+
}
|
|
15082
|
+
}
|
|
15083
|
+
}
|
|
15084
|
+
const handedOn = (a) => {
|
|
15085
|
+
const c = a && (ts.isIdentifier(a) || ts.isPropertyAccessExpression(a)) ? classOf(a) : null;
|
|
15086
|
+
if (c && !w.handed.has(declKey(c.decl)) && !ts.findAncestor(a, ts.isDecorator))
|
|
15087
|
+
w.handed.set(declKey(c.decl), a);
|
|
15088
|
+
};
|
|
15089
|
+
if (test) {
|
|
15090
|
+
} else if (ts.isCallExpression(node2) || ts.isNewExpression(node2))
|
|
15091
|
+
for (const a of node2.arguments ?? []) handedOn(a);
|
|
15092
|
+
else if (ts.isPropertyAssignment(node2) && !["provide", ...PROVIDER_KINDS].includes(nameOf(node2.name) ?? ""))
|
|
15093
|
+
handedOn(node2.initializer);
|
|
15094
|
+
else if (ts.isShorthandPropertyAssignment(node2)) handedOn(node2.name);
|
|
15095
|
+
else if (ts.isBinaryExpression(node2) && node2.operatorToken.kind === ts.SyntaxKind.EqualsToken)
|
|
15096
|
+
handedOn(node2.right);
|
|
15097
|
+
else if (ts.isVariableDeclaration(node2) && node2.initializer) handedOn(node2.initializer);
|
|
15098
|
+
if (ts.isPropertyAccessExpression(node2) || ts.isElementAccessExpression(node2)) {
|
|
15099
|
+
if (written2(node2)) {
|
|
15100
|
+
const key = ts.isPropertyAccessExpression(node2) ? node2.name.text : ts.isStringLiteralLike(node2.argumentExpression) ? node2.argumentExpression.text : null;
|
|
15101
|
+
if (key !== null) push(w.writes, key, { at: node2, obj: node2.expression, test, file: file2 });
|
|
15102
|
+
else w.wholesale.push({ at: node2, obj: node2.expression, test, file: file2, computed: true });
|
|
15103
|
+
} else {
|
|
15104
|
+
const key = ts.isPropertyAccessExpression(node2) ? node2.name.text : ts.isStringLiteralLike(node2.argumentExpression) ? node2.argumentExpression.text : null;
|
|
15105
|
+
if (key !== null && methodNames.has(key) && node2.expression.kind !== ts.SyntaxKind.SuperKeyword && handedOut(node2))
|
|
15106
|
+
push(w.borrowed, key, { at: node2, obj: node2.expression, test, file: file2, borrow: true });
|
|
15107
|
+
}
|
|
15108
|
+
}
|
|
15109
|
+
if (ts.isBindingElement(node2) && ts.isObjectBindingPattern(node2.parent) && !node2.dotDotDotToken) {
|
|
15110
|
+
const key = nameOf(node2.propertyName ?? (ts.isIdentifier(node2.name) ? node2.name : void 0));
|
|
15111
|
+
if (key && methodNames.has(key))
|
|
15112
|
+
push(w.borrowed, key, {
|
|
15113
|
+
at: node2,
|
|
15114
|
+
obj: node2.parent,
|
|
15115
|
+
test,
|
|
15116
|
+
file: file2,
|
|
15117
|
+
borrow: true
|
|
15118
|
+
});
|
|
15119
|
+
}
|
|
15120
|
+
ts.forEachChild(node2, visit2);
|
|
15121
|
+
};
|
|
15122
|
+
visit2(sf);
|
|
15123
|
+
}
|
|
15124
|
+
return wiring = w;
|
|
15125
|
+
};
|
|
15126
|
+
const subclassesOf = (c) => {
|
|
15127
|
+
const out = [];
|
|
15128
|
+
const seen = /* @__PURE__ */ new Set([declKey(c)]);
|
|
15129
|
+
const queue = [c];
|
|
15130
|
+
while (queue.length) {
|
|
15131
|
+
const at = queue.shift();
|
|
15132
|
+
for (const sub of graph.subtypes.get(declKey(at)) ?? []) {
|
|
15133
|
+
if (sub.kind !== "class" || seen.has(declKey(sub.decl))) continue;
|
|
15134
|
+
if (graph.parentOf.get(declKey(sub.decl))?.base?.name !== at.name) continue;
|
|
15135
|
+
seen.add(declKey(sub.decl));
|
|
15136
|
+
out.push(sub.decl);
|
|
15137
|
+
queue.push(sub.decl);
|
|
15138
|
+
}
|
|
15139
|
+
}
|
|
15140
|
+
return out;
|
|
15141
|
+
};
|
|
15142
|
+
const bodyCtor = (c) => c.members.find((m) => ts.isConstructorDeclaration(m) && !!m.body);
|
|
15143
|
+
const suppliedBy = (c, index, param, seen) => {
|
|
15144
|
+
if (seen.has(declKey(c))) return `${c.name}'s classes extend each other in a cycle`;
|
|
15145
|
+
seen.add(declKey(c));
|
|
15146
|
+
for (const n2 of wired().news.get(declKey(c)) ?? []) {
|
|
15147
|
+
const args = n2.args;
|
|
15148
|
+
if (args === "all" || args.length > index || args.slice(0, index + 1).some((a) => ts.isSpreadElement(a)))
|
|
15149
|
+
return `${c.name} is constructed at ${where(n2.at)}, which may pass its own ${param}`;
|
|
15150
|
+
}
|
|
15151
|
+
const handed = wired().handed.get(declKey(c));
|
|
15152
|
+
const given = wired().givenNews.find(
|
|
15153
|
+
(n2) => fileIdOf2(n2.getSourceFile())?.startsWith(`${graph.sourceOf(c.file)}:`)
|
|
15154
|
+
);
|
|
15155
|
+
if (handed && given)
|
|
15156
|
+
return `${c.name} is handed on as a value at ${where(handed)}, and the project constructs classes it is given (${where(given)})`;
|
|
15157
|
+
const mixin = wired().mixins.get(declKey(c));
|
|
15158
|
+
if (mixin) return `${c.name} is extended through a mixin at ${where(mixin)}`;
|
|
15159
|
+
for (const sub of graph.subtypes.get(declKey(c)) ?? []) {
|
|
15160
|
+
if (sub.kind !== "class" || graph.parentOf.get(declKey(sub.decl))?.base?.name !== c.name) continue;
|
|
15161
|
+
const node2 = classNode(sub.decl);
|
|
15162
|
+
if (!node2) return `${sub.decl.name} (${pathOf9(sub.decl.file)}) can't be read`;
|
|
15163
|
+
const ctor = bodyCtor(node2);
|
|
15164
|
+
if (!ctor) {
|
|
15165
|
+
const why = suppliedBy(sub.decl, index, param, seen);
|
|
15166
|
+
if (why) return why;
|
|
15167
|
+
continue;
|
|
15168
|
+
}
|
|
15169
|
+
let sup;
|
|
15170
|
+
const find2 = (node3) => {
|
|
15171
|
+
if (sup) return;
|
|
15172
|
+
if (ts.isCallExpression(node3) && node3.expression.kind === ts.SyntaxKind.SuperKeyword) sup = node3;
|
|
15173
|
+
else ts.forEachChild(node3, find2);
|
|
15174
|
+
};
|
|
15175
|
+
find2(ctor.body);
|
|
15176
|
+
const args = sup?.arguments ?? [];
|
|
15177
|
+
const arg = args[index];
|
|
15178
|
+
if (!sup || args.slice(0, index + 1).some((a) => ts.isSpreadElement(a)))
|
|
15179
|
+
return `${sub.decl.name}'s constructor doesn't pass ${param} on to ${c.name}`;
|
|
15180
|
+
if (!arg) continue;
|
|
15181
|
+
const p = ts.isIdentifier(arg) ? resolved(arg)?.valueDeclaration : void 0;
|
|
15182
|
+
const from = p && ts.isParameter(p) && p.parent === ctor ? fromParameter(p, seen) : null;
|
|
15183
|
+
const own2 = tokenOfParam(c, index);
|
|
15184
|
+
if (!from || "why" in from || !own2 || declKey(from.token) !== declKey(own2))
|
|
15185
|
+
return `${sub.decl.name} passes its own ${param} to ${c.name} (${where(arg)})`;
|
|
15186
|
+
}
|
|
15187
|
+
return null;
|
|
15188
|
+
};
|
|
15189
|
+
const tokenOfParam = (c, index) => {
|
|
15190
|
+
const node2 = classNode(c);
|
|
15191
|
+
const t = node2 && bodyCtor(node2)?.parameters[index]?.type;
|
|
15192
|
+
return t && ts.isTypeReferenceNode(t) ? classOf(t.typeName)?.decl ?? null : null;
|
|
15193
|
+
};
|
|
15194
|
+
const fromParameter = (p, seen = /* @__PURE__ */ new Set()) => {
|
|
15195
|
+
const ctor = p.parent;
|
|
15196
|
+
if (!ts.isConstructorDeclaration(ctor) || !ctor.body) return null;
|
|
15197
|
+
const holder = ctor.parent;
|
|
15198
|
+
if (!ts.isClassDeclaration(holder) || !holder.name) return null;
|
|
15199
|
+
const holderDecl = declOfClass(holder);
|
|
15200
|
+
const deco = diDecorator(holder);
|
|
15201
|
+
if (!holderDecl || !deco || !p.type || !ts.isTypeReferenceNode(p.type) || !ts.isIdentifier(p.name))
|
|
15202
|
+
return null;
|
|
15203
|
+
const token = classOf(p.type.typeName);
|
|
15204
|
+
if (!token) return null;
|
|
15205
|
+
const name = p.name.text;
|
|
15206
|
+
for (const { call, from } of decorators(p)) {
|
|
15207
|
+
if (from && LOOKUP_DECORATORS.has(from.name)) continue;
|
|
15208
|
+
if (from?.name === "Inject" && call) {
|
|
15209
|
+
let arg = call.arguments[0];
|
|
15210
|
+
if (arg && ts.isCallExpression(arg) && importedAs(arg.expression)?.name === "forwardRef") {
|
|
15211
|
+
const fn = arg.arguments[0];
|
|
15212
|
+
arg = fn && ts.isArrowFunction(fn) && !ts.isBlock(fn.body) ? fn.body : void 0;
|
|
15213
|
+
}
|
|
15214
|
+
const named = arg && classOf(arg);
|
|
15215
|
+
if (named && declKey(named.decl) === declKey(token.decl)) continue;
|
|
15216
|
+
return { why: `${name} is injected by the token ${call.arguments[0]?.getText() ?? "(none)"}` };
|
|
15217
|
+
}
|
|
15218
|
+
return { why: `${name} has the decorator ${call?.expression.getText() ?? "(unknown)"}` };
|
|
15219
|
+
}
|
|
15220
|
+
const why = suppliedBy(holderDecl, ctor.parameters.indexOf(p), name, seen);
|
|
15221
|
+
if (why) return { why };
|
|
15222
|
+
return {
|
|
15223
|
+
token: token.decl,
|
|
15224
|
+
detail: `${name} is injected into ${holder.name.text}'s constructor (${deco})`
|
|
15225
|
+
};
|
|
15226
|
+
};
|
|
15227
|
+
const under = (obj) => {
|
|
15228
|
+
while (ts.isParenthesizedExpression(obj) || ts.isNonNullExpression(obj) || ts.isAsExpression(obj) || ts.isTypeAssertionExpression(obj) || ts.isSatisfiesExpression(obj))
|
|
15229
|
+
obj = obj.expression;
|
|
15230
|
+
return obj;
|
|
15231
|
+
};
|
|
15232
|
+
const objectClass = /* @__PURE__ */ new Map();
|
|
15233
|
+
const classOfObject = (obj, at) => {
|
|
15234
|
+
if (obj.kind === ts.SyntaxKind.ThisKeyword) return enclosingClass(at);
|
|
15235
|
+
const hit = objectClass.get(obj);
|
|
15236
|
+
if (hit !== void 0) return hit;
|
|
15237
|
+
const t = checker.getTypeAtLocation(obj).getSymbol();
|
|
15238
|
+
const c = declOfClass(t?.valueDeclaration ?? t?.declarations?.[0]);
|
|
15239
|
+
objectClass.set(obj, c);
|
|
15240
|
+
return c;
|
|
15241
|
+
};
|
|
15242
|
+
const heldLiteral = (obj) => {
|
|
15243
|
+
if (!ts.isIdentifier(obj)) return void 0;
|
|
15244
|
+
const d = resolved(obj)?.valueDeclaration;
|
|
15245
|
+
if (!d || !ts.isVariableDeclaration(d) || !d.initializer) return void 0;
|
|
15246
|
+
let init = d.initializer;
|
|
15247
|
+
while (ts.isParenthesizedExpression(init) || ts.isAsExpression(init) || ts.isSatisfiesExpression(init))
|
|
15248
|
+
init = init.expression;
|
|
15249
|
+
return ts.isObjectLiteralExpression(init) ? init : void 0;
|
|
15250
|
+
};
|
|
15251
|
+
const freshClass = (e) => {
|
|
15252
|
+
const c = classOf(e);
|
|
15253
|
+
if (!c) return false;
|
|
15254
|
+
const seen = /* @__PURE__ */ new Set();
|
|
15255
|
+
for (let at = c.decl; at; at = graph.parentOf.get(declKey(at))?.base) {
|
|
15256
|
+
if (seen.has(declKey(at))) return false;
|
|
15257
|
+
seen.add(declKey(at));
|
|
15258
|
+
const node2 = classNode(at);
|
|
15259
|
+
const parent = graph.parentOf.get(declKey(at));
|
|
15260
|
+
if (!node2 || ts.getDecorators(node2)?.length || parent && !parent.base) return false;
|
|
15261
|
+
const ctor = node2.members.find((m) => ts.isConstructorDeclaration(m) && m.body);
|
|
15262
|
+
const returns = (n2) => n2 !== ctor && ts.isFunctionLike(n2) ? false : ts.isReturnStatement(n2) && !!n2.expression || !!ts.forEachChild(n2, (x) => returns(x) || void 0);
|
|
15263
|
+
if (ctor && returns(ctor)) return false;
|
|
15264
|
+
}
|
|
15265
|
+
return true;
|
|
15266
|
+
};
|
|
15267
|
+
const packageObject = (obj) => {
|
|
15268
|
+
if (!fromPackage(obj)) return false;
|
|
15269
|
+
let root = obj;
|
|
15270
|
+
while (ts.isPropertyAccessExpression(root)) root = root.expression;
|
|
15271
|
+
const decls = resolved(root)?.declarations ?? [];
|
|
15272
|
+
return !decls.some((d) => !!fileIdOf2(d.getSourceFile()));
|
|
15273
|
+
};
|
|
15274
|
+
const freshLocal = (obj) => {
|
|
15275
|
+
let root = obj;
|
|
15276
|
+
while (ts.isPropertyAccessExpression(root) || ts.isElementAccessExpression(root)) root = root.expression;
|
|
15277
|
+
if (!ts.isIdentifier(root) || root !== obj) return false;
|
|
15278
|
+
const d = resolved(root)?.valueDeclaration;
|
|
15279
|
+
if (!d || !ts.isVariableDeclaration(d) || !(ts.getCombinedNodeFlags(d) & ts.NodeFlags.Const) || !d.initializer)
|
|
15280
|
+
return false;
|
|
15281
|
+
let init = d.initializer;
|
|
15282
|
+
while (ts.isParenthesizedExpression(init) || ts.isAsExpression(init) || ts.isSatisfiesExpression(init))
|
|
15283
|
+
init = init.expression;
|
|
15284
|
+
const callee = ts.isCallExpression(init) ? init.expression : void 0;
|
|
15285
|
+
const made = callee ? ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression) ? `${callee.expression.text}.${callee.name.text}` : ts.isIdentifier(callee) ? callee.text : null : null;
|
|
15286
|
+
if (made === "Object.create") {
|
|
15287
|
+
const proto = init.kind === ts.SyntaxKind.CallExpression ? init.arguments[0] : void 0;
|
|
15288
|
+
return !!proto && (proto.kind === ts.SyntaxKind.NullKeyword || ts.isObjectLiteralExpression(proto));
|
|
15289
|
+
}
|
|
15290
|
+
if (made && FRESH_MAKERS.has(made) && !fileIdOf2(resolved(callee)?.valueDeclaration?.getSourceFile() ?? obj.getSourceFile()))
|
|
15291
|
+
return true;
|
|
15292
|
+
if (ts.isObjectLiteralExpression(init))
|
|
15293
|
+
return !init.properties.some(
|
|
15294
|
+
(p) => ts.isPropertyAssignment(p) && nameOf(p.name) === "__proto__" && !ts.isComputedPropertyName(p.name)
|
|
15295
|
+
);
|
|
15296
|
+
return ts.isArrayLiteralExpression(init) || ts.isNewExpression(init) && ts.isIdentifier(init.expression) && (freshClass(init.expression) || ["Map", "Set", "WeakMap", "WeakSet", "Array", "Object"].includes(init.expression.text) && !fileIdOf2(resolved(init.expression)?.valueDeclaration?.getSourceFile() ?? obj.getSourceFile()));
|
|
15297
|
+
};
|
|
15298
|
+
const thisOwner = (node2) => {
|
|
15299
|
+
let at = node2.parent;
|
|
15300
|
+
while (at && !ts.isSourceFile(at)) {
|
|
15301
|
+
if (ts.isFunctionLike(at) && !ts.isArrowFunction(at) || ts.isPropertyDeclaration(at)) break;
|
|
15302
|
+
at = at.parent;
|
|
15303
|
+
}
|
|
15304
|
+
if (!at || ts.isSourceFile(at) || !at.parent || !ts.isClassDeclaration(at.parent)) return null;
|
|
15305
|
+
return ts.isMethodDeclaration(at) || ts.isConstructorDeclaration(at) || ts.isGetAccessorDeclaration(at) || ts.isSetAccessorDeclaration(at) || ts.isPropertyDeclaration(at) ? declOfClass(at.parent) : null;
|
|
15306
|
+
};
|
|
15307
|
+
const instanceTypes = /* @__PURE__ */ new Map();
|
|
15308
|
+
const instanceType = (key) => {
|
|
15309
|
+
if (instanceTypes.has(key)) return instanceTypes.get(key);
|
|
15310
|
+
const [file2, name] = key.split("\0");
|
|
15311
|
+
const node2 = classNode({ file: file2, name });
|
|
15312
|
+
const sym = node2?.name ? checker.getSymbolAtLocation(node2.name) : void 0;
|
|
15313
|
+
const t = sym ? checker.getDeclaredTypeOfSymbol(sym) : null;
|
|
15314
|
+
instanceTypes.set(key, t);
|
|
15315
|
+
return t;
|
|
15316
|
+
};
|
|
15317
|
+
let byText;
|
|
15318
|
+
const references = (name) => {
|
|
15319
|
+
if (!byText) {
|
|
15320
|
+
byText = /* @__PURE__ */ new Map();
|
|
15321
|
+
for (const sf of program.getSourceFiles()) {
|
|
15322
|
+
if (!fileIdOf2(sf)) continue;
|
|
15323
|
+
const visit2 = (n2) => {
|
|
15324
|
+
if (ts.isIdentifier(n2)) {
|
|
15325
|
+
const list = byText.get(n2.text);
|
|
15326
|
+
if (list) list.push(n2);
|
|
15327
|
+
else byText.set(n2.text, [n2]);
|
|
15328
|
+
}
|
|
15329
|
+
ts.forEachChild(n2, visit2);
|
|
15330
|
+
};
|
|
15331
|
+
visit2(sf);
|
|
15332
|
+
}
|
|
15333
|
+
}
|
|
15334
|
+
return byText.get(name) ?? [];
|
|
15335
|
+
};
|
|
15336
|
+
const argsSeen = /* @__PURE__ */ new Map();
|
|
15337
|
+
const argumentsFor = (obj) => {
|
|
15338
|
+
if (!ts.isIdentifier(obj)) return null;
|
|
15339
|
+
const p = resolved(obj)?.valueDeclaration;
|
|
15340
|
+
if (!p || !ts.isParameter(p) || !ts.isIdentifier(p.name) || p.dotDotDotToken) return null;
|
|
15341
|
+
const hit = argsSeen.get(p);
|
|
15342
|
+
if (hit !== void 0) return hit;
|
|
15343
|
+
argsSeen.set(p, null);
|
|
15344
|
+
const fn = p.parent;
|
|
15345
|
+
const holder = ts.isFunctionDeclaration(fn) && fn.name ? fn.name : (ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) && ts.isVariableDeclaration(fn.parent) && fn.parent.initializer === fn && ts.isIdentifier(fn.parent.name) && ts.getCombinedNodeFlags(fn.parent) & ts.NodeFlags.Const ? fn.parent.name : null;
|
|
15346
|
+
if (!ts.isExternalModule(fn.getSourceFile())) return null;
|
|
15347
|
+
const index = fn.parameters.filter((x) => !(ts.isIdentifier(x.name) && x.name.text === "this")).indexOf(p);
|
|
15348
|
+
const sym = holder && resolved(holder);
|
|
15349
|
+
if (!holder || !sym || index < 0) return null;
|
|
15350
|
+
const out = [];
|
|
15351
|
+
for (const id of references(holder.text)) {
|
|
15352
|
+
if (id === holder || resolved(id) !== sym) continue;
|
|
15353
|
+
const parent = id.parent;
|
|
15354
|
+
if (ts.isImportSpecifier(parent) || ts.isExportSpecifier(parent)) {
|
|
15355
|
+
if (parent.propertyName) return null;
|
|
15356
|
+
continue;
|
|
15357
|
+
}
|
|
15358
|
+
if (ts.isImportClause(parent)) return null;
|
|
15359
|
+
if (ts.findAncestor(id, (n2) => ts.isTypeNode(n2))) continue;
|
|
15360
|
+
if (!ts.isCallExpression(parent) || parent.expression !== id) return null;
|
|
15361
|
+
const args = parent.arguments;
|
|
15362
|
+
if (args.slice(0, index + 1).some((a) => ts.isSpreadElement(a))) return null;
|
|
15363
|
+
const arg = args[index];
|
|
15364
|
+
if (arg && !(ts.isIdentifier(arg) && arg.text === "undefined")) out.push(arg);
|
|
15365
|
+
else if (p.initializer) out.push(p.initializer);
|
|
15366
|
+
}
|
|
15367
|
+
let written2 = false;
|
|
15368
|
+
const look = (n2) => {
|
|
15369
|
+
if (written2) return;
|
|
15370
|
+
if (ts.isIdentifier(n2) && n2 !== p.name && resolved(n2) === resolved(p.name) && assigned(n2))
|
|
15371
|
+
written2 = true;
|
|
15372
|
+
else ts.forEachChild(n2, look);
|
|
15373
|
+
};
|
|
15374
|
+
if (fn.body) look(fn.body);
|
|
15375
|
+
if (written2) return null;
|
|
15376
|
+
const exported = ts.getCombinedModifierFlags(
|
|
15377
|
+
fn.kind === ts.SyntaxKind.FunctionDeclaration ? fn : fn.parent.parent.parent
|
|
15378
|
+
) & ts.ModifierFlags.Default;
|
|
15379
|
+
if (exported) return null;
|
|
15380
|
+
argsSeen.set(p, out);
|
|
15381
|
+
return out;
|
|
15382
|
+
};
|
|
15383
|
+
const assigned = (n2) => {
|
|
15384
|
+
let at = n2;
|
|
15385
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isShorthandPropertyAssignment(at.parent) || ts.isPropertyAssignment(at.parent) && at.parent.initializer === at || ts.isObjectLiteralExpression(at.parent) || ts.isArrayLiteralExpression(at.parent) || ts.isSpreadElement(at.parent) || ts.isSpreadAssignment(at.parent))
|
|
15386
|
+
at = at.parent;
|
|
15387
|
+
const parent = at.parent;
|
|
15388
|
+
if (ts.isBinaryExpression(parent))
|
|
15389
|
+
return parent.left === at && parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
|
|
15390
|
+
if (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent))
|
|
15391
|
+
return parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken;
|
|
15392
|
+
return (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) && parent.initializer === at;
|
|
15393
|
+
};
|
|
15394
|
+
const infos = /* @__PURE__ */ new WeakMap();
|
|
15395
|
+
const writeInfo = (x) => {
|
|
15396
|
+
const hit = infos.get(x);
|
|
15397
|
+
if (hit) return hit;
|
|
15398
|
+
const obj = under(x.obj);
|
|
15399
|
+
let type;
|
|
15400
|
+
const info = obj.kind === ts.SyntaxKind.ThisKeyword ? {
|
|
15401
|
+
obj,
|
|
15402
|
+
thisOwner: thisOwner(obj),
|
|
15403
|
+
fresh: false,
|
|
15404
|
+
cls: null,
|
|
15405
|
+
target: () => checker.getTypeAtLocation(obj)
|
|
15406
|
+
} : {
|
|
15407
|
+
obj,
|
|
15408
|
+
fresh: freshLocal(obj),
|
|
15409
|
+
holds: heldLiteral(obj),
|
|
15410
|
+
cls: classOfObject(obj, x.at),
|
|
15411
|
+
target: () => type ??= checker.getTypeAtLocation(obj)
|
|
15412
|
+
};
|
|
15413
|
+
infos.set(x, info);
|
|
15414
|
+
return info;
|
|
15415
|
+
};
|
|
15416
|
+
const fitsSeen = /* @__PURE__ */ new Map();
|
|
15417
|
+
const assignable = (from, to) => {
|
|
15418
|
+
let row = fitsSeen.get(to);
|
|
15419
|
+
if (!row) fitsSeen.set(to, row = /* @__PURE__ */ new Map());
|
|
15420
|
+
let hit = row.get(from);
|
|
15421
|
+
if (hit === void 0) row.set(from, hit = checker.isTypeAssignableTo(from, to));
|
|
15422
|
+
return hit;
|
|
15423
|
+
};
|
|
15424
|
+
const mayBeIn = (x, chain, types, literal2, depth = 0) => {
|
|
15425
|
+
const info = writeInfo(x);
|
|
15426
|
+
if (literal2 && info.holds === literal2) return true;
|
|
15427
|
+
if (info.thisOwner !== void 0) return !info.thisOwner || chain.has(declKey(info.thisOwner));
|
|
15428
|
+
if (info.cls && chain.has(declKey(info.cls))) return true;
|
|
15429
|
+
if (info.fresh) return false;
|
|
15430
|
+
const obj = info.obj;
|
|
15431
|
+
if (!x.computed && !x.whole && !x.borrow) return true;
|
|
15432
|
+
if (x.borrow && packageObject(obj)) return false;
|
|
15433
|
+
const target2 = info.target();
|
|
15434
|
+
const subjects = types ?? [...chain].map(instanceType).filter((t) => !!t);
|
|
15435
|
+
const fits = () => subjects.some((t) => t === target2 || assignable(t, target2));
|
|
15436
|
+
const loose = !!(target2.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter | ts.TypeFlags.NonPrimitive));
|
|
15437
|
+
if (loose && (x.computed || x.whole) && depth < 3) {
|
|
15438
|
+
const passed = argumentsFor(obj);
|
|
15439
|
+
if (passed)
|
|
15440
|
+
return passed.some(
|
|
15441
|
+
(a) => mayBeIn(
|
|
15442
|
+
{ at: a, obj: a, ...x.computed ? { computed: true } : { whole: true } },
|
|
15443
|
+
chain,
|
|
15444
|
+
types,
|
|
15445
|
+
literal2,
|
|
15446
|
+
depth + 1
|
|
15447
|
+
)
|
|
15448
|
+
);
|
|
15449
|
+
}
|
|
15450
|
+
if (x.computed) {
|
|
15451
|
+
if (subjects.some((t) => t === target2)) return true;
|
|
15452
|
+
if (target2.flags & ts.TypeFlags.Any || checker.getIndexInfosOfType(target2).length > 0) return false;
|
|
15453
|
+
if (target2.flags & (ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter | ts.TypeFlags.NonPrimitive))
|
|
15454
|
+
return true;
|
|
15455
|
+
return target2.getProperties().length > 0 && fits();
|
|
15456
|
+
}
|
|
15457
|
+
if (target2.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter)) return true;
|
|
15458
|
+
return fits();
|
|
15459
|
+
};
|
|
15460
|
+
const writeTo = (name, chain, options2 = {}) => {
|
|
15461
|
+
const w = wired();
|
|
15462
|
+
const files = options2.file ? [options2.file] : filesOfChain(chain);
|
|
15463
|
+
const ok = (x) => (options2.tests || !x.test) && files.some((f) => together(x.file, f)) && !options2.except?.(x.at) && mayBeIn(x, chain, options2.types, options2.literal);
|
|
15464
|
+
if (name !== null) {
|
|
15465
|
+
const byName = (w.writes.get(name) ?? []).find(ok);
|
|
15466
|
+
if (byName) return byName.at;
|
|
15467
|
+
}
|
|
15468
|
+
if (options2.except) return w.wholesale.find(ok)?.at ?? null;
|
|
15469
|
+
if (options2.literal) return w.wholesale.find(ok)?.at ?? null;
|
|
15470
|
+
const key = `${options2.tests}\0${options2.file ?? ""}\0${[...chain].sort().join("\0")}`;
|
|
15471
|
+
if (wholeSeen.has(key)) return wholeSeen.get(key);
|
|
15472
|
+
const found = w.wholesale.find(ok)?.at ?? null;
|
|
15473
|
+
wholeSeen.set(key, found);
|
|
15474
|
+
return found;
|
|
15475
|
+
};
|
|
15476
|
+
const wholeSeen = /* @__PURE__ */ new Map();
|
|
15477
|
+
const borrowedSeen = /* @__PURE__ */ new Map();
|
|
15478
|
+
const borrowedAt = (name, chain, tests = false) => {
|
|
15479
|
+
const key = `${tests}\0${name}\0${[...chain].sort().join("\0")}`;
|
|
15480
|
+
if (borrowedSeen.has(key)) return borrowedSeen.get(key);
|
|
15481
|
+
const files = filesOfChain(chain);
|
|
15482
|
+
const found = [...wired().borrowed.get(name) ?? [], ...wired().borrowed.get("") ?? []].find(
|
|
15483
|
+
(x) => (tests || !x.test) && files.some((f) => together(x.file, f)) && mayBeIn(x, chain)
|
|
15484
|
+
)?.at ?? null;
|
|
15485
|
+
borrowedSeen.set(key, found);
|
|
15486
|
+
return found;
|
|
15487
|
+
};
|
|
15488
|
+
const filesOfChain = (chain) => [
|
|
15489
|
+
...new Set([...chain].map((k) => k.split("\0")[0]))
|
|
15490
|
+
];
|
|
15491
|
+
const thisBorrowed = (thisNode, tests = false) => {
|
|
15492
|
+
let at = thisNode.parent;
|
|
15493
|
+
while (at && !ts.isSourceFile(at) && !(ts.isFunctionLike(at) && !ts.isArrowFunction(at))) at = at.parent;
|
|
15494
|
+
if (!at || !(ts.isMethodDeclaration(at) || ts.isGetAccessorDeclaration(at) || ts.isSetAccessorDeclaration(at)) || !ts.isClassDeclaration(at.parent))
|
|
15495
|
+
return null;
|
|
15496
|
+
const name = nameOf(at.name);
|
|
15497
|
+
const owner = declOfClass(at.parent);
|
|
15498
|
+
if (!name || !owner) return null;
|
|
15499
|
+
const chain = new Set([owner, ...subclassesOf(owner)].map(declKey));
|
|
15500
|
+
for (let up = graph.parentOf.get(declKey(owner))?.base; up && !chain.has(declKey(up)); up = graph.parentOf.get(declKey(up))?.base)
|
|
15501
|
+
chain.add(declKey(up));
|
|
15502
|
+
return borrowedAt(name, chain, tests);
|
|
15503
|
+
};
|
|
15504
|
+
const fromField = (sym, tests = false) => {
|
|
15505
|
+
const decls = sym.declarations ?? [];
|
|
15506
|
+
if (decls.length !== 1) return null;
|
|
15507
|
+
const d = decls[0];
|
|
15508
|
+
const name = sym.name;
|
|
15509
|
+
const holder = ts.isParameter(d) ? d.parent.parent : d.parent;
|
|
15510
|
+
if (!holder || !ts.isClassDeclaration(holder) || !diDecorator(holder)) return null;
|
|
15511
|
+
const holderDecl = declOfClass(holder);
|
|
15512
|
+
if (!holderDecl) return null;
|
|
15513
|
+
let source = null;
|
|
15514
|
+
const fromCtor = /* @__PURE__ */ new Set();
|
|
15515
|
+
if (ts.isParameter(d)) source = fromParameter(d);
|
|
15516
|
+
else if (ts.isPropertyDeclaration(d)) {
|
|
15517
|
+
if (d.initializer) {
|
|
15518
|
+
const init = d.initializer;
|
|
15519
|
+
const from = ts.isCallExpression(init) ? importedAs(init.expression) : null;
|
|
15520
|
+
const arg = ts.isCallExpression(init) ? init.arguments[0] : void 0;
|
|
15521
|
+
const token = from?.pkg === "@angular/core" && from.name === "inject" && arg ? classOf(arg) : null;
|
|
15522
|
+
if (!token) return null;
|
|
15523
|
+
source = {
|
|
15524
|
+
token: token.decl,
|
|
15525
|
+
detail: `${name} is inject(${token.decl.name}) in ${holder.name?.text} (${diDecorator(holder)})`
|
|
15526
|
+
};
|
|
15527
|
+
} else {
|
|
15528
|
+
const ctor = bodyCtor(holder);
|
|
15529
|
+
for (const st of ctor?.body?.statements ?? []) {
|
|
15530
|
+
const e = ts.isExpressionStatement(st) ? st.expression : void 0;
|
|
15531
|
+
if (!e || !ts.isBinaryExpression(e) || e.operatorToken.kind !== ts.SyntaxKind.EqualsToken || !ts.isPropertyAccessExpression(e.left) || e.left.expression.kind !== ts.SyntaxKind.ThisKeyword || e.left.name.text !== name || !ts.isIdentifier(e.right))
|
|
15532
|
+
continue;
|
|
15533
|
+
const p = resolved(e.right)?.valueDeclaration;
|
|
15534
|
+
if (!p || !ts.isParameter(p) || p.parent !== ctor) return null;
|
|
15535
|
+
const from = fromParameter(p);
|
|
15536
|
+
if (!from) return null;
|
|
15537
|
+
if ("why" in from) return from;
|
|
15538
|
+
if (source && "token" in source && declKey(source.token) !== declKey(from.token))
|
|
15539
|
+
return { why: `${name} is set from parameters of different classes` };
|
|
15540
|
+
fromCtor.add(e.left);
|
|
15541
|
+
source = {
|
|
15542
|
+
token: from.token,
|
|
15543
|
+
detail: `${name} is set from the parameter ${p.name.getText()}, which ${from.detail.replace(/^\S+ is /, "is ")}`
|
|
15544
|
+
};
|
|
15545
|
+
}
|
|
15546
|
+
}
|
|
15547
|
+
}
|
|
15548
|
+
if (!source || "why" in source) return source;
|
|
15549
|
+
const chain = new Set([holderDecl, ...subclassesOf(holderDecl)].map(declKey));
|
|
15550
|
+
for (let up = graph.parentOf.get(declKey(holderDecl))?.base; up; up = graph.parentOf.get(declKey(up))?.base) {
|
|
15551
|
+
if (chain.has(declKey(up))) break;
|
|
15552
|
+
chain.add(declKey(up));
|
|
15553
|
+
}
|
|
15554
|
+
const other = (wired().writes.get(name) ?? []).find(
|
|
15555
|
+
(x) => (tests || !x.test) && !fromCtor.has(x.at) && mayBeIn(x, chain)
|
|
15556
|
+
);
|
|
15557
|
+
if (other) return { why: `${name} is also assigned at ${where(other.at)}, so it may hold another value` };
|
|
15558
|
+
const whole = writeTo(null, chain, { tests });
|
|
15559
|
+
if (whole)
|
|
15560
|
+
return { why: `${name} may be written at ${where(whole)}, which writes properties it doesn't name` };
|
|
15561
|
+
for (const c of [holderDecl, ...subclassesOf(holderDecl)]) {
|
|
15562
|
+
const node2 = classNode(c);
|
|
15563
|
+
const again = c !== holderDecl && node2?.members.some(
|
|
15564
|
+
(m) => nameOf(m.name) === name || ts.isConstructorDeclaration(m) && m.parameters.some(
|
|
15565
|
+
(x) => ts.isIdentifier(x.name) && x.name.text === name && ts.getModifiers(x)?.length
|
|
15566
|
+
)
|
|
15567
|
+
);
|
|
15568
|
+
if (again) return { why: `${c.name} declares ${name} again` };
|
|
15569
|
+
}
|
|
15570
|
+
return source;
|
|
15571
|
+
};
|
|
15572
|
+
const providedFor = (token) => {
|
|
15573
|
+
const node2 = classNode(token);
|
|
15574
|
+
const regs = (wired().provides.get(declKey(token)) ?? []).filter(
|
|
15575
|
+
(r) => !(r.kind === "useClass" && r.target && declKey(r.target) === declKey(token))
|
|
15576
|
+
);
|
|
15577
|
+
const unreadable = wired().unreadable[0];
|
|
15578
|
+
if (unreadable)
|
|
15579
|
+
return {
|
|
15580
|
+
why: `a provider at ${where(unreadable)} registers a token that can't be told, so ${token.name} may be provided otherwise`
|
|
15581
|
+
};
|
|
15582
|
+
if (regs.length === 0) {
|
|
15583
|
+
if (node2 && isAbstract(node2))
|
|
15584
|
+
return { why: `${token.name} is abstract, and nothing provides a class for it` };
|
|
15585
|
+
if (!wired().listed.has(declKey(token)))
|
|
15586
|
+
return {
|
|
15587
|
+
why: `nothing in the project lists ${token.name} as a provider, so how it is provided can't be told`
|
|
15588
|
+
};
|
|
15589
|
+
return { cls: token, detail: `${token.name} is provided as itself` };
|
|
15590
|
+
}
|
|
15591
|
+
const first = regs[0];
|
|
15592
|
+
const targets = new Set(
|
|
15593
|
+
regs.map((r) => r.kind === "useClass" && r.target ? declKey(r.target) : r.kind)
|
|
15594
|
+
);
|
|
15595
|
+
if (first.kind !== "useClass" || !first.target || targets.size > 1)
|
|
15596
|
+
return { why: `${token.name} is provided with ${first.kind} at ${where(first.at)}` };
|
|
15597
|
+
if (!node2 || !isAbstract(node2))
|
|
15598
|
+
return {
|
|
15599
|
+
why: `${token.name} is provided as ${first.target.name} at ${where(first.at)}, and may be itself elsewhere`
|
|
15600
|
+
};
|
|
15601
|
+
return {
|
|
15602
|
+
cls: first.target,
|
|
15603
|
+
detail: `${token.name} is provided as ${first.target.name} (${where(first.at)})`
|
|
15604
|
+
};
|
|
15605
|
+
};
|
|
15606
|
+
return {
|
|
15607
|
+
wired,
|
|
15608
|
+
fromField,
|
|
15609
|
+
providedFor,
|
|
15610
|
+
subclassesOf,
|
|
15611
|
+
classOfObject,
|
|
15612
|
+
under,
|
|
15613
|
+
mayBeIn,
|
|
15614
|
+
writeTo,
|
|
15615
|
+
thisBorrowed,
|
|
15616
|
+
diDecorator,
|
|
15617
|
+
onlyDiDecorators,
|
|
15618
|
+
classNode,
|
|
15619
|
+
where
|
|
15620
|
+
};
|
|
15621
|
+
}
|
|
15622
|
+
function injectedResolutions(input2, graph, s, inj = makeInjection(graph, s)) {
|
|
15623
|
+
const out = { resolutions: [], edges: [] };
|
|
15624
|
+
if (!inj || !s?.checker) return out;
|
|
15625
|
+
const { ts, checker, toUtf16 } = s;
|
|
15626
|
+
const { fromField, providedFor } = inj;
|
|
15627
|
+
const open3 = input2.resolutions.filter(
|
|
15628
|
+
(r) => r.status === "unresolved" && r.importOutcome === null && (!r.candidates || r.exhaustive === false)
|
|
15629
|
+
);
|
|
15630
|
+
if (open3.length === 0) return out;
|
|
15631
|
+
const sites = new Map(input2.sites.map((x) => [x.id, x]));
|
|
15632
|
+
for (const r of open3) {
|
|
15633
|
+
const site = sites.get(r.site);
|
|
15634
|
+
if (!site || site.reason !== "dynamic-dispatch") continue;
|
|
15635
|
+
const sf = s.sourceFile(site.file);
|
|
15636
|
+
if (!sf) continue;
|
|
15637
|
+
const call = callAt(ts, sf, toUtf16(sf, site.evidence.span.start.offset), site.expression);
|
|
15638
|
+
if (!call || !ts.isPropertyAccessExpression(call.expression)) continue;
|
|
15639
|
+
const member = call.expression.name.text;
|
|
15640
|
+
let subject = call.expression.expression;
|
|
15641
|
+
while (ts.isParenthesizedExpression(subject) || ts.isNonNullExpression(subject))
|
|
15642
|
+
subject = subject.expression;
|
|
15643
|
+
if (!ts.isPropertyAccessExpression(subject) || subject.expression.kind !== ts.SyntaxKind.ThisKeyword)
|
|
15644
|
+
continue;
|
|
15645
|
+
const sym = checker.getSymbolAtLocation(subject.name);
|
|
15646
|
+
const tests = isTestPath(site.file.slice(site.file.indexOf(":") + 1));
|
|
15647
|
+
const source = sym ? fromField(sym, tests) : null;
|
|
15648
|
+
if (!source) continue;
|
|
15649
|
+
const borrowed = "why" in source ? null : inj.thisBorrowed(subject.expression, tests);
|
|
15650
|
+
if (borrowed) {
|
|
15651
|
+
if (r.candidates?.length)
|
|
15652
|
+
out.resolutions.push({
|
|
15653
|
+
...r,
|
|
15654
|
+
steps: [
|
|
15655
|
+
...r.steps,
|
|
15656
|
+
{
|
|
15657
|
+
kind: "borrowed",
|
|
15658
|
+
detail: `the method is used at ${inj.where(borrowed)} without being called, so it may run with another this`
|
|
15659
|
+
}
|
|
15660
|
+
]
|
|
15661
|
+
});
|
|
15662
|
+
continue;
|
|
15663
|
+
}
|
|
15664
|
+
const provided = "why" in source ? source : providedFor(source.token);
|
|
15665
|
+
const body = "cls" in provided ? graph.bodyOf(provided.cls, member) : null;
|
|
15666
|
+
if ("why" in provided || !body || typeof body === "string") {
|
|
15667
|
+
if ("why" in provided && r.candidates?.length)
|
|
15668
|
+
out.resolutions.push({ ...r, steps: [...r.steps, { kind: "injection", detail: provided.why }] });
|
|
15669
|
+
continue;
|
|
15670
|
+
}
|
|
15671
|
+
if (body.fn.file === site.file) continue;
|
|
15672
|
+
const steps = [
|
|
15673
|
+
{ kind: "injection", detail: source.detail },
|
|
15674
|
+
{
|
|
15675
|
+
kind: "provided",
|
|
15676
|
+
detail: `${provided.detail}: a ${provided.cls.name} runs ${body.definer.name}.${member}`
|
|
15677
|
+
}
|
|
15678
|
+
];
|
|
15679
|
+
out.resolutions.push({
|
|
15680
|
+
type: "SiteResolution",
|
|
15681
|
+
id: r.id,
|
|
15682
|
+
site: r.site,
|
|
15683
|
+
file: r.file,
|
|
15684
|
+
status: "linked",
|
|
15685
|
+
target: body.fn.id,
|
|
15686
|
+
externalPackage: null,
|
|
15687
|
+
via: "injection",
|
|
15688
|
+
importOutcome: null,
|
|
15689
|
+
steps,
|
|
15690
|
+
evidence: r.evidence
|
|
15691
|
+
});
|
|
15692
|
+
out.edges.push({
|
|
15693
|
+
type: "CALLS_CROSS_FILE",
|
|
15694
|
+
id: ids.linkedCall(site.id),
|
|
15695
|
+
from: site.enclosingFn,
|
|
15696
|
+
to: body.fn.id,
|
|
15697
|
+
form: "plain",
|
|
15698
|
+
resolution: "static-exact",
|
|
15699
|
+
confidence: 1,
|
|
15700
|
+
evidence: site.evidence,
|
|
15701
|
+
via: "injection"
|
|
15702
|
+
});
|
|
15703
|
+
}
|
|
15704
|
+
return out;
|
|
15705
|
+
}
|
|
15706
|
+
function callAt(ts, sf, pos, expression) {
|
|
15707
|
+
let found;
|
|
15708
|
+
const visit2 = (node2) => {
|
|
15709
|
+
if (found || node2.end <= pos || node2.getStart(sf) > pos) return;
|
|
15710
|
+
if (ts.isCallExpression(node2) && node2.getStart(sf) === pos && node2.expression.getText(sf).replace(/\s+/g, "") === expression.replace(/\s+/g, "")) {
|
|
15711
|
+
found = node2;
|
|
15712
|
+
return;
|
|
15713
|
+
}
|
|
15714
|
+
ts.forEachChild(node2, visit2);
|
|
15715
|
+
};
|
|
15716
|
+
visit2(sf);
|
|
15717
|
+
return found;
|
|
15718
|
+
}
|
|
15719
|
+
var LIBRARY_WRITERS, FRESH_MAKERS, DI_DECORATORS, LOOKUP_DECORATORS, PROVIDER_KINDS;
|
|
15720
|
+
var init_di = __esm({
|
|
15721
|
+
"packages/stage2/src/di.ts"() {
|
|
15722
|
+
"use strict";
|
|
15723
|
+
init_src();
|
|
15724
|
+
init_typegraph();
|
|
15725
|
+
LIBRARY_WRITERS = /* @__PURE__ */ new Set([
|
|
15726
|
+
"assign",
|
|
15727
|
+
"assignIn",
|
|
15728
|
+
"assignWith",
|
|
15729
|
+
"extend",
|
|
15730
|
+
"merge",
|
|
15731
|
+
"mergeWith",
|
|
15732
|
+
"defaults",
|
|
15733
|
+
"defaultsDeep",
|
|
15734
|
+
"set",
|
|
15735
|
+
"setWith",
|
|
15736
|
+
"update",
|
|
15737
|
+
"updateWith",
|
|
15738
|
+
"unset"
|
|
15739
|
+
]);
|
|
15740
|
+
FRESH_MAKERS = /* @__PURE__ */ new Set([
|
|
15741
|
+
"Object.fromEntries",
|
|
15742
|
+
"Object.create",
|
|
15743
|
+
"JSON.parse",
|
|
15744
|
+
"structuredClone",
|
|
15745
|
+
"Array.from",
|
|
15746
|
+
"Array.of"
|
|
15747
|
+
]);
|
|
15748
|
+
DI_DECORATORS = {
|
|
15749
|
+
"@nestjs/common": ["Injectable", "Controller", "Module", "Catch"],
|
|
15750
|
+
"@nestjs/graphql": ["Resolver"],
|
|
15751
|
+
"@nestjs/websockets": ["WebSocketGateway"],
|
|
15752
|
+
"@nestjs/bull": ["Processor"],
|
|
15753
|
+
"@nestjs/bullmq": ["Processor"],
|
|
15754
|
+
"@angular/core": ["Injectable", "Component", "Directive", "Pipe", "NgModule"]
|
|
15755
|
+
};
|
|
15756
|
+
LOOKUP_DECORATORS = /* @__PURE__ */ new Set(["Optional", "Self", "SkipSelf", "Host"]);
|
|
15757
|
+
PROVIDER_KINDS = ["useClass", "useValue", "useFactory", "useExisting"];
|
|
15758
|
+
}
|
|
15759
|
+
});
|
|
15760
|
+
|
|
15761
|
+
// packages/stage2/src/callers.ts
|
|
15762
|
+
function posixJoin(dir, rel) {
|
|
15763
|
+
const at = dir.indexOf(":");
|
|
15764
|
+
const parts = dir.slice(at + 1).split("/").filter(Boolean);
|
|
15765
|
+
for (const r of rel.split("/")) {
|
|
15766
|
+
if (r === "..") parts.pop();
|
|
15767
|
+
else if (r !== "." && r !== "") parts.push(r);
|
|
15768
|
+
}
|
|
15769
|
+
return `${dir.slice(0, at + 1)}${parts.length ? `${parts.join("/")}/` : ""}`;
|
|
15770
|
+
}
|
|
15771
|
+
function makeCallers(s, options2) {
|
|
15772
|
+
const { ts, fileIdOf: fileIdOf2 } = s;
|
|
15773
|
+
const checker = s.checker;
|
|
15774
|
+
const pathOf9 = (file2) => file2.slice(file2.indexOf(":") + 1);
|
|
15775
|
+
const files = () => (s.program?.getSourceFiles() ?? []).filter((sf) => {
|
|
15776
|
+
const f = fileIdOf2(sf);
|
|
15777
|
+
return !!f && !isTestPath(pathOf9(f));
|
|
15778
|
+
});
|
|
15779
|
+
let byText;
|
|
15780
|
+
let specifiers;
|
|
15781
|
+
const index = () => {
|
|
15782
|
+
if (byText) return;
|
|
15783
|
+
byText = /* @__PURE__ */ new Map();
|
|
15784
|
+
specifiers = [];
|
|
15785
|
+
for (const sf of files()) {
|
|
15786
|
+
const visit2 = (node2) => {
|
|
15787
|
+
if (ts.isIdentifier(node2)) {
|
|
15788
|
+
const list = byText.get(node2.text);
|
|
15789
|
+
if (list) list.push(node2);
|
|
15790
|
+
else byText.set(node2.text, [node2]);
|
|
15791
|
+
}
|
|
15792
|
+
if (ts.isImportSpecifier(node2) || ts.isExportSpecifier(node2) || ts.isImportClause(node2) && node2.name || ts.isNamespaceImport(node2) || ts.isImportEqualsDeclaration(node2))
|
|
15793
|
+
specifiers.push(node2);
|
|
15794
|
+
ts.forEachChild(node2, visit2);
|
|
15795
|
+
};
|
|
15796
|
+
visit2(sf);
|
|
15797
|
+
}
|
|
15798
|
+
};
|
|
15799
|
+
let aliases;
|
|
15800
|
+
const aliasNames = () => {
|
|
15801
|
+
if (aliases) return aliases;
|
|
15802
|
+
index();
|
|
15803
|
+
aliases = /* @__PURE__ */ new Map();
|
|
15804
|
+
for (const sp of specifiers) {
|
|
15805
|
+
const local = ts.isImportClause(sp) ? sp.name : ts.isNamespaceImport(sp) || ts.isImportEqualsDeclaration(sp) ? void 0 : sp.name;
|
|
15806
|
+
const sym = local && resolve9(checker.getSymbolAtLocation(local));
|
|
15807
|
+
if (!local || !sym) continue;
|
|
15808
|
+
const list = aliases.get(sym);
|
|
15809
|
+
if (list) list.push(local.text);
|
|
15810
|
+
else aliases.set(sym, [local.text]);
|
|
15811
|
+
}
|
|
15812
|
+
return aliases;
|
|
15813
|
+
};
|
|
15814
|
+
const named = /* @__PURE__ */ new Map();
|
|
15815
|
+
const byName = (name) => {
|
|
15816
|
+
const hit = named.get(name);
|
|
15817
|
+
if (hit) return hit;
|
|
15818
|
+
index();
|
|
15819
|
+
const out = /* @__PURE__ */ new Map();
|
|
15820
|
+
for (const id of byText.get(name) ?? []) {
|
|
15821
|
+
const sym = ts.isShorthandPropertyAssignment(id.parent) ? checker.getShorthandAssignmentValueSymbol(id.parent) : resolve9(checker.getSymbolAtLocation(id));
|
|
15822
|
+
const list = out.get(sym);
|
|
15823
|
+
if (list) list.push(id);
|
|
15824
|
+
else out.set(sym, [id]);
|
|
15825
|
+
}
|
|
15826
|
+
named.set(name, out);
|
|
15827
|
+
return out;
|
|
15828
|
+
};
|
|
15829
|
+
const resolve9 = (sym) => sym && sym.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(sym) : sym;
|
|
15830
|
+
const symbolOfCallee = (c) => c.name ? checker.getSymbolAtLocation(c.name) : c.symbol;
|
|
15831
|
+
const exported = (c) => {
|
|
15832
|
+
if (!ts.isExternalModule(c.getSourceFile()) && ts.isSourceFile(ts.isVariableDeclaration(c) ? c.parent.parent.parent : c.parent))
|
|
15833
|
+
return true;
|
|
15834
|
+
const statement = ts.isVariableDeclaration(c) ? c.parent.parent : c;
|
|
15835
|
+
if (ts.canHaveModifiers(statement) && ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword))
|
|
15836
|
+
return true;
|
|
15837
|
+
const name = c.name && ts.isIdentifier(c.name) ? c.name.text : null;
|
|
15838
|
+
return c.getSourceFile().statements.some(
|
|
15839
|
+
(st) => ts.isExportDeclaration(st) && !st.moduleSpecifier && !!st.exportClause && ts.isNamedExports(st.exportClause) && st.exportClause.elements.some((el) => (el.propertyName ?? el.name).text === name) || ts.isExportAssignment(st) && ts.isIdentifier(st.expression) && st.expression.text === name
|
|
15840
|
+
);
|
|
15841
|
+
};
|
|
15842
|
+
let indirect;
|
|
15843
|
+
let moduleObjects;
|
|
15844
|
+
const scanIndirect = () => {
|
|
15845
|
+
if (indirect) return;
|
|
15846
|
+
indirect = [];
|
|
15847
|
+
moduleObjects = [];
|
|
15848
|
+
for (const sf of files()) {
|
|
15849
|
+
const visit2 = (n2) => {
|
|
15850
|
+
if (ts.isNewExpression(n2)) {
|
|
15851
|
+
let e = n2.expression;
|
|
15852
|
+
while (ts.isParenthesizedExpression(e) || ts.isAsExpression(e) || ts.isNonNullExpression(e))
|
|
15853
|
+
e = e.expression;
|
|
15854
|
+
const sym = ts.isIdentifier(e) || ts.isPropertyAccessExpression(e) ? resolve9(checker.getSymbolAtLocation(e)) : void 0;
|
|
15855
|
+
const named2 = !!sym && !!(sym.flags & ts.SymbolFlags.Class);
|
|
15856
|
+
if (!named2) indirect.push({ at: n2, type: checker.getTypeAtLocation(n2) });
|
|
15857
|
+
}
|
|
15858
|
+
if (ts.isCallExpression(n2) && ts.isPropertyAccessExpression(n2.expression) && n2.expression.name.text === "construct" && ts.isIdentifier(n2.expression.expression) && n2.expression.expression.text === "Reflect" && n2.arguments[0]) {
|
|
15859
|
+
const made = checker.getTypeAtLocation(n2.arguments[0]).getConstructSignatures()[0]?.getReturnType();
|
|
15860
|
+
indirect.push({ at: n2, type: made ?? checker.getAnyType() });
|
|
15861
|
+
}
|
|
15862
|
+
if (ts.isExportDeclaration(n2) && n2.exportClause && ts.isNamespaceExport(n2.exportClause) && n2.moduleSpecifier) {
|
|
15863
|
+
const mod = checker.getSymbolAtLocation(n2.moduleSpecifier);
|
|
15864
|
+
moduleObjects.push({ at: n2, module: mod, plain: false });
|
|
15865
|
+
}
|
|
15866
|
+
if (ts.isNamespaceImport(n2)) {
|
|
15867
|
+
const mod = resolve9(checker.getSymbolAtLocation(n2.name));
|
|
15868
|
+
if (mod) {
|
|
15869
|
+
const sym = checker.getSymbolAtLocation(n2.name);
|
|
15870
|
+
let plain2 = true;
|
|
15871
|
+
for (const id of byName(n2.name.text).get(resolve9(sym)) ?? byName(n2.name.text).get(sym) ?? [])
|
|
15872
|
+
if (id !== n2.name && !(ts.isPropertyAccessExpression(id.parent) && id.parent.expression === id) && !ts.isQualifiedName(id.parent) && !ts.findAncestor(id, ts.isTypeNode) && // Another import of the module names it; it isn't a use.
|
|
15873
|
+
!ts.isImportClause(id.parent) && !ts.isNamespaceImport(id.parent) && !ts.isImportSpecifier(id.parent) && !ts.isExportSpecifier(id.parent) && !ts.isImportEqualsDeclaration(id.parent))
|
|
15874
|
+
plain2 = false;
|
|
15875
|
+
moduleObjects.push({ at: n2, module: mod, plain: plain2 });
|
|
15876
|
+
}
|
|
15877
|
+
}
|
|
15878
|
+
if (ts.isCallExpression(n2) && n2.expression.kind === ts.SyntaxKind.ImportKeyword) {
|
|
15879
|
+
const arg = n2.arguments[0];
|
|
15880
|
+
const mod = arg && ts.isStringLiteralLike(arg) ? checker.getSymbolAtLocation(arg) : void 0;
|
|
15881
|
+
let up = n2.parent;
|
|
15882
|
+
let inner = n2;
|
|
15883
|
+
while (ts.isAwaitExpression(up) || ts.isParenthesizedExpression(up)) {
|
|
15884
|
+
inner = up;
|
|
15885
|
+
up = up.parent;
|
|
15886
|
+
}
|
|
15887
|
+
const member = ts.isPropertyAccessExpression(up) && up.expression === inner && !!mod && checker.getExportsOfModule(mod).some((x) => x.name === up.name.text);
|
|
15888
|
+
const plain2 = ts.isVariableDeclaration(up) && ts.isObjectBindingPattern(up.name) && !up.name.elements.some(
|
|
15889
|
+
(e) => e.dotDotDotToken || e.propertyName && ts.isComputedPropertyName(e.propertyName)
|
|
15890
|
+
);
|
|
15891
|
+
let folder;
|
|
15892
|
+
if (!mod && arg && !ts.isStringLiteralLike(arg)) {
|
|
15893
|
+
let left = arg;
|
|
15894
|
+
while (ts.isBinaryExpression(left) && left.operatorToken.kind === ts.SyntaxKind.PlusToken)
|
|
15895
|
+
left = left.left;
|
|
15896
|
+
const head = ts.isTemplateExpression(left) ? left.head.text : ts.isStringLiteralLike(left) && left !== arg ? left.text : "";
|
|
15897
|
+
const from = fileIdOf2(sf);
|
|
15898
|
+
const dir = from.slice(0, from.lastIndexOf("/") + 1);
|
|
15899
|
+
folder = head === "" ? null : head.startsWith(".") ? posixJoin(dir, head.slice(0, head.lastIndexOf("/") + 1)) : void 0;
|
|
15900
|
+
}
|
|
15901
|
+
moduleObjects.push({
|
|
15902
|
+
at: n2,
|
|
15903
|
+
module: mod,
|
|
15904
|
+
plain: (plain2 || member) && (!!mod || !!arg && ts.isStringLiteralLike(arg)),
|
|
15905
|
+
...folder !== void 0 ? { folder } : {}
|
|
15906
|
+
});
|
|
15907
|
+
}
|
|
15908
|
+
ts.forEachChild(n2, visit2);
|
|
15909
|
+
};
|
|
15910
|
+
visit2(sf);
|
|
15911
|
+
}
|
|
15912
|
+
};
|
|
15913
|
+
const madeIndirectly = (c) => {
|
|
15914
|
+
scanIndirect();
|
|
15915
|
+
const sym = c.name ? checker.getSymbolAtLocation(c.name) : void 0;
|
|
15916
|
+
const own2 = sym ? checker.getDeclaredTypeOfSymbol(sym) : void 0;
|
|
15917
|
+
if (!own2) return null;
|
|
15918
|
+
return indirect.find(
|
|
15919
|
+
(x) => x.type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown) || x.type === own2 || checker.isTypeAssignableTo(x.type, own2)
|
|
15920
|
+
)?.at ?? null;
|
|
15921
|
+
};
|
|
15922
|
+
const exportsOf = /* @__PURE__ */ new Map();
|
|
15923
|
+
const moduleObjectEscapes = (c, target2) => {
|
|
15924
|
+
scanIndirect();
|
|
15925
|
+
for (const m of moduleObjects) {
|
|
15926
|
+
if (m.plain) continue;
|
|
15927
|
+
if (!m.module) {
|
|
15928
|
+
const file2 = fileIdOf2(c.getSourceFile()) ?? "";
|
|
15929
|
+
if (m.folder === null || m.folder !== void 0 && file2.startsWith(m.folder)) return m.at;
|
|
15930
|
+
continue;
|
|
15931
|
+
}
|
|
15932
|
+
let set2 = exportsOf.get(m.module);
|
|
15933
|
+
if (!set2) {
|
|
15934
|
+
set2 = new Set(
|
|
15935
|
+
checker.getExportsOfModule(m.module).map((e) => resolve9(e)).filter(Boolean)
|
|
15936
|
+
);
|
|
15937
|
+
exportsOf.set(m.module, set2);
|
|
15938
|
+
}
|
|
15939
|
+
if (set2.has(target2)) return m.at;
|
|
15940
|
+
}
|
|
15941
|
+
void c;
|
|
15942
|
+
return null;
|
|
15943
|
+
};
|
|
15944
|
+
const cache = /* @__PURE__ */ new Map();
|
|
15945
|
+
const callersOf = (c, seen = /* @__PURE__ */ new Set()) => {
|
|
15946
|
+
const hit = cache.get(c);
|
|
15947
|
+
if (hit) return hit;
|
|
15948
|
+
if (seen.has(c)) return { why: "its classes extend each other in a cycle" };
|
|
15949
|
+
seen.add(c);
|
|
15950
|
+
const found = find2(c, seen);
|
|
15951
|
+
cache.set(c, found);
|
|
15952
|
+
return found;
|
|
15953
|
+
};
|
|
15954
|
+
const find2 = (c, seen) => {
|
|
15955
|
+
const label = c.name && ts.isIdentifier(c.name) ? c.name.text : "the default export";
|
|
15956
|
+
const target2 = symbolOfCallee(c);
|
|
15957
|
+
if (!target2) return { why: `${label} can't be followed` };
|
|
15958
|
+
const isExported = exported(c);
|
|
15959
|
+
const file2 = fileIdOf2(c.getSourceFile());
|
|
15960
|
+
if (isExported && !options2.closedWorld)
|
|
15961
|
+
return { why: `${label} is exported, so code outside the analysis may call it` };
|
|
15962
|
+
if (isExported && !ts.isExternalModule(c.getSourceFile()))
|
|
15963
|
+
return { why: `${label} is a global of a script, which the page may call` };
|
|
15964
|
+
if (isExported && file2 && options2.importedFromOutside(file2))
|
|
15965
|
+
return { why: `${label}'s module is imported by a file whose code isn't typed here` };
|
|
15966
|
+
index();
|
|
15967
|
+
if (ts.isClassDeclaration(c)) {
|
|
15968
|
+
const made = madeIndirectly(c);
|
|
15969
|
+
if (made)
|
|
15970
|
+
return {
|
|
15971
|
+
why: `${label} may be constructed at ${options2.where(made)}, by a value that isn't its name`
|
|
15972
|
+
};
|
|
15973
|
+
}
|
|
15974
|
+
if (isExported) {
|
|
15975
|
+
const through = moduleObjectEscapes(c, target2);
|
|
15976
|
+
if (through)
|
|
15977
|
+
return {
|
|
15978
|
+
why: `${label}'s module is used as an object at ${options2.where(through)}, which may call it`
|
|
15979
|
+
};
|
|
15980
|
+
}
|
|
15981
|
+
const names = new Set(c.name && ts.isIdentifier(c.name) ? [c.name.text] : []);
|
|
15982
|
+
for (const n2 of aliasNames().get(target2) ?? []) names.add(n2);
|
|
15983
|
+
const calls = [];
|
|
15984
|
+
for (const name of names)
|
|
15985
|
+
for (const [sym, ids2] of byName(name))
|
|
15986
|
+
for (const id of ids2) {
|
|
15987
|
+
if (id === c.name) continue;
|
|
15988
|
+
const p = id.parent;
|
|
15989
|
+
if (sym !== target2) {
|
|
15990
|
+
if (!isExported || sym) break;
|
|
15991
|
+
if (isExported && !sym && ts.isPropertyAccessExpression(p) && p.name === id && checker.getTypeAtLocation(p.expression).flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown))
|
|
15992
|
+
return {
|
|
15993
|
+
why: `${p.getText()} at ${options2.where(p)} may be ${label}, on a value whose type isn't known`
|
|
15994
|
+
};
|
|
15995
|
+
continue;
|
|
15996
|
+
}
|
|
15997
|
+
if (ts.isImportSpecifier(p) || ts.isExportSpecifier(p) || ts.isImportClause(p) || ts.findAncestor(id, (n2) => ts.isTypeNode(n2) || ts.isTypeQueryNode(n2)))
|
|
15998
|
+
continue;
|
|
15999
|
+
const called = ts.isPropertyAccessExpression(p) && p.name === id ? p : id;
|
|
16000
|
+
const user = called.parent;
|
|
16001
|
+
if ((ts.isCallExpression(user) || ts.isNewExpression(user)) && user.expression === called) {
|
|
16002
|
+
if (ts.isClassDeclaration(c) !== ts.isNewExpression(user))
|
|
16003
|
+
return {
|
|
16004
|
+
why: `${label} is ${ts.isNewExpression(user) ? "constructed" : "called"} at ${options2.where(user)}`
|
|
16005
|
+
};
|
|
16006
|
+
calls.push({ call: user, args: user.arguments ?? [] });
|
|
16007
|
+
continue;
|
|
16008
|
+
}
|
|
16009
|
+
if (ts.isClassDeclaration(c)) {
|
|
16010
|
+
const heritage = ts.findAncestor(id, ts.isHeritageClause);
|
|
16011
|
+
const sub = heritage?.parent;
|
|
16012
|
+
if (heritage?.token === ts.SyntaxKind.ExtendsKeyword && sub && ts.isClassDeclaration(sub) && ts.isExpressionWithTypeArguments(called.parent) && called.parent.parent === heritage) {
|
|
16013
|
+
const ctor = sub.members.find(
|
|
16014
|
+
(m) => ts.isConstructorDeclaration(m) && !!m.body
|
|
16015
|
+
);
|
|
16016
|
+
if (ctor) {
|
|
16017
|
+
const sups = [];
|
|
16018
|
+
const look = (n2) => {
|
|
16019
|
+
if (n2 !== ctor.body && ts.isFunctionLike(n2)) return;
|
|
16020
|
+
if (ts.isCallExpression(n2) && n2.expression.kind === ts.SyntaxKind.SuperKeyword)
|
|
16021
|
+
sups.push(n2);
|
|
16022
|
+
else ts.forEachChild(n2, look);
|
|
16023
|
+
};
|
|
16024
|
+
look(ctor.body);
|
|
16025
|
+
if (!sups.length) return { why: `${sub.name?.text ?? "a subclass"} never calls super` };
|
|
16026
|
+
for (const sup of sups) calls.push({ call: sup, args: sup.arguments });
|
|
16027
|
+
} else {
|
|
16028
|
+
const through = callersOf(sub, seen);
|
|
16029
|
+
if ("why" in through) return through;
|
|
16030
|
+
calls.push(...through.calls);
|
|
16031
|
+
}
|
|
16032
|
+
continue;
|
|
16033
|
+
}
|
|
16034
|
+
if (ts.isPropertyAccessExpression(p) && p.expression === id && p.name.text !== "prototype")
|
|
16035
|
+
continue;
|
|
16036
|
+
if (ts.isBinaryExpression(p) && p.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword)
|
|
16037
|
+
continue;
|
|
16038
|
+
}
|
|
16039
|
+
return {
|
|
16040
|
+
why: `${label} is used as a value at ${options2.where(id)}, so what calls it can't all be seen`
|
|
16041
|
+
};
|
|
16042
|
+
}
|
|
16043
|
+
return { calls, closedWorld: isExported };
|
|
16044
|
+
};
|
|
16045
|
+
return { callersOf };
|
|
16046
|
+
}
|
|
16047
|
+
var init_callers = __esm({
|
|
16048
|
+
"packages/stage2/src/callers.ts"() {
|
|
16049
|
+
"use strict";
|
|
16050
|
+
init_src();
|
|
16051
|
+
}
|
|
16052
|
+
});
|
|
16053
|
+
|
|
16054
|
+
// packages/stage2/src/returns.ts
|
|
16055
|
+
function returnedResolutions(input2, graph, s, inj = null) {
|
|
16056
|
+
const out = { resolutions: [], edges: [] };
|
|
16057
|
+
if (!s?.checker) return out;
|
|
16058
|
+
const { ts, checker, fileIdOf: fileIdOf2, toByte, toUtf16 } = s;
|
|
16059
|
+
const open3 = input2.resolutions.filter(
|
|
16060
|
+
(r) => r.status === "unresolved" && r.importOutcome === null && (!r.candidates || r.exhaustive === false)
|
|
16061
|
+
);
|
|
16062
|
+
if (open3.length === 0) return out;
|
|
16063
|
+
const sites = new Map(input2.sites.map((x) => [x.id, x]));
|
|
16064
|
+
const pathOf9 = (file2) => file2.slice(file2.indexOf(":") + 1);
|
|
16065
|
+
const where = (node2) => {
|
|
16066
|
+
const sf = node2.getSourceFile();
|
|
16067
|
+
const file2 = fileIdOf2(sf);
|
|
16068
|
+
return `${file2 ? pathOf9(file2) : sf.fileName}:${sf.getLineAndCharacterOfPosition(node2.getStart(sf)).line + 1}`;
|
|
16069
|
+
};
|
|
16070
|
+
const evidenceOf2 = (node2) => {
|
|
16071
|
+
const sf = node2.getSourceFile();
|
|
16072
|
+
const file2 = fileIdOf2(sf);
|
|
16073
|
+
if (!file2) return null;
|
|
16074
|
+
const pos = (p) => {
|
|
16075
|
+
const lc = sf.getLineAndCharacterOfPosition(p);
|
|
16076
|
+
return { line: lc.line + 1, column: lc.character + 1, offset: toByte(sf, p) };
|
|
16077
|
+
};
|
|
16078
|
+
return {
|
|
16079
|
+
file: file2,
|
|
16080
|
+
span: { start: pos(node2.getStart(sf)), end: pos(node2.end) },
|
|
16081
|
+
commit: input2.revisions.get(file2.slice(0, file2.indexOf(":"))) ?? ""
|
|
16082
|
+
};
|
|
16083
|
+
};
|
|
16084
|
+
const fnByEnd = /* @__PURE__ */ new Map();
|
|
16085
|
+
for (const fn of input2.functions) {
|
|
16086
|
+
const k = `${fn.file}\0${fn.evidence.span.end.offset}`;
|
|
16087
|
+
const list = fnByEnd.get(k);
|
|
16088
|
+
if (list) list.push(fn);
|
|
16089
|
+
else fnByEnd.set(k, [fn]);
|
|
16090
|
+
}
|
|
16091
|
+
const functionOf = (node2) => {
|
|
16092
|
+
const sf = node2.getSourceFile();
|
|
16093
|
+
const file2 = fileIdOf2(sf);
|
|
16094
|
+
if (!file2) return void 0;
|
|
16095
|
+
const start = toByte(sf, node2.getStart(sf));
|
|
16096
|
+
return (fnByEnd.get(`${file2}\0${toByte(sf, node2.end)}`) ?? []).filter((f) => f.evidence.span.start.offset >= start).sort((a, b) => a.evidence.span.start.offset - b.evidence.span.start.offset)[0];
|
|
16097
|
+
};
|
|
16098
|
+
const unwrap = (e) => {
|
|
16099
|
+
while (ts.isParenthesizedExpression(e) || ts.isAsExpression(e) || ts.isSatisfiesExpression(e) || ts.isNonNullExpression(e) || ts.isTypeAssertionExpression(e))
|
|
16100
|
+
e = e.expression;
|
|
16101
|
+
return e;
|
|
16102
|
+
};
|
|
16103
|
+
const symbolOf = (node2) => {
|
|
16104
|
+
const sym = checker.getSymbolAtLocation(node2);
|
|
16105
|
+
return sym && sym.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(sym) : sym;
|
|
16106
|
+
};
|
|
16107
|
+
const isConst = (d) => ts.isVariableDeclaration(d) && (ts.getCombinedNodeFlags(d) & ts.NodeFlags.Const) !== 0 && ts.isVariableDeclarationList(d.parent);
|
|
16108
|
+
const isFunctionNode = (n2) => ts.isArrowFunction(n2) || ts.isFunctionExpression(n2) || ts.isFunctionDeclaration(n2);
|
|
16109
|
+
const classOf = (e) => {
|
|
16110
|
+
const d = symbolOf(unwrap(e))?.valueDeclaration;
|
|
16111
|
+
if (!d || !ts.isClassDeclaration(d) || !d.name || !ts.isSourceFile(d.parent)) return null;
|
|
16112
|
+
const file2 = fileIdOf2(d.getSourceFile());
|
|
16113
|
+
const decl = file2 ? { file: file2, name: d.name.text } : null;
|
|
16114
|
+
return decl && graph.declared(decl) ? decl : null;
|
|
16115
|
+
};
|
|
16116
|
+
const importedAs = (callee) => {
|
|
16117
|
+
const e = unwrap(callee);
|
|
16118
|
+
if (ts.isPropertyAccessExpression(e) && ts.isIdentifier(e.expression)) {
|
|
16119
|
+
const d = checker.getSymbolAtLocation(e.expression)?.declarations?.[0];
|
|
16120
|
+
const imp = d && ts.findAncestor(d, ts.isImportDeclaration);
|
|
16121
|
+
return imp && ts.isStringLiteral(imp.moduleSpecifier) ? { pkg: imp.moduleSpecifier.text, name: e.name.text } : null;
|
|
16122
|
+
}
|
|
16123
|
+
if (!ts.isIdentifier(e)) return null;
|
|
16124
|
+
const spec = checker.getSymbolAtLocation(e)?.declarations?.[0];
|
|
16125
|
+
if (!spec || !ts.isImportSpecifier(spec)) return null;
|
|
16126
|
+
const decl = spec.parent.parent.parent;
|
|
16127
|
+
return ts.isImportDeclaration(decl) && ts.isStringLiteral(decl.moduleSpecifier) ? { pkg: decl.moduleSpecifier.text, name: (spec.propertyName ?? spec.name).text } : null;
|
|
16128
|
+
};
|
|
16129
|
+
const badUses = /* @__PURE__ */ new Map();
|
|
16130
|
+
const onlyRead = (name, scope2, except) => {
|
|
16131
|
+
const sym = checker.getSymbolAtLocation(name);
|
|
16132
|
+
if (!sym) return false;
|
|
16133
|
+
let byScope = badUses.get(sym);
|
|
16134
|
+
if (!byScope) badUses.set(sym, byScope = /* @__PURE__ */ new Map());
|
|
16135
|
+
let bad = byScope.get(scope2);
|
|
16136
|
+
if (!bad) {
|
|
16137
|
+
work += 10;
|
|
16138
|
+
const found = [];
|
|
16139
|
+
const visit2 = (node2) => {
|
|
16140
|
+
if (ts.isIdentifier(node2) && node2 !== name && checker.getSymbolAtLocation(node2) === sym) {
|
|
16141
|
+
const p = node2.parent;
|
|
16142
|
+
const member = (ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p)) && p.expression === node2;
|
|
16143
|
+
if (!member || written2(p)) found.push(node2);
|
|
16144
|
+
return;
|
|
16145
|
+
}
|
|
16146
|
+
if (ts.isShorthandPropertyAssignment(node2) && checker.getShorthandAssignmentValueSymbol(node2) === sym) {
|
|
16147
|
+
found.push(node2);
|
|
16148
|
+
return;
|
|
16149
|
+
}
|
|
16150
|
+
ts.forEachChild(node2, visit2);
|
|
16151
|
+
};
|
|
16152
|
+
visit2(scope2);
|
|
16153
|
+
byScope.set(scope2, bad = found);
|
|
16154
|
+
}
|
|
16155
|
+
return bad.every(
|
|
16156
|
+
(b) => !!except && (b === except || b.parent === except || ts.isShorthandPropertyAssignment(b) && b.name === except)
|
|
16157
|
+
);
|
|
16158
|
+
};
|
|
16159
|
+
const written2 = (node2) => {
|
|
16160
|
+
let at = node2;
|
|
16161
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isPropertyAssignment(at.parent) || ts.isShorthandPropertyAssignment(at.parent) || ts.isObjectLiteralExpression(at.parent) || ts.isArrayLiteralExpression(at.parent) || ts.isSpreadElement(at.parent) || ts.isSpreadAssignment(at.parent)) {
|
|
16162
|
+
if (ts.isPropertyAssignment(at.parent) && at.parent.initializer !== at) return false;
|
|
16163
|
+
at = at.parent;
|
|
16164
|
+
}
|
|
16165
|
+
const p = at.parent;
|
|
16166
|
+
if (ts.isDeleteExpression(p)) return true;
|
|
16167
|
+
if (ts.isBinaryExpression(p))
|
|
16168
|
+
return p.left === at && p.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && p.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
|
|
16169
|
+
if (ts.isPrefixUnaryExpression(p) || ts.isPostfixUnaryExpression(p)) return true;
|
|
16170
|
+
return (ts.isForOfStatement(p) || ts.isForInStatement(p)) && p.initializer === at;
|
|
16171
|
+
};
|
|
16172
|
+
const thisEscapes = (fn, writes) => {
|
|
16173
|
+
let escapes = false;
|
|
16174
|
+
const visit2 = (node2) => {
|
|
16175
|
+
if (escapes) return;
|
|
16176
|
+
if (node2 !== fn && (ts.isFunctionDeclaration(node2) || ts.isFunctionExpression(node2) || ts.isClassLike(node2)))
|
|
16177
|
+
return;
|
|
16178
|
+
if (node2.kind === ts.SyntaxKind.ThisKeyword) {
|
|
16179
|
+
const p = node2.parent;
|
|
16180
|
+
const member = (ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p)) && p.expression === node2;
|
|
16181
|
+
if (!member || writes && written2(p)) escapes = true;
|
|
16182
|
+
return;
|
|
16183
|
+
}
|
|
16184
|
+
ts.forEachChild(node2, visit2);
|
|
16185
|
+
};
|
|
16186
|
+
visit2(fn);
|
|
16187
|
+
return escapes;
|
|
16188
|
+
};
|
|
16189
|
+
const thisSafe = (v) => {
|
|
16190
|
+
if (ts.isMethodDeclaration(v)) return !thisEscapes(v, true);
|
|
16191
|
+
if (ts.isShorthandPropertyAssignment(v.parent) && v.parent.name === v) {
|
|
16192
|
+
const d = checker.getShorthandAssignmentValueSymbol(v.parent)?.valueDeclaration;
|
|
16193
|
+
return d ? thisSafeDecl(d) : false;
|
|
16194
|
+
}
|
|
16195
|
+
const e = unwrap(v);
|
|
16196
|
+
if (ts.isArrowFunction(e) || ts.isObjectLiteralExpression(e) || ts.isArrayLiteralExpression(e))
|
|
16197
|
+
return true;
|
|
16198
|
+
if (ts.isFunctionExpression(e)) return !thisEscapes(e, true);
|
|
16199
|
+
if (ts.isStringLiteralLike(e) || ts.isNumericLiteral(e) || ts.isTemplateExpression(e) || e.kind === ts.SyntaxKind.TrueKeyword || e.kind === ts.SyntaxKind.FalseKeyword || e.kind === ts.SyntaxKind.NullKeyword)
|
|
16200
|
+
return true;
|
|
16201
|
+
if (ts.isIdentifier(e)) {
|
|
16202
|
+
const d = symbolOf(e)?.valueDeclaration;
|
|
16203
|
+
return d ? thisSafeDecl(d) : false;
|
|
16204
|
+
}
|
|
16205
|
+
return false;
|
|
16206
|
+
};
|
|
16207
|
+
const thisSafeDecl = (d) => {
|
|
16208
|
+
if (ts.isFunctionDeclaration(d)) return isFixedFunction(d) && !thisEscapes(d, true);
|
|
16209
|
+
if (!isConst(d)) return false;
|
|
16210
|
+
const init = d.initializer;
|
|
16211
|
+
return !!init && !ts.isIdentifier(unwrap(init)) && thisSafe(init);
|
|
16212
|
+
};
|
|
16213
|
+
const fixedName = /* @__PURE__ */ new Map();
|
|
16214
|
+
const isFixedFunction = (d) => {
|
|
16215
|
+
const name = d.name;
|
|
16216
|
+
if (!name) return true;
|
|
16217
|
+
const sym = checker.getSymbolAtLocation(name);
|
|
16218
|
+
if (!sym) return false;
|
|
16219
|
+
const hit = fixedName.get(sym);
|
|
16220
|
+
if (hit !== void 0) return hit;
|
|
16221
|
+
let fixed = (sym.declarations ?? []).filter((x) => ts.isFunctionDeclaration(x)).length === 1;
|
|
16222
|
+
for (const id of writtenNames(d.getSourceFile()).get(name.text) ?? []) {
|
|
16223
|
+
if (!fixed) break;
|
|
16224
|
+
if (id !== name && checker.getSymbolAtLocation(id) === sym) fixed = false;
|
|
16225
|
+
}
|
|
16226
|
+
fixedName.set(sym, fixed);
|
|
16227
|
+
return fixed;
|
|
16228
|
+
};
|
|
16229
|
+
const writtenByFile = /* @__PURE__ */ new Map();
|
|
16230
|
+
const writtenNames = (sf) => {
|
|
16231
|
+
const hit = writtenByFile.get(sf);
|
|
16232
|
+
if (hit) return hit;
|
|
16233
|
+
const out2 = /* @__PURE__ */ new Map();
|
|
16234
|
+
const visit2 = (node2) => {
|
|
16235
|
+
if (ts.isIdentifier(node2) && written2(node2)) {
|
|
16236
|
+
const list = out2.get(node2.text);
|
|
16237
|
+
if (list) list.push(node2);
|
|
16238
|
+
else out2.set(node2.text, [node2]);
|
|
16239
|
+
}
|
|
16240
|
+
ts.forEachChild(node2, visit2);
|
|
16241
|
+
};
|
|
16242
|
+
visit2(sf);
|
|
16243
|
+
writtenByFile.set(sf, out2);
|
|
16244
|
+
return out2;
|
|
16245
|
+
};
|
|
16246
|
+
const exportedSeen = /* @__PURE__ */ new Map();
|
|
16247
|
+
const exported = (decl) => {
|
|
16248
|
+
const hit = exportedSeen.get(decl);
|
|
16249
|
+
if (hit !== void 0) return hit;
|
|
16250
|
+
const is = isExported(decl);
|
|
16251
|
+
exportedSeen.set(decl, is);
|
|
16252
|
+
return is;
|
|
16253
|
+
};
|
|
16254
|
+
const isExported = (decl) => {
|
|
16255
|
+
const statement = decl.parent.parent;
|
|
16256
|
+
if (ts.canHaveModifiers(statement) && ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword))
|
|
16257
|
+
return true;
|
|
16258
|
+
const name = ts.isIdentifier(decl.name) ? decl.name.text : null;
|
|
16259
|
+
return decl.getSourceFile().statements.some(
|
|
16260
|
+
(st) => ts.isExportDeclaration(st) && !st.moduleSpecifier && st.exportClause && ts.isNamedExports(st.exportClause) && st.exportClause.elements.some((el) => (el.propertyName ?? el.name).text === name) || ts.isExportAssignment(st) && ts.isIdentifier(st.expression) && st.expression.text === name
|
|
16261
|
+
);
|
|
16262
|
+
};
|
|
16263
|
+
const ANY = "\0any";
|
|
16264
|
+
const HANDED = "\0handed";
|
|
16265
|
+
const onlyReadProto = (p, depth = 0) => {
|
|
16266
|
+
let at = p;
|
|
16267
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isNonNullExpression(at.parent) || ts.isAsExpression(at.parent))
|
|
16268
|
+
at = at.parent;
|
|
16269
|
+
const parent = at.parent;
|
|
16270
|
+
if ((ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) && parent.expression === at)
|
|
16271
|
+
return !written2(parent);
|
|
16272
|
+
if (ts.isCallExpression(parent) && parent.expression !== at) {
|
|
16273
|
+
const c = unwrap(parent.expression);
|
|
16274
|
+
if (!ts.isPropertyAccessExpression(c)) return false;
|
|
16275
|
+
if (ts.isIdentifier(c.expression))
|
|
16276
|
+
return (c.expression.text === "Object" || c.expression.text === "Reflect") && PROTOTYPE_READERS.has(c.name.text);
|
|
16277
|
+
const fn = unwrap(c.expression);
|
|
16278
|
+
return (c.name.text === "call" || c.name.text === "apply") && ts.isPropertyAccessExpression(fn) && ["hasOwnProperty", "propertyIsEnumerable", "isPrototypeOf"].includes(fn.name.text);
|
|
16279
|
+
}
|
|
16280
|
+
if (ts.isBinaryExpression(parent) && [
|
|
16281
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
16282
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
16283
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
16284
|
+
ts.SyntaxKind.ExclamationEqualsToken
|
|
16285
|
+
].includes(parent.operatorToken.kind))
|
|
16286
|
+
return true;
|
|
16287
|
+
if (ts.isTypeOfExpression(parent)) return true;
|
|
16288
|
+
if (ts.isVariableDeclaration(parent) && parent.initializer === at && isConst(parent) && depth < 3) {
|
|
16289
|
+
if (!ts.isIdentifier(parent.name)) return false;
|
|
16290
|
+
const sym = checker.getSymbolAtLocation(parent.name);
|
|
16291
|
+
const scope2 = ts.findAncestor(parent, (n2) => isFunctionNode(n2) || ts.isSourceFile(n2));
|
|
16292
|
+
if (!sym || !scope2) return false;
|
|
16293
|
+
let ok = true;
|
|
16294
|
+
const visit2 = (node2) => {
|
|
16295
|
+
if (!ok) return;
|
|
16296
|
+
if (ts.isIdentifier(node2) && node2 !== parent.name && checker.getSymbolAtLocation(node2) === sym)
|
|
16297
|
+
ok = onlyReadProto(node2, depth + 1);
|
|
16298
|
+
else ts.forEachChild(node2, visit2);
|
|
16299
|
+
};
|
|
16300
|
+
visit2(scope2);
|
|
16301
|
+
return ok;
|
|
16302
|
+
}
|
|
16303
|
+
return false;
|
|
16304
|
+
};
|
|
16305
|
+
const heldClass = (e) => {
|
|
16306
|
+
const c = classOf(e);
|
|
16307
|
+
if (c) return c;
|
|
16308
|
+
if (!ts.isIdentifier(unwrap(e))) return "any";
|
|
16309
|
+
const sym = symbolOf(unwrap(e));
|
|
16310
|
+
const decls = sym?.declarations ?? [];
|
|
16311
|
+
if (decls.length && decls.every((d2) => !fileIdOf2(d2.getSourceFile())))
|
|
16312
|
+
return ts.isIdentifier(unwrap(e)) && unwrap(e).text === "Object" ? "Object" : "lib";
|
|
16313
|
+
const t = checker.getTypeAtLocation(e);
|
|
16314
|
+
if (t.isUnion()) return null;
|
|
16315
|
+
const sigs = t.getConstructSignatures();
|
|
16316
|
+
const made = sigs.length === 1 ? sigs[0].getReturnType().getSymbol() : void 0;
|
|
16317
|
+
const d = made?.valueDeclaration ?? made?.declarations?.[0];
|
|
16318
|
+
if (!d || !ts.isClassDeclaration(d) || !d.name || !ts.isSourceFile(d.parent)) return null;
|
|
16319
|
+
const file2 = fileIdOf2(d.getSourceFile());
|
|
16320
|
+
const decl = file2 ? { file: file2, name: d.name.text } : null;
|
|
16321
|
+
return decl && graph.declared(decl) ? decl : null;
|
|
16322
|
+
};
|
|
16323
|
+
let touched;
|
|
16324
|
+
const prototypeTouched = () => {
|
|
16325
|
+
if (touched) return touched;
|
|
16326
|
+
const found = /* @__PURE__ */ new Set();
|
|
16327
|
+
for (const sf of s.program?.getSourceFiles() ?? []) {
|
|
16328
|
+
const file2 = fileIdOf2(sf);
|
|
16329
|
+
if (!file2 || isTestPath(pathOf9(file2))) continue;
|
|
16330
|
+
const visit2 = (node2) => {
|
|
16331
|
+
const key = ts.isPropertyAccessExpression(node2) ? node2.name.text : ts.isElementAccessExpression(node2) && ts.isStringLiteralLike(node2.argumentExpression) ? node2.argumentExpression.text : null;
|
|
16332
|
+
if (key === "prototype" && !onlyReadProto(node2)) {
|
|
16333
|
+
const c = ts.isPropertyAccessExpression(node2) ? heldClass(node2.expression) : null;
|
|
16334
|
+
if (c === "Object" || c === "any") found.add(ANY);
|
|
16335
|
+
else if (c === "lib") {
|
|
16336
|
+
} else if (c) found.add(declKey(c));
|
|
16337
|
+
else found.add(HANDED);
|
|
16338
|
+
} else if (key === "__proto__" && !onlyReadProto(node2)) found.add(ANY);
|
|
16339
|
+
else if (ts.isCallExpression(node2) && ts.isPropertyAccessExpression(node2.expression)) {
|
|
16340
|
+
const name = node2.expression.name.text;
|
|
16341
|
+
if (name === "setPrototypeOf" || name === "getPrototypeOf" && !onlyReadProto(node2))
|
|
16342
|
+
found.add(ANY);
|
|
16343
|
+
}
|
|
16344
|
+
ts.forEachChild(node2, visit2);
|
|
16345
|
+
};
|
|
16346
|
+
visit2(sf);
|
|
16347
|
+
}
|
|
16348
|
+
return touched = found;
|
|
16349
|
+
};
|
|
16350
|
+
const prototypeWritten = (k) => prototypeTouched().has(ANY) || prototypeTouched().has(k) || prototypeTouched().has(HANDED) && (!inj || inj.wired().handed.has(k));
|
|
16351
|
+
const classDecl = (c) => s.sourceFile(c.file)?.statements.find((x) => ts.isClassDeclaration(x) && x.name?.text === c.name);
|
|
16352
|
+
const soundClass = /* @__PURE__ */ new Map();
|
|
16353
|
+
const isSoundClass = (c, strict = true) => {
|
|
16354
|
+
const key = `${strict ? "s" : "l"}\0${declKey(c)}`;
|
|
16355
|
+
const hit = soundClass.get(key);
|
|
16356
|
+
if (hit !== void 0) return hit;
|
|
16357
|
+
let ok = true;
|
|
16358
|
+
const seen = /* @__PURE__ */ new Set();
|
|
16359
|
+
for (let at = c; at && ok; at = graph.parentOf.get(`${at.file}\0${at.name}`)?.base) {
|
|
16360
|
+
const k = `${at.file}\0${at.name}`;
|
|
16361
|
+
if (seen.has(k)) break;
|
|
16362
|
+
seen.add(k);
|
|
16363
|
+
const node2 = classDecl(at);
|
|
16364
|
+
const parent = graph.parentOf.get(k);
|
|
16365
|
+
const decorated = !!node2 && !!ts.getDecorators(node2)?.length && (strict || !inj?.onlyDiDecorators(node2));
|
|
16366
|
+
if (!node2 || decorated || parent && !parent.base || prototypeWritten(k)) {
|
|
16367
|
+
ok = false;
|
|
16368
|
+
break;
|
|
16369
|
+
}
|
|
16370
|
+
for (const m of node2.members) {
|
|
16371
|
+
if (ts.isConstructorDeclaration(m) && m.body) {
|
|
16372
|
+
const returns = (n2) => ts.isFunctionLike(n2) && n2 !== m ? false : ts.isReturnStatement(n2) && !!n2.expression || !!ts.forEachChild(n2, (x) => returns(x) || void 0);
|
|
16373
|
+
if (returns(m.body)) ok = false;
|
|
16374
|
+
}
|
|
16375
|
+
if (ok && strict && thisEscapes(m, false)) ok = false;
|
|
16376
|
+
}
|
|
16377
|
+
}
|
|
16378
|
+
soundClass.set(key, ok);
|
|
16379
|
+
return ok;
|
|
16380
|
+
};
|
|
16381
|
+
const calleeOf = (call) => {
|
|
16382
|
+
const d = symbolOf(unwrap(call.expression))?.valueDeclaration;
|
|
16383
|
+
if (!d || !fileIdOf2(d.getSourceFile())) return null;
|
|
16384
|
+
if (ts.isFunctionDeclaration(d)) return d.body && isFixedFunction(d) ? d : null;
|
|
16385
|
+
if (isConst(d)) {
|
|
16386
|
+
const init = d.initializer;
|
|
16387
|
+
const f = init && unwrap(init);
|
|
16388
|
+
return f && (ts.isArrowFunction(f) || ts.isFunctionExpression(f)) ? f : null;
|
|
16389
|
+
}
|
|
16390
|
+
if (ts.isMethodDeclaration(d) && d.body && ts.getCombinedModifierFlags(d) & ts.ModifierFlags.Static) {
|
|
16391
|
+
const on = ts.isPropertyAccessExpression(unwrap(call.expression)) ? classOf(unwrap(call.expression).expression) : null;
|
|
16392
|
+
const own2 = d.parent && ts.isClassDeclaration(d.parent) && d.parent.name?.text;
|
|
16393
|
+
if (!on || on.name !== own2 || !ts.isIdentifier(d.name)) return null;
|
|
16394
|
+
const st = graph.staticOf(on, d.name.text);
|
|
16395
|
+
return typeof st !== "string" && st.fn.id === functionOf(d)?.id ? d : null;
|
|
16396
|
+
}
|
|
16397
|
+
return null;
|
|
16398
|
+
};
|
|
16399
|
+
let notes;
|
|
16400
|
+
let flowed;
|
|
16401
|
+
let work;
|
|
16402
|
+
let closed;
|
|
16403
|
+
let siteTest = false;
|
|
16404
|
+
const note = (kind, detail) => {
|
|
16405
|
+
if (!notes.some((n2) => n2.detail === detail)) notes.push({ kind, detail });
|
|
16406
|
+
};
|
|
16407
|
+
const short = (n2) => {
|
|
16408
|
+
const t = n2.getText(n2.getSourceFile()).replace(/\s+/g, " ");
|
|
16409
|
+
return t.length > 60 ? `${t.slice(0, 57)}\u2026` : t;
|
|
16410
|
+
};
|
|
16411
|
+
const keyOf2 = (n2) => n2 && (ts.isIdentifier(n2) || ts.isStringLiteralLike(n2) || ts.isPrivateIdentifier(n2)) ? n2.text : null;
|
|
16412
|
+
const describe4 = (v) => v.length === 0 ? "nothing that runs" : [
|
|
16413
|
+
...new Set(
|
|
16414
|
+
v.map(
|
|
16415
|
+
(a) => a.kind === "fn" ? a.fn.qualifiedName : a.kind === "instance" ? `an instance of ${a.cls.name}` : "an object"
|
|
16416
|
+
)
|
|
16417
|
+
)
|
|
16418
|
+
].join(" or ");
|
|
16419
|
+
const share = (a) => a.kind === "fn" ? a : { ...a, shared: true };
|
|
16420
|
+
const upChain = (c) => {
|
|
16421
|
+
const out2 = [];
|
|
16422
|
+
const seen = /* @__PURE__ */ new Set();
|
|
16423
|
+
for (let at = c; at && !seen.has(declKey(at)); at = graph.parentOf.get(declKey(at))?.base) {
|
|
16424
|
+
seen.add(declKey(at));
|
|
16425
|
+
out2.push(at);
|
|
16426
|
+
}
|
|
16427
|
+
return out2;
|
|
16428
|
+
};
|
|
16429
|
+
const writesSeen = /* @__PURE__ */ new Map();
|
|
16430
|
+
const writeOf = (name, chain, except, literal2) => {
|
|
16431
|
+
if (!inj) return ts.factory.createIdentifier(name);
|
|
16432
|
+
const types = literal2 ? [checker.getTypeAtLocation(literal2)] : void 0;
|
|
16433
|
+
const key = `${siteTest}\0${name}\0${[...chain].sort().join("\0")}`;
|
|
16434
|
+
const cacheable = !except && !literal2;
|
|
16435
|
+
if (cacheable && writesSeen.has(key)) return writesSeen.get(key);
|
|
16436
|
+
const found = inj.writeTo(name, chain, {
|
|
16437
|
+
tests: siteTest,
|
|
16438
|
+
...literal2 ? { file: fileIdOf2(literal2.getSourceFile()) ?? "" } : {},
|
|
16439
|
+
...types ? { types, literal: literal2 } : {},
|
|
16440
|
+
...except ? { except: (at) => at === except } : {}
|
|
16441
|
+
});
|
|
16442
|
+
if (cacheable) writesSeen.set(key, found);
|
|
16443
|
+
return found;
|
|
16444
|
+
};
|
|
16445
|
+
const memberWritten = (a, name) => {
|
|
16446
|
+
if (a.kind === "fn" || !a.shared) return false;
|
|
16447
|
+
if (!inj) return true;
|
|
16448
|
+
return a.kind === "instance" ? writeOf(name, new Set(upChain(a.cls).map(declKey))) !== null : writeOf(name, /* @__PURE__ */ new Set(), void 0, a.at) !== null;
|
|
16449
|
+
};
|
|
16450
|
+
const thisClass = (node2) => {
|
|
16451
|
+
let at = node2.parent;
|
|
16452
|
+
while (at && !ts.isSourceFile(at)) {
|
|
16453
|
+
if (ts.isFunctionLike(at) && !ts.isArrowFunction(at)) break;
|
|
16454
|
+
if (ts.isPropertyDeclaration(at) || ts.isClassStaticBlockDeclaration(at)) break;
|
|
16455
|
+
at = at.parent;
|
|
16456
|
+
}
|
|
16457
|
+
if (!at || ts.isSourceFile(at) || ts.isClassStaticBlockDeclaration(at)) return null;
|
|
16458
|
+
const ok = ts.isMethodDeclaration(at) || ts.isConstructorDeclaration(at) || ts.isGetAccessorDeclaration(at) || ts.isSetAccessorDeclaration(at) || ts.isPropertyDeclaration(at);
|
|
16459
|
+
if (!ok || !at.parent || !ts.isClassDeclaration(at.parent)) return null;
|
|
16460
|
+
if (ts.getCombinedModifierFlags(at) & ts.ModifierFlags.Static) return null;
|
|
16461
|
+
return classOf(at.parent.name ?? at.parent);
|
|
16462
|
+
};
|
|
16463
|
+
const declaresField = (node2, name) => {
|
|
16464
|
+
for (const m of node2.members) {
|
|
16465
|
+
if (ts.isConstructorDeclaration(m)) {
|
|
16466
|
+
const p = m.parameters.find(
|
|
16467
|
+
(x) => ts.isIdentifier(x.name) && x.name.text === name && ts.isParameterPropertyDeclaration(x, m)
|
|
16468
|
+
);
|
|
16469
|
+
if (p) return p;
|
|
16470
|
+
}
|
|
16471
|
+
if (keyOf2(m.name) !== name) continue;
|
|
16472
|
+
if (ts.getCombinedModifierFlags(m) & ts.ModifierFlags.Static) continue;
|
|
16473
|
+
return ts.isPropertyDeclaration(m) ? m : "other";
|
|
16474
|
+
}
|
|
16475
|
+
return null;
|
|
16476
|
+
};
|
|
16477
|
+
const usesThis = (n2) => {
|
|
16478
|
+
let found = false;
|
|
16479
|
+
const visit2 = (x) => {
|
|
16480
|
+
if (found) return;
|
|
16481
|
+
if (x !== n2 && (ts.isFunctionDeclaration(x) || ts.isFunctionExpression(x) || ts.isClassLike(x))) return;
|
|
16482
|
+
if (x.kind === ts.SyntaxKind.ThisKeyword || x.kind === ts.SyntaxKind.SuperKeyword) {
|
|
16483
|
+
const p = x.parent;
|
|
16484
|
+
if (x.kind === ts.SyntaxKind.SuperKeyword && ts.isCallExpression(p) && p.expression === x) return;
|
|
16485
|
+
const member = (ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p)) && p.expression === x;
|
|
16486
|
+
const called = member && (ts.isCallExpression(p.parent) && p.parent.expression === p || ts.isTaggedTemplateExpression(p.parent) && p.parent.tag === p);
|
|
16487
|
+
if (!member || called) found = true;
|
|
16488
|
+
return;
|
|
16489
|
+
}
|
|
16490
|
+
ts.forEachChild(x, visit2);
|
|
16491
|
+
};
|
|
16492
|
+
visit2(n2);
|
|
16493
|
+
return found;
|
|
16494
|
+
};
|
|
16495
|
+
const fieldOf = (owner, name, exact, injected, depth, made) => {
|
|
16496
|
+
if (!inj || depth > DEPTH) return null;
|
|
16497
|
+
let declaring = null;
|
|
16498
|
+
let member = null;
|
|
16499
|
+
for (const at of upChain(owner)) {
|
|
16500
|
+
const node2 = classDecl(at);
|
|
16501
|
+
if (!node2) return null;
|
|
16502
|
+
const found = declaresField(node2, name);
|
|
16503
|
+
if (found === "other") return null;
|
|
16504
|
+
if (found) {
|
|
16505
|
+
declaring = { cls: at, node: node2 };
|
|
16506
|
+
member = found;
|
|
16507
|
+
break;
|
|
16508
|
+
}
|
|
16509
|
+
}
|
|
16510
|
+
if (!declaring || !member) return null;
|
|
16511
|
+
for (const c of upChain(declaring.cls).slice(1)) {
|
|
16512
|
+
const node2 = classDecl(c);
|
|
16513
|
+
if (!node2 || declaresField(node2, name) === "other") return null;
|
|
16514
|
+
}
|
|
16515
|
+
const holders = exact ? [owner] : [owner, ...inj.subclassesOf(owner)];
|
|
16516
|
+
for (const h of holders) {
|
|
16517
|
+
if (!isSoundClass(h, false)) return null;
|
|
16518
|
+
const node2 = h === owner ? null : classDecl(h);
|
|
16519
|
+
if (h !== owner && (!node2 || declaresField(node2, name) !== null)) return null;
|
|
16520
|
+
}
|
|
16521
|
+
for (const c of upChain(declaring.cls)) {
|
|
16522
|
+
const node2 = classDecl(c);
|
|
16523
|
+
if (!node2) return null;
|
|
16524
|
+
for (const m of node2.members) {
|
|
16525
|
+
const statik = ts.getCombinedModifierFlags(m) & ts.ModifierFlags.Static;
|
|
16526
|
+
if (ts.isConstructorDeclaration(m) && m.body && usesThis(m.body)) return null;
|
|
16527
|
+
if (ts.isPropertyDeclaration(m) && !statik && m.initializer && usesThis(m.initializer)) return null;
|
|
16528
|
+
}
|
|
16529
|
+
}
|
|
16530
|
+
const chain = new Set(holders.flatMap((h) => upChain(h)).map(declKey));
|
|
16531
|
+
const label = `${declaring.cls.name}.${name}`;
|
|
16532
|
+
const injectedValue = (sym) => {
|
|
16533
|
+
if (exact && !injected) return null;
|
|
16534
|
+
const source = sym ? inj.fromField(sym) : null;
|
|
16535
|
+
if (!source || "why" in source) return null;
|
|
16536
|
+
const provided = inj.providedFor(source.token);
|
|
16537
|
+
if ("why" in provided || !isSoundClass(provided.cls, false)) return null;
|
|
16538
|
+
note("injection", source.detail);
|
|
16539
|
+
note("provided", provided.detail);
|
|
16540
|
+
return [{ kind: "instance", cls: provided.cls, at: member, shared: true, injected: true }];
|
|
16541
|
+
};
|
|
16542
|
+
const passedValue = (p) => {
|
|
16543
|
+
if (injected) return null;
|
|
16544
|
+
const ctor = p.parent;
|
|
16545
|
+
if (exact && made) {
|
|
16546
|
+
for (const c of upChain(owner)) {
|
|
16547
|
+
if (declKey(c) === declKey(declaring.cls)) break;
|
|
16548
|
+
if (classDecl(c)?.members.some((m) => ts.isConstructorDeclaration(m) && !!m.body)) return null;
|
|
16549
|
+
}
|
|
16550
|
+
if (reassigned(p) || usesArguments(ctor)) return null;
|
|
16551
|
+
const index = argIndex(ctor, p);
|
|
16552
|
+
const args = made.arguments ?? [];
|
|
16553
|
+
if (args.slice(0, index + 1).some((x) => ts.isSpreadElement(x))) return null;
|
|
16554
|
+
const arg = args[index] ?? p.initializer;
|
|
16555
|
+
if (!arg) return [];
|
|
16556
|
+
const v = valueOf(arg, made, depth + 1, null);
|
|
16557
|
+
if (v) note("parameter", `${label} is what ${short(made)} passes: ${short(arg)} (${where(arg)})`);
|
|
16558
|
+
return v;
|
|
16559
|
+
}
|
|
16560
|
+
return exact ? null : paramValue(p, depth + 1);
|
|
16561
|
+
};
|
|
16562
|
+
let value;
|
|
16563
|
+
let defining;
|
|
16564
|
+
if (ts.isParameter(member)) {
|
|
16565
|
+
value = injectedValue(checker.getSymbolsOfParameterPropertyDeclaration(member, name)[1]) ?? (inj.diDecorator(declaring.node) ? null : passedValue(member));
|
|
16566
|
+
} else if (member.initializer) {
|
|
16567
|
+
const init = unwrap(member.initializer);
|
|
16568
|
+
const angular = ts.isCallExpression(init) ? importedAs(init.expression) : null;
|
|
16569
|
+
if (angular?.pkg === "@angular/core" && angular.name === "inject")
|
|
16570
|
+
value = injectedValue(checker.getSymbolAtLocation(member.name));
|
|
16571
|
+
else {
|
|
16572
|
+
value = valueOf(member.initializer, member, depth + 1, null);
|
|
16573
|
+
if (value) note("field", `${label} is ${short(member.initializer)} (${where(member.initializer)})`);
|
|
16574
|
+
}
|
|
16575
|
+
} else {
|
|
16576
|
+
const ctor = declaring.node.members.find(
|
|
16577
|
+
(m) => ts.isConstructorDeclaration(m) && !!m.body
|
|
16578
|
+
);
|
|
16579
|
+
const sets = (ctor?.body?.statements ?? []).flatMap((st) => {
|
|
16580
|
+
const e = ts.isExpressionStatement(st) ? st.expression : void 0;
|
|
16581
|
+
return e && ts.isBinaryExpression(e) && e.operatorToken.kind === ts.SyntaxKind.EqualsToken && ts.isPropertyAccessExpression(e.left) && e.left.expression.kind === ts.SyntaxKind.ThisKeyword && e.left.name.text === name ? [e] : [];
|
|
16582
|
+
});
|
|
16583
|
+
if (sets.length !== 1) return null;
|
|
16584
|
+
const set2 = sets[0];
|
|
16585
|
+
defining = set2.left;
|
|
16586
|
+
const right = unwrap(set2.right);
|
|
16587
|
+
const param = ts.isIdentifier(right) ? symbolOf(right)?.valueDeclaration : void 0;
|
|
16588
|
+
if (param && ts.isParameter(param))
|
|
16589
|
+
value = injectedValue(checker.getSymbolAtLocation(member.name)) ?? (inj.diDecorator(declaring.node) || param.parent !== ctor ? null : passedValue(param));
|
|
16590
|
+
else {
|
|
16591
|
+
value = valueOf(set2.right, ctor, depth + 1, null);
|
|
16592
|
+
if (value) note("field", `${label} is set to ${short(set2.right)} (${where(set2.right)})`);
|
|
16593
|
+
}
|
|
16594
|
+
}
|
|
16595
|
+
if (!value) return null;
|
|
16596
|
+
if (writeOf(name, chain, defining)) return null;
|
|
16597
|
+
flowed = true;
|
|
16598
|
+
return value.map(share);
|
|
16599
|
+
};
|
|
16600
|
+
const callers2 = makeCallers(s, {
|
|
16601
|
+
closedWorld: !!input2.closedWorld,
|
|
16602
|
+
importedFromOutside: input2.importedFromOutside ?? (() => false),
|
|
16603
|
+
where
|
|
16604
|
+
});
|
|
16605
|
+
const inParam = /* @__PURE__ */ new Set();
|
|
16606
|
+
const argIndex = (fn, p) => fn.parameters.filter((x) => !(ts.isIdentifier(x.name) && x.name.text === "this")).indexOf(p);
|
|
16607
|
+
const usesArguments = (fn) => {
|
|
16608
|
+
let found = false;
|
|
16609
|
+
const visit2 = (n2) => {
|
|
16610
|
+
if (found) return;
|
|
16611
|
+
if (n2 !== fn && ts.isFunctionLike(n2) && !ts.isArrowFunction(n2)) return;
|
|
16612
|
+
if (ts.isIdentifier(n2) && n2.text === "arguments") found = true;
|
|
16613
|
+
else ts.forEachChild(n2, visit2);
|
|
16614
|
+
};
|
|
16615
|
+
const body = fn.body;
|
|
16616
|
+
if (body) visit2(body);
|
|
16617
|
+
return found;
|
|
16618
|
+
};
|
|
16619
|
+
const paramSeen = /* @__PURE__ */ new Map();
|
|
16620
|
+
const reassigned = (p) => {
|
|
16621
|
+
const sym = checker.getSymbolAtLocation(p.name);
|
|
16622
|
+
const body = p.parent.body;
|
|
16623
|
+
let found = false;
|
|
16624
|
+
const visit2 = (n2) => {
|
|
16625
|
+
if (found) return;
|
|
16626
|
+
if (ts.isIdentifier(n2) && n2 !== p.name && checker.getSymbolAtLocation(n2) === sym && assignedHere(n2))
|
|
16627
|
+
found = true;
|
|
16628
|
+
else ts.forEachChild(n2, visit2);
|
|
16629
|
+
};
|
|
16630
|
+
if (body) visit2(body);
|
|
16631
|
+
return found;
|
|
16632
|
+
};
|
|
16633
|
+
const assignedHere = (n2) => {
|
|
16634
|
+
let at = n2;
|
|
16635
|
+
while (ts.isParenthesizedExpression(at.parent) || ts.isShorthandPropertyAssignment(at.parent) || ts.isPropertyAssignment(at.parent) && at.parent.initializer === at || ts.isObjectLiteralExpression(at.parent) || ts.isArrayLiteralExpression(at.parent) || ts.isSpreadElement(at.parent))
|
|
16636
|
+
at = at.parent;
|
|
16637
|
+
const parent = at.parent;
|
|
16638
|
+
if (ts.isBinaryExpression(parent))
|
|
16639
|
+
return parent.left === at && parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
|
|
16640
|
+
if (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent))
|
|
16641
|
+
return parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken;
|
|
16642
|
+
return (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) && parent.initializer === at;
|
|
16643
|
+
};
|
|
16644
|
+
const paramValue = (p, depth) => {
|
|
16645
|
+
if (siteTest || depth > DEPTH || inParam.has(p) || !ts.isIdentifier(p.name) || p.dotDotDotToken)
|
|
16646
|
+
return null;
|
|
16647
|
+
const fn = p.parent;
|
|
16648
|
+
let callee = null;
|
|
16649
|
+
let label = "";
|
|
16650
|
+
if (ts.isFunctionDeclaration(fn) && fn.body && isFixedFunction(fn)) {
|
|
16651
|
+
callee = fn;
|
|
16652
|
+
label = fn.name?.text ?? "the default export";
|
|
16653
|
+
} else if ((ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) && ts.isVariableDeclaration(fn.parent) && fn.parent.initializer === fn && isConst(fn.parent) && ts.isIdentifier(fn.parent.name)) {
|
|
16654
|
+
callee = fn.parent;
|
|
16655
|
+
label = fn.parent.name.text;
|
|
16656
|
+
} else if (ts.isConstructorDeclaration(fn) && ts.isClassDeclaration(fn.parent) && fn.parent.name && ts.isSourceFile(fn.parent.parent) && !inj?.diDecorator(fn.parent)) {
|
|
16657
|
+
callee = fn.parent;
|
|
16658
|
+
label = `new ${fn.parent.name.text}`;
|
|
16659
|
+
}
|
|
16660
|
+
const hit = paramSeen.get(p);
|
|
16661
|
+
if (hit) {
|
|
16662
|
+
for (const n2 of hit.notes) note(n2.kind, n2.detail);
|
|
16663
|
+
if (hit.closed) closed = true;
|
|
16664
|
+
if (hit.value) flowed = true;
|
|
16665
|
+
return hit.value;
|
|
16666
|
+
}
|
|
16667
|
+
const outer = { notes, closed, flowed };
|
|
16668
|
+
notes = [];
|
|
16669
|
+
closed = false;
|
|
16670
|
+
const value = paramOnce(p, fn, callee, label, depth);
|
|
16671
|
+
const said = notes;
|
|
16672
|
+
paramSeen.set(p, { value, notes: said, closed });
|
|
16673
|
+
const used = closed;
|
|
16674
|
+
notes = outer.notes;
|
|
16675
|
+
closed = outer.closed || !!value && used;
|
|
16676
|
+
flowed = outer.flowed;
|
|
16677
|
+
for (const n2 of said) note(n2.kind, n2.detail);
|
|
16678
|
+
if (value) flowed = true;
|
|
16679
|
+
return value;
|
|
16680
|
+
};
|
|
16681
|
+
const paramOnce = (p, fn, callee, label, depth) => {
|
|
16682
|
+
if (!callee || reassigned(p) || usesArguments(fn)) return null;
|
|
16683
|
+
const found = callers2.callersOf(callee);
|
|
16684
|
+
if ("why" in found || found.calls.length > CALLS) return null;
|
|
16685
|
+
const index = argIndex(fn, p);
|
|
16686
|
+
inParam.add(p);
|
|
16687
|
+
try {
|
|
16688
|
+
const out2 = [];
|
|
16689
|
+
for (const { call, args } of found.calls) {
|
|
16690
|
+
if (args.slice(0, index + 1).some((a) => ts.isSpreadElement(a))) return null;
|
|
16691
|
+
const arg = args[index];
|
|
16692
|
+
const passed = arg && !(ts.isIdentifier(arg) && arg.text === "undefined") ? arg : p.initializer ?? null;
|
|
16693
|
+
if (!passed) continue;
|
|
16694
|
+
const v = valueOf(passed, call, depth + 1, null);
|
|
16695
|
+
if (!v) return null;
|
|
16696
|
+
out2.push(...v);
|
|
16697
|
+
}
|
|
16698
|
+
if (found.closedWorld) {
|
|
16699
|
+
closed = true;
|
|
16700
|
+
note(
|
|
16701
|
+
"closed-world",
|
|
16702
|
+
`${label} may be called from outside its module; the project is analyzed as a closed world (analysis.closedWorld), so the calls it has here are all there are`
|
|
16703
|
+
);
|
|
16704
|
+
}
|
|
16705
|
+
const first = found.calls[0];
|
|
16706
|
+
note(
|
|
16707
|
+
"parameter",
|
|
16708
|
+
`${p.name.text} is what ${label} is passed${first ? ` (${found.calls.length} call${found.calls.length === 1 ? "" : "s"}, as at ${where(first.call)})` : " (it is never called)"}: ${describe4(out2)}`
|
|
16709
|
+
);
|
|
16710
|
+
flowed = true;
|
|
16711
|
+
return out2.map(share);
|
|
16712
|
+
} finally {
|
|
16713
|
+
inParam.delete(p);
|
|
16714
|
+
}
|
|
16715
|
+
};
|
|
16716
|
+
const memberNode = (definer, name) => {
|
|
16717
|
+
for (const m of classDecl(definer)?.members ?? []) {
|
|
16718
|
+
if (keyOf2(m.name) !== name || ts.getCombinedModifierFlags(m) & ts.ModifierFlags.Static)
|
|
16719
|
+
continue;
|
|
16720
|
+
if (ts.isMethodDeclaration(m) && m.body) return m;
|
|
16721
|
+
if (ts.isPropertyDeclaration(m) && m.initializer) {
|
|
16722
|
+
const f = unwrap(m.initializer);
|
|
16723
|
+
if (ts.isArrowFunction(f) || ts.isFunctionExpression(f)) return f;
|
|
16724
|
+
}
|
|
16725
|
+
}
|
|
16726
|
+
return null;
|
|
16727
|
+
};
|
|
16728
|
+
const methodOf = (a, name, depth) => {
|
|
16729
|
+
if (a.kind === "fn" || memberWritten(a, name)) return null;
|
|
16730
|
+
if (a.kind === "instance") {
|
|
16731
|
+
const body = graph.bodyOf(a.cls, name);
|
|
16732
|
+
if (body === "none") return "none";
|
|
16733
|
+
if (typeof body === "string") return null;
|
|
16734
|
+
const node2 = memberNode(body.definer, name);
|
|
16735
|
+
return node2 ? { fn: node2, self: [a] } : null;
|
|
16736
|
+
}
|
|
16737
|
+
if (a.open) return null;
|
|
16738
|
+
const m = a.members.get(name);
|
|
16739
|
+
if (!m) return OBJECT_MEMBERS.has(name) ? null : "none";
|
|
16740
|
+
if (ts.isMethodDeclaration(m)) return m.body ? { fn: m, self: [a] } : null;
|
|
16741
|
+
const v = valueOf(m, a.at, depth + 1, null);
|
|
16742
|
+
const f = v?.length === 1 && v[0].kind === "fn" ? v[0].at : null;
|
|
16743
|
+
if (!f || !isFunctionNode(f)) return null;
|
|
16744
|
+
return { fn: f, self: ts.isArrowFunction(f) ? null : [a] };
|
|
16745
|
+
};
|
|
16746
|
+
const returnedBy = (call, awaited, depth, self2 = null) => {
|
|
16747
|
+
if (depth > DEPTH || ++work > WORK) return null;
|
|
16748
|
+
const f = calleeOf(call);
|
|
16749
|
+
if (f) return returnsOf(f, awaited, depth, null);
|
|
16750
|
+
const callee = unwrap(call.expression);
|
|
16751
|
+
if (!ts.isPropertyAccessExpression(callee)) return null;
|
|
16752
|
+
const recv = valueOf(callee.expression, call, depth, self2);
|
|
16753
|
+
if (!recv) return null;
|
|
16754
|
+
const out2 = [];
|
|
16755
|
+
for (const a of recv) {
|
|
16756
|
+
const m = methodOf(a, callee.name.text, depth);
|
|
16757
|
+
if (m === null) return null;
|
|
16758
|
+
if (m === "none") continue;
|
|
16759
|
+
const v = returnsOf(m.fn, awaited, depth, m.self);
|
|
16760
|
+
if (!v) return null;
|
|
16761
|
+
out2.push(...v);
|
|
16762
|
+
}
|
|
16763
|
+
flowed = true;
|
|
16764
|
+
note("returned", `${short(callee.expression)}.${callee.name.text}(\u2026) returns ${describe4(out2)}`);
|
|
16765
|
+
return out2;
|
|
16766
|
+
};
|
|
16767
|
+
const returnsOf = (f, awaited, depth, self2) => {
|
|
16768
|
+
if (f.asteriskToken || !f.body) return null;
|
|
16769
|
+
const isAsync2 = (ts.getCombinedModifierFlags(f) & ts.ModifierFlags.Async) !== 0;
|
|
16770
|
+
if (isAsync2 && !awaited) return null;
|
|
16771
|
+
const body = f.body;
|
|
16772
|
+
if (!ts.isBlock(body)) {
|
|
16773
|
+
const v = valueOf(body, f, depth, self2);
|
|
16774
|
+
return v && (!awaited || !thenable(v)) ? v : null;
|
|
16775
|
+
}
|
|
16776
|
+
const alts = [];
|
|
16777
|
+
let known = true;
|
|
16778
|
+
const visit2 = (node2) => {
|
|
16779
|
+
if (!known || node2 !== body && (ts.isFunctionLike(node2) || ts.isClassLike(node2))) return;
|
|
16780
|
+
if (ts.isReturnStatement(node2)) {
|
|
16781
|
+
if (!node2.expression) return;
|
|
16782
|
+
const v = valueOf(node2.expression, f, depth, self2);
|
|
16783
|
+
if (v === null) known = false;
|
|
16784
|
+
else alts.push(...v);
|
|
16785
|
+
return;
|
|
16786
|
+
}
|
|
16787
|
+
ts.forEachChild(node2, visit2);
|
|
16788
|
+
};
|
|
16789
|
+
visit2(body);
|
|
16790
|
+
return known && (!awaited || !thenable(alts)) ? alts : null;
|
|
16791
|
+
};
|
|
16792
|
+
const thenable = (alts) => alts.some(
|
|
16793
|
+
(a) => a.kind === "object" ? a.open || a.members.has("then") : a.kind === "instance" && graph.inherited(a.cls, "then", false) !== "none"
|
|
16794
|
+
);
|
|
16795
|
+
const valueOf = (expr, scope2, depth, self2 = null) => {
|
|
16796
|
+
if (depth > DEPTH || ++work > WORK) return null;
|
|
16797
|
+
const e = unwrap(expr);
|
|
16798
|
+
if (e.kind === ts.SyntaxKind.ThisKeyword) return self2;
|
|
16799
|
+
if (ts.isAwaitExpression(e)) {
|
|
16800
|
+
const inner = unwrap(e.expression);
|
|
16801
|
+
return ts.isCallExpression(inner) ? returnedBy(inner, true, depth + 1, self2) : valueOf(inner, scope2, depth, self2);
|
|
16802
|
+
}
|
|
16803
|
+
if (ts.isConditionalExpression(e)) {
|
|
16804
|
+
const a = valueOf(e.whenTrue, scope2, depth, self2);
|
|
16805
|
+
const b = a && valueOf(e.whenFalse, scope2, depth, self2);
|
|
16806
|
+
return a && b ? [...a, ...b] : null;
|
|
16807
|
+
}
|
|
16808
|
+
if (ts.isArrowFunction(e) || ts.isFunctionExpression(e)) {
|
|
16809
|
+
const fn = functionOf(e);
|
|
16810
|
+
return fn ? [{ kind: "fn", fn, at: e }] : null;
|
|
16811
|
+
}
|
|
16812
|
+
if (ts.isNewExpression(e)) {
|
|
16813
|
+
const cls = classOf(e.expression);
|
|
16814
|
+
if (!cls) return null;
|
|
16815
|
+
if (isSoundClass(cls)) return [{ kind: "instance", cls, at: e, shared: false }];
|
|
16816
|
+
return inj && isSoundClass(cls, false) ? [{ kind: "instance", cls, at: e, shared: true }] : null;
|
|
16817
|
+
}
|
|
16818
|
+
if (ts.isPropertyAccessExpression(e)) {
|
|
16819
|
+
const name = e.name.text;
|
|
16820
|
+
const recv = unwrap(e.expression);
|
|
16821
|
+
if (recv.kind === ts.SyntaxKind.ThisKeyword && !self2) {
|
|
16822
|
+
const owner = thisClass(recv);
|
|
16823
|
+
if (!owner || !inj || inj.thisBorrowed(recv, siteTest)) return null;
|
|
16824
|
+
return fieldOf(owner, name, false, false, depth + 1);
|
|
16825
|
+
}
|
|
16826
|
+
const base2 = valueOf(recv, scope2, depth + 1, self2);
|
|
16827
|
+
if (!base2) return null;
|
|
16828
|
+
const out2 = [];
|
|
16829
|
+
for (const a of base2) {
|
|
16830
|
+
if (a.kind === "instance") {
|
|
16831
|
+
const v = fieldOf(
|
|
16832
|
+
a.cls,
|
|
16833
|
+
name,
|
|
16834
|
+
true,
|
|
16835
|
+
!!a.injected,
|
|
16836
|
+
depth + 1,
|
|
16837
|
+
ts.isNewExpression(a.at) ? a.at : void 0
|
|
16838
|
+
);
|
|
16839
|
+
if (!v) return null;
|
|
16840
|
+
out2.push(...v);
|
|
16841
|
+
} else if (a.kind === "object") {
|
|
16842
|
+
if (a.open || memberWritten(a, name)) return null;
|
|
16843
|
+
const m = a.members.get(name);
|
|
16844
|
+
if (!m) {
|
|
16845
|
+
if (OBJECT_MEMBERS.has(name)) return null;
|
|
16846
|
+
continue;
|
|
16847
|
+
}
|
|
16848
|
+
const v = ts.isMethodDeclaration(m) ? (() => {
|
|
16849
|
+
const fn = functionOf(m);
|
|
16850
|
+
return fn ? [{ kind: "fn", fn, at: m }] : null;
|
|
16851
|
+
})() : valueOf(m, a.at, depth + 1, null);
|
|
16852
|
+
if (!v) return null;
|
|
16853
|
+
out2.push(...v.map(share));
|
|
16854
|
+
} else return null;
|
|
16855
|
+
}
|
|
16856
|
+
flowed = true;
|
|
16857
|
+
return out2;
|
|
16858
|
+
}
|
|
16859
|
+
if (ts.isObjectLiteralExpression(e)) {
|
|
16860
|
+
const members2 = /* @__PURE__ */ new Map();
|
|
16861
|
+
let isOpen = false;
|
|
16862
|
+
for (const p of e.properties) {
|
|
16863
|
+
const key = p.name && (ts.isIdentifier(p.name) || ts.isStringLiteralLike(p.name)) ? p.name.text : null;
|
|
16864
|
+
if (ts.isSpreadAssignment(p) || key === null || key === "__proto__") isOpen = true;
|
|
16865
|
+
else if (ts.isPropertyAssignment(p)) members2.set(key, p.initializer);
|
|
16866
|
+
else if (ts.isShorthandPropertyAssignment(p)) members2.set(key, p.name);
|
|
16867
|
+
else if (ts.isMethodDeclaration(p)) members2.set(key, p);
|
|
16868
|
+
else isOpen = true;
|
|
16869
|
+
if (!isOpen && !thisSafe(ts.isMethodDeclaration(p) ? p : ts.isPropertyAssignment(p) ? p.initializer : p.name))
|
|
16870
|
+
isOpen = true;
|
|
16871
|
+
}
|
|
16872
|
+
return [{ kind: "object", members: members2, open: isOpen, at: e }];
|
|
16873
|
+
}
|
|
16874
|
+
if (ts.isCallExpression(e)) {
|
|
16875
|
+
const from = importedAs(e.expression);
|
|
16876
|
+
if (from?.pkg === "react" && from.name === "useCallback" && e.arguments[0])
|
|
16877
|
+
return valueOf(e.arguments[0], scope2, depth, self2);
|
|
16878
|
+
if (from?.pkg === "react" && from.name === "useMemo" && e.arguments[0]) {
|
|
16879
|
+
const factory = unwrap(e.arguments[0]);
|
|
16880
|
+
if (!ts.isArrowFunction(factory) && !ts.isFunctionExpression(factory)) return null;
|
|
16881
|
+
if (!ts.isBlock(factory.body)) return valueOf(factory.body, factory, depth, self2);
|
|
16882
|
+
const ret = factory.body.statements.length === 1 ? factory.body.statements[0] : void 0;
|
|
16883
|
+
return ret && ts.isReturnStatement(ret) && ret.expression ? valueOf(ret.expression, factory, depth, self2) : null;
|
|
16884
|
+
}
|
|
16885
|
+
return returnedBy(e, false, depth + 1, self2);
|
|
16886
|
+
}
|
|
16887
|
+
if (ts.isIdentifier(e)) {
|
|
16888
|
+
const sym = ts.isShorthandPropertyAssignment(e.parent) && e.parent.name === e ? checker.getShorthandAssignmentValueSymbol(e.parent) : symbolOf(e);
|
|
16889
|
+
const d = sym?.valueDeclaration;
|
|
16890
|
+
if (!d || !fileIdOf2(d.getSourceFile())) return null;
|
|
16891
|
+
if (ts.isFunctionDeclaration(d)) {
|
|
16892
|
+
const fn = isFixedFunction(d) ? functionOf(d) : void 0;
|
|
16893
|
+
return fn ? [{ kind: "fn", fn, at: d }] : null;
|
|
16894
|
+
}
|
|
16895
|
+
if (ts.isParameter(d)) return paramValue(d, depth + 1);
|
|
16896
|
+
if (!isConst(d)) return null;
|
|
16897
|
+
const v = d;
|
|
16898
|
+
if (!v.initializer || !ts.isIdentifier(v.name)) return null;
|
|
16899
|
+
const init = unwrap(v.initializer);
|
|
16900
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) return valueOf(init, scope2, depth + 1);
|
|
16901
|
+
const val = valueOf(init, scope2, depth + 1);
|
|
16902
|
+
if (!val || val.every((a) => a.kind === "fn")) return val;
|
|
16903
|
+
const where2 = ts.findAncestor(v, (n2) => isFunctionNode(n2) || ts.isSourceFile(n2));
|
|
16904
|
+
if (!where2) return null;
|
|
16905
|
+
if (!ts.isSourceFile(where2) && onlyRead(v.name, where2, e)) return val;
|
|
16906
|
+
if (ts.isSourceFile(where2) && exported(v)) return null;
|
|
16907
|
+
return inj ? val.map(share) : null;
|
|
16908
|
+
}
|
|
16909
|
+
return null;
|
|
16910
|
+
};
|
|
16911
|
+
const runs = (value, member, depth = 0) => {
|
|
16912
|
+
if (!value || depth > DEPTH) return null;
|
|
16913
|
+
const outFns = /* @__PURE__ */ new Map();
|
|
16914
|
+
for (const a of value) {
|
|
16915
|
+
if (member === null) {
|
|
16916
|
+
if (a.kind !== "fn") return null;
|
|
16917
|
+
outFns.set(a.fn.id, { fn: a.fn, at: a.at });
|
|
16918
|
+
continue;
|
|
16919
|
+
}
|
|
16920
|
+
if (memberWritten(a, member)) return null;
|
|
16921
|
+
if (a.kind === "instance") {
|
|
16922
|
+
const body = graph.bodyOf(a.cls, member);
|
|
16923
|
+
if (body === "none") continue;
|
|
16924
|
+
if (typeof body === "string") return null;
|
|
16925
|
+
outFns.set(body.fn.id, { fn: body.fn, at: a.at });
|
|
16926
|
+
} else if (a.kind === "object") {
|
|
16927
|
+
if (a.open) return null;
|
|
16928
|
+
const m = a.members.get(member);
|
|
16929
|
+
if (!m) {
|
|
16930
|
+
if (OBJECT_MEMBERS.has(member)) return null;
|
|
16931
|
+
continue;
|
|
16932
|
+
}
|
|
16933
|
+
const inner = ts.isMethodDeclaration(m) ? (() => {
|
|
16934
|
+
const fn = functionOf(m);
|
|
16935
|
+
return fn ? [{ fn, at: m }] : null;
|
|
16936
|
+
})() : runs(valueOf(m, a.at, depth), null, depth + 1);
|
|
16937
|
+
if (!inner) return null;
|
|
16938
|
+
for (const x of inner) outFns.set(x.fn.id, x);
|
|
16939
|
+
} else return null;
|
|
16940
|
+
}
|
|
16941
|
+
return [...outFns.values()];
|
|
16942
|
+
};
|
|
16943
|
+
const subjectOf = (call) => {
|
|
16944
|
+
const callee = unwrap(call.expression);
|
|
16945
|
+
let member = null;
|
|
16946
|
+
let target2 = callee;
|
|
16947
|
+
if (ts.isPropertyAccessExpression(callee)) {
|
|
16948
|
+
member = callee.name.text;
|
|
16949
|
+
target2 = unwrap(callee.expression);
|
|
16950
|
+
}
|
|
16951
|
+
const followed = () => ({
|
|
16952
|
+
value: valueOf(target2, call, 0, null),
|
|
16953
|
+
member,
|
|
16954
|
+
producer: null,
|
|
16955
|
+
binding: null,
|
|
16956
|
+
target: target2
|
|
16957
|
+
});
|
|
16958
|
+
if (ts.isAwaitExpression(target2) || ts.isCallExpression(target2)) {
|
|
16959
|
+
const awaited2 = ts.isAwaitExpression(target2);
|
|
16960
|
+
const c2 = unwrap(awaited2 ? target2.expression : target2);
|
|
16961
|
+
if (!ts.isCallExpression(c2)) return null;
|
|
16962
|
+
if (!calleeOf(c2)) return followed();
|
|
16963
|
+
return { value: returnedBy(c2, awaited2, 0), member, producer: c2, binding: null, target: target2 };
|
|
16964
|
+
}
|
|
16965
|
+
if (!ts.isIdentifier(target2)) return followed();
|
|
16966
|
+
const d = checker.getSymbolAtLocation(target2)?.valueDeclaration;
|
|
16967
|
+
if (!d) return null;
|
|
16968
|
+
let decl;
|
|
16969
|
+
let property = null;
|
|
16970
|
+
if (ts.isVariableDeclaration(d) && ts.isIdentifier(d.name)) decl = d;
|
|
16971
|
+
else if (ts.isBindingElement(d) && ts.isObjectBindingPattern(d.parent) && !d.initializer && !d.dotDotDotToken) {
|
|
16972
|
+
const key = d.propertyName ?? d.name;
|
|
16973
|
+
if (!ts.isIdentifier(key) && !ts.isStringLiteral(key)) return null;
|
|
16974
|
+
if (d.parent.elements.some((x) => x.dotDotDotToken)) return null;
|
|
16975
|
+
const keyOfBinding = (x) => {
|
|
16976
|
+
const k = x.propertyName ?? x.name;
|
|
16977
|
+
return ts.isIdentifier(k) || ts.isStringLiteral(k) ? k.text : null;
|
|
16978
|
+
};
|
|
16979
|
+
if (d.parent.elements.some((x) => x !== d && keyOfBinding(x) === key.text)) return null;
|
|
16980
|
+
property = key.text;
|
|
16981
|
+
decl = d.parent.parent;
|
|
16982
|
+
if (!ts.isVariableDeclaration(decl)) return null;
|
|
16983
|
+
}
|
|
16984
|
+
if (!decl) return property === null && ts.isParameter(d) ? followed() : null;
|
|
16985
|
+
if (!isConst(decl) || !decl.initializer) return null;
|
|
16986
|
+
const init = unwrap(decl.initializer);
|
|
16987
|
+
const awaited = ts.isAwaitExpression(init);
|
|
16988
|
+
const c = unwrap(awaited ? init.expression : init);
|
|
16989
|
+
if (!ts.isCallExpression(c) || !calleeOf(c)) return property === null ? followed() : null;
|
|
16990
|
+
const binding2 = target2.text;
|
|
16991
|
+
let value = returnedBy(c, awaited, 0);
|
|
16992
|
+
if (!value) return { value, member, producer: c, binding: binding2, target: target2 };
|
|
16993
|
+
if (property !== null) {
|
|
16994
|
+
const picked = [];
|
|
16995
|
+
for (const a of value) {
|
|
16996
|
+
if (a.kind !== "object" || a.open) return { value: null, member, producer: c, binding: binding2, target: target2 };
|
|
16997
|
+
const m = a.members.get(property);
|
|
16998
|
+
if (!m) {
|
|
16999
|
+
if (OBJECT_MEMBERS.has(property)) return { value: null, member, producer: c, binding: binding2, target: target2 };
|
|
17000
|
+
continue;
|
|
17001
|
+
}
|
|
17002
|
+
const v = ts.isMethodDeclaration(m) ? (() => {
|
|
17003
|
+
const fn = functionOf(m);
|
|
17004
|
+
return fn ? [{ kind: "fn", fn, at: m }] : null;
|
|
17005
|
+
})() : valueOf(m, a.at, 1);
|
|
17006
|
+
if (!v) return { value: null, member, producer: c, binding: binding2, target: target2 };
|
|
17007
|
+
picked.push(...v);
|
|
17008
|
+
}
|
|
17009
|
+
value = picked;
|
|
17010
|
+
if (member !== null && value.some((a) => a.kind !== "fn")) {
|
|
17011
|
+
const scope2 = ts.findAncestor(decl, (n2) => isFunctionNode(n2) || ts.isSourceFile(n2));
|
|
17012
|
+
const name = d.name;
|
|
17013
|
+
if (!scope2 || !ts.isIdentifier(name) || ts.isSourceFile(scope2) && exported(decl) || !onlyRead(name, scope2))
|
|
17014
|
+
value = inj ? value.map(share) : null;
|
|
17015
|
+
}
|
|
17016
|
+
} else if (member !== null && value.some((a) => a.kind !== "fn")) {
|
|
17017
|
+
const scope2 = ts.findAncestor(decl, (n2) => isFunctionNode(n2) || ts.isSourceFile(n2));
|
|
17018
|
+
if (!scope2 || ts.isSourceFile(scope2) && exported(decl) || !onlyRead(decl.name, scope2))
|
|
17019
|
+
value = inj ? value.map(share) : null;
|
|
17020
|
+
}
|
|
17021
|
+
return { value, member, producer: c, binding: binding2, target: target2 };
|
|
17022
|
+
};
|
|
17023
|
+
for (const r of open3) {
|
|
17024
|
+
const site = sites.get(r.site);
|
|
17025
|
+
if (!site || site.reason !== "dynamic-dispatch" && site.reason !== "imported-symbol") continue;
|
|
17026
|
+
const sf = s.sourceFile(site.file);
|
|
17027
|
+
if (!sf) continue;
|
|
17028
|
+
const call = callAt2(ts, sf, toUtf16(sf, site.evidence.span.start.offset), site.expression);
|
|
17029
|
+
if (!call) continue;
|
|
17030
|
+
notes = [];
|
|
17031
|
+
flowed = false;
|
|
17032
|
+
closed = false;
|
|
17033
|
+
work = 0;
|
|
17034
|
+
siteTest = isTestPath(pathOf9(site.file));
|
|
17035
|
+
const subject = subjectOf(call);
|
|
17036
|
+
if (!subject?.value) continue;
|
|
17037
|
+
const fns = runs(subject.value, subject.member);
|
|
17038
|
+
if (!fns || fns.length === 0 || fns.some((f) => f.fn.file === site.file)) continue;
|
|
17039
|
+
const what = subject.member ?? subject.binding ?? "its result";
|
|
17040
|
+
const stop = r.steps.findIndex((x) => OPEN_STEPS.has(x.kind));
|
|
17041
|
+
const prior = r.steps.slice(0, stop < 0 ? r.steps.length : stop);
|
|
17042
|
+
const producer = subject.producer;
|
|
17043
|
+
const producerName = producer?.expression.getText(sf);
|
|
17044
|
+
const how = producer ? [
|
|
17045
|
+
{
|
|
17046
|
+
kind: "returned",
|
|
17047
|
+
detail: subject.binding ? `${subject.binding} holds what ${producerName} returns (${where(producer)})` : `${producerName} is called here, and what it returns is followed`
|
|
17048
|
+
},
|
|
17049
|
+
...notes
|
|
17050
|
+
] : [
|
|
17051
|
+
...notes,
|
|
17052
|
+
{
|
|
17053
|
+
kind: "value",
|
|
17054
|
+
detail: `${short(subject.target)} is ${describe4(subject.value)}, and nothing else`
|
|
17055
|
+
}
|
|
17056
|
+
];
|
|
17057
|
+
const via = closed ? "closed-world" : flowed || !producer ? "flow" : "returned";
|
|
17058
|
+
const names = fns.map((f) => `${f.fn.qualifiedName} (${pathOf9(f.fn.file)})`);
|
|
17059
|
+
if (fns.length === 1) {
|
|
17060
|
+
const fn = fns[0].fn;
|
|
17061
|
+
out.resolutions.push({
|
|
17062
|
+
type: "SiteResolution",
|
|
17063
|
+
id: r.id,
|
|
17064
|
+
site: r.site,
|
|
17065
|
+
file: r.file,
|
|
17066
|
+
status: "linked",
|
|
17067
|
+
target: fn.id,
|
|
17068
|
+
externalPackage: null,
|
|
17069
|
+
via,
|
|
17070
|
+
importOutcome: null,
|
|
17071
|
+
steps: [...prior, ...how, { kind: "returned-value", detail: `${what} is ${names[0]}` }],
|
|
17072
|
+
evidence: r.evidence
|
|
17073
|
+
});
|
|
17074
|
+
out.edges.push({
|
|
17075
|
+
type: "CALLS_CROSS_FILE",
|
|
17076
|
+
id: ids.linkedCall(site.id),
|
|
17077
|
+
from: site.enclosingFn,
|
|
17078
|
+
to: fn.id,
|
|
17079
|
+
form: "plain",
|
|
17080
|
+
resolution: "static-exact",
|
|
17081
|
+
confidence: 1,
|
|
17082
|
+
evidence: site.evidence,
|
|
17083
|
+
via
|
|
17084
|
+
});
|
|
17085
|
+
continue;
|
|
17086
|
+
}
|
|
17087
|
+
const candidates = fns.flatMap((f) => {
|
|
17088
|
+
const basis = evidenceOf2(f.at);
|
|
17089
|
+
return basis ? [{ function: f.fn.id, basis }] : [];
|
|
17090
|
+
});
|
|
17091
|
+
if (candidates.length !== fns.length) continue;
|
|
17092
|
+
out.resolutions.push({
|
|
17093
|
+
type: "SiteResolution",
|
|
17094
|
+
id: r.id,
|
|
17095
|
+
site: r.site,
|
|
17096
|
+
file: r.file,
|
|
17097
|
+
status: "unresolved",
|
|
17098
|
+
target: null,
|
|
17099
|
+
externalPackage: null,
|
|
17100
|
+
importOutcome: null,
|
|
17101
|
+
steps: [
|
|
17102
|
+
...prior,
|
|
17103
|
+
...how,
|
|
17104
|
+
{
|
|
17105
|
+
kind: "candidates",
|
|
17106
|
+
detail: `${what} is one of ${fns.length} functions, and nothing else: ${names.join(", ")}`
|
|
17107
|
+
}
|
|
17108
|
+
],
|
|
17109
|
+
candidates,
|
|
17110
|
+
evidence: r.evidence
|
|
17111
|
+
});
|
|
17112
|
+
}
|
|
17113
|
+
return out;
|
|
17114
|
+
}
|
|
17115
|
+
function callAt2(ts, sf, pos, expression) {
|
|
17116
|
+
let found;
|
|
17117
|
+
const visit2 = (node2) => {
|
|
17118
|
+
if (found || node2.end <= pos || node2.getStart(sf) > pos) return;
|
|
17119
|
+
if (ts.isCallExpression(node2) && node2.getStart(sf) === pos && node2.expression.getText(sf).replace(/\s+/g, "") === expression.replace(/\s+/g, "")) {
|
|
17120
|
+
found = node2;
|
|
17121
|
+
return;
|
|
17122
|
+
}
|
|
17123
|
+
ts.forEachChild(node2, visit2);
|
|
17124
|
+
};
|
|
17125
|
+
visit2(sf);
|
|
17126
|
+
return found;
|
|
17127
|
+
}
|
|
17128
|
+
var DEPTH, CALLS, WORK, PROTOTYPE_READERS, OPEN_STEPS, OBJECT_MEMBERS;
|
|
17129
|
+
var init_returns = __esm({
|
|
17130
|
+
"packages/stage2/src/returns.ts"() {
|
|
17131
|
+
"use strict";
|
|
17132
|
+
init_src();
|
|
17133
|
+
init_callers();
|
|
17134
|
+
init_typegraph();
|
|
17135
|
+
DEPTH = 8;
|
|
17136
|
+
CALLS = 64;
|
|
17137
|
+
WORK = 4e3;
|
|
17138
|
+
PROTOTYPE_READERS = /* @__PURE__ */ new Set([
|
|
17139
|
+
"getOwnPropertyNames",
|
|
17140
|
+
"getOwnPropertyDescriptors",
|
|
17141
|
+
"getOwnPropertyDescriptor",
|
|
17142
|
+
"getOwnPropertySymbols",
|
|
17143
|
+
"getPrototypeOf",
|
|
17144
|
+
"keys",
|
|
17145
|
+
"entries",
|
|
17146
|
+
"values",
|
|
17147
|
+
"hasOwn",
|
|
17148
|
+
"ownKeys",
|
|
17149
|
+
"has",
|
|
17150
|
+
"getMetadata",
|
|
17151
|
+
"getOwnMetadata",
|
|
17152
|
+
"getMetadataKeys",
|
|
17153
|
+
"getOwnMetadataKeys",
|
|
17154
|
+
"hasMetadata",
|
|
17155
|
+
"hasOwnMetadata"
|
|
17156
|
+
]);
|
|
17157
|
+
OPEN_STEPS = /* @__PURE__ */ new Set([
|
|
17158
|
+
"type",
|
|
17159
|
+
"candidates",
|
|
17160
|
+
"untyped",
|
|
17161
|
+
"undeclared",
|
|
17162
|
+
"no-body",
|
|
17163
|
+
"bodies-unknown",
|
|
17164
|
+
"default",
|
|
17165
|
+
"injection",
|
|
17166
|
+
// Where linking stopped (#237): a member of what an import gives.
|
|
17167
|
+
"not-a-function",
|
|
17168
|
+
"instance-member",
|
|
17169
|
+
"written",
|
|
17170
|
+
"unknown-wrapper",
|
|
17171
|
+
"member",
|
|
17172
|
+
"computed"
|
|
17173
|
+
]);
|
|
17174
|
+
OBJECT_MEMBERS = new Set(Object.getOwnPropertyNames(Object.prototype));
|
|
17175
|
+
}
|
|
17176
|
+
});
|
|
17177
|
+
|
|
17178
|
+
// packages/stage2/src/closed.ts
|
|
17179
|
+
function closedWorldResolutions(input2, graph, s, inj = null) {
|
|
17180
|
+
const out = { resolutions: [], edges: [] };
|
|
17181
|
+
if (!s?.checker || !s.program) return out;
|
|
17182
|
+
const { ts, checker, program, fileIdOf: fileIdOf2, toByte, toUtf16 } = s;
|
|
17183
|
+
const pathOf9 = (file2) => file2.slice(file2.indexOf(":") + 1);
|
|
17184
|
+
const open3 = input2.resolutions.filter(
|
|
17185
|
+
(r) => r.status === "unresolved" && r.importOutcome === null && (!!r.candidates?.length && r.exhaustive === false || !r.candidates?.length && r.steps.at(-1)?.kind === "bodies-unknown") && !isTestPath(pathOf9(r.file))
|
|
17186
|
+
);
|
|
17187
|
+
if (open3.length === 0) return out;
|
|
17188
|
+
const sites = new Map(input2.sites.map((x) => [x.id, x]));
|
|
17189
|
+
const where = (node2) => {
|
|
17190
|
+
const sf = node2.getSourceFile();
|
|
17191
|
+
const file2 = fileIdOf2(sf);
|
|
17192
|
+
return `${file2 ? pathOf9(file2) : sf.fileName}:${sf.getLineAndCharacterOfPosition(node2.getStart(sf)).line + 1}`;
|
|
17193
|
+
};
|
|
17194
|
+
const evidenceOf2 = (node2) => {
|
|
17195
|
+
const sf = node2.getSourceFile();
|
|
17196
|
+
const file2 = fileIdOf2(sf);
|
|
17197
|
+
if (!file2) return null;
|
|
17198
|
+
const pos = (p) => {
|
|
17199
|
+
const lc = sf.getLineAndCharacterOfPosition(p);
|
|
17200
|
+
return { line: lc.line + 1, column: lc.character + 1, offset: toByte(sf, p) };
|
|
17201
|
+
};
|
|
17202
|
+
return {
|
|
17203
|
+
file: file2,
|
|
17204
|
+
span: { start: pos(node2.getStart(sf)), end: pos(node2.end) },
|
|
17205
|
+
commit: input2.revisions.get(file2.slice(0, file2.indexOf(":"))) ?? ""
|
|
17206
|
+
};
|
|
17207
|
+
};
|
|
17208
|
+
const fnByEnd = /* @__PURE__ */ new Map();
|
|
17209
|
+
for (const fn of input2.functions) {
|
|
17210
|
+
const k = `${fn.file}\0${fn.evidence.span.end.offset}`;
|
|
17211
|
+
const list = fnByEnd.get(k);
|
|
17212
|
+
if (list) list.push(fn);
|
|
17213
|
+
else fnByEnd.set(k, [fn]);
|
|
17214
|
+
}
|
|
17215
|
+
const functionOf = (node2) => {
|
|
17216
|
+
const sf = node2.getSourceFile();
|
|
17217
|
+
const file2 = fileIdOf2(sf);
|
|
17218
|
+
if (!file2) return void 0;
|
|
17219
|
+
const start = toByte(sf, node2.getStart(sf));
|
|
17220
|
+
return (fnByEnd.get(`${file2}\0${toByte(sf, node2.end)}`) ?? []).filter((f) => f.evidence.span.start.offset >= start).sort((a, b) => a.evidence.span.start.offset - b.evidence.span.start.offset)[0];
|
|
17221
|
+
};
|
|
17222
|
+
const keyOf2 = (n2) => n2 && (ts.isIdentifier(n2) || ts.isStringLiteralLike(n2) || ts.isPrivateIdentifier(n2)) ? n2.text : null;
|
|
17223
|
+
let made;
|
|
17224
|
+
const scan = () => {
|
|
17225
|
+
if (made) return made;
|
|
17226
|
+
const m = { classes: [], literals: [], casts: [], guards: [], generic: [] };
|
|
17227
|
+
for (const sf of program.getSourceFiles()) {
|
|
17228
|
+
const file2 = fileIdOf2(sf);
|
|
17229
|
+
if (!file2 || isTestPath(pathOf9(file2))) continue;
|
|
17230
|
+
const visit2 = (node2) => {
|
|
17231
|
+
if (ts.isClassDeclaration(node2) || ts.isClassExpression(node2)) {
|
|
17232
|
+
const abstract = (ts.getCombinedModifierFlags(node2) & ts.ModifierFlags.Abstract) !== 0;
|
|
17233
|
+
const sym = node2.name ? checker.getSymbolAtLocation(node2.name) : checker.getTypeAtLocation(node2).getSymbol();
|
|
17234
|
+
const type = sym ? checker.getDeclaredTypeOfSymbol(sym) : void 0;
|
|
17235
|
+
const top = ts.isClassDeclaration(node2) && !!node2.name && ts.isSourceFile(node2.parent);
|
|
17236
|
+
const decl = top ? { file: file2, name: node2.name.text } : null;
|
|
17237
|
+
if (type && !abstract)
|
|
17238
|
+
m.classes.push({ decl: decl && graph.declared(decl) ? decl : null, node: node2, type, file: file2 });
|
|
17239
|
+
} else if (ts.isObjectLiteralExpression(node2)) m.literals.push({ node: node2, file: file2 });
|
|
17240
|
+
else if (ts.isAsExpression(node2) || ts.isTypeAssertionExpression(node2)) {
|
|
17241
|
+
const type = checker.getTypeFromTypeNode(node2.type);
|
|
17242
|
+
const enclosing = ts.findAncestor(
|
|
17243
|
+
node2.parent,
|
|
17244
|
+
(n2) => ts.isFunctionLike(n2) && (!!n2.typeParameters?.length || !!ts.findAncestor(n2.parent, (c) => ts.isClassLike(c) && !!c.typeParameters?.length))
|
|
17245
|
+
);
|
|
17246
|
+
if (type.flags & ts.TypeFlags.Instantiable) {
|
|
17247
|
+
const base2 = checker.getBaseConstraintOfType(type);
|
|
17248
|
+
const primitive = !!base2 && !(base2.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) && (base2.isUnion() ? base2.types : [base2]).every(
|
|
17249
|
+
(x) => !!(x.flags & (ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike | ts.TypeFlags.BooleanLike | ts.TypeFlags.ESSymbolLike | ts.TypeFlags.VoidLike | ts.TypeFlags.Null))
|
|
17250
|
+
);
|
|
17251
|
+
if (enclosing && !primitive) m.generic.push({ at: node2, fn: enclosing, guard: false, file: file2 });
|
|
17252
|
+
} else if (!(type.flags & ts.TypeFlags.Never))
|
|
17253
|
+
m.casts.push({ at: node2, type, from: checker.getTypeAtLocation(node2.expression), file: file2 });
|
|
17254
|
+
} else if (ts.isTypePredicateNode(node2) && node2.type) {
|
|
17255
|
+
const type = checker.getTypeFromTypeNode(node2.type);
|
|
17256
|
+
if (type.flags & ts.TypeFlags.Instantiable) {
|
|
17257
|
+
if (node2.parent.typeParameters?.length)
|
|
17258
|
+
m.generic.push({ at: node2, fn: node2.parent, guard: true, file: file2 });
|
|
17259
|
+
} else if (!(type.flags & ts.TypeFlags.Never)) m.guards.push({ at: node2, type, file: file2 });
|
|
17260
|
+
}
|
|
17261
|
+
ts.forEachChild(node2, visit2);
|
|
17262
|
+
};
|
|
17263
|
+
visit2(sf);
|
|
17264
|
+
}
|
|
17265
|
+
return made = m;
|
|
17266
|
+
};
|
|
17267
|
+
const fits = (from, to) => from === to || checker.isTypeAssignableTo(from, to);
|
|
17268
|
+
const nearSeen = /* @__PURE__ */ new Map();
|
|
17269
|
+
const nearFiles = (file2) => {
|
|
17270
|
+
let hit = nearSeen.get(file2);
|
|
17271
|
+
if (!hit) {
|
|
17272
|
+
hit = program.getSourceFiles().filter((sf) => {
|
|
17273
|
+
const f = fileIdOf2(sf);
|
|
17274
|
+
return !!f && !isTestPath(pathOf9(f)) && (!input2.together || input2.together(f, file2));
|
|
17275
|
+
});
|
|
17276
|
+
nearSeen.set(file2, hit);
|
|
17277
|
+
}
|
|
17278
|
+
return hit;
|
|
17279
|
+
};
|
|
17280
|
+
const leaves = (e) => {
|
|
17281
|
+
while (ts.isParenthesizedExpression(e) || ts.isAsExpression(e) || ts.isTypeAssertionExpression(e) || ts.isNonNullExpression(e) || ts.isSatisfiesExpression(e))
|
|
17282
|
+
e = e.expression;
|
|
17283
|
+
if (ts.isConditionalExpression(e)) return [...leaves(e.whenTrue), ...leaves(e.whenFalse)];
|
|
17284
|
+
if (ts.isBinaryExpression(e) && [
|
|
17285
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
17286
|
+
ts.SyntaxKind.BarBarToken,
|
|
17287
|
+
ts.SyntaxKind.AmpersandAmpersandToken
|
|
17288
|
+
].includes(e.operatorToken.kind))
|
|
17289
|
+
return [...leaves(e.left), ...leaves(e.right)];
|
|
17290
|
+
return [e];
|
|
17291
|
+
};
|
|
17292
|
+
const rootedOutside = (e) => {
|
|
17293
|
+
let root = e;
|
|
17294
|
+
for (; ; ) {
|
|
17295
|
+
if (ts.isAwaitExpression(root) || ts.isParenthesizedExpression(root) || ts.isNonNullExpression(root))
|
|
17296
|
+
root = root.expression;
|
|
17297
|
+
else if (ts.isCallExpression(root) || ts.isNewExpression(root)) root = root.expression;
|
|
17298
|
+
else if (ts.isPropertyAccessExpression(root) || ts.isElementAccessExpression(root))
|
|
17299
|
+
root = root.expression;
|
|
17300
|
+
else break;
|
|
17301
|
+
}
|
|
17302
|
+
if (!ts.isIdentifier(root)) return false;
|
|
17303
|
+
const decls = checker.getSymbolAtLocation(root)?.declarations ?? [];
|
|
17304
|
+
return decls.every((d) => !fileIdOf2(d.getSourceFile()));
|
|
17305
|
+
};
|
|
17306
|
+
const madeOutside = (type) => {
|
|
17307
|
+
if (type.isUnionOrIntersection()) return type.types.some(madeOutside);
|
|
17308
|
+
const decls = type.getSymbol()?.declarations ?? [];
|
|
17309
|
+
return decls.length > 0 && decls.every((d) => !fileIdOf2(d.getSourceFile()));
|
|
17310
|
+
};
|
|
17311
|
+
const outsideSeen = /* @__PURE__ */ new Map();
|
|
17312
|
+
const outsideIn = (sf) => {
|
|
17313
|
+
const hit = outsideSeen.get(sf);
|
|
17314
|
+
if (hit) return hit;
|
|
17315
|
+
const out2 = [];
|
|
17316
|
+
const visit2 = (node2) => {
|
|
17317
|
+
const p = node2.parent;
|
|
17318
|
+
const e = node2;
|
|
17319
|
+
const placed = !!p && ts.isExpression(node2) && (ts.isCallExpression(p) && p.arguments.includes(e) || ts.isNewExpression(p) && !!p.arguments?.includes(e) || ts.isArrayLiteralExpression(p) && p.elements.includes(e) || ts.isReturnStatement(p) && p.expression === node2 || ts.isArrowFunction(p) && p.body === node2 || ts.isVariableDeclaration(p) && p.initializer === node2 || ts.isPropertyAssignment(p) && p.initializer === node2 || ts.isShorthandPropertyAssignment(p) || ts.isBinaryExpression(p) && p.right === node2 && p.operatorToken.kind === ts.SyntaxKind.EqualsToken);
|
|
17320
|
+
if (placed) {
|
|
17321
|
+
const expected = () => checker.getContextualType(e);
|
|
17322
|
+
for (const leaf of leaves(e)) {
|
|
17323
|
+
if (!(ts.isIdentifier(leaf) || ts.isPropertyAccessExpression(leaf) || ts.isElementAccessExpression(leaf) || ts.isCallExpression(leaf) || ts.isNewExpression(leaf) || ts.isAwaitExpression(leaf)))
|
|
17324
|
+
continue;
|
|
17325
|
+
const proxy = ts.isNewExpression(leaf) && ts.isIdentifier(leaf.expression) && leaf.expression.text === "Proxy";
|
|
17326
|
+
if (!proxy && !rootedOutside(leaf)) continue;
|
|
17327
|
+
const type = checker.getTypeAtLocation(leaf);
|
|
17328
|
+
if (!anyish(type) && (proxy || madeOutside(type) && type.flags & (ts.TypeFlags.Object | ts.TypeFlags.Intersection | ts.TypeFlags.Union))) {
|
|
17329
|
+
const want = expected();
|
|
17330
|
+
if (!want || !anyish(want)) out2.push({ at: leaf, type, expected: want });
|
|
17331
|
+
}
|
|
17332
|
+
}
|
|
17333
|
+
}
|
|
17334
|
+
ts.forEachChild(node2, visit2);
|
|
17335
|
+
};
|
|
17336
|
+
visit2(sf);
|
|
17337
|
+
outsideSeen.set(sf, out2);
|
|
17338
|
+
return out2;
|
|
17339
|
+
};
|
|
17340
|
+
const chainOf = (c) => {
|
|
17341
|
+
const out2 = [];
|
|
17342
|
+
for (let at = c; at && !out2.includes(declKey(at)); at = graph.parentOf.get(declKey(at))?.base)
|
|
17343
|
+
out2.push(declKey(at));
|
|
17344
|
+
return out2;
|
|
17345
|
+
};
|
|
17346
|
+
const callers2 = makeCallers(s, {
|
|
17347
|
+
closedWorld: true,
|
|
17348
|
+
importedFromOutside: input2.importedFromOutside,
|
|
17349
|
+
where: (n2) => where(n2)
|
|
17350
|
+
});
|
|
17351
|
+
const genericMakes = (g, t) => {
|
|
17352
|
+
const calls = genericCalls(g.fn);
|
|
17353
|
+
if (calls === null) return true;
|
|
17354
|
+
return calls.some((call) => {
|
|
17355
|
+
const sig = checker.getResolvedSignature(call);
|
|
17356
|
+
if (!sig) return true;
|
|
17357
|
+
const made2 = g.guard ? checker.getTypePredicateOfSignature(sig)?.type : sig.getReturnType();
|
|
17358
|
+
return !made2 || !anyish(made2) && fits(made2, t);
|
|
17359
|
+
});
|
|
17360
|
+
};
|
|
17361
|
+
const callsSeen = /* @__PURE__ */ new Map();
|
|
17362
|
+
const genericCalls = (fn) => {
|
|
17363
|
+
if (!fn) return null;
|
|
17364
|
+
const hit = callsSeen.get(fn);
|
|
17365
|
+
if (hit !== void 0) return hit;
|
|
17366
|
+
let out2 = null;
|
|
17367
|
+
const callee = ts.isFunctionDeclaration(fn) ? fn : (ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) && ts.isVariableDeclaration(fn.parent) ? fn.parent : null;
|
|
17368
|
+
if (callee) {
|
|
17369
|
+
const found = callers2.callersOf(callee);
|
|
17370
|
+
out2 = "why" in found ? null : found.calls.flatMap(({ call }) => ts.isCallExpression(call) ? [call] : []);
|
|
17371
|
+
} else if (ts.isMethodDeclaration(fn) && ts.isIdentifier(fn.name)) {
|
|
17372
|
+
out2 = [];
|
|
17373
|
+
for (const id of named(fn.name.text)) {
|
|
17374
|
+
if (id === fn.name) continue;
|
|
17375
|
+
const sym = checker.getSymbolAtLocation(id);
|
|
17376
|
+
const decl = sym?.declarations?.includes(fn);
|
|
17377
|
+
const parent = id.parent;
|
|
17378
|
+
const call = ts.isPropertyAccessExpression(parent) && parent.name === id && ts.isCallExpression(parent.parent) && parent.parent.expression === parent ? parent.parent : null;
|
|
17379
|
+
if (call) {
|
|
17380
|
+
if (checker.getResolvedSignature(call)?.declaration === fn) out2.push(call);
|
|
17381
|
+
} else if (decl) {
|
|
17382
|
+
out2 = null;
|
|
17383
|
+
break;
|
|
17384
|
+
}
|
|
17385
|
+
}
|
|
17386
|
+
}
|
|
17387
|
+
callsSeen.set(fn, out2);
|
|
17388
|
+
return out2;
|
|
17389
|
+
};
|
|
17390
|
+
let byText;
|
|
17391
|
+
const named = (name) => {
|
|
17392
|
+
if (!byText) {
|
|
17393
|
+
byText = /* @__PURE__ */ new Map();
|
|
17394
|
+
for (const sf of program.getSourceFiles()) {
|
|
17395
|
+
if (!fileIdOf2(sf)) continue;
|
|
17396
|
+
const visit2 = (n2) => {
|
|
17397
|
+
if (ts.isIdentifier(n2)) {
|
|
17398
|
+
const list = byText.get(n2.text);
|
|
17399
|
+
if (list) list.push(n2);
|
|
17400
|
+
else byText.set(n2.text, [n2]);
|
|
17401
|
+
}
|
|
17402
|
+
ts.forEachChild(n2, visit2);
|
|
17403
|
+
};
|
|
17404
|
+
visit2(sf);
|
|
17405
|
+
}
|
|
17406
|
+
}
|
|
17407
|
+
return byText.get(name) ?? [];
|
|
17408
|
+
};
|
|
17409
|
+
const anyish = (t) => !!(t.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown));
|
|
17410
|
+
const seen = /* @__PURE__ */ new Map();
|
|
17411
|
+
const runsOf = (t, member) => {
|
|
17412
|
+
const key = `${t.id}\0${member}`;
|
|
17413
|
+
const hit = seen.get(key);
|
|
17414
|
+
if (hit) return hit;
|
|
17415
|
+
const found = find2(t, member);
|
|
17416
|
+
seen.set(key, found);
|
|
17417
|
+
return found;
|
|
17418
|
+
};
|
|
17419
|
+
const find2 = (t, member) => {
|
|
17420
|
+
const name = checker.typeToString(t);
|
|
17421
|
+
const decl = t.getSymbol()?.declarations?.[0];
|
|
17422
|
+
const file2 = decl && fileIdOf2(decl.getSourceFile());
|
|
17423
|
+
if (!file2) return { why: `${name} isn't the project's` };
|
|
17424
|
+
if (input2.importedFromOutside(file2))
|
|
17425
|
+
return {
|
|
17426
|
+
why: `${pathOf9(file2)} is imported by a file whose code isn't typed here, which may make a ${name}`
|
|
17427
|
+
};
|
|
17428
|
+
const all2 = scan();
|
|
17429
|
+
const near = (f) => !input2.together || input2.together(f, file2);
|
|
17430
|
+
const m = {
|
|
17431
|
+
classes: all2.classes.filter((x) => near(x.file)),
|
|
17432
|
+
literals: all2.literals.filter((x) => near(x.file)).map((x) => x.node),
|
|
17433
|
+
casts: all2.casts.filter((x) => near(x.file)),
|
|
17434
|
+
guards: all2.guards.filter((x) => near(x.file)),
|
|
17435
|
+
generic: all2.generic.filter((x) => near(x.file)),
|
|
17436
|
+
outside: nearFiles(file2).flatMap(outsideIn)
|
|
17437
|
+
};
|
|
17438
|
+
const outsider = m.outside.find(
|
|
17439
|
+
(o) => o.type.getProperty(member) && fits(o.type, t) && (!o.expected || o.expected === t || fits(t, o.expected))
|
|
17440
|
+
);
|
|
17441
|
+
if (outsider)
|
|
17442
|
+
return {
|
|
17443
|
+
why: `a value the project doesn't make (${checker.typeToString(outsider.type)}) is used as a ${name} at ${where(outsider.at)}`
|
|
17444
|
+
};
|
|
17445
|
+
const instantiated = m.generic.find((g) => genericMakes(g, t));
|
|
17446
|
+
if (instantiated)
|
|
17447
|
+
return {
|
|
17448
|
+
why: `a generic ${instantiated.guard ? "type guard" : "cast"} at ${where(instantiated.at)} may make a ${name}`
|
|
17449
|
+
};
|
|
17450
|
+
const cast = m.casts.find(
|
|
17451
|
+
(c) => fits(c.type, t) && !anyish(c.type) && (anyish(c.from) || !fits(c.from, t))
|
|
17452
|
+
);
|
|
17453
|
+
if (cast) return { why: `a value is cast to ${name} at ${where(cast.at)}` };
|
|
17454
|
+
const guard = m.guards.find((g) => fits(g.type, t) && !anyish(g.type));
|
|
17455
|
+
if (guard) return { why: `a type guard says a value is ${name} at ${where(guard.at)}` };
|
|
17456
|
+
const runs = /* @__PURE__ */ new Map();
|
|
17457
|
+
for (const c of m.classes) {
|
|
17458
|
+
const generic = !!c.node.typeParameters?.length;
|
|
17459
|
+
if (!c.type.getProperty(member) || !generic && !fits(c.type, t)) continue;
|
|
17460
|
+
if (!c.decl)
|
|
17461
|
+
return { why: `a class that isn't declared at a file's top level fits ${name} (${where(c.node)})` };
|
|
17462
|
+
const body = graph.bodyOf(c.decl, member);
|
|
17463
|
+
if (body === "none") continue;
|
|
17464
|
+
if (typeof body === "string")
|
|
17465
|
+
return { why: `${c.decl.name} fits ${name}, and what it runs for ${member} can't be told` };
|
|
17466
|
+
const written2 = inj?.writeTo(member, new Set(chainOf(c.decl)));
|
|
17467
|
+
if (written2) return { why: `${member} of a ${c.decl.name} may be written at ${where(written2)}` };
|
|
17468
|
+
runs.set(body.fn.id, { fn: body.fn, basis: body.fn.evidence });
|
|
17469
|
+
}
|
|
17470
|
+
for (const lit of m.literals) {
|
|
17471
|
+
const p = lit.properties.find((x) => keyOf2(x.name) === member);
|
|
17472
|
+
const spread = lit.properties.some((x) => ts.isSpreadAssignment(x));
|
|
17473
|
+
if (!p && !spread) continue;
|
|
17474
|
+
const litType = checker.getTypeAtLocation(lit);
|
|
17475
|
+
const inGeneric = !!ts.findAncestor(
|
|
17476
|
+
lit,
|
|
17477
|
+
(n2) => ts.isFunctionLike(n2) && !!n2.typeParameters?.length
|
|
17478
|
+
);
|
|
17479
|
+
if (anyish(litType) || (!inGeneric || !p) && !fits(litType, t)) continue;
|
|
17480
|
+
if (spread || !p) return { why: `an object with a spread fits ${name} (${where(lit)})` };
|
|
17481
|
+
const v = ts.isPropertyAssignment(p) ? p.initializer : ts.isMethodDeclaration(p) ? p : void 0;
|
|
17482
|
+
const fnNode = v && (ts.isMethodDeclaration(v) || ts.isArrowFunction(v) || ts.isFunctionExpression(v)) ? v : void 0;
|
|
17483
|
+
const fn = fnNode && functionOf(fnNode);
|
|
17484
|
+
if (!fn)
|
|
17485
|
+
return {
|
|
17486
|
+
why: `an object of ${name}'s shape at ${where(lit)} has a ${member} that isn't a function written there`
|
|
17487
|
+
};
|
|
17488
|
+
const written2 = inj?.writeTo(member, /* @__PURE__ */ new Set(), {
|
|
17489
|
+
types: [litType],
|
|
17490
|
+
literal: lit,
|
|
17491
|
+
file: fileIdOf2(lit.getSourceFile()) ?? ""
|
|
17492
|
+
});
|
|
17493
|
+
if (written2)
|
|
17494
|
+
return { why: `${member} of an object of ${name}'s shape may be written at ${where(written2)}` };
|
|
17495
|
+
runs.set(fn.id, { fn, basis: evidenceOf2(fnNode) });
|
|
17496
|
+
}
|
|
17497
|
+
return [...runs.values()];
|
|
17498
|
+
};
|
|
17499
|
+
for (const r of open3) {
|
|
17500
|
+
const site = sites.get(r.site);
|
|
17501
|
+
if (!site || site.reason !== "dynamic-dispatch") continue;
|
|
17502
|
+
const sf = s.sourceFile(site.file);
|
|
17503
|
+
if (!sf) continue;
|
|
17504
|
+
const call = callAt3(ts, sf, toUtf16(sf, site.evidence.span.start.offset), site.expression);
|
|
17505
|
+
if (!call) continue;
|
|
17506
|
+
let callee = call.expression;
|
|
17507
|
+
while (ts.isParenthesizedExpression(callee) || ts.isNonNullExpression(callee)) callee = callee.expression;
|
|
17508
|
+
if (!ts.isPropertyAccessExpression(callee)) continue;
|
|
17509
|
+
const member = callee.name.text;
|
|
17510
|
+
const recv = checker.getTypeAtLocation(callee.expression);
|
|
17511
|
+
const parts = recv.isUnion() ? recv.types.filter((x) => !(x.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null))) : [recv];
|
|
17512
|
+
if (parts.length === 0 || parts.some((x) => anyish(x) || !(x.flags & ts.TypeFlags.Object))) continue;
|
|
17513
|
+
const runs = /* @__PURE__ */ new Map();
|
|
17514
|
+
let why = null;
|
|
17515
|
+
for (const part of parts) {
|
|
17516
|
+
const found = runsOf(part, member);
|
|
17517
|
+
if ("why" in found) {
|
|
17518
|
+
why = found.why;
|
|
17519
|
+
break;
|
|
17520
|
+
}
|
|
17521
|
+
for (const x of found) runs.set(x.fn.id, x);
|
|
17522
|
+
}
|
|
17523
|
+
const typeName = checker.typeToString(recv);
|
|
17524
|
+
if (why !== null) {
|
|
17525
|
+
out.resolutions.push({ ...r, steps: [...r.steps, { kind: "closed-world", detail: why }] });
|
|
17526
|
+
continue;
|
|
17527
|
+
}
|
|
17528
|
+
const fns = [...runs.values()];
|
|
17529
|
+
if (fns.length === 0 || fns.some((f) => f.fn.file === site.file)) continue;
|
|
17530
|
+
const names = fns.map((f) => `${f.fn.qualifiedName} (${pathOf9(f.fn.file)})`);
|
|
17531
|
+
const step = {
|
|
17532
|
+
kind: "closed-world",
|
|
17533
|
+
detail: `the project is analyzed as a closed world (analysis.closedWorld): the values of ${typeName} it makes run ${fns.length === 1 ? names[0] : `one of ${names.join(", ")}`}, and nothing else`
|
|
17534
|
+
};
|
|
17535
|
+
if (fns.length === 1) {
|
|
17536
|
+
const fn = fns[0].fn;
|
|
17537
|
+
out.resolutions.push({
|
|
17538
|
+
type: "SiteResolution",
|
|
17539
|
+
id: r.id,
|
|
17540
|
+
site: r.site,
|
|
17541
|
+
file: r.file,
|
|
17542
|
+
status: "linked",
|
|
17543
|
+
target: fn.id,
|
|
17544
|
+
externalPackage: null,
|
|
17545
|
+
via: "closed-world",
|
|
17546
|
+
importOutcome: null,
|
|
17547
|
+
steps: [...r.steps, step],
|
|
17548
|
+
evidence: r.evidence
|
|
17549
|
+
});
|
|
17550
|
+
out.edges.push({
|
|
17551
|
+
type: "CALLS_CROSS_FILE",
|
|
17552
|
+
id: ids.linkedCall(site.id),
|
|
17553
|
+
from: site.enclosingFn,
|
|
17554
|
+
to: fn.id,
|
|
17555
|
+
form: "plain",
|
|
17556
|
+
resolution: "static-exact",
|
|
17557
|
+
confidence: 1,
|
|
17558
|
+
evidence: site.evidence,
|
|
17559
|
+
via: "closed-world"
|
|
17560
|
+
});
|
|
17561
|
+
continue;
|
|
17562
|
+
}
|
|
17563
|
+
const candidates = fns.flatMap((f) => f.basis ? [{ function: f.fn.id, basis: f.basis }] : []);
|
|
17564
|
+
if (candidates.length !== fns.length) continue;
|
|
17565
|
+
const { exhaustive: _open, ...rest } = r;
|
|
17566
|
+
void _open;
|
|
17567
|
+
out.resolutions.push({ ...rest, steps: [...r.steps, step], candidates });
|
|
17568
|
+
continue;
|
|
17569
|
+
}
|
|
17570
|
+
return out;
|
|
17571
|
+
}
|
|
17572
|
+
function callAt3(ts, sf, pos, expression) {
|
|
17573
|
+
let found;
|
|
17574
|
+
const visit2 = (node2) => {
|
|
17575
|
+
if (found || node2.end <= pos || node2.getStart(sf) > pos) return;
|
|
17576
|
+
if (ts.isCallExpression(node2) && node2.getStart(sf) === pos && node2.expression.getText(sf).replace(/\s+/g, "") === expression.replace(/\s+/g, "")) {
|
|
17577
|
+
found = node2;
|
|
17578
|
+
return;
|
|
17579
|
+
}
|
|
17580
|
+
ts.forEachChild(node2, visit2);
|
|
17581
|
+
};
|
|
17582
|
+
visit2(sf);
|
|
17583
|
+
return found;
|
|
17584
|
+
}
|
|
17585
|
+
var init_closed = __esm({
|
|
17586
|
+
"packages/stage2/src/closed.ts"() {
|
|
17587
|
+
"use strict";
|
|
17588
|
+
init_src();
|
|
17589
|
+
init_callers();
|
|
17590
|
+
init_typegraph();
|
|
17591
|
+
}
|
|
17592
|
+
});
|
|
17593
|
+
|
|
14767
17594
|
// packages/stage2/src/components.ts
|
|
14768
17595
|
import { posix as posix4 } from "node:path";
|
|
14769
17596
|
function linkComponents(input2) {
|
|
@@ -17903,7 +20730,7 @@ function typedResolutions(input2, graph, s) {
|
|
|
17903
20730
|
};
|
|
17904
20731
|
return { kind: "no-body" };
|
|
17905
20732
|
};
|
|
17906
|
-
const
|
|
20733
|
+
const callAt4 = (sf, pos, expression) => {
|
|
17907
20734
|
let found;
|
|
17908
20735
|
const visit2 = (node2) => {
|
|
17909
20736
|
if (found || node2.end <= pos || node2.getStart(sf) > pos) return;
|
|
@@ -17961,7 +20788,7 @@ function typedResolutions(input2, graph, s) {
|
|
|
17961
20788
|
for (const site of asked) {
|
|
17962
20789
|
const sf = s.sourceFile(site.file);
|
|
17963
20790
|
if (!sf) continue;
|
|
17964
|
-
const call =
|
|
20791
|
+
const call = callAt4(sf, toUtf16(sf, site.evidence.span.start.offset), site.expression);
|
|
17965
20792
|
if (!call) continue;
|
|
17966
20793
|
const callee = call.expression;
|
|
17967
20794
|
const isMember = ts.isPropertyAccessExpression(callee) || ts.isElementAccessExpression(callee);
|
|
@@ -18122,6 +20949,274 @@ var init_types = __esm({
|
|
|
18122
20949
|
}
|
|
18123
20950
|
});
|
|
18124
20951
|
|
|
20952
|
+
// packages/stage2/src/together.ts
|
|
20953
|
+
function makeTogether(plans, resolved, s = null) {
|
|
20954
|
+
const roots = new Map(
|
|
20955
|
+
plans.map((p) => [
|
|
20956
|
+
p.sourceId,
|
|
20957
|
+
p.manifests.map((m) => m.path).filter((path) => path === "package.json" || path.endsWith("/package.json")).map((path) => path.slice(0, path.length - "package.json".length)).sort((a, b) => b.length - a.length)
|
|
20958
|
+
])
|
|
20959
|
+
);
|
|
20960
|
+
const packages = /* @__PURE__ */ new Map();
|
|
20961
|
+
const packageOf = (fileId) => {
|
|
20962
|
+
const hit = packages.get(fileId);
|
|
20963
|
+
if (hit) return hit;
|
|
20964
|
+
const p = findPackage(fileId);
|
|
20965
|
+
packages.set(fileId, p);
|
|
20966
|
+
return p;
|
|
20967
|
+
};
|
|
20968
|
+
const findPackage = (fileId) => {
|
|
20969
|
+
const at = fileId.indexOf(":");
|
|
20970
|
+
const source = fileId.slice(0, at);
|
|
20971
|
+
const path = fileId.slice(at + 1);
|
|
20972
|
+
const dir = (roots.get(source) ?? []).find((d) => path.startsWith(d)) ?? "";
|
|
20973
|
+
return `${source}:${dir}`;
|
|
20974
|
+
};
|
|
20975
|
+
const importers = /* @__PURE__ */ new Map();
|
|
20976
|
+
const imports = (from, to) => {
|
|
20977
|
+
if (from === to) return;
|
|
20978
|
+
const set2 = importers.get(to) ?? /* @__PURE__ */ new Set();
|
|
20979
|
+
set2.add(from);
|
|
20980
|
+
importers.set(to, set2);
|
|
20981
|
+
};
|
|
20982
|
+
for (const r of resolved) if (r.target.kind === "file") imports(r.statement.fileId, r.target.file.id);
|
|
20983
|
+
const names = /* @__PURE__ */ new Map();
|
|
20984
|
+
for (const p of plans)
|
|
20985
|
+
for (const m of p.manifests) {
|
|
20986
|
+
if (m.path !== "package.json" && !m.path.endsWith("/package.json")) continue;
|
|
20987
|
+
let name = "";
|
|
20988
|
+
try {
|
|
20989
|
+
name = String(JSON.parse(m.content).name ?? "");
|
|
20990
|
+
} catch {
|
|
20991
|
+
}
|
|
20992
|
+
names.set(`${p.sourceId}:${m.path.slice(0, m.path.length - "package.json".length)}`, name);
|
|
20993
|
+
}
|
|
20994
|
+
const allPackages = plans.flatMap(
|
|
20995
|
+
(p) => (roots.get(p.sourceId) ?? [""]).concat("").map((d) => `${p.sourceId}:${d}`)
|
|
20996
|
+
);
|
|
20997
|
+
const loads = [];
|
|
20998
|
+
const link2 = (fromFile, toPackage) => loads.push([fromFile, toPackage]);
|
|
20999
|
+
const prefixLoads = [];
|
|
21000
|
+
const aliases = aliasesOf(plans);
|
|
21001
|
+
const linkLoad = (file2, head) => {
|
|
21002
|
+
const from = file2;
|
|
21003
|
+
const source = file2.slice(0, file2.indexOf(":"));
|
|
21004
|
+
const folder = (prefix) => prefixLoads.push([from, `${source}:${prefix}`]);
|
|
21005
|
+
const anywhere = () => {
|
|
21006
|
+
for (const pkg of allPackages) link2(from, pkg);
|
|
21007
|
+
};
|
|
21008
|
+
if (head.startsWith(".")) {
|
|
21009
|
+
const dir = folderOf(file2, head);
|
|
21010
|
+
return dir === null ? anywhere() : folder(dir + head.slice(head.lastIndexOf("/") + 1));
|
|
21011
|
+
}
|
|
21012
|
+
if (head.startsWith("/")) return folder(head.slice(1));
|
|
21013
|
+
const alias = aliases.filter(([src, key]) => src === source && head.startsWith(key)).sort((a, b) => b[1].length - a[1].length)[0];
|
|
21014
|
+
if (alias) {
|
|
21015
|
+
const rest = head.slice(alias[1].length).replace(/^\//, "");
|
|
21016
|
+
return folder(`${alias[2]}/${rest}`.replace(/\/+/g, "/").replace(/^\//, ""));
|
|
21017
|
+
}
|
|
21018
|
+
const named = [...names].filter(
|
|
21019
|
+
([pkg, name]) => pkg.startsWith(`${source}:`) && head !== "" && name.startsWith(head)
|
|
21020
|
+
);
|
|
21021
|
+
if (named.length) for (const [pkg] of named) link2(from, pkg);
|
|
21022
|
+
else for (const pkg of allPackages) if (pkg.startsWith(`${source}:`)) link2(from, pkg);
|
|
21023
|
+
};
|
|
21024
|
+
const declaredHere = (e) => {
|
|
21025
|
+
let root = e;
|
|
21026
|
+
while (s?.ts.isPropertyAccessExpression(root)) root = root.expression;
|
|
21027
|
+
const decls = s?.checker?.getSymbolAtLocation(root)?.declarations ?? [];
|
|
21028
|
+
return decls.some((d) => !!s?.fileIdOf(d.getSourceFile()));
|
|
21029
|
+
};
|
|
21030
|
+
if (s?.program) {
|
|
21031
|
+
const t = s.ts;
|
|
21032
|
+
const headOf = (arg) => {
|
|
21033
|
+
if (!arg) return null;
|
|
21034
|
+
if (t.isStringLiteralLike(arg)) return arg.text.replace(/[*?{[].*$/, "");
|
|
21035
|
+
if (t.isTemplateExpression(arg)) return arg.head.text;
|
|
21036
|
+
if (t.isBinaryExpression(arg) && arg.operatorToken.kind === t.SyntaxKind.PlusToken) {
|
|
21037
|
+
let left = arg;
|
|
21038
|
+
while (t.isBinaryExpression(left) && left.operatorToken.kind === t.SyntaxKind.PlusToken)
|
|
21039
|
+
left = left.left;
|
|
21040
|
+
if (t.isStringLiteralLike(left)) return left.text;
|
|
21041
|
+
if (t.isTemplateExpression(left)) return left.head.text;
|
|
21042
|
+
}
|
|
21043
|
+
return "";
|
|
21044
|
+
};
|
|
21045
|
+
for (const sf of s.program.getSourceFiles()) {
|
|
21046
|
+
const file2 = s.fileIdOf(sf);
|
|
21047
|
+
if (!file2) continue;
|
|
21048
|
+
const requirers = /* @__PURE__ */ new Set();
|
|
21049
|
+
const visit2 = (node2) => {
|
|
21050
|
+
if (t.isVariableDeclaration(node2) && node2.initializer && t.isCallExpression(node2.initializer)) {
|
|
21051
|
+
const c = node2.initializer.expression;
|
|
21052
|
+
const name = t.isIdentifier(c) ? c.text : t.isPropertyAccessExpression(c) ? c.name.text : "";
|
|
21053
|
+
const sym = name === "createRequire" ? s.checker?.getSymbolAtLocation(node2.name) : void 0;
|
|
21054
|
+
if (sym) requirers.add(sym);
|
|
21055
|
+
}
|
|
21056
|
+
if (t.isCallExpression(node2)) {
|
|
21057
|
+
const c = node2.expression;
|
|
21058
|
+
const text = c.getText(sf).replace(/\s+/g, "");
|
|
21059
|
+
const arg = node2.arguments[0];
|
|
21060
|
+
const loader = c.kind === t.SyntaxKind.ImportKeyword || // Node's `require`, not a function of the project's by that name (an AMD bundle's own).
|
|
21061
|
+
(text === "require" || text === "require.context") && !declaredHere(c) || text === "module.require" || text === "import.meta.glob" || text === "import.meta.globEager" || t.isIdentifier(c) && !!s.checker && requirers.has(s.checker.getSymbolAtLocation(c));
|
|
21062
|
+
const literal2 = arg && t.isStringLiteralLike(arg) && !/[*?{[]/.test(arg.text) && !text.startsWith("import.meta.glob") && text !== "require.context";
|
|
21063
|
+
if (loader && !literal2) {
|
|
21064
|
+
const head = headOf(arg);
|
|
21065
|
+
if (head !== null) linkLoad(file2, head);
|
|
21066
|
+
}
|
|
21067
|
+
}
|
|
21068
|
+
t.forEachChild(node2, visit2);
|
|
21069
|
+
};
|
|
21070
|
+
visit2(sf);
|
|
21071
|
+
}
|
|
21072
|
+
}
|
|
21073
|
+
if (loads.length || prefixLoads.length) {
|
|
21074
|
+
const byPackage = /* @__PURE__ */ new Map();
|
|
21075
|
+
for (const p of plans)
|
|
21076
|
+
for (const f of p.files) {
|
|
21077
|
+
const id = `${p.sourceId}:${f.path}`;
|
|
21078
|
+
const pkg = packageOf(id);
|
|
21079
|
+
const list = byPackage.get(pkg);
|
|
21080
|
+
if (list) list.push(id);
|
|
21081
|
+
else byPackage.set(pkg, [id]);
|
|
21082
|
+
}
|
|
21083
|
+
for (const [from, pkg] of loads) for (const f of byPackage.get(pkg) ?? []) imports(from, f);
|
|
21084
|
+
if (prefixLoads.length)
|
|
21085
|
+
for (const p of plans)
|
|
21086
|
+
for (const f of p.files) {
|
|
21087
|
+
const id = `${p.sourceId}:${f.path}`;
|
|
21088
|
+
for (const [from, prefix] of prefixLoads) if (id.startsWith(prefix)) imports(from, id);
|
|
21089
|
+
}
|
|
21090
|
+
}
|
|
21091
|
+
const ancestors2 = /* @__PURE__ */ new Map();
|
|
21092
|
+
const ancestorsOf = (p) => {
|
|
21093
|
+
const hit = ancestors2.get(p);
|
|
21094
|
+
if (hit) return hit;
|
|
21095
|
+
const out = /* @__PURE__ */ new Set([p]);
|
|
21096
|
+
const queue = [p];
|
|
21097
|
+
while (queue.length) {
|
|
21098
|
+
for (const up of importers.get(queue.shift()) ?? []) {
|
|
21099
|
+
if (out.has(up)) continue;
|
|
21100
|
+
out.add(up);
|
|
21101
|
+
queue.push(up);
|
|
21102
|
+
}
|
|
21103
|
+
}
|
|
21104
|
+
ancestors2.set(p, out);
|
|
21105
|
+
return out;
|
|
21106
|
+
};
|
|
21107
|
+
const pairs = /* @__PURE__ */ new Map();
|
|
21108
|
+
const scripts2 = /* @__PURE__ */ new Set();
|
|
21109
|
+
if (s?.program) {
|
|
21110
|
+
const t = s.ts;
|
|
21111
|
+
const globals = /* @__PURE__ */ new Set();
|
|
21112
|
+
for (const sf of s.program.getSourceFiles()) {
|
|
21113
|
+
const id = s.fileIdOf(sf);
|
|
21114
|
+
if (!id || t.isExternalModule(sf)) continue;
|
|
21115
|
+
scripts2.add(id);
|
|
21116
|
+
for (const st of sf.statements) {
|
|
21117
|
+
if ((t.isFunctionDeclaration(st) || t.isClassDeclaration(st)) && st.name) globals.add(st.name.text);
|
|
21118
|
+
if (t.isVariableStatement(st)) {
|
|
21119
|
+
for (const d of st.declarationList.declarations)
|
|
21120
|
+
if (t.isIdentifier(d.name)) globals.add(d.name.text);
|
|
21121
|
+
}
|
|
21122
|
+
}
|
|
21123
|
+
}
|
|
21124
|
+
if (globals.size)
|
|
21125
|
+
for (const sf of s.program.getSourceFiles()) {
|
|
21126
|
+
const id = s.fileIdOf(sf);
|
|
21127
|
+
if (!id || !t.isExternalModule(sf)) continue;
|
|
21128
|
+
let uses = false;
|
|
21129
|
+
const visit2 = (n2) => {
|
|
21130
|
+
if (uses) return;
|
|
21131
|
+
if (t.isIdentifier(n2) && globals.has(n2.text)) {
|
|
21132
|
+
const decls = s.checker?.getSymbolAtLocation(n2)?.declarations ?? [];
|
|
21133
|
+
if (decls.some((d) => !t.isExternalModule(d.getSourceFile()) && !!s.fileIdOf(d.getSourceFile())))
|
|
21134
|
+
uses = true;
|
|
21135
|
+
}
|
|
21136
|
+
if (!uses) t.forEachChild(n2, visit2);
|
|
21137
|
+
};
|
|
21138
|
+
visit2(sf);
|
|
21139
|
+
if (uses) scripts2.add(id);
|
|
21140
|
+
}
|
|
21141
|
+
}
|
|
21142
|
+
const sourceOf7 = (f) => f.slice(0, f.indexOf(":"));
|
|
21143
|
+
return (a, b) => {
|
|
21144
|
+
if (a === b) return true;
|
|
21145
|
+
if (scripts2.has(a) && scripts2.has(b) && sourceOf7(a) === sourceOf7(b)) return true;
|
|
21146
|
+
const key = `${a}\0${b}`;
|
|
21147
|
+
const hit = pairs.get(key);
|
|
21148
|
+
if (hit !== void 0) return hit;
|
|
21149
|
+
const [small, large] = [ancestorsOf(a), ancestorsOf(b)].sort((x, y) => x.size - y.size);
|
|
21150
|
+
let found = false;
|
|
21151
|
+
for (const f of small)
|
|
21152
|
+
if (large.has(f)) {
|
|
21153
|
+
found = true;
|
|
21154
|
+
break;
|
|
21155
|
+
}
|
|
21156
|
+
pairs.set(key, found);
|
|
21157
|
+
return found;
|
|
21158
|
+
};
|
|
21159
|
+
}
|
|
21160
|
+
function folderOf(fileId, head) {
|
|
21161
|
+
const path = fileId.slice(fileId.indexOf(":") + 1);
|
|
21162
|
+
const parts = path.split("/").slice(0, -1);
|
|
21163
|
+
const rel = head.split("/");
|
|
21164
|
+
rel.pop();
|
|
21165
|
+
for (const r of rel) {
|
|
21166
|
+
if (r === "..") {
|
|
21167
|
+
if (parts.length === 0) return null;
|
|
21168
|
+
parts.pop();
|
|
21169
|
+
} else if (r !== "." && r !== "") parts.push(r);
|
|
21170
|
+
}
|
|
21171
|
+
return parts.length ? `${parts.join("/")}/` : "";
|
|
21172
|
+
}
|
|
21173
|
+
function aliasesOf(plans) {
|
|
21174
|
+
const out = [];
|
|
21175
|
+
const dirOf = (path) => path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";
|
|
21176
|
+
const under = (dir, rel) => {
|
|
21177
|
+
const parts = dir ? dir.split("/") : [];
|
|
21178
|
+
for (const r of rel.split("/")) {
|
|
21179
|
+
if (r === "..") parts.pop();
|
|
21180
|
+
else if (r !== "." && r !== "") parts.push(r);
|
|
21181
|
+
}
|
|
21182
|
+
return parts.join("/");
|
|
21183
|
+
};
|
|
21184
|
+
for (const p of plans) {
|
|
21185
|
+
for (const m of p.manifests) {
|
|
21186
|
+
if (!/(^|\/)[tj]sconfig[^/]*\.json$/.test(m.path)) continue;
|
|
21187
|
+
let json2;
|
|
21188
|
+
try {
|
|
21189
|
+
json2 = JSON.parse(stripJsonc(m.content));
|
|
21190
|
+
} catch {
|
|
21191
|
+
continue;
|
|
21192
|
+
}
|
|
21193
|
+
const base2 = under(dirOf(m.path), json2?.compilerOptions?.baseUrl ?? ".");
|
|
21194
|
+
for (const [key, targets] of Object.entries(json2?.compilerOptions?.paths ?? {}))
|
|
21195
|
+
for (const target2 of targets ?? [])
|
|
21196
|
+
out.push([p.sourceId, key.replace(/\*$/, ""), under(base2, target2.replace(/\*.*$/, ""))]);
|
|
21197
|
+
}
|
|
21198
|
+
for (const f of p.files) {
|
|
21199
|
+
if (f.content === void 0) continue;
|
|
21200
|
+
const name = f.path.slice(f.path.lastIndexOf("/") + 1);
|
|
21201
|
+
if (/^(vite|webpack)\.config\.[cm]?[jt]s$/.test(name))
|
|
21202
|
+
for (const [key, folder] of parseAliases(f.content))
|
|
21203
|
+
out.push([p.sourceId, key, under(dirOf(f.path), folder)]);
|
|
21204
|
+
else if (/^svelte\.config\.[cm]?[jt]s$/.test(name)) {
|
|
21205
|
+
const block = /alias\s*:\s*\{([^}]*)\}/.exec(f.content)?.[1] ?? "";
|
|
21206
|
+
for (const m of block.matchAll(/['"]?([$@~\w/-]+)['"]?\s*:\s*['"]([^'"*]+)\/?\*?['"]/g))
|
|
21207
|
+
out.push([p.sourceId, m[1].replace(/\/\*$/, ""), under(dirOf(f.path), m[2].replace(/\/$/, ""))]);
|
|
21208
|
+
}
|
|
21209
|
+
}
|
|
21210
|
+
}
|
|
21211
|
+
return out;
|
|
21212
|
+
}
|
|
21213
|
+
var init_together = __esm({
|
|
21214
|
+
"packages/stage2/src/together.ts"() {
|
|
21215
|
+
"use strict";
|
|
21216
|
+
init_imports();
|
|
21217
|
+
}
|
|
21218
|
+
});
|
|
21219
|
+
|
|
18125
21220
|
// packages/stage2/src/index.ts
|
|
18126
21221
|
function scopeOf2(fact) {
|
|
18127
21222
|
switch (fact.type) {
|
|
@@ -18256,6 +21351,64 @@ async function runStage2(ctx, options2 = {}) {
|
|
|
18256
21351
|
);
|
|
18257
21352
|
const typedIds = new Set(typed.resolutions.map((r) => r.id));
|
|
18258
21353
|
linked.resolutions = linked.resolutions.filter((r) => !typedIds.has(r.id));
|
|
21354
|
+
const together = makeTogether(ctx.plans, resolved, program);
|
|
21355
|
+
const injection = makeInjection(graph, program, together);
|
|
21356
|
+
const injected = injectedResolutions(
|
|
21357
|
+
{ sites: linkInput.sites, resolutions: [...narrowed, ...typed.resolutions] },
|
|
21358
|
+
graph,
|
|
21359
|
+
program,
|
|
21360
|
+
injection
|
|
21361
|
+
);
|
|
21362
|
+
const injectedById = new Map(injected.resolutions.map((r) => [r.id, r]));
|
|
21363
|
+
const settle2 = (list) => list.map((r) => injectedById.get(r.id) ?? r);
|
|
21364
|
+
let settledNarrowed = settle2(narrowed);
|
|
21365
|
+
typed.resolutions = settle2(typed.resolutions);
|
|
21366
|
+
const programFiles = new Set(
|
|
21367
|
+
(program?.program?.getSourceFiles() ?? []).flatMap((sf) => {
|
|
21368
|
+
const id = program.fileIdOf(sf);
|
|
21369
|
+
return id ? [id] : [];
|
|
21370
|
+
})
|
|
21371
|
+
);
|
|
21372
|
+
const importedOutside = new Set(
|
|
21373
|
+
resolved.flatMap(
|
|
21374
|
+
(r) => r.target.kind === "file" && !programFiles.has(r.statement.fileId) ? [r.target.file.id] : []
|
|
21375
|
+
)
|
|
21376
|
+
);
|
|
21377
|
+
const closedWorld = ctx.settings?.closedWorld === true;
|
|
21378
|
+
const returned = returnedResolutions(
|
|
21379
|
+
{
|
|
21380
|
+
functions: linkInput.functions,
|
|
21381
|
+
sites: linkInput.sites,
|
|
21382
|
+
resolutions: [...linked.resolutions, ...settledNarrowed, ...typed.resolutions],
|
|
21383
|
+
revisions: linkInput.revisions,
|
|
21384
|
+
closedWorld,
|
|
21385
|
+
importedFromOutside: (f) => importedOutside.has(f)
|
|
21386
|
+
},
|
|
21387
|
+
graph,
|
|
21388
|
+
program,
|
|
21389
|
+
injection
|
|
21390
|
+
);
|
|
21391
|
+
const returnedById = new Map(returned.resolutions.map((r) => [r.id, r]));
|
|
21392
|
+
const settleReturned = (list) => list.map((r) => returnedById.get(r.id) ?? r);
|
|
21393
|
+
linked.resolutions = settleReturned(linked.resolutions);
|
|
21394
|
+
settledNarrowed = settleReturned(settledNarrowed);
|
|
21395
|
+
typed.resolutions = settleReturned(typed.resolutions);
|
|
21396
|
+
const closedOut = closedWorld ? closedWorldResolutions(
|
|
21397
|
+
{
|
|
21398
|
+
functions: linkInput.functions,
|
|
21399
|
+
sites: linkInput.sites,
|
|
21400
|
+
resolutions: [...settledNarrowed, ...typed.resolutions],
|
|
21401
|
+
revisions: linkInput.revisions,
|
|
21402
|
+
importedFromOutside: (f) => importedOutside.has(f),
|
|
21403
|
+
together
|
|
21404
|
+
},
|
|
21405
|
+
graph,
|
|
21406
|
+
program,
|
|
21407
|
+
injection
|
|
21408
|
+
) : { resolutions: [], edges: [] };
|
|
21409
|
+
const closedById = new Map(closedOut.resolutions.map((r) => [r.id, r]));
|
|
21410
|
+
settledNarrowed = settledNarrowed.map((r) => closedById.get(r.id) ?? r);
|
|
21411
|
+
typed.resolutions = typed.resolutions.map((r) => closedById.get(r.id) ?? r);
|
|
18259
21412
|
await step(4, "naming where paths leave the project");
|
|
18260
21413
|
const fnIds = new Set(linkInput.functions.map((f) => f.id));
|
|
18261
21414
|
const named = exits(
|
|
@@ -18263,7 +21416,12 @@ async function runStage2(ctx, options2 = {}) {
|
|
|
18263
21416
|
files,
|
|
18264
21417
|
functions: linkInput.functions,
|
|
18265
21418
|
sites: linkInput.sites,
|
|
18266
|
-
resolutions: [
|
|
21419
|
+
resolutions: [
|
|
21420
|
+
...linked.resolutions,
|
|
21421
|
+
...settledNarrowed,
|
|
21422
|
+
...typed.resolutions,
|
|
21423
|
+
...components.resolutions
|
|
21424
|
+
],
|
|
18267
21425
|
references: references.filter((r) => fnIds.has(r.to)),
|
|
18268
21426
|
results,
|
|
18269
21427
|
plans: ctx.plans,
|
|
@@ -18281,6 +21439,9 @@ async function runStage2(ctx, options2 = {}) {
|
|
|
18281
21439
|
calls: [
|
|
18282
21440
|
...linked.edges,
|
|
18283
21441
|
...typed.edges,
|
|
21442
|
+
...injected.edges,
|
|
21443
|
+
...returned.edges,
|
|
21444
|
+
...closedOut.edges,
|
|
18284
21445
|
...keptCalls,
|
|
18285
21446
|
...components.edges,
|
|
18286
21447
|
...view.facts("CALLS_CROSS_FILE").filter((e) => e.via === "template-link")
|
|
@@ -18305,6 +21466,9 @@ async function runStage2(ctx, options2 = {}) {
|
|
|
18305
21466
|
...kept,
|
|
18306
21467
|
...named.resolutions,
|
|
18307
21468
|
...typed.edges,
|
|
21469
|
+
...injected.edges,
|
|
21470
|
+
...returned.edges,
|
|
21471
|
+
...closedOut.edges,
|
|
18308
21472
|
...linked.edges,
|
|
18309
21473
|
...components.edges,
|
|
18310
21474
|
...map2.groups,
|
|
@@ -18334,6 +21498,9 @@ var init_src8 = __esm({
|
|
|
18334
21498
|
init_src4();
|
|
18335
21499
|
init_imports();
|
|
18336
21500
|
init_candidates();
|
|
21501
|
+
init_di();
|
|
21502
|
+
init_returns();
|
|
21503
|
+
init_closed();
|
|
18337
21504
|
init_components();
|
|
18338
21505
|
init_exits2();
|
|
18339
21506
|
init_groups();
|
|
@@ -18342,6 +21509,7 @@ var init_src8 = __esm({
|
|
|
18342
21509
|
init_program();
|
|
18343
21510
|
init_types();
|
|
18344
21511
|
init_typegraph();
|
|
21512
|
+
init_together();
|
|
18345
21513
|
init_imports();
|
|
18346
21514
|
init_link();
|
|
18347
21515
|
init_candidates();
|
|
@@ -18355,7 +21523,7 @@ var init_src8 = __esm({
|
|
|
18355
21523
|
symbolsScope = (fileId) => `${STAGE23}:symbols:${fileId}`;
|
|
18356
21524
|
groupSource = (groupId) => groupId.split("|")[1];
|
|
18357
21525
|
STEPS = 5;
|
|
18358
|
-
STAGE2_VERSION =
|
|
21526
|
+
STAGE2_VERSION = 5;
|
|
18359
21527
|
stage2 = createStage2();
|
|
18360
21528
|
}
|
|
18361
21529
|
});
|
|
@@ -56172,12 +59340,19 @@ var init_server3 = __esm({
|
|
|
56172
59340
|
call: (e, a, cwd) => e.browse(a, cwd)
|
|
56173
59341
|
},
|
|
56174
59342
|
project_settings: {
|
|
56175
|
-
description: "The project's analysis settings: maxFileMB, the per-file size limit (default 10; files over it are not analyzed unless chosen). Pass maxFileMB (up to 64) to change
|
|
56176
|
-
input: {
|
|
56177
|
-
|
|
59343
|
+
description: "The project's analysis settings: maxFileMB, the per-file size limit (default 10; files over it are not analyzed unless chosen), and closedWorld (default false: the analyzed code is the whole program, so the callers and classes seen are all there are; links settled by it say so). Pass maxFileMB (up to 64) or closedWorld to change them; they apply at the next run.",
|
|
59344
|
+
input: {
|
|
59345
|
+
project,
|
|
59346
|
+
maxFileMB: external_exports.number().optional().describe("New size limit in MB"),
|
|
59347
|
+
closedWorld: external_exports.boolean().optional().describe("Analyze the project as a closed world")
|
|
59348
|
+
},
|
|
59349
|
+
call: (e, a, cwd) => a.maxFileMB === void 0 && a.closedWorld === void 0 ? e.projectSettings(a, cwd) : e.setProjectSettings(
|
|
56178
59350
|
{
|
|
56179
59351
|
...a.project !== void 0 ? { project: a.project } : {},
|
|
56180
|
-
settings: {
|
|
59352
|
+
settings: {
|
|
59353
|
+
...a.maxFileMB !== void 0 ? { maxFileMB: a.maxFileMB } : {},
|
|
59354
|
+
...a.closedWorld !== void 0 ? { closedWorld: a.closedWorld } : {}
|
|
59355
|
+
}
|
|
56181
59356
|
},
|
|
56182
59357
|
cwd
|
|
56183
59358
|
)
|
|
@@ -99065,7 +102240,11 @@ async function createServer(engine, options2 = {}) {
|
|
|
99065
102240
|
{
|
|
99066
102241
|
schema: {
|
|
99067
102242
|
params: projectParams,
|
|
99068
|
-
body: {
|
|
102243
|
+
body: {
|
|
102244
|
+
type: "object",
|
|
102245
|
+
additionalProperties: false,
|
|
102246
|
+
properties: { maxFileMB: { type: "number" }, closedWorld: { type: "boolean" } }
|
|
102247
|
+
}
|
|
99069
102248
|
}
|
|
99070
102249
|
},
|
|
99071
102250
|
async (req) => engine.setProjectSettings({ ...req.params, settings: req.body })
|
|
@@ -100037,7 +103216,8 @@ Usage:
|
|
|
100037
103216
|
veoflow config [get [<key>] | set <key> <value> | test] [--project <id>]
|
|
100038
103217
|
model settings: model.baseUrl, model.name, model.apiKey
|
|
100039
103218
|
("set model.apiKey -" reads the key from stdin);
|
|
100040
|
-
project settings: analysis.maxFileMB (size limit, default 10)
|
|
103219
|
+
project settings: analysis.maxFileMB (size limit, default 10),
|
|
103220
|
+
analysis.closedWorld (true: the analyzed code is the whole program)
|
|
100041
103221
|
veoflow license show|add <file|->|refresh [--json]
|
|
100042
103222
|
the license in force; install a license key (from a file,
|
|
100043
103223
|
or "-" for stdin); check the installed one again (offline)
|
|
@@ -101034,12 +104214,31 @@ try: ${answer.suggestions.join(" \xB7 ")}`);
|
|
|
101034
104214
|
}
|
|
101035
104215
|
const [action = "get", key, value] = rest;
|
|
101036
104216
|
if (key?.startsWith("analysis.") || action === "get" && projectArg && !key) {
|
|
101037
|
-
if (key && key !== "analysis.maxFileMB")
|
|
101038
|
-
throw new UsageError(`unknown key ${key} (analysis.maxFileMB)`);
|
|
104217
|
+
if (key && key !== "analysis.maxFileMB" && key !== "analysis.closedWorld")
|
|
104218
|
+
throw new UsageError(`unknown key ${key} (analysis.maxFileMB, analysis.closedWorld)`);
|
|
101039
104219
|
const project2 = pickProject(engine, projectArg, io.cwd);
|
|
101040
104220
|
if (action === "get") {
|
|
104221
|
+
const { settings } = engine.projectSettings({ project: project2 });
|
|
104222
|
+
if (key === "analysis.closedWorld") io.out(String(settings.closedWorld));
|
|
104223
|
+
else if (key) io.out(String(settings.maxFileMB));
|
|
104224
|
+
else
|
|
104225
|
+
io.out(
|
|
104226
|
+
`analysis.maxFileMB = ${settings.maxFileMB}
|
|
104227
|
+
analysis.closedWorld = ${settings.closedWorld}`
|
|
104228
|
+
);
|
|
104229
|
+
return 0;
|
|
104230
|
+
}
|
|
104231
|
+
if (action === "set" && key === "analysis.closedWorld") {
|
|
104232
|
+
if (value !== "true" && value !== "false")
|
|
104233
|
+
throw new UsageError(
|
|
104234
|
+
"usage: veoflow config set analysis.closedWorld true|false [--project <id>]"
|
|
104235
|
+
);
|
|
104236
|
+
const { settings } = engine.setProjectSettings({
|
|
104237
|
+
project: project2,
|
|
104238
|
+
settings: { closedWorld: value === "true" }
|
|
104239
|
+
});
|
|
101041
104240
|
io.out(
|
|
101042
|
-
|
|
104241
|
+
`analysis.closedWorld = ${settings.closedWorld} (project ${project2}; takes effect at the next run${settings.closedWorld ? ': links it settles say "closed world"' : ""})`
|
|
101043
104242
|
);
|
|
101044
104243
|
return 0;
|
|
101045
104244
|
}
|
|
@@ -101056,7 +104255,7 @@ try: ${answer.suggestions.join(" \xB7 ")}`);
|
|
|
101056
104255
|
return 0;
|
|
101057
104256
|
}
|
|
101058
104257
|
throw new UsageError(
|
|
101059
|
-
"usage: veoflow config [get|set] analysis.maxFileMB [<value>] [--project <id>]"
|
|
104258
|
+
"usage: veoflow config [get|set] analysis.maxFileMB|analysis.closedWorld [<value>] [--project <id>]"
|
|
101060
104259
|
);
|
|
101061
104260
|
}
|
|
101062
104261
|
const { loadSettings: loadSettings2, publicSettings: publicSettings2, saveSettings: saveSettings2, testConnection: testConnection2 } = await Promise.resolve().then(() => (init_src10(), src_exports3));
|