@gate-forge/pack-http 0.14.0 → 0.15.1
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/README.md +14 -0
- package/dist/client-calls.d.ts +33 -4
- package/dist/client-calls.d.ts.map +1 -1
- package/dist/client-calls.js +1796 -110
- package/dist/client-calls.js.map +1 -1
- package/dist/detector.js +1 -1
- package/dist/detector.js.map +1 -1
- package/package.json +4 -4
package/dist/client-calls.js
CHANGED
|
@@ -40,7 +40,18 @@
|
|
|
40
40
|
* in the scanned set is a single `return <client call>(...)` arrow or
|
|
41
41
|
* function resolves its internal call with the callsite's first
|
|
42
42
|
* argument substituted for the wrapper's first parameter (one level,
|
|
43
|
-
* one parameter — anything deeper is typed unresolved).
|
|
43
|
+
* one parameter — anything deeper is typed unresolved). A wrapper may
|
|
44
|
+
* also name the endpoint parameter (`parameter`) and, when
|
|
45
|
+
* `methodFromBody` is set, keep each concrete verb in its body. The
|
|
46
|
+
* declaration may live in another scanned file, including a default
|
|
47
|
+
* export, and a JSX attribute of that parameter is a callsite.
|
|
48
|
+
* - **Local shapes** (still fail closed): a single-assignment `const`
|
|
49
|
+
* (a closer `let` stays unresolved), both branches of a conditional,
|
|
50
|
+
* a `const` object-literal lookup (every literal value), a destructured
|
|
51
|
+
* relative dynamic-import alias of a configured symbol or builder, and
|
|
52
|
+
* a same-file function whose returns are a finite resolvable set.
|
|
53
|
+
* A `const` named `fetch` whose initializer is not the Fetch API is
|
|
54
|
+
* not a fetch call.
|
|
44
55
|
*/
|
|
45
56
|
import ts from 'typescript';
|
|
46
57
|
import { posix } from 'node:path';
|
|
@@ -243,7 +254,9 @@ function unwrapOwnExpression(node) {
|
|
|
243
254
|
current = current.expression;
|
|
244
255
|
continue;
|
|
245
256
|
}
|
|
246
|
-
if (ts.isAsExpression(current) ||
|
|
257
|
+
if (ts.isAsExpression(current) ||
|
|
258
|
+
ts.isSatisfiesExpression(current) ||
|
|
259
|
+
ts.isNonNullExpression(current)) {
|
|
247
260
|
current = current.expression;
|
|
248
261
|
continue;
|
|
249
262
|
}
|
|
@@ -640,6 +653,18 @@ const UNPROVEN_OPTIONS = { kind: 'unproven' };
|
|
|
640
653
|
*/
|
|
641
654
|
const MAX_OPTIONS_HOPS = 4;
|
|
642
655
|
const UNRESOLVED_VALUE = { kind: 'unresolved', text: '' };
|
|
656
|
+
/**
|
|
657
|
+
* Binds a prop whose per-usage value cannot be proven (attribute
|
|
658
|
+
* omitted with no literal default, non-literal default, attribute
|
|
659
|
+
* present without a value): every evaluator refuses this node, so a
|
|
660
|
+
* computation that consults it fails closed while one that never reads
|
|
661
|
+
* the prop still resolves.
|
|
662
|
+
*/
|
|
663
|
+
const UNPROVEN_PROP_VALUE = ts.factory.createVoidExpression(ts.factory.createNumericLiteral('0'));
|
|
664
|
+
/** Cap on alternatives one expression may prove. Above this, fail closed — never a partial set. */
|
|
665
|
+
const MAX_CALL_TARGETS = 32;
|
|
666
|
+
/** Calls of a declared wrapper into another declared wrapper, before fail closed. */
|
|
667
|
+
const MAX_WRAPPER_DEPTH = 4;
|
|
643
668
|
function languageKindFor(file) {
|
|
644
669
|
if (file.endsWith('.tsx'))
|
|
645
670
|
return ts.ScriptKind.TSX;
|
|
@@ -792,11 +817,25 @@ export function scanClientCalls(file, sourceText, config, scannedFiles) {
|
|
|
792
817
|
const model = table.modelOf(file);
|
|
793
818
|
const calls = [];
|
|
794
819
|
const unresolved = [];
|
|
795
|
-
const wrapperSpans =
|
|
820
|
+
const wrapperSpans = modeledWrapperSpans(model, config, file);
|
|
796
821
|
const insideWrapper = (node) => wrapperSpans.some(([start, end]) => node.getStart(source) >= start && node.getEnd() <= end);
|
|
822
|
+
// `client(...error.config...)` calls inside a registered response-error
|
|
823
|
+
// handler re-send a request this scan has already recorded; they are
|
|
824
|
+
// replays, never new call targets. The set fills when the walk reaches
|
|
825
|
+
// the `.use(...)` registration — an ancestor of the handler body — so
|
|
826
|
+
// every replay call is known before its node is reached.
|
|
827
|
+
const replayCalls = new Set();
|
|
797
828
|
const visit = (node) => {
|
|
798
|
-
if (
|
|
799
|
-
|
|
829
|
+
if (!insideWrapper(node)) {
|
|
830
|
+
if (ts.isCallExpression(node)) {
|
|
831
|
+
collectInterceptorReplays(node, file, config, replayCalls);
|
|
832
|
+
if (!replayCalls.has(node)) {
|
|
833
|
+
extractCall(node, file, config, table, model, calls, unresolved);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
else if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) {
|
|
837
|
+
extractJsxWrapper(node, file, config, table, calls, unresolved);
|
|
838
|
+
}
|
|
800
839
|
}
|
|
801
840
|
ts.forEachChild(node, visit);
|
|
802
841
|
};
|
|
@@ -830,12 +869,14 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
830
869
|
const expression = call.expression;
|
|
831
870
|
// fetch(url[, {method}])
|
|
832
871
|
if (ts.isIdentifier(expression) && expression.text === 'fetch') {
|
|
833
|
-
|
|
872
|
+
if (!table.shadowsGlobalFetch(expression)) {
|
|
873
|
+
extractClientCall(call, 'fetch', 'GET', file, config, table, calls, unresolved, location, undefined, reads);
|
|
874
|
+
}
|
|
834
875
|
return;
|
|
835
876
|
}
|
|
836
877
|
// axios.get(url), apiClient.post(url), window.fetch(url)
|
|
837
878
|
if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.expression)) {
|
|
838
|
-
const objectName = expression.expression
|
|
879
|
+
const objectName = table.configuredClientName(expression.expression, file);
|
|
839
880
|
const verb = expression.name.text.toLowerCase();
|
|
840
881
|
const isFetchObject = objectName === 'window' && expression.name.text === 'fetch';
|
|
841
882
|
if (isFetchObject) {
|
|
@@ -884,12 +925,50 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
884
925
|
if (wrapperConfig !== undefined) {
|
|
885
926
|
const wrapperModel = table.modelOf(file)?.wrappers.get(wrapperConfig.name);
|
|
886
927
|
if (wrapperModel === undefined) {
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
928
|
+
if (resolvesToDeclaredWrapper(table, wrapperConfig.name, file, config)) {
|
|
929
|
+
const bindings = new Map();
|
|
930
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
931
|
+
const paramName = wrapperConfig.parameter ?? (declared === undefined ? undefined : firstIdentifierParam(declared.fn));
|
|
932
|
+
const takesObjectPattern = declared !== undefined
|
|
933
|
+
&& declared.fn.parameters[0] !== undefined
|
|
934
|
+
&& ts.isObjectBindingPattern(declared.fn.parameters[0].name);
|
|
935
|
+
if (declared !== undefined && (paramName !== undefined || takesObjectPattern)) {
|
|
936
|
+
const argument = paramName !== undefined
|
|
937
|
+
? argumentForParameter(declared.fn, paramName, call)
|
|
938
|
+
: call.arguments[0];
|
|
939
|
+
if (argument !== undefined) {
|
|
940
|
+
if (paramName !== undefined)
|
|
941
|
+
bindings.set(paramName, [argument, file]);
|
|
942
|
+
const sandboxCalls = [];
|
|
943
|
+
const sandboxUnresolved = [];
|
|
944
|
+
const sandboxReads = [];
|
|
945
|
+
const singleHandled = resolveWrapperTargetThroughJsxProps(argument, call, paramName ?? '', wrapperConfig, file, config, table, bindings, sandboxCalls, sandboxUnresolved, location, sandboxReads);
|
|
946
|
+
const positionalCalls = [];
|
|
947
|
+
const positionalUnresolved = [];
|
|
948
|
+
const positionalReads = [];
|
|
949
|
+
const positionalHandled = resolveWrapperTargetsThroughPositionalArgs(call, declared, wrapperConfig, file, config, table, positionalCalls, positionalUnresolved, location, positionalReads);
|
|
950
|
+
if (positionalHandled && positionalUnresolved.length === 0) {
|
|
951
|
+
calls.push(...positionalCalls);
|
|
952
|
+
unresolved.push(...positionalUnresolved);
|
|
953
|
+
reads.push(...positionalReads);
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
if (singleHandled) {
|
|
957
|
+
calls.push(...sandboxCalls);
|
|
958
|
+
unresolved.push(...sandboxUnresolved);
|
|
959
|
+
reads.push(...sandboxReads);
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
if (positionalHandled) {
|
|
963
|
+
calls.push(...positionalCalls);
|
|
964
|
+
unresolved.push(...positionalUnresolved);
|
|
965
|
+
reads.push(...positionalReads);
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, reads);
|
|
971
|
+
}
|
|
893
972
|
return;
|
|
894
973
|
}
|
|
895
974
|
const bound = new BoundTable(table, wrapperModel.parameter, call.arguments[0], file);
|
|
@@ -979,6 +1058,12 @@ class BoundTable {
|
|
|
979
1058
|
}
|
|
980
1059
|
return this.inner.evaluate(node, file);
|
|
981
1060
|
}
|
|
1061
|
+
callTargets(node, file, bindings) {
|
|
1062
|
+
const next = new Map(bindings);
|
|
1063
|
+
if (this.argument !== undefined)
|
|
1064
|
+
next.set(this.parameter, [this.argument, this.argumentFile]);
|
|
1065
|
+
return this.inner.callTargets(node, file, next);
|
|
1066
|
+
}
|
|
982
1067
|
optionsMethod(node, file, bindings, depth = 0) {
|
|
983
1068
|
if (ts.isIdentifier(node) &&
|
|
984
1069
|
node.text === this.parameter &&
|
|
@@ -1137,8 +1222,8 @@ function joinInstanceBaseURL(baseURL, callPath) {
|
|
|
1137
1222
|
};
|
|
1138
1223
|
}
|
|
1139
1224
|
function resolveAndRecord(urlNode, framework, method, file, config, table, calls, unresolved, location, reads = [], baseURL) {
|
|
1140
|
-
const
|
|
1141
|
-
if (
|
|
1225
|
+
const targets = table.callTargets(urlNode, file);
|
|
1226
|
+
if (targets === undefined || targets.length === 0) {
|
|
1142
1227
|
unresolved.push({
|
|
1143
1228
|
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
1144
1229
|
detail: `call target cannot be resolved statically (${framework})`,
|
|
@@ -1146,25 +1231,42 @@ function resolveAndRecord(urlNode, framework, method, file, config, table, calls
|
|
|
1146
1231
|
});
|
|
1147
1232
|
return;
|
|
1148
1233
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1234
|
+
recordRawTargets(targets, method, framework, config, calls, unresolved, location, reads, baseURL);
|
|
1235
|
+
}
|
|
1236
|
+
function recordRawTargets(raws, method, framework, config, calls, unresolved, location, reads, baseURL) {
|
|
1237
|
+
const resolved = [];
|
|
1238
|
+
for (const raw of raws) {
|
|
1239
|
+
const joined = joinInstanceBaseURL(baseURL, raw);
|
|
1240
|
+
const canonical = normalizeHttpPath(joined.path, { sameOriginHosts: config.sameOriginHosts });
|
|
1241
|
+
if (!canonical.ok) {
|
|
1242
|
+
unresolved.push({
|
|
1243
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
1244
|
+
detail: `call target '${joined.path}': ${canonical.detail}`,
|
|
1245
|
+
location,
|
|
1246
|
+
});
|
|
1247
|
+
return;
|
|
1248
|
+
}
|
|
1249
|
+
resolved.push({ raw, canonical: canonical.canonical, joined: joined.joined });
|
|
1250
|
+
}
|
|
1251
|
+
const unique = new Map();
|
|
1252
|
+
for (const item of resolved) {
|
|
1253
|
+
if (!unique.has(item.canonical))
|
|
1254
|
+
unique.set(item.canonical, { raw: item.raw, joined: item.joined });
|
|
1255
|
+
}
|
|
1256
|
+
const many = unique.size > 1;
|
|
1257
|
+
const ordered = [...unique.entries()].sort((left, right) => (left[0] < right[0] ? -1 : left[0] > right[0] ? 1 : 0));
|
|
1258
|
+
for (const [canonical, item] of ordered) {
|
|
1259
|
+
calls.push({
|
|
1260
|
+
method,
|
|
1261
|
+
rawPath: item.raw,
|
|
1262
|
+
canonicalPath: canonical,
|
|
1263
|
+
...(item.joined !== undefined ? { joinedBaseURL: item.joined } : {}),
|
|
1264
|
+
framework,
|
|
1155
1265
|
location,
|
|
1266
|
+
...(many ? { idDisambiguator: canonical } : {}),
|
|
1267
|
+
...(reads.length > 0 ? { responseReads: reads } : {}),
|
|
1156
1268
|
});
|
|
1157
|
-
return;
|
|
1158
1269
|
}
|
|
1159
|
-
calls.push({
|
|
1160
|
-
method,
|
|
1161
|
-
rawPath: resolved.text,
|
|
1162
|
-
canonicalPath: canonical.canonical,
|
|
1163
|
-
...(joined.joined !== undefined ? { joinedBaseURL: joined.joined } : {}),
|
|
1164
|
-
framework,
|
|
1165
|
-
location,
|
|
1166
|
-
...(reads.length > 0 ? { responseReads: reads } : {}),
|
|
1167
|
-
});
|
|
1168
1270
|
}
|
|
1169
1271
|
/**
|
|
1170
1272
|
* The ONE expression a function-like returns, or undefined when it
|
|
@@ -1760,94 +1862,1678 @@ class ValueTable {
|
|
|
1760
1862
|
}
|
|
1761
1863
|
return null;
|
|
1762
1864
|
}
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1865
|
+
/**
|
|
1866
|
+
* Every literal path `node` can take, or undefined when any branch
|
|
1867
|
+
* cannot be proven. A resolved target is exactly a value the code can
|
|
1868
|
+
* call — never a guess, never a partial set.
|
|
1869
|
+
*/
|
|
1870
|
+
callTargets(node, file, bindings, seen = new Set()) {
|
|
1871
|
+
const peeled = unwrapOwnExpression(node);
|
|
1872
|
+
if (peeled !== node)
|
|
1873
|
+
return this.callTargets(peeled, file, bindings, seen);
|
|
1874
|
+
if (ts.isSatisfiesExpression(node))
|
|
1875
|
+
return this.callTargets(node.expression, file, bindings, seen);
|
|
1876
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
1877
|
+
return [node.text];
|
|
1878
|
+
if (ts.isTemplateExpression(node))
|
|
1879
|
+
return this.expandTemplate(node, file, bindings, seen);
|
|
1880
|
+
if (ts.isConditionalExpression(node))
|
|
1881
|
+
return this.conditionalTargets(node, file, bindings, seen);
|
|
1882
|
+
if (ts.isIdentifier(node))
|
|
1883
|
+
return this.identifierTargets(node, file, bindings, seen);
|
|
1884
|
+
if (ts.isElementAccessExpression(node))
|
|
1885
|
+
return this.lookupTargets(node, file, bindings, seen);
|
|
1886
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
1887
|
+
const alias = this.dynamicImportAlias(node.expression, file);
|
|
1888
|
+
const builderName = alias?.kind === 'builder' ? alias.name : node.expression.text;
|
|
1889
|
+
const builder = alias?.kind === 'client' ? undefined : activeUrlBuilderIn(this.config, builderName, file);
|
|
1890
|
+
if (builder !== undefined) {
|
|
1891
|
+
const argument = node.arguments[0];
|
|
1892
|
+
if (argument === undefined)
|
|
1893
|
+
return undefined;
|
|
1894
|
+
const inner = this.callTargets(argument, file, bindings, seen);
|
|
1895
|
+
if (inner === undefined)
|
|
1896
|
+
return undefined;
|
|
1897
|
+
const base = builder.base ?? '';
|
|
1898
|
+
return inner.map((text) => (text.startsWith('/') || base === '' ? `${base}${text}` : `${base}/${text}`));
|
|
1899
|
+
}
|
|
1900
|
+
if (alias === undefined)
|
|
1901
|
+
return this.finiteFunctionTargets(node, file, bindings, seen);
|
|
1779
1902
|
}
|
|
1780
|
-
|
|
1903
|
+
return undefined;
|
|
1781
1904
|
}
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
* include/exclude next to their existing `method` / `base` fields; the
|
|
1799
|
-
* top-level `clientScanRoots` / `serverScanRoots` arrays scope where
|
|
1800
|
-
* client-call and server-route scanning apply at all. Consistent with the
|
|
1801
|
-
* pre-existing parser posture: unknown keys are ignored, non-array known
|
|
1802
|
-
* keys are ignored, but malformed ENTRY values throw (the wrapper verb
|
|
1803
|
-
* check predates this; the scoping fields throw on wrong shapes and
|
|
1804
|
-
* missing names because silently dropping a scope widens the scan
|
|
1805
|
-
* instead of narrowing it).
|
|
1806
|
-
*
|
|
1807
|
-
* Args:
|
|
1808
|
-
* section: the parsed `scan.httpClients` section, or null/undefined when absent.
|
|
1809
|
-
* at: where the bytes came from, named in every refusal.
|
|
1810
|
-
*
|
|
1811
|
-
* Returns:
|
|
1812
|
-
* ClientScanConfig: the validated configuration.
|
|
1813
|
-
*/
|
|
1814
|
-
export function parseClientScanConfigDocument(section, at) {
|
|
1815
|
-
if (section === null || section === undefined)
|
|
1816
|
-
return DEFAULT_CLIENT_SCAN_CONFIG;
|
|
1817
|
-
if (typeof section !== 'object' || Array.isArray(section)) {
|
|
1818
|
-
throw new Error(`invalid http client config: expected an object at ${at}`);
|
|
1905
|
+
/** A const binding named `fetch` whose initializer is not the Fetch API. */
|
|
1906
|
+
shadowsGlobalFetch(use) {
|
|
1907
|
+
const local = this.localConstBinding(use);
|
|
1908
|
+
if (local.state !== 'found')
|
|
1909
|
+
return false;
|
|
1910
|
+
const init = unwrapOwnExpression(local.initializer);
|
|
1911
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init))
|
|
1912
|
+
return true;
|
|
1913
|
+
if (!ts.isCallExpression(init))
|
|
1914
|
+
return false;
|
|
1915
|
+
const callee = init.expression;
|
|
1916
|
+
if (ts.isIdentifier(callee) && callee.text === 'fetch')
|
|
1917
|
+
return false;
|
|
1918
|
+
if (ts.isPropertyAccessExpression(callee) && callee.name.text === 'fetch')
|
|
1919
|
+
return false;
|
|
1920
|
+
return true;
|
|
1819
1921
|
}
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1922
|
+
/** Configured client-symbol name for a callsite object, following a dynamic-import alias. */
|
|
1923
|
+
configuredClientName(use, file) {
|
|
1924
|
+
const alias = this.dynamicImportAlias(use, file);
|
|
1925
|
+
return alias?.kind === 'client' ? alias.name : use.text;
|
|
1824
1926
|
}
|
|
1825
|
-
|
|
1826
|
-
|
|
1927
|
+
/** A function named `name` visible from `file`: same file, or a relative import including default. */
|
|
1928
|
+
findFunction(name, file) {
|
|
1929
|
+
const own = this.modelOf(file)?.functions.get(name);
|
|
1930
|
+
if (own !== undefined)
|
|
1931
|
+
return { fn: own, file };
|
|
1932
|
+
const imported = this.importedExport(file, name);
|
|
1933
|
+
if (imported === null)
|
|
1934
|
+
return undefined;
|
|
1935
|
+
const model = this.modelOf(imported.file);
|
|
1936
|
+
if (model === undefined)
|
|
1937
|
+
return undefined;
|
|
1938
|
+
const fn = imported.exported === 'default' ? defaultExportedFunction(model) : model.functions.get(imported.exported);
|
|
1939
|
+
return fn === undefined ? undefined : { fn, file: imported.file };
|
|
1940
|
+
}
|
|
1941
|
+
identifierTargets(node, file, bindings, seen) {
|
|
1942
|
+
const bound = bindings?.get(node.text);
|
|
1943
|
+
if (bound !== undefined) {
|
|
1944
|
+
const rest = new Map(bindings);
|
|
1945
|
+
rest.delete(node.text);
|
|
1946
|
+
return this.callTargets(bound[0], bound[1], rest, seen);
|
|
1947
|
+
}
|
|
1948
|
+
const local = this.localConstBinding(node);
|
|
1949
|
+
if (local.state === 'shadowed')
|
|
1950
|
+
return undefined;
|
|
1951
|
+
if (local.state === 'found') {
|
|
1952
|
+
const key = `${file}::${node.text}`;
|
|
1953
|
+
if (seen.has(key))
|
|
1954
|
+
return undefined;
|
|
1955
|
+
seen.add(key);
|
|
1956
|
+
const value = this.callTargets(local.initializer, file, bindings, seen);
|
|
1957
|
+
seen.delete(key);
|
|
1958
|
+
return value;
|
|
1959
|
+
}
|
|
1960
|
+
const imported = this.importedFrom(file, node.text);
|
|
1961
|
+
const origin = imported ?? file;
|
|
1962
|
+
const initializer = this.modelOf(origin)?.constants.get(node.text);
|
|
1963
|
+
if (initializer === undefined)
|
|
1964
|
+
return undefined;
|
|
1965
|
+
const key = `${origin}::${node.text}`;
|
|
1966
|
+
if (seen.has(key))
|
|
1967
|
+
return undefined;
|
|
1968
|
+
seen.add(key);
|
|
1969
|
+
const value = this.callTargets(initializer, origin, bindings, seen);
|
|
1970
|
+
seen.delete(key);
|
|
1971
|
+
return value;
|
|
1972
|
+
}
|
|
1973
|
+
conditionalTargets(node, file, bindings, seen) {
|
|
1974
|
+
const proven = this.proveEquality(node.condition, file, bindings, seen)
|
|
1975
|
+
?? this.proveStartsWith(node.condition, file, bindings, seen);
|
|
1976
|
+
if (proven === true)
|
|
1977
|
+
return this.callTargets(node.whenTrue, file, bindings, seen);
|
|
1978
|
+
if (proven === false)
|
|
1979
|
+
return this.callTargets(node.whenFalse, file, bindings, seen);
|
|
1980
|
+
const whenTrue = this.callTargets(node.whenTrue, file, bindings, seen);
|
|
1981
|
+
const whenFalse = this.callTargets(node.whenFalse, file, bindings, seen);
|
|
1982
|
+
if (whenTrue === undefined || whenFalse === undefined)
|
|
1983
|
+
return undefined;
|
|
1984
|
+
const combined = [...whenTrue, ...whenFalse];
|
|
1985
|
+
return combined.length > MAX_CALL_TARGETS ? undefined : combined;
|
|
1986
|
+
}
|
|
1987
|
+
/** `expr.startsWith('literal')` when `expr` is exactly one literal. Otherwise unproven. */
|
|
1988
|
+
proveStartsWith(node, file, bindings, seen) {
|
|
1989
|
+
const expr = unwrapOwnExpression(node);
|
|
1990
|
+
if (ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.ExclamationToken) {
|
|
1991
|
+
const inner = this.proveStartsWith(expr.operand, file, bindings, seen);
|
|
1992
|
+
return inner === undefined ? undefined : !inner;
|
|
1993
|
+
}
|
|
1994
|
+
if (!ts.isCallExpression(expr) ||
|
|
1995
|
+
!ts.isPropertyAccessExpression(expr.expression) ||
|
|
1996
|
+
expr.expression.name.text !== 'startsWith' ||
|
|
1997
|
+
expr.arguments.length !== 1) {
|
|
1998
|
+
return undefined;
|
|
1999
|
+
}
|
|
2000
|
+
const probe = expr.arguments[0];
|
|
2001
|
+
if (probe === undefined || !ts.isStringLiteral(probe))
|
|
2002
|
+
return undefined;
|
|
2003
|
+
const receiver = this.callTargets(expr.expression.expression, file, bindings, seen);
|
|
2004
|
+
if (receiver === undefined || receiver.length !== 1)
|
|
2005
|
+
return undefined;
|
|
2006
|
+
return (receiver[0] ?? '').startsWith(probe.text);
|
|
1827
2007
|
}
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
2008
|
+
/**
|
|
2009
|
+
* Proves an `===`/`==`/`!==`/`!=` condition whose two sides are
|
|
2010
|
+
* proven scalars — the guard a component writes around a defaulted
|
|
2011
|
+
* prop (`detail === null`). Loose equality is only decided when one
|
|
2012
|
+
* side is null or undefined: those comparisons never coerce, while
|
|
2013
|
+
* `0 == false` would. Undefined when either side is unproven — the
|
|
2014
|
+
* caller then keeps both arms.
|
|
2015
|
+
*/
|
|
2016
|
+
proveEquality(node, file, bindings, seen) {
|
|
2017
|
+
const expr = unwrapOwnExpression(node);
|
|
2018
|
+
if (ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.ExclamationToken) {
|
|
2019
|
+
const inner = this.proveEquality(expr.operand, file, bindings, seen);
|
|
2020
|
+
return inner === undefined ? undefined : !inner;
|
|
2021
|
+
}
|
|
2022
|
+
if (!ts.isBinaryExpression(expr))
|
|
2023
|
+
return undefined;
|
|
2024
|
+
const op = expr.operatorToken.kind;
|
|
2025
|
+
const strict = op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken;
|
|
2026
|
+
const loose = op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsToken;
|
|
2027
|
+
if (!strict && !loose)
|
|
2028
|
+
return undefined;
|
|
2029
|
+
const left = this.provenScalar(expr.left, file, bindings, seen);
|
|
2030
|
+
if (left === 'unknown')
|
|
2031
|
+
return undefined;
|
|
2032
|
+
const right = this.provenScalar(expr.right, file, bindings, seen);
|
|
2033
|
+
if (right === 'unknown')
|
|
2034
|
+
return undefined;
|
|
2035
|
+
if (loose && !(left === null || left === undefined) && !(right === null || right === undefined))
|
|
2036
|
+
return undefined;
|
|
2037
|
+
const equal = strict
|
|
2038
|
+
? left === right
|
|
2039
|
+
: left === right || ((left === null || left === undefined) && (right === null || right === undefined));
|
|
2040
|
+
return (op === ts.SyntaxKind.ExclamationEqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsToken)
|
|
2041
|
+
? !equal
|
|
2042
|
+
: equal;
|
|
2043
|
+
}
|
|
2044
|
+
/**
|
|
2045
|
+
* The proven scalar of an expression — null, undefined, string,
|
|
2046
|
+
* number, boolean — or 'unknown' when the bounded model cannot prove
|
|
2047
|
+
* it. Per-usage prop bindings take precedence, mirroring
|
|
2048
|
+
* identifierTargets: inside the component the destructured prop
|
|
2049
|
+
* shadows any outer constant. Module constants resolve through the
|
|
2050
|
+
* same local/import precedence, so a named `NO_ENDPOINT` sentinel
|
|
2051
|
+
* proves like its literal.
|
|
2052
|
+
*/
|
|
2053
|
+
provenScalar(node, file, bindings, seen) {
|
|
2054
|
+
const expr = unwrapOwnExpression(node);
|
|
2055
|
+
if (expr.kind === ts.SyntaxKind.NullKeyword)
|
|
2056
|
+
return null;
|
|
2057
|
+
if (expr.kind === ts.SyntaxKind.TrueKeyword)
|
|
2058
|
+
return true;
|
|
2059
|
+
if (expr.kind === ts.SyntaxKind.FalseKeyword)
|
|
2060
|
+
return false;
|
|
2061
|
+
if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr))
|
|
2062
|
+
return expr.text;
|
|
2063
|
+
if (ts.isNumericLiteral(expr))
|
|
2064
|
+
return Number(expr.text);
|
|
2065
|
+
if (ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.MinusToken && ts.isNumericLiteral(expr.operand)) {
|
|
2066
|
+
return -Number(expr.operand.text);
|
|
2067
|
+
}
|
|
2068
|
+
if (!ts.isIdentifier(expr))
|
|
2069
|
+
return 'unknown';
|
|
2070
|
+
if (expr.text === 'undefined') {
|
|
2071
|
+
return this.localConstBinding(expr).state === 'found' ? 'unknown' : undefined;
|
|
2072
|
+
}
|
|
2073
|
+
const bound = bindings?.get(expr.text);
|
|
2074
|
+
if (bound !== undefined) {
|
|
2075
|
+
const key = `${bound[1]}::${expr.text}`;
|
|
2076
|
+
if (seen.has(key))
|
|
2077
|
+
return 'unknown';
|
|
2078
|
+
seen.add(key);
|
|
2079
|
+
const value = this.provenScalar(bound[0], bound[1], bindings, seen);
|
|
2080
|
+
seen.delete(key);
|
|
2081
|
+
return value;
|
|
2082
|
+
}
|
|
2083
|
+
const local = this.localConstBinding(expr);
|
|
2084
|
+
if (local.state === 'shadowed')
|
|
2085
|
+
return 'unknown';
|
|
2086
|
+
if (local.state === 'found') {
|
|
2087
|
+
const key = `${file}::${expr.text}`;
|
|
2088
|
+
if (seen.has(key))
|
|
2089
|
+
return 'unknown';
|
|
2090
|
+
seen.add(key);
|
|
2091
|
+
const value = this.provenScalar(local.initializer, file, bindings, seen);
|
|
2092
|
+
seen.delete(key);
|
|
2093
|
+
return value;
|
|
2094
|
+
}
|
|
2095
|
+
const imported = this.importedFrom(file, expr.text);
|
|
2096
|
+
const origin = imported ?? file;
|
|
2097
|
+
const initializer = this.modelOf(origin)?.constants.get(expr.text);
|
|
2098
|
+
if (initializer === undefined)
|
|
2099
|
+
return 'unknown';
|
|
2100
|
+
const key = `${origin}::${expr.text}`;
|
|
2101
|
+
if (seen.has(key))
|
|
2102
|
+
return 'unknown';
|
|
2103
|
+
seen.add(key);
|
|
2104
|
+
const value = this.provenScalar(initializer, origin, bindings, seen);
|
|
2105
|
+
seen.delete(key);
|
|
2106
|
+
return value;
|
|
2107
|
+
}
|
|
2108
|
+
expandTemplate(node, file, bindings, seen) {
|
|
2109
|
+
let pieces = [{
|
|
2110
|
+
text: node.head.text,
|
|
2111
|
+
rooted: node.head.text.startsWith('/') || node.head.text.startsWith('${'),
|
|
2112
|
+
}];
|
|
2113
|
+
for (const span of node.templateSpans) {
|
|
2114
|
+
const next = [];
|
|
2115
|
+
const substituted = this.pathHoleTargets(span.expression, file, bindings, seen);
|
|
2116
|
+
if (substituted !== undefined) {
|
|
2117
|
+
for (const piece of pieces) {
|
|
2118
|
+
for (const hole of substituted) {
|
|
2119
|
+
next.push({
|
|
2120
|
+
text: piece.text + hole + span.literal.text,
|
|
2121
|
+
rooted: piece.rooted || hole.startsWith('/'),
|
|
2122
|
+
});
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
1835
2125
|
}
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
2126
|
+
else {
|
|
2127
|
+
const bound = ts.isIdentifier(span.expression) ? bindings?.get(span.expression.text) : undefined;
|
|
2128
|
+
const holeExpr = bound?.[0] ?? span.expression;
|
|
2129
|
+
const holeFile = bound?.[1] ?? file;
|
|
2130
|
+
const hole = bound === undefined ? this.evaluate(span.expression, file) : this.evaluate(holeExpr, holeFile);
|
|
2131
|
+
if (hole.kind === 'unresolved') {
|
|
2132
|
+
if (pieces.some((piece) => !piece.rooted))
|
|
2133
|
+
return undefined;
|
|
2134
|
+
const suffix = this.trailingQuerySuffix(node, span, holeExpr, holeFile);
|
|
2135
|
+
const slot = suffix ? '?' : '${}';
|
|
2136
|
+
for (const piece of pieces)
|
|
2137
|
+
next.push({ text: piece.text + slot + span.literal.text, rooted: true });
|
|
2138
|
+
}
|
|
2139
|
+
else {
|
|
2140
|
+
for (const piece of pieces) {
|
|
2141
|
+
next.push({
|
|
2142
|
+
text: piece.text + hole.text + span.literal.text,
|
|
2143
|
+
rooted: piece.rooted || hole.text.startsWith('/'),
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
1839
2147
|
}
|
|
1840
|
-
|
|
1841
|
-
|
|
2148
|
+
if (next.length === 0 || next.length > MAX_CALL_TARGETS)
|
|
2149
|
+
return undefined;
|
|
2150
|
+
pieces = next;
|
|
2151
|
+
}
|
|
2152
|
+
return pieces.map((piece) => piece.text);
|
|
1842
2153
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
2154
|
+
/** Path-absolute hole alternatives only. Query-shaped holes stay on the suffix model. */
|
|
2155
|
+
pathHoleTargets(expr, file, bindings, seen) {
|
|
2156
|
+
const targets = this.callTargets(expr, file, bindings, seen);
|
|
2157
|
+
if (targets === undefined || targets.length === 0)
|
|
2158
|
+
return undefined;
|
|
2159
|
+
if (!targets.every((text) => text === '' || text.startsWith('/')))
|
|
2160
|
+
return undefined;
|
|
2161
|
+
return targets;
|
|
2162
|
+
}
|
|
2163
|
+
lookupTargets(node, file, bindings, seen) {
|
|
2164
|
+
const object = this.constObjectLiteral(node.expression, file, bindings, seen);
|
|
2165
|
+
if (object === undefined)
|
|
2166
|
+
return undefined;
|
|
2167
|
+
for (const property of object.properties) {
|
|
2168
|
+
if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property))
|
|
2169
|
+
return undefined;
|
|
2170
|
+
if (ts.isComputedPropertyName(property.name))
|
|
2171
|
+
return undefined;
|
|
2172
|
+
}
|
|
2173
|
+
const index = node.argumentExpression;
|
|
2174
|
+
const selected = index !== undefined && ts.isStringLiteral(index)
|
|
2175
|
+
? object.properties.filter((property) => propertyNameText(property.name) === index.text)
|
|
2176
|
+
: object.properties;
|
|
2177
|
+
if (selected.length === 0)
|
|
2178
|
+
return undefined;
|
|
2179
|
+
const targets = [];
|
|
2180
|
+
for (const property of selected) {
|
|
2181
|
+
if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property))
|
|
2182
|
+
return undefined;
|
|
2183
|
+
const initializer = ts.isPropertyAssignment(property) ? property.initializer : property.name;
|
|
2184
|
+
const part = this.callTargets(initializer, file, bindings, seen);
|
|
2185
|
+
if (part === undefined)
|
|
2186
|
+
return undefined;
|
|
2187
|
+
targets.push(...part);
|
|
2188
|
+
if (targets.length > MAX_CALL_TARGETS)
|
|
2189
|
+
return undefined;
|
|
2190
|
+
}
|
|
2191
|
+
return targets;
|
|
2192
|
+
}
|
|
2193
|
+
constObjectLiteral(expr, file, bindings, seen) {
|
|
2194
|
+
const node = unwrapOwnExpression(expr);
|
|
2195
|
+
if (ts.isObjectLiteralExpression(node))
|
|
2196
|
+
return node;
|
|
2197
|
+
if (!ts.isIdentifier(node))
|
|
2198
|
+
return undefined;
|
|
2199
|
+
const local = this.localConstBinding(node);
|
|
2200
|
+
if (local.state !== 'found')
|
|
2201
|
+
return undefined;
|
|
2202
|
+
const key = `${file}::${node.text}`;
|
|
2203
|
+
if (seen.has(key))
|
|
2204
|
+
return undefined;
|
|
2205
|
+
const initializer = unwrapOwnExpression(local.initializer);
|
|
2206
|
+
return ts.isObjectLiteralExpression(initializer) ? initializer : undefined;
|
|
2207
|
+
}
|
|
2208
|
+
finiteFunctionTargets(call, file, bindings, seen) {
|
|
2209
|
+
if (!ts.isIdentifier(call.expression))
|
|
2210
|
+
return undefined;
|
|
2211
|
+
const name = call.expression.text;
|
|
2212
|
+
const local = this.localConstBinding(call.expression);
|
|
2213
|
+
let fn;
|
|
2214
|
+
if (local.state === 'shadowed')
|
|
2215
|
+
return undefined;
|
|
2216
|
+
if (local.state === 'found') {
|
|
2217
|
+
const init = unwrapOwnExpression(local.initializer);
|
|
2218
|
+
if (!ts.isArrowFunction(init) && !ts.isFunctionExpression(init))
|
|
2219
|
+
return undefined;
|
|
2220
|
+
fn = init;
|
|
2221
|
+
}
|
|
2222
|
+
else {
|
|
2223
|
+
fn = this.modelOf(file)?.functions.get(name);
|
|
2224
|
+
}
|
|
2225
|
+
if (fn === undefined)
|
|
2226
|
+
return undefined;
|
|
2227
|
+
const key = `${file}::fn::${name}`;
|
|
2228
|
+
if (seen.has(key))
|
|
2229
|
+
return undefined;
|
|
2230
|
+
seen.add(key);
|
|
2231
|
+
const returns = collectUrlReturns(fn);
|
|
2232
|
+
if (returns === undefined) {
|
|
2233
|
+
seen.delete(key);
|
|
2234
|
+
return undefined;
|
|
2235
|
+
}
|
|
2236
|
+
const targets = [];
|
|
2237
|
+
for (const returned of returns) {
|
|
2238
|
+
const part = this.callTargets(returned, file, bindings, seen);
|
|
2239
|
+
if (part === undefined) {
|
|
2240
|
+
seen.delete(key);
|
|
2241
|
+
return undefined;
|
|
2242
|
+
}
|
|
2243
|
+
targets.push(...part);
|
|
2244
|
+
if (targets.length > MAX_CALL_TARGETS) {
|
|
2245
|
+
seen.delete(key);
|
|
2246
|
+
return undefined;
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
seen.delete(key);
|
|
2250
|
+
return targets.length === 0 ? undefined : targets;
|
|
2251
|
+
}
|
|
2252
|
+
dynamicImportAlias(use, file) {
|
|
2253
|
+
const binding = this.nearestDestructure(use);
|
|
2254
|
+
if (binding === undefined)
|
|
2255
|
+
return undefined;
|
|
2256
|
+
if (binding.exported !== 'default') {
|
|
2257
|
+
if (activeUrlBuilderIn(this.config, binding.exported, file) !== undefined) {
|
|
2258
|
+
return { kind: 'builder', name: binding.exported };
|
|
2259
|
+
}
|
|
2260
|
+
if (clientSymbolActiveIn(this.config, binding.exported, file)) {
|
|
2261
|
+
return { kind: 'client', name: binding.exported };
|
|
2262
|
+
}
|
|
2263
|
+
return undefined;
|
|
2264
|
+
}
|
|
2265
|
+
const resolved = this.resolveSpecifier(file, binding.specifier);
|
|
2266
|
+
if (resolved === null)
|
|
2267
|
+
return undefined;
|
|
2268
|
+
const exported = defaultExportIdentifier(this.modelOf(resolved)?.source);
|
|
2269
|
+
if (exported === undefined || !clientSymbolActiveIn(this.config, exported, file))
|
|
2270
|
+
return undefined;
|
|
2271
|
+
return { kind: 'client', name: exported };
|
|
2272
|
+
}
|
|
2273
|
+
nearestDestructure(use) {
|
|
2274
|
+
let scope = use.parent;
|
|
2275
|
+
while (scope !== undefined) {
|
|
2276
|
+
if (ts.isFunctionLike(scope) || ts.isSourceFile(scope)) {
|
|
2277
|
+
const found = this.destructureInScope(use, scope);
|
|
2278
|
+
if (found === 'other')
|
|
2279
|
+
return undefined;
|
|
2280
|
+
if (found !== undefined)
|
|
2281
|
+
return found;
|
|
2282
|
+
}
|
|
2283
|
+
scope = scope.parent;
|
|
2284
|
+
}
|
|
2285
|
+
return undefined;
|
|
2286
|
+
}
|
|
2287
|
+
destructureInScope(use, scope) {
|
|
2288
|
+
let best;
|
|
2289
|
+
const visit = (node) => {
|
|
2290
|
+
if (node !== scope && ts.isFunctionLike(node))
|
|
2291
|
+
return;
|
|
2292
|
+
if (ts.isVariableDeclaration(node) && node.name.getEnd() <= use.getStart()) {
|
|
2293
|
+
const isConst = (ts.getCombinedNodeFlags(node) & ts.NodeFlags.Const) !== 0;
|
|
2294
|
+
if (ts.isIdentifier(node.name) && node.name.text === use.text) {
|
|
2295
|
+
if (best === undefined || node.name.getStart() > best.pos) {
|
|
2296
|
+
best = { pos: node.name.getStart(), result: 'other' };
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
if (ts.isObjectBindingPattern(node.name)) {
|
|
2300
|
+
for (const element of node.name.elements) {
|
|
2301
|
+
if (!ts.isIdentifier(element.name) || element.name.text !== use.text || element.dotDotDotToken !== undefined) {
|
|
2302
|
+
continue;
|
|
2303
|
+
}
|
|
2304
|
+
if (best !== undefined && element.name.getStart() <= best.pos)
|
|
2305
|
+
continue;
|
|
2306
|
+
const specifier = isConst ? awaitImportSpecifier(node.initializer) : undefined;
|
|
2307
|
+
const exported = element.propertyName !== undefined && ts.isIdentifier(element.propertyName)
|
|
2308
|
+
? element.propertyName.text
|
|
2309
|
+
: element.name.text;
|
|
2310
|
+
best = {
|
|
2311
|
+
pos: element.name.getStart(),
|
|
2312
|
+
result: specifier === undefined ? 'other' : { exported, specifier },
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
1849
2316
|
}
|
|
1850
|
-
|
|
2317
|
+
ts.forEachChild(node, visit);
|
|
2318
|
+
};
|
|
2319
|
+
visit(scope);
|
|
2320
|
+
return best?.result;
|
|
2321
|
+
}
|
|
2322
|
+
importedExport(file, localName) {
|
|
2323
|
+
const model = this.modelOf(file);
|
|
2324
|
+
if (model === undefined)
|
|
2325
|
+
return null;
|
|
2326
|
+
let found = null;
|
|
2327
|
+
const visit = (node) => {
|
|
2328
|
+
if (found !== null)
|
|
2329
|
+
return;
|
|
2330
|
+
if (!ts.isImportDeclaration(node) || !ts.isStringLiteral(node.moduleSpecifier)) {
|
|
2331
|
+
ts.forEachChild(node, visit);
|
|
2332
|
+
return;
|
|
2333
|
+
}
|
|
2334
|
+
const specifier = node.moduleSpecifier.text;
|
|
2335
|
+
if (!specifier.startsWith('./') && !specifier.startsWith('../'))
|
|
2336
|
+
return;
|
|
2337
|
+
const resolved = this.resolveSpecifier(file, specifier);
|
|
2338
|
+
if (resolved === null)
|
|
2339
|
+
return;
|
|
2340
|
+
const clause = node.importClause;
|
|
2341
|
+
if (clause?.name?.text === localName) {
|
|
2342
|
+
found = { file: resolved, exported: 'default' };
|
|
2343
|
+
return;
|
|
2344
|
+
}
|
|
2345
|
+
if (clause?.namedBindings !== undefined && ts.isNamedImports(clause.namedBindings)) {
|
|
2346
|
+
for (const element of clause.namedBindings.elements) {
|
|
2347
|
+
if (element.name.text === localName) {
|
|
2348
|
+
found = { file: resolved, exported: element.propertyName?.text ?? element.name.text };
|
|
2349
|
+
return;
|
|
2350
|
+
}
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
};
|
|
2354
|
+
visit(model.source);
|
|
2355
|
+
return found;
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
function modeledWrapperSpans(model, config, file) {
|
|
2359
|
+
if (model === undefined)
|
|
2360
|
+
return [];
|
|
2361
|
+
const spans = [...model.wrappers.values()].map((wrapper) => [wrapper.start, wrapper.end]);
|
|
2362
|
+
for (const entry of config.wrapperFunctions ?? []) {
|
|
2363
|
+
if (!entryInScope(file, entry))
|
|
2364
|
+
continue;
|
|
2365
|
+
const fn = model.functions.get(entry.name);
|
|
2366
|
+
if (fn === undefined)
|
|
2367
|
+
continue;
|
|
2368
|
+
if (containsClientCall(fn, config, file) || containsConfiguredWrapperCall(fn, config, file)) {
|
|
2369
|
+
spans.push([fn.getStart(), fn.getEnd()]);
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
return spans;
|
|
2373
|
+
}
|
|
2374
|
+
function containsConfiguredWrapperCall(node, config, file) {
|
|
2375
|
+
let found = false;
|
|
2376
|
+
const visit = (current) => {
|
|
2377
|
+
if (found || !ts.isCallExpression(current)) {
|
|
2378
|
+
ts.forEachChild(current, visit);
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2381
|
+
if (ts.isIdentifier(current.expression) && activeWrapperIn(config, current.expression.text, file) !== undefined) {
|
|
2382
|
+
found = true;
|
|
2383
|
+
return;
|
|
2384
|
+
}
|
|
2385
|
+
ts.forEachChild(current, visit);
|
|
2386
|
+
};
|
|
2387
|
+
visit(node);
|
|
2388
|
+
return found;
|
|
2389
|
+
}
|
|
2390
|
+
function collectUrlReturns(fn) {
|
|
2391
|
+
const body = fn.body;
|
|
2392
|
+
if (body === undefined)
|
|
2393
|
+
return undefined;
|
|
2394
|
+
if (!ts.isBlock(body))
|
|
2395
|
+
return [body];
|
|
2396
|
+
const returns = [];
|
|
2397
|
+
let poison = false;
|
|
2398
|
+
const visit = (node) => {
|
|
2399
|
+
if (poison)
|
|
2400
|
+
return;
|
|
2401
|
+
if (node !== body && ts.isFunctionLike(node))
|
|
2402
|
+
return;
|
|
2403
|
+
if (ts.isReturnStatement(node)) {
|
|
2404
|
+
if (node.expression === undefined) {
|
|
2405
|
+
poison = true;
|
|
2406
|
+
return;
|
|
2407
|
+
}
|
|
2408
|
+
const expr = unwrapOwnExpression(node.expression);
|
|
2409
|
+
if (expr.kind === ts.SyntaxKind.NullKeyword)
|
|
2410
|
+
return;
|
|
2411
|
+
if (ts.isIdentifier(expr) && expr.text === 'undefined')
|
|
2412
|
+
return;
|
|
2413
|
+
returns.push(node.expression);
|
|
2414
|
+
return;
|
|
2415
|
+
}
|
|
2416
|
+
ts.forEachChild(node, visit);
|
|
2417
|
+
};
|
|
2418
|
+
visit(body);
|
|
2419
|
+
if (poison || returns.length === 0)
|
|
2420
|
+
return undefined;
|
|
2421
|
+
return returns;
|
|
2422
|
+
}
|
|
2423
|
+
function awaitImportSpecifier(expr) {
|
|
2424
|
+
if (expr === undefined)
|
|
2425
|
+
return undefined;
|
|
2426
|
+
const inner = unwrapOwnExpression(ts.isAwaitExpression(expr) ? expr.expression : expr);
|
|
2427
|
+
if (!ts.isCallExpression(inner) || inner.expression.kind !== ts.SyntaxKind.ImportKeyword)
|
|
2428
|
+
return undefined;
|
|
2429
|
+
const arg = inner.arguments[0];
|
|
2430
|
+
if (arg === undefined || !ts.isStringLiteral(arg))
|
|
2431
|
+
return undefined;
|
|
2432
|
+
if (!arg.text.startsWith('./') && !arg.text.startsWith('../'))
|
|
2433
|
+
return undefined;
|
|
2434
|
+
return arg.text;
|
|
2435
|
+
}
|
|
2436
|
+
function defaultExportedFunction(model) {
|
|
2437
|
+
for (const statement of model.source.statements) {
|
|
2438
|
+
if (ts.isFunctionDeclaration(statement) &&
|
|
2439
|
+
statement.name !== undefined &&
|
|
2440
|
+
statement.modifiers?.some((mod) => mod.kind === ts.SyntaxKind.DefaultKeyword) === true) {
|
|
2441
|
+
return statement;
|
|
2442
|
+
}
|
|
2443
|
+
if (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression)) {
|
|
2444
|
+
return model.functions.get(statement.expression.text);
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
return undefined;
|
|
2448
|
+
}
|
|
2449
|
+
function defaultExportIdentifier(source) {
|
|
2450
|
+
if (source === undefined)
|
|
2451
|
+
return undefined;
|
|
2452
|
+
for (const statement of source.statements) {
|
|
2453
|
+
if (ts.isFunctionDeclaration(statement) &&
|
|
2454
|
+
statement.name !== undefined &&
|
|
2455
|
+
statement.modifiers?.some((mod) => mod.kind === ts.SyntaxKind.DefaultKeyword) === true) {
|
|
2456
|
+
return statement.name.text;
|
|
2457
|
+
}
|
|
2458
|
+
if (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression)) {
|
|
2459
|
+
return statement.expression.text;
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
return undefined;
|
|
2463
|
+
}
|
|
2464
|
+
function firstIdentifierParam(fn) {
|
|
2465
|
+
const first = fn.parameters[0];
|
|
2466
|
+
return first !== undefined && ts.isIdentifier(first.name) ? first.name.text : undefined;
|
|
2467
|
+
}
|
|
2468
|
+
function argumentForParameter(fn, paramName, call) {
|
|
2469
|
+
for (let index = 0; index < fn.parameters.length; index += 1) {
|
|
2470
|
+
const parameter = fn.parameters[index];
|
|
2471
|
+
if (parameter === undefined)
|
|
2472
|
+
continue;
|
|
2473
|
+
if (ts.isIdentifier(parameter.name) && parameter.name.text === paramName)
|
|
2474
|
+
return call.arguments[index];
|
|
2475
|
+
if (!ts.isObjectBindingPattern(parameter.name))
|
|
2476
|
+
continue;
|
|
2477
|
+
for (const element of parameter.name.elements) {
|
|
2478
|
+
if (!ts.isIdentifier(element.name) || element.name.text !== paramName)
|
|
2479
|
+
continue;
|
|
2480
|
+
const prop = element.propertyName !== undefined && ts.isIdentifier(element.propertyName)
|
|
2481
|
+
? element.propertyName.text
|
|
2482
|
+
: element.name.text;
|
|
2483
|
+
const argument = call.arguments[index];
|
|
2484
|
+
return argument === undefined ? undefined : objectProperty(argument, prop);
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
return undefined;
|
|
2488
|
+
}
|
|
2489
|
+
function objectProperty(expr, name) {
|
|
2490
|
+
const unwrapped = unwrapOwnExpression(expr);
|
|
2491
|
+
if (!ts.isObjectLiteralExpression(unwrapped))
|
|
2492
|
+
return undefined;
|
|
2493
|
+
for (const property of unwrapped.properties) {
|
|
2494
|
+
if (ts.isShorthandPropertyAssignment(property) && property.name.text === name)
|
|
2495
|
+
return property.name;
|
|
2496
|
+
if (ts.isPropertyAssignment(property) &&
|
|
2497
|
+
ts.isIdentifier(property.name) &&
|
|
2498
|
+
property.name.text === name) {
|
|
2499
|
+
return property.initializer;
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
return undefined;
|
|
2503
|
+
}
|
|
2504
|
+
function jsxAttributeExpression(node, name) {
|
|
2505
|
+
for (const attr of node.attributes.properties) {
|
|
2506
|
+
if (!ts.isJsxAttribute(attr) || !ts.isIdentifier(attr.name) || attr.name.text !== name)
|
|
2507
|
+
continue;
|
|
2508
|
+
const init = attr.initializer;
|
|
2509
|
+
if (init === undefined)
|
|
2510
|
+
return undefined;
|
|
2511
|
+
if (ts.isStringLiteral(init))
|
|
2512
|
+
return init;
|
|
2513
|
+
if (ts.isJsxExpression(init) && init.expression !== undefined && ts.isExpression(init.expression)) {
|
|
2514
|
+
return init.expression;
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
return undefined;
|
|
2518
|
+
}
|
|
2519
|
+
function resolvesToDeclaredWrapper(table, name, file, config) {
|
|
2520
|
+
const declared = table.findFunction(name, file);
|
|
2521
|
+
// No local binding: the configured name still stands, and an unmodeled
|
|
2522
|
+
// declaration stays a finding. A binding that is a different function
|
|
2523
|
+
// (same local name, other module, no client call) is not this wrapper.
|
|
2524
|
+
if (declared === undefined)
|
|
2525
|
+
return true;
|
|
2526
|
+
return containsClientCall(declared.fn, config, declared.file)
|
|
2527
|
+
|| containsConfiguredWrapperCall(declared.fn, config, declared.file);
|
|
2528
|
+
}
|
|
2529
|
+
function extractJsxWrapper(node, file, config, table, calls, unresolved) {
|
|
2530
|
+
if (!ts.isIdentifier(node.tagName))
|
|
2531
|
+
return;
|
|
2532
|
+
const wrapperConfig = activeWrapperIn(config, node.tagName.text, file);
|
|
2533
|
+
if (wrapperConfig === undefined || !resolvesToDeclaredWrapper(table, wrapperConfig.name, file, config))
|
|
2534
|
+
return;
|
|
2535
|
+
const source = table.modelOf(file)?.source;
|
|
2536
|
+
if (source === undefined)
|
|
2537
|
+
return;
|
|
2538
|
+
const location = locationOf(file, source, node);
|
|
2539
|
+
const bindings = new Map();
|
|
2540
|
+
// Absent `parameter` means the first parameter — the original
|
|
2541
|
+
// single-parameter wrapper contract — the same fallback the direct
|
|
2542
|
+
// call path applies.
|
|
2543
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
2544
|
+
const paramName = wrapperConfig.parameter ?? (declared === undefined ? undefined : firstIdentifierParam(declared.fn));
|
|
2545
|
+
if (paramName !== undefined) {
|
|
2546
|
+
const attr = jsxAttributeExpression(node, paramName);
|
|
2547
|
+
if (attr !== undefined) {
|
|
2548
|
+
bindings.set(paramName, [attr, file]);
|
|
2549
|
+
if (resolveWrapperTargetThroughJsxProps(attr, node, paramName, wrapperConfig, file, config, table, bindings, calls, unresolved, location, [])) {
|
|
2550
|
+
return;
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, []);
|
|
2555
|
+
}
|
|
2556
|
+
function resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, reads) {
|
|
2557
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
2558
|
+
if (declared === undefined) {
|
|
2559
|
+
unresolved.push({
|
|
2560
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
2561
|
+
detail: (`wrapper '${wrapperConfig.name}' has no simple single-return client call in the ` +
|
|
2562
|
+
'scanned set; its call targets cannot be resolved statically'),
|
|
2563
|
+
location,
|
|
2564
|
+
});
|
|
2565
|
+
return;
|
|
2566
|
+
}
|
|
2567
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bindings, 0, new Set());
|
|
2568
|
+
if (body === 'unmodeled') {
|
|
2569
|
+
unresolved.push({
|
|
2570
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
2571
|
+
detail: (`wrapper '${wrapperConfig.name}' has no simple single-return client call in the ` +
|
|
2572
|
+
'scanned set; its call targets cannot be resolved statically'),
|
|
2573
|
+
location,
|
|
2574
|
+
});
|
|
2575
|
+
return;
|
|
2576
|
+
}
|
|
2577
|
+
if (body === 'unresolved') {
|
|
2578
|
+
unresolved.push({
|
|
2579
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
2580
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
2581
|
+
location,
|
|
2582
|
+
});
|
|
2583
|
+
return;
|
|
2584
|
+
}
|
|
2585
|
+
const byMethod = new Map();
|
|
2586
|
+
for (const target of body) {
|
|
2587
|
+
const list = byMethod.get(target.method) ?? [];
|
|
2588
|
+
list.push(target.raw);
|
|
2589
|
+
byMethod.set(target.method, list);
|
|
2590
|
+
}
|
|
2591
|
+
for (const [method, raws] of byMethod) {
|
|
2592
|
+
recordRawTargets(raws, method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
function wrapperBodyTargets(fn, fnFile, entry, config, table, bindings, depth, stack) {
|
|
2596
|
+
const stackKey = `${fnFile}::${entry.name}`;
|
|
2597
|
+
if (depth > MAX_WRAPPER_DEPTH || stack.has(stackKey))
|
|
2598
|
+
return 'unmodeled';
|
|
2599
|
+
stack.add(stackKey);
|
|
2600
|
+
const found = [];
|
|
2601
|
+
let saw = false;
|
|
2602
|
+
let failed = false;
|
|
2603
|
+
const visit = (node) => {
|
|
2604
|
+
if (failed || !ts.isCallExpression(node)) {
|
|
2605
|
+
if (!failed)
|
|
2606
|
+
ts.forEachChild(node, visit);
|
|
2607
|
+
return;
|
|
2608
|
+
}
|
|
2609
|
+
const client = clientCallOf(node, config, fnFile, table);
|
|
2610
|
+
if (client !== undefined) {
|
|
2611
|
+
saw = true;
|
|
2612
|
+
if (entry.methodFromBody !== true && client.method !== entry.method) {
|
|
2613
|
+
failed = true;
|
|
2614
|
+
return;
|
|
2615
|
+
}
|
|
2616
|
+
const targets = table.callTargets(client.url, fnFile, bindings);
|
|
2617
|
+
if (targets === undefined) {
|
|
2618
|
+
failed = true;
|
|
2619
|
+
return;
|
|
2620
|
+
}
|
|
2621
|
+
const method = entry.methodFromBody === true ? client.method : entry.method;
|
|
2622
|
+
for (const raw of targets)
|
|
2623
|
+
found.push({ method, raw });
|
|
2624
|
+
return;
|
|
2625
|
+
}
|
|
2626
|
+
const nestedName = ts.isIdentifier(node.expression) ? node.expression.text : undefined;
|
|
2627
|
+
const nested = nestedName === undefined ? undefined : activeWrapperIn(config, nestedName, fnFile);
|
|
2628
|
+
if (nested !== undefined && nested.name !== entry.name) {
|
|
2629
|
+
saw = true;
|
|
2630
|
+
const innerFn = table.findFunction(nested.name, fnFile);
|
|
2631
|
+
if (innerFn === undefined) {
|
|
2632
|
+
failed = true;
|
|
2633
|
+
return;
|
|
2634
|
+
}
|
|
2635
|
+
const paramName = nested.parameter ?? firstIdentifierParam(innerFn.fn);
|
|
2636
|
+
const next = new Map(bindings);
|
|
2637
|
+
if (paramName !== undefined) {
|
|
2638
|
+
const argument = argumentForParameter(innerFn.fn, paramName, node);
|
|
2639
|
+
if (argument !== undefined) {
|
|
2640
|
+
const followed = ts.isIdentifier(argument) ? bindings.get(argument.text) : undefined;
|
|
2641
|
+
next.set(paramName, followed ?? [argument, fnFile]);
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
const inner = wrapperBodyTargets(innerFn.fn, innerFn.file, nested, config, table, next, depth + 1, stack);
|
|
2645
|
+
if (inner === 'unmodeled' || inner === 'unresolved') {
|
|
2646
|
+
failed = true;
|
|
2647
|
+
return;
|
|
2648
|
+
}
|
|
2649
|
+
found.push(...inner);
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
ts.forEachChild(node, visit);
|
|
2653
|
+
};
|
|
2654
|
+
if (fn.body !== undefined)
|
|
2655
|
+
visit(fn.body);
|
|
2656
|
+
stack.delete(stackKey);
|
|
2657
|
+
if (!saw)
|
|
2658
|
+
return 'unmodeled';
|
|
2659
|
+
if (failed || found.length === 0)
|
|
2660
|
+
return 'unresolved';
|
|
2661
|
+
return found;
|
|
2662
|
+
}
|
|
2663
|
+
/**
|
|
2664
|
+
* The configured client symbol an `X.interceptors.response.use(...)`
|
|
2665
|
+
* call registers on, or null when the call is not such a registration
|
|
2666
|
+
* on `axios` or a configured client symbol. Request interceptors are
|
|
2667
|
+
* deliberately out of scope: a request-side handler edits the config
|
|
2668
|
+
* BEFORE the first send, so a re-send modeled there would record a call
|
|
2669
|
+
* the scan has never seen.
|
|
2670
|
+
*/
|
|
2671
|
+
function interceptorReplayClient(call, file, config) {
|
|
2672
|
+
const use = call.expression;
|
|
2673
|
+
if (!ts.isPropertyAccessExpression(use) || use.name.text !== 'use')
|
|
2674
|
+
return null;
|
|
2675
|
+
const response = use.expression;
|
|
2676
|
+
if (!ts.isPropertyAccessExpression(response) || response.name.text !== 'response')
|
|
2677
|
+
return null;
|
|
2678
|
+
const interceptors = response.expression;
|
|
2679
|
+
if (!ts.isPropertyAccessExpression(interceptors) || interceptors.name.text !== 'interceptors')
|
|
2680
|
+
return null;
|
|
2681
|
+
const client = interceptors.expression;
|
|
2682
|
+
if (!ts.isIdentifier(client))
|
|
2683
|
+
return null;
|
|
2684
|
+
if (client.text !== 'axios' && !clientSymbolActiveIn(config, client.text, file))
|
|
2685
|
+
return null;
|
|
2686
|
+
return client.text;
|
|
2687
|
+
}
|
|
2688
|
+
/** `error.config` for the handler's own error parameter — and nothing else. */
|
|
2689
|
+
function isOwnErrorConfig(expr, errorParamName) {
|
|
2690
|
+
if (errorParamName === undefined || expr === undefined)
|
|
2691
|
+
return false;
|
|
2692
|
+
// Real handlers type the config on the way through — PM's
|
|
2693
|
+
// `const config = error.config as RetriableConfig | undefined;` — so
|
|
2694
|
+
// the alias must be recognized through as/satisfies/non-null/parens.
|
|
2695
|
+
const peeled = unwrapOwnExpression(expr);
|
|
2696
|
+
return (ts.isPropertyAccessExpression(peeled) &&
|
|
2697
|
+
ts.isIdentifier(peeled.expression) &&
|
|
2698
|
+
peeled.expression.text === errorParamName &&
|
|
2699
|
+
peeled.name.text === 'config');
|
|
2700
|
+
}
|
|
2701
|
+
/**
|
|
2702
|
+
* Records the replay calls a response-error handler performs: calls on
|
|
2703
|
+
* the SAME client symbol whose single argument is the intercepted
|
|
2704
|
+
* error's own request config — `error.config`, a destructured
|
|
2705
|
+
* `{ config }` parameter binding, or a `const` bound to `error.config`.
|
|
2706
|
+
* Everything else inside the handler keeps today's model — a different
|
|
2707
|
+
* symbol, a different argument, a second argument, a verb call — because
|
|
2708
|
+
* only the re-send-the-original-request shape is a replay of a request
|
|
2709
|
+
* this scan has already recorded.
|
|
2710
|
+
*/
|
|
2711
|
+
function collectInterceptorReplays(call, file, config, replayCalls) {
|
|
2712
|
+
const clientName = interceptorReplayClient(call, file, config);
|
|
2713
|
+
if (clientName === null)
|
|
2714
|
+
return;
|
|
2715
|
+
const handler = call.arguments[1];
|
|
2716
|
+
if (handler === undefined || (!ts.isArrowFunction(handler) && !ts.isFunctionExpression(handler)))
|
|
2717
|
+
return;
|
|
2718
|
+
const first = handler.parameters[0];
|
|
2719
|
+
if (first === undefined)
|
|
2720
|
+
return;
|
|
2721
|
+
let errorParamName;
|
|
2722
|
+
const boundConfigNames = new Set();
|
|
2723
|
+
if (ts.isIdentifier(first.name)) {
|
|
2724
|
+
errorParamName = first.name.text;
|
|
2725
|
+
}
|
|
2726
|
+
else if (ts.isObjectBindingPattern(first.name)) {
|
|
2727
|
+
for (const element of first.name.elements) {
|
|
2728
|
+
if (element.dotDotDotToken !== undefined || !ts.isIdentifier(element.name))
|
|
2729
|
+
continue;
|
|
2730
|
+
const propertyName = element.propertyName === undefined
|
|
2731
|
+
? element.name.text
|
|
2732
|
+
: ts.isIdentifier(element.propertyName)
|
|
2733
|
+
? element.propertyName.text
|
|
2734
|
+
: undefined;
|
|
2735
|
+
if (propertyName === 'config')
|
|
2736
|
+
boundConfigNames.add(element.name.text);
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
else {
|
|
2740
|
+
return;
|
|
2741
|
+
}
|
|
2742
|
+
const configAliases = new Set();
|
|
2743
|
+
if (errorParamName !== undefined && handler.body !== undefined) {
|
|
2744
|
+
const collectAlias = (node) => {
|
|
2745
|
+
if (ts.isVariableDeclaration(node) &&
|
|
2746
|
+
ts.isIdentifier(node.name) &&
|
|
2747
|
+
isOwnErrorConfig(node.initializer, errorParamName)) {
|
|
2748
|
+
configAliases.add(node.name.text);
|
|
2749
|
+
}
|
|
2750
|
+
ts.forEachChild(node, collectAlias);
|
|
2751
|
+
};
|
|
2752
|
+
collectAlias(handler.body);
|
|
2753
|
+
}
|
|
2754
|
+
if (handler.body === undefined)
|
|
2755
|
+
return;
|
|
2756
|
+
const collectReplays = (node) => {
|
|
2757
|
+
if (ts.isCallExpression(node)) {
|
|
2758
|
+
const expression = node.expression;
|
|
2759
|
+
const argument = node.arguments[0];
|
|
2760
|
+
if (ts.isIdentifier(expression) &&
|
|
2761
|
+
expression.text === clientName &&
|
|
2762
|
+
node.arguments.length === 1 &&
|
|
2763
|
+
argument !== undefined &&
|
|
2764
|
+
(isOwnErrorConfig(argument, errorParamName) ||
|
|
2765
|
+
(ts.isIdentifier(argument) && (boundConfigNames.has(argument.text) || configAliases.has(argument.text))))) {
|
|
2766
|
+
replayCalls.add(node);
|
|
2767
|
+
return;
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
ts.forEachChild(node, collectReplays);
|
|
2771
|
+
};
|
|
2772
|
+
collectReplays(handler.body);
|
|
2773
|
+
}
|
|
2774
|
+
const jsxComponentIndexCache = new WeakMap();
|
|
2775
|
+
/**
|
|
2776
|
+
* Builds (once per scan) the component/usage index the JSX prop flow
|
|
2777
|
+
* resolves through: module-scope function-likes whose first parameter is
|
|
2778
|
+
* `props` or destructured, plus every JSX element with an identifier
|
|
2779
|
+
* tagName. The whole scanned set participates — usage sites are
|
|
2780
|
+
* dataflow inputs, and scan scoping narrows fact emission, not the
|
|
2781
|
+
* dataflow's ability to resolve product code.
|
|
2782
|
+
*/
|
|
2783
|
+
function jsxComponentIndex(table) {
|
|
2784
|
+
const cached = jsxComponentIndexCache.get(table);
|
|
2785
|
+
if (cached !== undefined)
|
|
2786
|
+
return cached;
|
|
2787
|
+
const index = { components: new Map(), usages: new Map() };
|
|
2788
|
+
for (const file of [...table.scannedFiles.keys()].sort()) {
|
|
2789
|
+
const model = table.modelOf(file);
|
|
2790
|
+
if (model === undefined)
|
|
2791
|
+
continue;
|
|
2792
|
+
for (const [name, fn] of model.functions) {
|
|
2793
|
+
if (index.components.has(name))
|
|
2794
|
+
continue;
|
|
2795
|
+
const first = fn.parameters[0];
|
|
2796
|
+
if (first === undefined)
|
|
2797
|
+
continue;
|
|
2798
|
+
if (!ts.isIdentifier(first.name) && !ts.isObjectBindingPattern(first.name))
|
|
2799
|
+
continue;
|
|
2800
|
+
index.components.set(name, { name, file, fn, source: model.source });
|
|
2801
|
+
}
|
|
2802
|
+
const collectUsages = (node) => {
|
|
2803
|
+
if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) {
|
|
2804
|
+
if (ts.isIdentifier(node.tagName)) {
|
|
2805
|
+
const sites = index.usages.get(node.tagName.text) ?? [];
|
|
2806
|
+
sites.push({ file, element: node });
|
|
2807
|
+
index.usages.set(node.tagName.text, sites);
|
|
2808
|
+
}
|
|
2809
|
+
}
|
|
2810
|
+
ts.forEachChild(node, collectUsages);
|
|
2811
|
+
};
|
|
2812
|
+
collectUsages(model.source);
|
|
2813
|
+
}
|
|
2814
|
+
jsxComponentIndexCache.set(table, index);
|
|
2815
|
+
return index;
|
|
2816
|
+
}
|
|
2817
|
+
/**
|
|
2818
|
+
* The prop name `localName` binds inside a destructured first parameter,
|
|
2819
|
+
* or undefined. Renames count (`{ apiEndpoint: endpoint }` binds the
|
|
2820
|
+
* `apiEndpoint` prop under the local `endpoint`); rest elements and
|
|
2821
|
+
* non-identifier names bind nothing — an unbound name fails closed.
|
|
2822
|
+
*/
|
|
2823
|
+
function componentPropName(component, localName) {
|
|
2824
|
+
const first = component.fn.parameters[0];
|
|
2825
|
+
if (first === undefined || !ts.isObjectBindingPattern(first.name))
|
|
2826
|
+
return undefined;
|
|
2827
|
+
for (const element of first.name.elements) {
|
|
2828
|
+
if (!ts.isBindingElement(element) || element.dotDotDotToken !== undefined || !ts.isIdentifier(element.name))
|
|
2829
|
+
continue;
|
|
2830
|
+
const propName = element.propertyName === undefined
|
|
2831
|
+
? element.name.text
|
|
2832
|
+
: ts.isIdentifier(element.propertyName)
|
|
2833
|
+
? element.propertyName.text
|
|
2834
|
+
: undefined;
|
|
2835
|
+
if (propName !== undefined && element.name.text === localName)
|
|
2836
|
+
return propName;
|
|
2837
|
+
}
|
|
2838
|
+
return undefined;
|
|
2839
|
+
}
|
|
2840
|
+
/**
|
|
2841
|
+
* The component-prop reference an expression resolves to inside
|
|
2842
|
+
* `component`: a destructured prop (`apiEndpoint`), a `props.` access
|
|
2843
|
+
* (`props.apiEndpoint`), either with a member path (`duplicate.apiBasePath`,
|
|
2844
|
+
* `props.row.apiBasePath`). Undefined for anything else.
|
|
2845
|
+
*/
|
|
2846
|
+
function componentPropRef(expr, component) {
|
|
2847
|
+
const first = component.fn.parameters[0];
|
|
2848
|
+
if (first === undefined)
|
|
2849
|
+
return undefined;
|
|
2850
|
+
const takesPropsObject = ts.isIdentifier(first.name) && first.name.text === 'props';
|
|
2851
|
+
const segments = [];
|
|
2852
|
+
let current = expr;
|
|
2853
|
+
while (ts.isPropertyAccessExpression(current)) {
|
|
2854
|
+
segments.unshift(current.name.text);
|
|
2855
|
+
current = current.expression;
|
|
2856
|
+
}
|
|
2857
|
+
if (!ts.isIdentifier(current))
|
|
2858
|
+
return undefined;
|
|
2859
|
+
if (segments.length === 0) {
|
|
2860
|
+
const propName = componentPropName(component, current.text);
|
|
2861
|
+
return propName === undefined ? undefined : { propName, path: [] };
|
|
2862
|
+
}
|
|
2863
|
+
if (takesPropsObject && current.text === 'props') {
|
|
2864
|
+
const propName = segments[0];
|
|
2865
|
+
return propName === undefined ? undefined : { propName, path: segments.slice(1) };
|
|
2866
|
+
}
|
|
2867
|
+
const propName = componentPropName(component, current.text);
|
|
2868
|
+
return propName === undefined ? undefined : { propName, path: segments };
|
|
2869
|
+
}
|
|
2870
|
+
/** The innermost indexed component of `file` whose span contains `node`. */
|
|
2871
|
+
function innermostComponent(components, file, node) {
|
|
2872
|
+
let best;
|
|
2873
|
+
let bestStart = -1;
|
|
2874
|
+
for (const component of components.values()) {
|
|
2875
|
+
if (component.file !== file)
|
|
2876
|
+
continue;
|
|
2877
|
+
const start = component.fn.getStart(component.source);
|
|
2878
|
+
if (node.getStart(component.source) < start || node.getEnd() > component.fn.getEnd())
|
|
2879
|
+
continue;
|
|
2880
|
+
if (best === undefined || start > bestStart) {
|
|
2881
|
+
best = component;
|
|
2882
|
+
bestStart = start;
|
|
2883
|
+
}
|
|
2884
|
+
}
|
|
2885
|
+
return best;
|
|
2886
|
+
}
|
|
2887
|
+
/**
|
|
2888
|
+
* Every target a component prop provably carries, read off the
|
|
2889
|
+
* component's JSX usage sites in the scanned set. Each site must pass a
|
|
2890
|
+
* literal or statically resolvable value; a value may also forward
|
|
2891
|
+
* another component's prop unchanged, which resolves through THAT
|
|
2892
|
+
* component's sites — exactly one hop, then the value must be concrete.
|
|
2893
|
+
* A missing attribute, an unresolvable value, a second hop, or a
|
|
2894
|
+
* component without usage sites fails closed: a guessed target would
|
|
2895
|
+
* certify a route the server may never serve.
|
|
2896
|
+
*/
|
|
2897
|
+
function propTargetsThroughUsages(index, component, propName, depth, stack, table) {
|
|
2898
|
+
const stackKey = `${component.file}::${component.name}::${propName}`;
|
|
2899
|
+
if (stack.has(stackKey))
|
|
2900
|
+
return 'unresolved';
|
|
2901
|
+
const sites = index.usages.get(component.name);
|
|
2902
|
+
if (sites === undefined || sites.length === 0)
|
|
2903
|
+
return 'unresolved';
|
|
2904
|
+
const nextStack = new Set(stack);
|
|
2905
|
+
nextStack.add(stackKey);
|
|
2906
|
+
const targets = [];
|
|
2907
|
+
for (const site of sites) {
|
|
2908
|
+
const attr = jsxAttributeExpression(site.element, propName);
|
|
2909
|
+
if (attr === undefined)
|
|
2910
|
+
return 'unresolved';
|
|
2911
|
+
const enclosing = innermostComponent(index.components, site.file, site.element);
|
|
2912
|
+
if (enclosing !== undefined) {
|
|
2913
|
+
const forwarded = componentPropRef(attr, enclosing);
|
|
2914
|
+
if (forwarded !== undefined) {
|
|
2915
|
+
if (forwarded.path.length > 0 || depth >= 1)
|
|
2916
|
+
return 'unresolved';
|
|
2917
|
+
const deeper = propTargetsThroughUsages(index, enclosing, forwarded.propName, depth + 1, nextStack, table);
|
|
2918
|
+
if (deeper === 'unresolved')
|
|
2919
|
+
return 'unresolved';
|
|
2920
|
+
targets.push(...deeper);
|
|
2921
|
+
continue;
|
|
2922
|
+
}
|
|
2923
|
+
}
|
|
2924
|
+
const direct = table.callTargets(attr, site.file);
|
|
2925
|
+
if (direct === undefined || direct.length === 0)
|
|
2926
|
+
return 'unresolved';
|
|
2927
|
+
targets.push(...direct);
|
|
2928
|
+
}
|
|
2929
|
+
return targets;
|
|
2930
|
+
}
|
|
2931
|
+
/** True for exactly the literal scalars a destructuring default may take: string, number, boolean, null, undefined. */
|
|
2932
|
+
function isLiteralScalar(expr) {
|
|
2933
|
+
const unwrapped = unwrapOwnExpression(expr);
|
|
2934
|
+
return unwrapped.kind === ts.SyntaxKind.NullKeyword
|
|
2935
|
+
|| unwrapped.kind === ts.SyntaxKind.TrueKeyword
|
|
2936
|
+
|| unwrapped.kind === ts.SyntaxKind.FalseKeyword
|
|
2937
|
+
|| ts.isStringLiteral(unwrapped)
|
|
2938
|
+
|| ts.isNoSubstitutionTemplateLiteral(unwrapped)
|
|
2939
|
+
|| ts.isNumericLiteral(unwrapped)
|
|
2940
|
+
|| (ts.isPrefixUnaryExpression(unwrapped)
|
|
2941
|
+
&& unwrapped.operator === ts.SyntaxKind.MinusToken
|
|
2942
|
+
&& ts.isNumericLiteral(unwrapped.operand))
|
|
2943
|
+
|| (ts.isIdentifier(unwrapped) && unwrapped.text === 'undefined');
|
|
2944
|
+
}
|
|
2945
|
+
/**
|
|
2946
|
+
* The literal initializer a destructured prop binds when its attribute
|
|
2947
|
+
* is omitted, or undefined. Renames count (`{ apiEndpoint: endpoint =
|
|
2948
|
+
* '/x/' }`); anything beyond a literal scalar — a call, an identifier,
|
|
2949
|
+
* a computed value — is not a provable default and fails closed, as
|
|
2950
|
+
* does a prop declared without an initializer.
|
|
2951
|
+
*/
|
|
2952
|
+
function componentPropDefault(component, propName) {
|
|
2953
|
+
const first = component.fn.parameters[0];
|
|
2954
|
+
if (first === undefined || !ts.isObjectBindingPattern(first.name))
|
|
2955
|
+
return undefined;
|
|
2956
|
+
for (const element of first.name.elements) {
|
|
2957
|
+
if (!ts.isBindingElement(element) || element.dotDotDotToken !== undefined || !ts.isIdentifier(element.name))
|
|
2958
|
+
continue;
|
|
2959
|
+
const bound = element.propertyName === undefined
|
|
2960
|
+
? element.name.text
|
|
2961
|
+
: ts.isIdentifier(element.propertyName)
|
|
2962
|
+
? element.propertyName.text
|
|
2963
|
+
: undefined;
|
|
2964
|
+
if (bound !== propName)
|
|
2965
|
+
continue;
|
|
2966
|
+
const initializer = element.initializer;
|
|
2967
|
+
if (initializer === undefined)
|
|
2968
|
+
return undefined;
|
|
2969
|
+
return isLiteralScalar(initializer) ? initializer : undefined;
|
|
2970
|
+
}
|
|
2971
|
+
return undefined;
|
|
2972
|
+
}
|
|
2973
|
+
/**
|
|
2974
|
+
* The argument's identifiers that name one of the component's
|
|
2975
|
+
* destructured props — the value references a per-usage binding must
|
|
2976
|
+
* cover. Property-access names (`x.detail`) and object-literal keys
|
|
2977
|
+
* are positions, not value references.
|
|
2978
|
+
*/
|
|
2979
|
+
function argumentPropLocals(argument, component) {
|
|
2980
|
+
const names = new Set();
|
|
2981
|
+
const visit = (node) => {
|
|
2982
|
+
if (ts.isIdentifier(node) && componentPropName(component, node.text) !== undefined && !isPositionalName(node)) {
|
|
2983
|
+
names.add(node.text);
|
|
2984
|
+
}
|
|
2985
|
+
node.forEachChild(visit);
|
|
2986
|
+
};
|
|
2987
|
+
visit(argument);
|
|
2988
|
+
return names;
|
|
2989
|
+
}
|
|
2990
|
+
/** True when `node` sits in a property-name position rather than a value position. */
|
|
2991
|
+
function isPositionalName(node) {
|
|
2992
|
+
const parent = node.parent;
|
|
2993
|
+
if (ts.isPropertyAccessExpression(parent) && parent.name === node)
|
|
2994
|
+
return true;
|
|
2995
|
+
if (ts.isPropertyAssignment(parent) && parent.name === node)
|
|
2996
|
+
return true;
|
|
2997
|
+
if (ts.isMethodDeclaration(parent) && parent.name === node)
|
|
2998
|
+
return true;
|
|
2999
|
+
return false;
|
|
3000
|
+
}
|
|
3001
|
+
/**
|
|
3002
|
+
* One usage site's binding of the referenced destructured props to
|
|
3003
|
+
* statically known values: the attribute the site passes, or — when
|
|
3004
|
+
* the site omits the attribute — the prop's literal destructuring
|
|
3005
|
+
* default (evaluated in the component's file). A prop whose value
|
|
3006
|
+
* cannot be proven at this site (omitted with no default or with a
|
|
3007
|
+
* non-literal default, attribute present without a value) binds to
|
|
3008
|
+
* `UNPROVEN_PROP_VALUE`: the evaluation fails only if it consults the
|
|
3009
|
+
* prop, so a guard the URL never reads cannot block a provable target.
|
|
3010
|
+
* Undefined when the site cannot bind the props AT ALL: a spread
|
|
3011
|
+
* attribute (`{...props}`) may carry any of them with unknown values.
|
|
3012
|
+
* Undefined fails closed at the caller.
|
|
3013
|
+
*/
|
|
3014
|
+
function usagePropBindings(component, site, referenced) {
|
|
3015
|
+
for (const attribute of site.element.attributes.properties) {
|
|
3016
|
+
if (ts.isJsxSpreadAttribute(attribute))
|
|
3017
|
+
return undefined;
|
|
3018
|
+
}
|
|
3019
|
+
const bindings = new Map();
|
|
3020
|
+
for (const localName of referenced) {
|
|
3021
|
+
const propName = componentPropName(component, localName);
|
|
3022
|
+
if (propName === undefined)
|
|
3023
|
+
return undefined;
|
|
3024
|
+
const attribute = jsxAttributeExpression(site.element, propName);
|
|
3025
|
+
if (attribute !== undefined) {
|
|
3026
|
+
bindings.set(localName, [attribute, site.file]);
|
|
3027
|
+
continue;
|
|
3028
|
+
}
|
|
3029
|
+
let presentWithoutValue = false;
|
|
3030
|
+
for (const attribute of site.element.attributes.properties) {
|
|
3031
|
+
if (ts.isJsxAttribute(attribute) && ts.isIdentifier(attribute.name) && attribute.name.text === propName) {
|
|
3032
|
+
presentWithoutValue = true;
|
|
3033
|
+
break;
|
|
3034
|
+
}
|
|
3035
|
+
}
|
|
3036
|
+
if (presentWithoutValue) {
|
|
3037
|
+
bindings.set(localName, [UNPROVEN_PROP_VALUE, component.file]);
|
|
3038
|
+
continue;
|
|
3039
|
+
}
|
|
3040
|
+
const initializer = componentPropDefault(component, propName);
|
|
3041
|
+
if (initializer === undefined) {
|
|
3042
|
+
bindings.set(localName, [UNPROVEN_PROP_VALUE, component.file]);
|
|
3043
|
+
continue;
|
|
3044
|
+
}
|
|
3045
|
+
bindings.set(localName, [initializer, component.file]);
|
|
3046
|
+
}
|
|
3047
|
+
return bindings;
|
|
3048
|
+
}
|
|
3049
|
+
/**
|
|
3050
|
+
* Decides a wrapper call whose endpoint argument DEPENDS on the host
|
|
3051
|
+
* component's props without being a bare prop reference — a ternary
|
|
3052
|
+
* guard like ``detail === null ? `${base}/x` : `${detail}/x` `` over a
|
|
3053
|
+
* destructured prop. Every JSX usage site of the component contributes
|
|
3054
|
+
* a value for each prop the argument references (the attribute passed,
|
|
3055
|
+
* or the prop's literal destructuring default when omitted), and the
|
|
3056
|
+
* argument is evaluated once per usage under those bindings: a
|
|
3057
|
+
* null/undefined default proves the guard and selects the matching
|
|
3058
|
+
* branch. The wrapper is then evaluated once per distinct resolved
|
|
3059
|
+
* target, emitting one call per distinct target. A usage that cannot
|
|
3060
|
+
* bind a referenced prop keeps today's unresolved finding, byte for
|
|
3061
|
+
* byte. Everything the resolver cannot itself prove — a host without
|
|
3062
|
+
* JSX usage sites (a hook), a usage-supplied value that still leaves
|
|
3063
|
+
* the argument unproven — defers: the caller falls through to the
|
|
3064
|
+
* plain configured-parameter model, which fails closed on its own.
|
|
3065
|
+
* Returns false when the argument references none of the component's
|
|
3066
|
+
* props — the caller keeps today's model.
|
|
3067
|
+
*/
|
|
3068
|
+
function resolveWrapperTargetThroughPropDefaults(argument, host, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads) {
|
|
3069
|
+
const referenced = argumentPropLocals(argument, host);
|
|
3070
|
+
if (referenced.size === 0)
|
|
3071
|
+
return false;
|
|
3072
|
+
const object = unwrapOwnExpression(argument);
|
|
3073
|
+
if (ts.isObjectLiteralExpression(object)) {
|
|
3074
|
+
return recordWrapperTargetsThroughObjectProps(object, host, wrapperConfig, file, config, table, calls, unresolved, location, reads);
|
|
3075
|
+
}
|
|
3076
|
+
const index = jsxComponentIndex(table);
|
|
3077
|
+
const sites = index.usages.get(host.name);
|
|
3078
|
+
if (sites === undefined || sites.length === 0) {
|
|
3079
|
+
// A host with no JSX usage sites (a hook called from other hooks)
|
|
3080
|
+
// has no per-usage values; the plain configured-parameter model may
|
|
3081
|
+
// still resolve the call. Defer rather than preempt it with a
|
|
3082
|
+
// finding the baseline did not emit.
|
|
3083
|
+
return false;
|
|
3084
|
+
}
|
|
3085
|
+
const targets = [];
|
|
3086
|
+
for (const site of sites) {
|
|
3087
|
+
const bindings = usagePropBindings(host, site, referenced);
|
|
3088
|
+
if (bindings === undefined) {
|
|
3089
|
+
unresolved.push({
|
|
3090
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3091
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3092
|
+
location,
|
|
3093
|
+
});
|
|
3094
|
+
return true;
|
|
3095
|
+
}
|
|
3096
|
+
const resolved = table.callTargets(argument, file, bindings);
|
|
3097
|
+
if (resolved === undefined || resolved.length === 0) {
|
|
3098
|
+
// The usage supplied values, but the argument still cannot be
|
|
3099
|
+
// proven under them. The plain configured-parameter model may
|
|
3100
|
+
// resolve the call more coarsely (an unbound hole becomes a
|
|
3101
|
+
// `${}` slot exactly as before this resolver existed). Defer.
|
|
3102
|
+
return false;
|
|
3103
|
+
}
|
|
3104
|
+
targets.push(...resolved);
|
|
3105
|
+
}
|
|
3106
|
+
return recordWrapperTargets(targets, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads);
|
|
3107
|
+
}
|
|
3108
|
+
/**
|
|
3109
|
+
* The hook-body bindings one config-object call argument carries: each
|
|
3110
|
+
* property binds the hook's destructured local to the property's value.
|
|
3111
|
+
* A value that NAMES a host prop resolves through that usage's prop
|
|
3112
|
+
* bindings (the passed attribute, or the literal destructuring
|
|
3113
|
+
* default); any other value binds as written and must resolve in the
|
|
3114
|
+
* call file. Undefined — fail closed — for shorthand properties whose
|
|
3115
|
+
* name is not a per-usage-known prop, for spread elements, and for
|
|
3116
|
+
* computed or method properties: what they carry cannot be proven.
|
|
3117
|
+
*/
|
|
3118
|
+
function objectUsageBindings(object, host, usage, callFile) {
|
|
3119
|
+
const bindings = new Map();
|
|
3120
|
+
for (const property of object.properties) {
|
|
3121
|
+
if (ts.isPropertyAssignment(property) && ts.isIdentifier(property.name)) {
|
|
3122
|
+
const value = unwrapOwnExpression(property.initializer);
|
|
3123
|
+
if (ts.isIdentifier(value) && componentPropName(host, value.text) !== undefined) {
|
|
3124
|
+
const bound = usage.get(value.text);
|
|
3125
|
+
if (bound === undefined)
|
|
3126
|
+
return undefined;
|
|
3127
|
+
bindings.set(property.name.text, bound);
|
|
3128
|
+
}
|
|
3129
|
+
else {
|
|
3130
|
+
bindings.set(property.name.text, [property.initializer, callFile]);
|
|
3131
|
+
}
|
|
3132
|
+
continue;
|
|
3133
|
+
}
|
|
3134
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
3135
|
+
const bound = usage.get(property.name.text);
|
|
3136
|
+
if (bound !== undefined) {
|
|
3137
|
+
bindings.set(property.name.text, bound);
|
|
3138
|
+
}
|
|
3139
|
+
else {
|
|
3140
|
+
bindings.set(property.name.text, [property.name, callFile]);
|
|
3141
|
+
}
|
|
3142
|
+
continue;
|
|
3143
|
+
}
|
|
3144
|
+
return undefined;
|
|
3145
|
+
}
|
|
3146
|
+
return bindings;
|
|
3147
|
+
}
|
|
3148
|
+
/**
|
|
3149
|
+
* Decides a wrapper call whose argument is a config OBJECT carrying the
|
|
3150
|
+
* host component's props into a destructured hook parameter —
|
|
3151
|
+
* `useSearchableDropdown({ endpoint: apiEndpoint, detailEndpoint })`.
|
|
3152
|
+
* Every JSX usage site of the component contributes the values it
|
|
3153
|
+
* passes and the literal destructuring defaults it omits; the hook
|
|
3154
|
+
* body is then evaluated once per usage under those bindings, so a
|
|
3155
|
+
* null/undefined default proves a guard inside the HOOK and selects
|
|
3156
|
+
* the matching branch. Raws are unioned across usages and recorded
|
|
3157
|
+
* once per distinct path per method. A usage that cannot bind a
|
|
3158
|
+
* referenced prop keeps today's unresolved finding, byte for byte.
|
|
3159
|
+
* Everything the resolver cannot itself prove — a host without JSX
|
|
3160
|
+
* usage sites (a hook), a hook body that stays unproven or a wrapper
|
|
3161
|
+
* with no modeled client call — defers: the caller falls through to
|
|
3162
|
+
* the plain configured-parameter model, which fails closed on its
|
|
3163
|
+
* own. Returns false when the object references none of the
|
|
3164
|
+
* component's props or the wrapper has no simple single-return client
|
|
3165
|
+
* call — the caller keeps today's model.
|
|
3166
|
+
*/
|
|
3167
|
+
function recordWrapperTargetsThroughObjectProps(object, host, wrapperConfig, file, config, table, calls, unresolved, location, reads) {
|
|
3168
|
+
const referenced = argumentPropLocals(object, host);
|
|
3169
|
+
if (referenced.size === 0)
|
|
3170
|
+
return false;
|
|
3171
|
+
const index = jsxComponentIndex(table);
|
|
3172
|
+
const sites = index.usages.get(host.name);
|
|
3173
|
+
if (sites === undefined || sites.length === 0) {
|
|
3174
|
+
// A host with no JSX usage sites (a hook called from other hooks)
|
|
3175
|
+
// has no per-usage values; the plain configured-parameter model may
|
|
3176
|
+
// still resolve the call. Defer rather than preempt it with a
|
|
3177
|
+
// finding the baseline did not emit.
|
|
3178
|
+
return false;
|
|
3179
|
+
}
|
|
3180
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
3181
|
+
if (declared === undefined)
|
|
3182
|
+
return false;
|
|
3183
|
+
const byMethod = new Map();
|
|
3184
|
+
for (const site of sites) {
|
|
3185
|
+
const usage = usagePropBindings(host, site, referenced);
|
|
3186
|
+
if (usage === undefined) {
|
|
3187
|
+
unresolved.push({
|
|
3188
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3189
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3190
|
+
location,
|
|
3191
|
+
});
|
|
3192
|
+
return true;
|
|
3193
|
+
}
|
|
3194
|
+
const bodyBindings = objectUsageBindings(object, host, usage, file);
|
|
3195
|
+
if (bodyBindings === undefined) {
|
|
3196
|
+
unresolved.push({
|
|
3197
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3198
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3199
|
+
location,
|
|
3200
|
+
});
|
|
3201
|
+
return true;
|
|
3202
|
+
}
|
|
3203
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bodyBindings, 0, new Set());
|
|
3204
|
+
if (body === 'unmodeled') {
|
|
3205
|
+
// The wrapper itself is not modeled - the plain extended-wrapper
|
|
3206
|
+
// model below emits exactly this finding with today's wording.
|
|
3207
|
+
// Defer so the wording and the fallback stay in one place.
|
|
3208
|
+
return false;
|
|
3209
|
+
}
|
|
3210
|
+
if (body === 'unresolved') {
|
|
3211
|
+
// The hook body cannot be proven under the per-usage values. The
|
|
3212
|
+
// plain configured-parameter model may still resolve the call
|
|
3213
|
+
// more coarsely (unbound holes become `${}` slots). Defer.
|
|
3214
|
+
return false;
|
|
3215
|
+
}
|
|
3216
|
+
for (const entry of body) {
|
|
3217
|
+
const raws = byMethod.get(entry.method) ?? [];
|
|
3218
|
+
raws.push(entry.raw);
|
|
3219
|
+
byMethod.set(entry.method, raws);
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
for (const [method, raws] of byMethod) {
|
|
3223
|
+
recordRawTargets([...new Set(raws)], method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
3224
|
+
}
|
|
3225
|
+
return true;
|
|
3226
|
+
}
|
|
3227
|
+
/**
|
|
3228
|
+
* Decides a wrapper call whose POSITIONAL arguments carry the host
|
|
3229
|
+
* component's props — `useSearchableDropdown(apiEndpoint,
|
|
3230
|
+
* detailEndpoint)`, or a config object the single-argument model
|
|
3231
|
+
* cannot bind (`parameter`-extracted or destructured-parameter
|
|
3232
|
+
* shapes). Runs after the single-argument chain: it engages only the
|
|
3233
|
+
* calls that chain left unresolved. Every JSX usage site of the host
|
|
3234
|
+
* component contributes the values it passes and the literal
|
|
3235
|
+
* destructuring defaults it omits; each declared parameter binds its
|
|
3236
|
+
* call argument (a bare prop resolves through that usage's bindings),
|
|
3237
|
+
* the referenced prop locals bind for complex arguments, and the hook
|
|
3238
|
+
* body is evaluated once per usage. Raws are unioned across usages and
|
|
3239
|
+
* recorded once per distinct path per method. The resolver is a
|
|
3240
|
+
* precision upgrade only: every failure to bind or prove — a host
|
|
3241
|
+
* without JSX usage sites (a hook), a usage that cannot bind a
|
|
3242
|
+
* referenced prop, an unbindable argument, an unproven hook body —
|
|
3243
|
+
* defers to the plain configured-parameter model, which resolves or
|
|
3244
|
+
* fails closed exactly as before this resolver existed. Returns false
|
|
3245
|
+
* when no argument references one of the component's props — the
|
|
3246
|
+
* caller keeps today's model.
|
|
3247
|
+
*/
|
|
3248
|
+
function resolveWrapperTargetsThroughPositionalArgs(call, declared, wrapperConfig, file, config, table, calls, unresolved, location, reads) {
|
|
3249
|
+
const index = jsxComponentIndex(table);
|
|
3250
|
+
const host = innermostComponent(index.components, file, call);
|
|
3251
|
+
if (host === undefined)
|
|
3252
|
+
return false;
|
|
3253
|
+
const referenced = new Set();
|
|
3254
|
+
for (const argument of call.arguments) {
|
|
3255
|
+
for (const name of argumentPropLocals(argument, host))
|
|
3256
|
+
referenced.add(name);
|
|
3257
|
+
}
|
|
3258
|
+
if (referenced.size === 0)
|
|
3259
|
+
return false;
|
|
3260
|
+
const sites = index.usages.get(host.name);
|
|
3261
|
+
if (sites === undefined || sites.length === 0) {
|
|
3262
|
+
// A host with no JSX usage sites (a hook called from other hooks,
|
|
3263
|
+
// never rendered) has no per-usage values to bind - but the plain
|
|
3264
|
+
// configured-parameter model may still resolve the call. Defer;
|
|
3265
|
+
// claiming it here would turn every hook-to-hook call into a
|
|
3266
|
+
// finding the baseline did not emit.
|
|
3267
|
+
return false;
|
|
3268
|
+
}
|
|
3269
|
+
const byMethod = new Map();
|
|
3270
|
+
for (const site of sites) {
|
|
3271
|
+
const usage = usagePropBindings(host, site, referenced);
|
|
3272
|
+
if (usage === undefined) {
|
|
3273
|
+
unresolved.push({
|
|
3274
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3275
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3276
|
+
location,
|
|
3277
|
+
});
|
|
3278
|
+
return true;
|
|
3279
|
+
}
|
|
3280
|
+
const bodyBindings = new Map();
|
|
3281
|
+
for (const localName of referenced) {
|
|
3282
|
+
const bound = usage.get(localName);
|
|
3283
|
+
if (bound !== undefined)
|
|
3284
|
+
bodyBindings.set(localName, bound);
|
|
3285
|
+
}
|
|
3286
|
+
let unbindable = false;
|
|
3287
|
+
for (let paramIndex = 0; paramIndex < declared.fn.parameters.length; paramIndex += 1) {
|
|
3288
|
+
const parameter = declared.fn.parameters[paramIndex];
|
|
3289
|
+
const argument = call.arguments[paramIndex];
|
|
3290
|
+
if (parameter === undefined || argument === undefined)
|
|
3291
|
+
continue;
|
|
3292
|
+
if (ts.isIdentifier(parameter.name)) {
|
|
3293
|
+
const value = unwrapOwnExpression(argument);
|
|
3294
|
+
if (ts.isIdentifier(value) && componentPropName(host, value.text) !== undefined) {
|
|
3295
|
+
const bound = usage.get(value.text);
|
|
3296
|
+
if (bound === undefined) {
|
|
3297
|
+
unbindable = true;
|
|
3298
|
+
break;
|
|
3299
|
+
}
|
|
3300
|
+
bodyBindings.set(parameter.name.text, bound);
|
|
3301
|
+
}
|
|
3302
|
+
else {
|
|
3303
|
+
bodyBindings.set(parameter.name.text, [argument, file]);
|
|
3304
|
+
}
|
|
3305
|
+
continue;
|
|
3306
|
+
}
|
|
3307
|
+
if (ts.isObjectBindingPattern(parameter.name)) {
|
|
3308
|
+
const object = unwrapOwnExpression(argument);
|
|
3309
|
+
if (!ts.isObjectLiteralExpression(object)) {
|
|
3310
|
+
unbindable = true;
|
|
3311
|
+
break;
|
|
3312
|
+
}
|
|
3313
|
+
const objectBindings = objectUsageBindings(object, host, usage, file);
|
|
3314
|
+
if (objectBindings === undefined) {
|
|
3315
|
+
unbindable = true;
|
|
3316
|
+
break;
|
|
3317
|
+
}
|
|
3318
|
+
for (const [name, bound] of objectBindings)
|
|
3319
|
+
bodyBindings.set(name, bound);
|
|
3320
|
+
}
|
|
3321
|
+
}
|
|
3322
|
+
if (unbindable) {
|
|
3323
|
+
// This call's arguments cannot be bound per usage. Defer: the
|
|
3324
|
+
// plain configured-parameter model decides, keeping today's
|
|
3325
|
+
// finding when it cannot resolve the call either.
|
|
3326
|
+
return false;
|
|
3327
|
+
}
|
|
3328
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bodyBindings, 0, new Set());
|
|
3329
|
+
if (body === 'unmodeled') {
|
|
3330
|
+
// The wrapper itself is not modeled - the plain extended-wrapper
|
|
3331
|
+
// model below emits exactly this finding with today's wording.
|
|
3332
|
+
// Defer so the wording and the fallback stay in one place.
|
|
3333
|
+
return false;
|
|
3334
|
+
}
|
|
3335
|
+
if (body === 'unresolved') {
|
|
3336
|
+
// The hook body cannot be proven under the per-usage values. The
|
|
3337
|
+
// plain configured-parameter model may still resolve the call
|
|
3338
|
+
// more coarsely (unbound holes become `${}` slots). Defer.
|
|
3339
|
+
return false;
|
|
3340
|
+
}
|
|
3341
|
+
for (const entry of body) {
|
|
3342
|
+
const raws = byMethod.get(entry.method) ?? [];
|
|
3343
|
+
raws.push(entry.raw);
|
|
3344
|
+
byMethod.set(entry.method, raws);
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
for (const [method, raws] of byMethod) {
|
|
3348
|
+
recordRawTargets([...new Set(raws)], method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
3349
|
+
}
|
|
3350
|
+
return true;
|
|
3351
|
+
}
|
|
3352
|
+
/**
|
|
3353
|
+
* Emits the wrapper's calls for an already-resolved set of endpoint
|
|
3354
|
+
* targets: the declared wrapper body is evaluated once per distinct
|
|
3355
|
+
* target with the target bound to the wrapper's parameter, and the
|
|
3356
|
+
* raws are recorded per method. Returns false when the wrapper has no
|
|
3357
|
+
* simple single-return client call in the scanned set — the caller
|
|
3358
|
+
* keeps today's model. A body that cannot be evaluated keeps today's
|
|
3359
|
+
* unresolved finding.
|
|
3360
|
+
*/
|
|
3361
|
+
function recordWrapperTargets(targets, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads) {
|
|
3362
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
3363
|
+
if (declared === undefined)
|
|
3364
|
+
return false;
|
|
3365
|
+
const byMethod = new Map();
|
|
3366
|
+
for (const target of new Set(targets)) {
|
|
3367
|
+
const bindings = new Map(baseBindings);
|
|
3368
|
+
bindings.set(paramName, [ts.factory.createStringLiteral(target), file]);
|
|
3369
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bindings, 0, new Set());
|
|
3370
|
+
if (body === 'unmodeled' || body === 'unresolved') {
|
|
3371
|
+
unresolved.push({
|
|
3372
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3373
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3374
|
+
location,
|
|
3375
|
+
});
|
|
3376
|
+
return true;
|
|
3377
|
+
}
|
|
3378
|
+
for (const entry of body) {
|
|
3379
|
+
const raws = byMethod.get(entry.method) ?? [];
|
|
3380
|
+
raws.push(entry.raw);
|
|
3381
|
+
byMethod.set(entry.method, raws);
|
|
3382
|
+
}
|
|
3383
|
+
}
|
|
3384
|
+
for (const [method, raws] of byMethod) {
|
|
3385
|
+
recordRawTargets([...new Set(raws)], method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
3386
|
+
}
|
|
3387
|
+
return true;
|
|
3388
|
+
}
|
|
3389
|
+
/**
|
|
3390
|
+
* Decides a wrapper call whose endpoint argument is a prop of the
|
|
3391
|
+
* enclosing function component: the prop is resolved through every JSX
|
|
3392
|
+
* usage site of that component (one forwarding hop allowed) and the
|
|
3393
|
+
* wrapper is evaluated once per distinct resolved target, emitting one
|
|
3394
|
+
* call per distinct target. When the argument is not a bare
|
|
3395
|
+
* component-prop reference, the argument is resolved through the
|
|
3396
|
+
* component's destructuring defaults instead (see
|
|
3397
|
+
* resolveWrapperTargetThroughPropDefaults); when it references no
|
|
3398
|
+
* destructured prop either, the caller keeps today's model. A prop the
|
|
3399
|
+
* usage sites cannot resolve keeps today's unresolved finding, byte for
|
|
3400
|
+
* byte.
|
|
3401
|
+
*/
|
|
3402
|
+
function resolveWrapperTargetThroughJsxProps(argument, hostNode, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads) {
|
|
3403
|
+
const index = jsxComponentIndex(table);
|
|
3404
|
+
const host = innermostComponent(index.components, file, hostNode);
|
|
3405
|
+
if (host === undefined)
|
|
3406
|
+
return false;
|
|
3407
|
+
const ref = componentPropRef(unwrapOwnExpression(argument), host);
|
|
3408
|
+
if (ref === undefined || ref.path.length > 0) {
|
|
3409
|
+
return resolveWrapperTargetThroughPropDefaults(argument, host, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads);
|
|
3410
|
+
}
|
|
3411
|
+
const targets = propTargetsThroughUsages(index, host, ref.propName, 0, new Set(), table);
|
|
3412
|
+
if (targets === 'unresolved') {
|
|
3413
|
+
unresolved.push({
|
|
3414
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3415
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3416
|
+
location,
|
|
3417
|
+
});
|
|
3418
|
+
return true;
|
|
3419
|
+
}
|
|
3420
|
+
return recordWrapperTargets(targets, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads);
|
|
3421
|
+
}
|
|
3422
|
+
function clientCallOf(call, config, file, table) {
|
|
3423
|
+
const expression = call.expression;
|
|
3424
|
+
if (ts.isIdentifier(expression) && expression.text === 'fetch' && !table.shadowsGlobalFetch(expression)) {
|
|
3425
|
+
const url = call.arguments[0];
|
|
3426
|
+
return url === undefined ? undefined : { url, method: 'GET' };
|
|
3427
|
+
}
|
|
3428
|
+
if (!ts.isPropertyAccessExpression(expression) || !ts.isIdentifier(expression.expression))
|
|
3429
|
+
return undefined;
|
|
3430
|
+
const objectName = table.configuredClientName(expression.expression, file);
|
|
3431
|
+
const verb = expression.name.text.toLowerCase();
|
|
3432
|
+
if (!(objectName === 'axios' || clientSymbolActiveIn(config, objectName, file)) || !VERB_METHODS.has(verb)) {
|
|
3433
|
+
return undefined;
|
|
3434
|
+
}
|
|
3435
|
+
const url = call.arguments[0];
|
|
3436
|
+
if (url === undefined)
|
|
3437
|
+
return undefined;
|
|
3438
|
+
return { url, method: VERB_METHODS.get(verb) ?? 'GET' };
|
|
3439
|
+
}
|
|
3440
|
+
/**
|
|
3441
|
+
* Validates the include/exclude scoping fields of one object-form
|
|
3442
|
+
* entry. New surface, so strict: a present-but-not-array include or
|
|
3443
|
+
* exclude is malformed and throws (fail closed — silently ignoring a
|
|
3444
|
+
* scoping directive would scan MORE than the configuration asked for,
|
|
3445
|
+
* resurfacing the harness false positives scoping exists to remove).
|
|
3446
|
+
*/
|
|
3447
|
+
function parseEntryScoping(record, kind, path) {
|
|
3448
|
+
const scoping = {};
|
|
3449
|
+
for (const key of ['include', 'exclude']) {
|
|
3450
|
+
const value = record[key];
|
|
3451
|
+
if (value === undefined)
|
|
3452
|
+
continue;
|
|
3453
|
+
if (!Array.isArray(value)) {
|
|
3454
|
+
throw new Error(`invalid http client config: ${kind} '${String(record['name'])}' ${key} must be an array of globs at ${path}`);
|
|
3455
|
+
}
|
|
3456
|
+
scoping[key] = value.map((glob) => String(glob));
|
|
3457
|
+
}
|
|
3458
|
+
return scoping;
|
|
3459
|
+
}
|
|
3460
|
+
/**
|
|
3461
|
+
* Parses the WHOLE client-scan document — every key of the owner's
|
|
3462
|
+
* `scan.httpClients` section (0.11.0: these bytes used to live in
|
|
3463
|
+
* `.gateforge/http-clients.json`, and the host handed them over
|
|
3464
|
+
* instead of letting every detector re-read a file).
|
|
3465
|
+
*
|
|
3466
|
+
* An ABSENT section is normal and yields the default config (no roots,
|
|
3467
|
+
* no symbols) — byte-identical to the absent document. A malformed one
|
|
3468
|
+
* throws (fail closed — the CLI surfaces the error instead of scanning
|
|
3469
|
+
* with partial trust).
|
|
3470
|
+
*
|
|
3471
|
+
* Accepted shapes (phase 3 scan-scoping): `clientSymbols` entries are a
|
|
3472
|
+
* plain string (back-compat, unscoped) or `{ name, include?, exclude? }`;
|
|
3473
|
+
* `wrapperFunctions` / `urlBuilders` entries carry the same optional
|
|
3474
|
+
* include/exclude next to their existing `method` / `base` fields; the
|
|
3475
|
+
* top-level `clientScanRoots` / `serverScanRoots` arrays scope where
|
|
3476
|
+
* client-call and server-route scanning apply at all. Consistent with the
|
|
3477
|
+
* pre-existing parser posture: unknown keys are ignored, non-array known
|
|
3478
|
+
* keys are ignored, but malformed ENTRY values throw (the wrapper verb
|
|
3479
|
+
* check predates this; the scoping fields throw on wrong shapes and
|
|
3480
|
+
* missing names because silently dropping a scope widens the scan
|
|
3481
|
+
* instead of narrowing it).
|
|
3482
|
+
*
|
|
3483
|
+
* Args:
|
|
3484
|
+
* section: the parsed `scan.httpClients` section, or null/undefined when absent.
|
|
3485
|
+
* at: where the bytes came from, named in every refusal.
|
|
3486
|
+
*
|
|
3487
|
+
* Returns:
|
|
3488
|
+
* ClientScanConfig: the validated configuration.
|
|
3489
|
+
*/
|
|
3490
|
+
export function parseClientScanConfigDocument(section, at) {
|
|
3491
|
+
if (section === null || section === undefined)
|
|
3492
|
+
return DEFAULT_CLIENT_SCAN_CONFIG;
|
|
3493
|
+
if (typeof section !== 'object' || Array.isArray(section)) {
|
|
3494
|
+
throw new Error(`invalid http client config: expected an object at ${at}`);
|
|
3495
|
+
}
|
|
3496
|
+
const document = section;
|
|
3497
|
+
const config = {};
|
|
3498
|
+
if (Array.isArray(document['clientScanRoots'])) {
|
|
3499
|
+
config.clientScanRoots = document['clientScanRoots'].map((root) => String(root));
|
|
3500
|
+
}
|
|
3501
|
+
if (Array.isArray(document['serverScanRoots'])) {
|
|
3502
|
+
config.serverScanRoots = document['serverScanRoots'].map((root) => String(root));
|
|
3503
|
+
}
|
|
3504
|
+
if (Array.isArray(document['clientSymbols'])) {
|
|
3505
|
+
config.clientSymbols = document['clientSymbols'].map((entry) => {
|
|
3506
|
+
// Plain string: the original shape, unscoped — byte-identical.
|
|
3507
|
+
if (typeof entry === 'string')
|
|
3508
|
+
return entry;
|
|
3509
|
+
if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
3510
|
+
throw new Error(`invalid http client config: clientSymbols entries must be a name or a {name, include?, exclude?} object at ${at}`);
|
|
3511
|
+
}
|
|
3512
|
+
const record = entry;
|
|
3513
|
+
if (record['name'] === undefined) {
|
|
3514
|
+
throw new Error(`invalid http client config: clientSymbols object entries must carry a name at ${at}`);
|
|
3515
|
+
}
|
|
3516
|
+
return { name: String(record['name']), ...parseEntryScoping(record, 'clientSymbols', at) };
|
|
3517
|
+
});
|
|
3518
|
+
}
|
|
3519
|
+
if (Array.isArray(document['wrapperFunctions'])) {
|
|
3520
|
+
config.wrapperFunctions = document['wrapperFunctions'].map((entry) => {
|
|
3521
|
+
const record = entry;
|
|
3522
|
+
const methodFromBody = record['methodFromBody'] === true;
|
|
3523
|
+
const method = methodFromBody && record['method'] === undefined
|
|
3524
|
+
? 'GET'
|
|
3525
|
+
: normalizeHttpMethodValue(String(record['method'] ?? 'GET'));
|
|
3526
|
+
if (method === null) {
|
|
3527
|
+
throw new Error(`invalid http client config: wrapper method must be a concrete verb at ${at}`);
|
|
3528
|
+
}
|
|
3529
|
+
const parameter = typeof record['parameter'] === 'string' ? record['parameter'] : undefined;
|
|
3530
|
+
return {
|
|
3531
|
+
name: String(record['name']),
|
|
3532
|
+
method,
|
|
3533
|
+
...(methodFromBody ? { methodFromBody: true } : {}),
|
|
3534
|
+
...(parameter === undefined ? {} : { parameter }),
|
|
3535
|
+
...parseEntryScoping(record, 'wrapperFunctions', at),
|
|
3536
|
+
};
|
|
1851
3537
|
});
|
|
1852
3538
|
}
|
|
1853
3539
|
if (Array.isArray(document['urlBuilders'])) {
|