@gate-forge/pack-http 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client-calls.d.ts.map +1 -1
- package/dist/client-calls.js +1199 -57
- package/dist/client-calls.js.map +1 -1
- package/package.json +4 -4
package/dist/client-calls.js
CHANGED
|
@@ -254,7 +254,9 @@ function unwrapOwnExpression(node) {
|
|
|
254
254
|
current = current.expression;
|
|
255
255
|
continue;
|
|
256
256
|
}
|
|
257
|
-
if (ts.isAsExpression(current) ||
|
|
257
|
+
if (ts.isAsExpression(current) ||
|
|
258
|
+
ts.isSatisfiesExpression(current) ||
|
|
259
|
+
ts.isNonNullExpression(current)) {
|
|
258
260
|
current = current.expression;
|
|
259
261
|
continue;
|
|
260
262
|
}
|
|
@@ -651,10 +653,20 @@ const UNPROVEN_OPTIONS = { kind: 'unproven' };
|
|
|
651
653
|
*/
|
|
652
654
|
const MAX_OPTIONS_HOPS = 4;
|
|
653
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'));
|
|
654
664
|
/** Cap on alternatives one expression may prove. Above this, fail closed — never a partial set. */
|
|
655
665
|
const MAX_CALL_TARGETS = 32;
|
|
656
666
|
/** Calls of a declared wrapper into another declared wrapper, before fail closed. */
|
|
657
667
|
const MAX_WRAPPER_DEPTH = 4;
|
|
668
|
+
/** Identifier hops followed when recognising a runtime base-URL host, before fail closed. */
|
|
669
|
+
const MAX_BASE_HOPS = 4;
|
|
658
670
|
function languageKindFor(file) {
|
|
659
671
|
if (file.endsWith('.tsx'))
|
|
660
672
|
return ts.ScriptKind.TSX;
|
|
@@ -668,6 +680,30 @@ function languageKindFor(file) {
|
|
|
668
680
|
function parseSource(sourceText, file) {
|
|
669
681
|
return ts.createSourceFile(file, sourceText, ts.ScriptTarget.Latest, true, languageKindFor(file));
|
|
670
682
|
}
|
|
683
|
+
/**
|
|
684
|
+
* True when the subtree reads runtime configuration: an `import.meta.env`
|
|
685
|
+
* or `process.env` access at any depth (optional chains and computed
|
|
686
|
+
* members included). Such a value is a host/port the scan can never
|
|
687
|
+
* prove, which is what the template base-hole model keys on.
|
|
688
|
+
*/
|
|
689
|
+
function referencesRuntimeEnv(root) {
|
|
690
|
+
let found = false;
|
|
691
|
+
const visit = (node) => {
|
|
692
|
+
if (found)
|
|
693
|
+
return;
|
|
694
|
+
if (ts.isPropertyAccessExpression(node) &&
|
|
695
|
+
node.name.text === 'env' &&
|
|
696
|
+
// `import.meta` parses as a MetaProperty, not a property chain.
|
|
697
|
+
((ts.isMetaProperty(node.expression) && node.expression.name.text === 'meta') ||
|
|
698
|
+
(ts.isIdentifier(node.expression) && node.expression.text === 'process'))) {
|
|
699
|
+
found = true;
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
ts.forEachChild(node, visit);
|
|
703
|
+
};
|
|
704
|
+
visit(root);
|
|
705
|
+
return found;
|
|
706
|
+
}
|
|
671
707
|
/** Collects module-scope constants and configured wrapper declarations. */
|
|
672
708
|
function modelFile(source, config, file) {
|
|
673
709
|
const constants = new Map();
|
|
@@ -809,10 +845,19 @@ export function scanClientCalls(file, sourceText, config, scannedFiles) {
|
|
|
809
845
|
const unresolved = [];
|
|
810
846
|
const wrapperSpans = modeledWrapperSpans(model, config, file);
|
|
811
847
|
const insideWrapper = (node) => wrapperSpans.some(([start, end]) => node.getStart(source) >= start && node.getEnd() <= end);
|
|
848
|
+
// `client(...error.config...)` calls inside a registered response-error
|
|
849
|
+
// handler re-send a request this scan has already recorded; they are
|
|
850
|
+
// replays, never new call targets. The set fills when the walk reaches
|
|
851
|
+
// the `.use(...)` registration — an ancestor of the handler body — so
|
|
852
|
+
// every replay call is known before its node is reached.
|
|
853
|
+
const replayCalls = new Set();
|
|
812
854
|
const visit = (node) => {
|
|
813
855
|
if (!insideWrapper(node)) {
|
|
814
856
|
if (ts.isCallExpression(node)) {
|
|
815
|
-
|
|
857
|
+
collectInterceptorReplays(node, file, config, replayCalls);
|
|
858
|
+
if (!replayCalls.has(node)) {
|
|
859
|
+
extractCall(node, file, config, table, model, calls, unresolved);
|
|
860
|
+
}
|
|
816
861
|
}
|
|
817
862
|
else if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) {
|
|
818
863
|
extractJsxWrapper(node, file, config, table, calls, unresolved);
|
|
@@ -851,7 +896,7 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
851
896
|
// fetch(url[, {method}])
|
|
852
897
|
if (ts.isIdentifier(expression) && expression.text === 'fetch') {
|
|
853
898
|
if (!table.shadowsGlobalFetch(expression)) {
|
|
854
|
-
extractClientCall(call, 'fetch', 'GET', file, config, table, calls, unresolved, location, undefined, reads);
|
|
899
|
+
extractClientCall(call, 'fetch', 'GET', false, file, config, table, calls, unresolved, location, undefined, reads);
|
|
855
900
|
}
|
|
856
901
|
return;
|
|
857
902
|
}
|
|
@@ -861,7 +906,7 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
861
906
|
const verb = expression.name.text.toLowerCase();
|
|
862
907
|
const isFetchObject = objectName === 'window' && expression.name.text === 'fetch';
|
|
863
908
|
if (isFetchObject) {
|
|
864
|
-
extractClientCall(call, 'fetch', 'GET', file, config, table, calls, unresolved, location, undefined, reads);
|
|
909
|
+
extractClientCall(call, 'fetch', 'GET', false, file, config, table, calls, unresolved, location, undefined, reads);
|
|
865
910
|
return;
|
|
866
911
|
}
|
|
867
912
|
// Per-symbol scoping (phase 3): the symbol counts only where its
|
|
@@ -873,7 +918,7 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
873
918
|
// proven literal baseURL joins into the emitted path (fetch has no
|
|
874
919
|
// instance and never joins).
|
|
875
920
|
const baseURL = table.instanceBaseURL(objectName, file);
|
|
876
|
-
extractClientCall(call, objectName, method, file, config, table, calls, unresolved, location, baseURL, reads);
|
|
921
|
+
extractClientCall(call, objectName, method, true, file, config, table, calls, unresolved, location, baseURL, reads);
|
|
877
922
|
return;
|
|
878
923
|
}
|
|
879
924
|
}
|
|
@@ -910,10 +955,57 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
910
955
|
const bindings = new Map();
|
|
911
956
|
const declared = table.findFunction(wrapperConfig.name, file);
|
|
912
957
|
const paramName = wrapperConfig.parameter ?? (declared === undefined ? undefined : firstIdentifierParam(declared.fn));
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
958
|
+
// Every remaining identifier parameter binds positionally, so the
|
|
959
|
+
// request-options model can prove a verb the body forwards
|
|
960
|
+
// (`fetch(url, { ...options })`); an absent argument binds nothing.
|
|
961
|
+
if (declared !== undefined) {
|
|
962
|
+
declared.fn.parameters.forEach((parameter, index) => {
|
|
963
|
+
if (index === 0 && paramName !== undefined)
|
|
964
|
+
return;
|
|
965
|
+
if (!ts.isIdentifier(parameter.name))
|
|
966
|
+
return;
|
|
967
|
+
const positional = call.arguments[index];
|
|
968
|
+
if (positional !== undefined)
|
|
969
|
+
bindings.set(parameter.name.text, [positional, file]);
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
const takesObjectPattern = declared !== undefined
|
|
973
|
+
&& declared.fn.parameters[0] !== undefined
|
|
974
|
+
&& ts.isObjectBindingPattern(declared.fn.parameters[0].name);
|
|
975
|
+
if (declared !== undefined && (paramName !== undefined || takesObjectPattern)) {
|
|
976
|
+
const argument = paramName !== undefined
|
|
977
|
+
? argumentForParameter(declared.fn, paramName, call)
|
|
978
|
+
: call.arguments[0];
|
|
979
|
+
if (argument !== undefined) {
|
|
980
|
+
if (paramName !== undefined)
|
|
981
|
+
bindings.set(paramName, [argument, file]);
|
|
982
|
+
const sandboxCalls = [];
|
|
983
|
+
const sandboxUnresolved = [];
|
|
984
|
+
const sandboxReads = [];
|
|
985
|
+
const singleHandled = resolveWrapperTargetThroughJsxProps(argument, call, paramName ?? '', wrapperConfig, file, config, table, bindings, sandboxCalls, sandboxUnresolved, location, sandboxReads);
|
|
986
|
+
const positionalCalls = [];
|
|
987
|
+
const positionalUnresolved = [];
|
|
988
|
+
const positionalReads = [];
|
|
989
|
+
const positionalHandled = resolveWrapperTargetsThroughPositionalArgs(call, declared, wrapperConfig, file, config, table, positionalCalls, positionalUnresolved, location, positionalReads);
|
|
990
|
+
if (positionalHandled && positionalUnresolved.length === 0) {
|
|
991
|
+
calls.push(...positionalCalls);
|
|
992
|
+
unresolved.push(...positionalUnresolved);
|
|
993
|
+
reads.push(...positionalReads);
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
if (singleHandled) {
|
|
997
|
+
calls.push(...sandboxCalls);
|
|
998
|
+
unresolved.push(...sandboxUnresolved);
|
|
999
|
+
reads.push(...sandboxReads);
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
if (positionalHandled) {
|
|
1003
|
+
calls.push(...positionalCalls);
|
|
1004
|
+
unresolved.push(...positionalUnresolved);
|
|
1005
|
+
reads.push(...positionalReads);
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
917
1009
|
}
|
|
918
1010
|
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, reads);
|
|
919
1011
|
}
|
|
@@ -1021,7 +1113,15 @@ class BoundTable {
|
|
|
1021
1113
|
return this.inner.optionsMethod(node, file, bindings, depth);
|
|
1022
1114
|
}
|
|
1023
1115
|
}
|
|
1024
|
-
function extractClientCall(call, framework, defaultMethod,
|
|
1116
|
+
function extractClientCall(call, framework, defaultMethod,
|
|
1117
|
+
/**
|
|
1118
|
+
* True when the call's verb comes from a verb-named member
|
|
1119
|
+
* (`.get/.post/.put/.patch/.delete/...` on axios or a configured
|
|
1120
|
+
* client symbol): the member name IS the method and `arguments[1]` is
|
|
1121
|
+
* the request BODY — a spread there says nothing about the verb and is
|
|
1122
|
+
* never read as request options.
|
|
1123
|
+
*/
|
|
1124
|
+
methodFromVerb, file, config, table, calls, unresolved, location, baseURL, reads = []) {
|
|
1025
1125
|
const urlNode = call.arguments[0];
|
|
1026
1126
|
if (urlNode === undefined) {
|
|
1027
1127
|
unresolved.push({
|
|
@@ -1031,42 +1131,71 @@ function extractClientCall(call, framework, defaultMethod, file, config, table,
|
|
|
1031
1131
|
});
|
|
1032
1132
|
return;
|
|
1033
1133
|
}
|
|
1034
|
-
// Method: explicit option first, else the verb default
|
|
1134
|
+
// Method: explicit option first, else the verb default — except for a
|
|
1135
|
+
// verb-named call, where the member name decides and arguments[1] is
|
|
1136
|
+
// the request body, never request options.
|
|
1035
1137
|
let method = defaultMethod === 'GET' ? 'GET' : defaultMethod;
|
|
1036
1138
|
const optionsNode = call.arguments[1];
|
|
1037
|
-
if (
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1139
|
+
if (methodFromVerb) {
|
|
1140
|
+
// `apiClient.patch(url, { polygon_json, ...extra })`: the spread is
|
|
1141
|
+
// the body's shape, not an options override — the verb stays the
|
|
1142
|
+
// member's verb and is never un-proven by what the body spreads.
|
|
1143
|
+
}
|
|
1144
|
+
else if (optionsNode !== undefined && ts.isObjectLiteralExpression(optionsNode)) {
|
|
1145
|
+
if (optionsNode.properties.some(ts.isSpreadAssignment)) {
|
|
1146
|
+
// `{...base, method: ...}`: the verb participates in JavaScript's
|
|
1147
|
+
// last-writer-wins order across the spread, so a wrapper's options
|
|
1148
|
+
// parameter spread into the literal carries the CALL SITE's verb.
|
|
1149
|
+
// The bounded options model answers for the whole literal — a verb
|
|
1150
|
+
// it cannot prove stays unresolved and is never assumed to be GET.
|
|
1151
|
+
const resolved = table.optionsMethod(optionsNode, file);
|
|
1152
|
+
if (resolved.kind === 'unproven') {
|
|
1153
|
+
unresolved.push({
|
|
1154
|
+
code: HTTP_METHOD_DYNAMIC,
|
|
1155
|
+
detail: 'call request options spread another options object whose method ' +
|
|
1156
|
+
'cannot be proven statically; the verb is never assumed to be GET',
|
|
1157
|
+
location,
|
|
1158
|
+
});
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
if (resolved.kind === 'proven')
|
|
1162
|
+
method = resolved.method;
|
|
1163
|
+
}
|
|
1164
|
+
else {
|
|
1165
|
+
for (const property of optionsNode.properties) {
|
|
1166
|
+
const isMethod = (ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && property.name.text === 'method') ||
|
|
1167
|
+
(ts.isShorthandPropertyAssignment(property) && property.name.text === 'method');
|
|
1168
|
+
if (isMethod) {
|
|
1169
|
+
const initializer = ts.isPropertyAssignment(property)
|
|
1170
|
+
? property.initializer
|
|
1171
|
+
: property.name;
|
|
1172
|
+
const resolved = table.evaluate(initializer, file);
|
|
1173
|
+
if (resolved.kind === 'literal') {
|
|
1174
|
+
const normalized = normalizeHttpMethodValue(resolved.text);
|
|
1175
|
+
if (normalized === null) {
|
|
1176
|
+
unresolved.push({
|
|
1177
|
+
code: HTTP_METHOD_DYNAMIC,
|
|
1178
|
+
detail: `fetch call method '${resolved.text}' is not a concrete supported verb`,
|
|
1179
|
+
location,
|
|
1180
|
+
});
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
method = normalized;
|
|
1184
|
+
}
|
|
1185
|
+
else {
|
|
1049
1186
|
unresolved.push({
|
|
1050
1187
|
code: HTTP_METHOD_DYNAMIC,
|
|
1051
|
-
detail:
|
|
1188
|
+
detail: 'fetch call method is computed and cannot be proven statically',
|
|
1052
1189
|
location,
|
|
1053
1190
|
});
|
|
1054
1191
|
return;
|
|
1055
1192
|
}
|
|
1056
|
-
method = normalized;
|
|
1057
|
-
}
|
|
1058
|
-
else {
|
|
1059
|
-
unresolved.push({
|
|
1060
|
-
code: HTTP_METHOD_DYNAMIC,
|
|
1061
|
-
detail: 'fetch call method is computed and cannot be proven statically',
|
|
1062
|
-
location,
|
|
1063
|
-
});
|
|
1064
|
-
return;
|
|
1065
1193
|
}
|
|
1066
1194
|
}
|
|
1067
1195
|
}
|
|
1068
1196
|
}
|
|
1069
|
-
if (
|
|
1197
|
+
if (!methodFromVerb &&
|
|
1198
|
+
framework === 'fetch' &&
|
|
1070
1199
|
optionsNode !== undefined &&
|
|
1071
1200
|
!ts.isObjectLiteralExpression(optionsNode)) {
|
|
1072
1201
|
// `fetch(url, options)` with options built elsewhere: the verb is
|
|
@@ -1733,7 +1862,14 @@ class ValueTable {
|
|
|
1733
1862
|
let result = ABSENT_OPTIONS;
|
|
1734
1863
|
for (const property of object.properties) {
|
|
1735
1864
|
if (ts.isSpreadAssignment(property)) {
|
|
1736
|
-
|
|
1865
|
+
const spread = this.optionsMethod(property.expression, file, bindings, depth + 1);
|
|
1866
|
+
// JavaScript overwrite order: the LAST writer of `method` wins.
|
|
1867
|
+
// An unproven spread may overwrite the verb with an unknown value
|
|
1868
|
+
// (stays unproven unless re-proven), a proven spread writes its
|
|
1869
|
+
// verb, and a spread provably free of `method` adds nothing, so
|
|
1870
|
+
// any earlier or later explicit method survives it.
|
|
1871
|
+
if (spread.kind !== 'absent')
|
|
1872
|
+
result = spread;
|
|
1737
1873
|
}
|
|
1738
1874
|
else if ((ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) &&
|
|
1739
1875
|
ts.isIdentifier(property.name) &&
|
|
@@ -1741,11 +1877,6 @@ class ValueTable {
|
|
|
1741
1877
|
const initializer = ts.isPropertyAssignment(property) ? property.initializer : property.name;
|
|
1742
1878
|
result = this.verbOfLiteral(initializer, file, bindings);
|
|
1743
1879
|
}
|
|
1744
|
-
else {
|
|
1745
|
-
continue;
|
|
1746
|
-
}
|
|
1747
|
-
if (result.kind === 'unproven')
|
|
1748
|
-
return UNPROVEN_OPTIONS;
|
|
1749
1880
|
}
|
|
1750
1881
|
return result;
|
|
1751
1882
|
}
|
|
@@ -1919,11 +2050,15 @@ class ValueTable {
|
|
|
1919
2050
|
return value;
|
|
1920
2051
|
}
|
|
1921
2052
|
conditionalTargets(node, file, bindings, seen) {
|
|
1922
|
-
const proven = this.
|
|
2053
|
+
const proven = this.proveEquality(node.condition, file, bindings, seen)
|
|
2054
|
+
?? this.proveStartsWith(node.condition, file, bindings, seen);
|
|
1923
2055
|
if (proven === true)
|
|
1924
2056
|
return this.callTargets(node.whenTrue, file, bindings, seen);
|
|
1925
2057
|
if (proven === false)
|
|
1926
2058
|
return this.callTargets(node.whenFalse, file, bindings, seen);
|
|
2059
|
+
const joined = this.pathJoinTargets(node, file, bindings, seen);
|
|
2060
|
+
if (joined !== undefined)
|
|
2061
|
+
return joined;
|
|
1927
2062
|
const whenTrue = this.callTargets(node.whenTrue, file, bindings, seen);
|
|
1928
2063
|
const whenFalse = this.callTargets(node.whenFalse, file, bindings, seen);
|
|
1929
2064
|
if (whenTrue === undefined || whenFalse === undefined)
|
|
@@ -1931,6 +2066,70 @@ class ValueTable {
|
|
|
1931
2066
|
const combined = [...whenTrue, ...whenFalse];
|
|
1932
2067
|
return combined.length > MAX_CALL_TARGETS ? undefined : combined;
|
|
1933
2068
|
}
|
|
2069
|
+
/**
|
|
2070
|
+
* The path-join idiom: ``x.endsWith('/') ? `${x}<rest>` : `${x}/<rest>` `` —
|
|
2071
|
+
* the test and both arms name the SAME `x`, and the arms differ only by
|
|
2072
|
+
* the one joining slash. Both arms build the same URL, so the
|
|
2073
|
+
* conditional proves exactly ONE target: the joined template
|
|
2074
|
+
* `${x}/<rest>` (the false arm; the true arm is its slash-less twin,
|
|
2075
|
+
* and rendering both emitted the ghost shape with the hole glued onto
|
|
2076
|
+
* the base). Every other conditional returns undefined here and keeps
|
|
2077
|
+
* the both-arms union.
|
|
2078
|
+
*/
|
|
2079
|
+
pathJoinTargets(node, file, bindings, seen) {
|
|
2080
|
+
const condition = unwrapOwnExpression(node.condition);
|
|
2081
|
+
if (!ts.isCallExpression(condition) ||
|
|
2082
|
+
!ts.isPropertyAccessExpression(condition.expression) ||
|
|
2083
|
+
condition.expression.name.text !== 'endsWith' ||
|
|
2084
|
+
condition.arguments.length !== 1) {
|
|
2085
|
+
return undefined;
|
|
2086
|
+
}
|
|
2087
|
+
const probe = condition.arguments[0];
|
|
2088
|
+
if (probe === undefined ||
|
|
2089
|
+
!(ts.isStringLiteral(probe) || ts.isNoSubstitutionTemplateLiteral(probe)) ||
|
|
2090
|
+
probe.text !== '/') {
|
|
2091
|
+
return undefined;
|
|
2092
|
+
}
|
|
2093
|
+
const receiver = unwrapOwnExpression(condition.expression.expression);
|
|
2094
|
+
if (!this.singleSlashJoinArms(node.whenTrue, node.whenFalse, receiver))
|
|
2095
|
+
return undefined;
|
|
2096
|
+
return this.callTargets(node.whenFalse, file, bindings, seen);
|
|
2097
|
+
}
|
|
2098
|
+
/** True when the two arms are `${x}<rest>` and `${x}/<rest>` for the same `x`. */
|
|
2099
|
+
singleSlashJoinArms(whenTrue, whenFalse, receiver) {
|
|
2100
|
+
const trueArm = unwrapOwnExpression(whenTrue);
|
|
2101
|
+
const falseArm = unwrapOwnExpression(whenFalse);
|
|
2102
|
+
if (!ts.isTemplateExpression(trueArm) || !ts.isTemplateExpression(falseArm))
|
|
2103
|
+
return false;
|
|
2104
|
+
if (trueArm.head.text !== '' || falseArm.head.text !== '')
|
|
2105
|
+
return false;
|
|
2106
|
+
const trueSpans = trueArm.templateSpans;
|
|
2107
|
+
const falseSpans = falseArm.templateSpans;
|
|
2108
|
+
if (trueSpans.length === 0 || trueSpans.length !== falseSpans.length)
|
|
2109
|
+
return false;
|
|
2110
|
+
const trueHead = trueSpans[0];
|
|
2111
|
+
const falseHead = falseSpans[0];
|
|
2112
|
+
if (trueHead === undefined || falseHead === undefined)
|
|
2113
|
+
return false;
|
|
2114
|
+
if (unwrapOwnExpression(trueHead.expression).getText() !== receiver.getText() ||
|
|
2115
|
+
unwrapOwnExpression(falseHead.expression).getText() !== receiver.getText()) {
|
|
2116
|
+
return false;
|
|
2117
|
+
}
|
|
2118
|
+
if (falseHead.literal.text !== `/${trueHead.literal.text}`)
|
|
2119
|
+
return false;
|
|
2120
|
+
for (let index = 1; index < trueSpans.length; index += 1) {
|
|
2121
|
+
const trueSpan = trueSpans[index];
|
|
2122
|
+
const falseSpan = falseSpans[index];
|
|
2123
|
+
if (trueSpan === undefined || falseSpan === undefined)
|
|
2124
|
+
return false;
|
|
2125
|
+
if (trueSpan.literal.text !== falseSpan.literal.text)
|
|
2126
|
+
return false;
|
|
2127
|
+
if (unwrapOwnExpression(trueSpan.expression).getText() !== unwrapOwnExpression(falseSpan.expression).getText()) {
|
|
2128
|
+
return false;
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
return true;
|
|
2132
|
+
}
|
|
1934
2133
|
/** `expr.startsWith('literal')` when `expr` is exactly one literal. Otherwise unproven. */
|
|
1935
2134
|
proveStartsWith(node, file, bindings, seen) {
|
|
1936
2135
|
const expr = unwrapOwnExpression(node);
|
|
@@ -1952,6 +2151,106 @@ class ValueTable {
|
|
|
1952
2151
|
return undefined;
|
|
1953
2152
|
return (receiver[0] ?? '').startsWith(probe.text);
|
|
1954
2153
|
}
|
|
2154
|
+
/**
|
|
2155
|
+
* Proves an `===`/`==`/`!==`/`!=` condition whose two sides are
|
|
2156
|
+
* proven scalars — the guard a component writes around a defaulted
|
|
2157
|
+
* prop (`detail === null`). Loose equality is only decided when one
|
|
2158
|
+
* side is null or undefined: those comparisons never coerce, while
|
|
2159
|
+
* `0 == false` would. Undefined when either side is unproven — the
|
|
2160
|
+
* caller then keeps both arms.
|
|
2161
|
+
*/
|
|
2162
|
+
proveEquality(node, file, bindings, seen) {
|
|
2163
|
+
const expr = unwrapOwnExpression(node);
|
|
2164
|
+
if (ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.ExclamationToken) {
|
|
2165
|
+
const inner = this.proveEquality(expr.operand, file, bindings, seen);
|
|
2166
|
+
return inner === undefined ? undefined : !inner;
|
|
2167
|
+
}
|
|
2168
|
+
if (!ts.isBinaryExpression(expr))
|
|
2169
|
+
return undefined;
|
|
2170
|
+
const op = expr.operatorToken.kind;
|
|
2171
|
+
const strict = op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken;
|
|
2172
|
+
const loose = op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsToken;
|
|
2173
|
+
if (!strict && !loose)
|
|
2174
|
+
return undefined;
|
|
2175
|
+
const left = this.provenScalar(expr.left, file, bindings, seen);
|
|
2176
|
+
if (left === 'unknown')
|
|
2177
|
+
return undefined;
|
|
2178
|
+
const right = this.provenScalar(expr.right, file, bindings, seen);
|
|
2179
|
+
if (right === 'unknown')
|
|
2180
|
+
return undefined;
|
|
2181
|
+
if (loose && !(left === null || left === undefined) && !(right === null || right === undefined))
|
|
2182
|
+
return undefined;
|
|
2183
|
+
const equal = strict
|
|
2184
|
+
? left === right
|
|
2185
|
+
: left === right || ((left === null || left === undefined) && (right === null || right === undefined));
|
|
2186
|
+
return (op === ts.SyntaxKind.ExclamationEqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsToken)
|
|
2187
|
+
? !equal
|
|
2188
|
+
: equal;
|
|
2189
|
+
}
|
|
2190
|
+
/**
|
|
2191
|
+
* The proven scalar of an expression — null, undefined, string,
|
|
2192
|
+
* number, boolean — or 'unknown' when the bounded model cannot prove
|
|
2193
|
+
* it. Per-usage prop bindings take precedence, mirroring
|
|
2194
|
+
* identifierTargets: inside the component the destructured prop
|
|
2195
|
+
* shadows any outer constant. Module constants resolve through the
|
|
2196
|
+
* same local/import precedence, so a named `NO_ENDPOINT` sentinel
|
|
2197
|
+
* proves like its literal.
|
|
2198
|
+
*/
|
|
2199
|
+
provenScalar(node, file, bindings, seen) {
|
|
2200
|
+
const expr = unwrapOwnExpression(node);
|
|
2201
|
+
if (expr.kind === ts.SyntaxKind.NullKeyword)
|
|
2202
|
+
return null;
|
|
2203
|
+
if (expr.kind === ts.SyntaxKind.TrueKeyword)
|
|
2204
|
+
return true;
|
|
2205
|
+
if (expr.kind === ts.SyntaxKind.FalseKeyword)
|
|
2206
|
+
return false;
|
|
2207
|
+
if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr))
|
|
2208
|
+
return expr.text;
|
|
2209
|
+
if (ts.isNumericLiteral(expr))
|
|
2210
|
+
return Number(expr.text);
|
|
2211
|
+
if (ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.MinusToken && ts.isNumericLiteral(expr.operand)) {
|
|
2212
|
+
return -Number(expr.operand.text);
|
|
2213
|
+
}
|
|
2214
|
+
if (!ts.isIdentifier(expr))
|
|
2215
|
+
return 'unknown';
|
|
2216
|
+
if (expr.text === 'undefined') {
|
|
2217
|
+
return this.localConstBinding(expr).state === 'found' ? 'unknown' : undefined;
|
|
2218
|
+
}
|
|
2219
|
+
const bound = bindings?.get(expr.text);
|
|
2220
|
+
if (bound !== undefined) {
|
|
2221
|
+
const key = `${bound[1]}::${expr.text}`;
|
|
2222
|
+
if (seen.has(key))
|
|
2223
|
+
return 'unknown';
|
|
2224
|
+
seen.add(key);
|
|
2225
|
+
const value = this.provenScalar(bound[0], bound[1], bindings, seen);
|
|
2226
|
+
seen.delete(key);
|
|
2227
|
+
return value;
|
|
2228
|
+
}
|
|
2229
|
+
const local = this.localConstBinding(expr);
|
|
2230
|
+
if (local.state === 'shadowed')
|
|
2231
|
+
return 'unknown';
|
|
2232
|
+
if (local.state === 'found') {
|
|
2233
|
+
const key = `${file}::${expr.text}`;
|
|
2234
|
+
if (seen.has(key))
|
|
2235
|
+
return 'unknown';
|
|
2236
|
+
seen.add(key);
|
|
2237
|
+
const value = this.provenScalar(local.initializer, file, bindings, seen);
|
|
2238
|
+
seen.delete(key);
|
|
2239
|
+
return value;
|
|
2240
|
+
}
|
|
2241
|
+
const imported = this.importedFrom(file, expr.text);
|
|
2242
|
+
const origin = imported ?? file;
|
|
2243
|
+
const initializer = this.modelOf(origin)?.constants.get(expr.text);
|
|
2244
|
+
if (initializer === undefined)
|
|
2245
|
+
return 'unknown';
|
|
2246
|
+
const key = `${origin}::${expr.text}`;
|
|
2247
|
+
if (seen.has(key))
|
|
2248
|
+
return 'unknown';
|
|
2249
|
+
seen.add(key);
|
|
2250
|
+
const value = this.provenScalar(initializer, origin, bindings, seen);
|
|
2251
|
+
seen.delete(key);
|
|
2252
|
+
return value;
|
|
2253
|
+
}
|
|
1955
2254
|
expandTemplate(node, file, bindings, seen) {
|
|
1956
2255
|
let pieces = [{
|
|
1957
2256
|
text: node.head.text,
|
|
@@ -1976,12 +2275,24 @@ class ValueTable {
|
|
|
1976
2275
|
const holeFile = bound?.[1] ?? file;
|
|
1977
2276
|
const hole = bound === undefined ? this.evaluate(span.expression, file) : this.evaluate(holeExpr, holeFile);
|
|
1978
2277
|
if (hole.kind === 'unresolved') {
|
|
1979
|
-
if (
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
2278
|
+
if (span.literal.text.startsWith('/') &&
|
|
2279
|
+
pieces.every((piece) => !piece.rooted) &&
|
|
2280
|
+
this.isRuntimeBaseHole(span.expression, file, bindings)) {
|
|
2281
|
+
// The hole is a runtime base URL (env/config host): the
|
|
2282
|
+
// request's addressable path starts at the static literal
|
|
2283
|
+
// that follows it, so the host is dropped, not invented.
|
|
2284
|
+
for (const piece of pieces) {
|
|
2285
|
+
next.push({ text: piece.text + span.literal.text, rooted: true });
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
else {
|
|
2289
|
+
if (pieces.some((piece) => !piece.rooted))
|
|
2290
|
+
return undefined;
|
|
2291
|
+
const suffix = this.trailingQuerySuffix(node, span, holeExpr, holeFile);
|
|
2292
|
+
const slot = suffix ? '?' : '${}';
|
|
2293
|
+
for (const piece of pieces)
|
|
2294
|
+
next.push({ text: piece.text + slot + span.literal.text, rooted: true });
|
|
2295
|
+
}
|
|
1985
2296
|
}
|
|
1986
2297
|
else {
|
|
1987
2298
|
for (const piece of pieces) {
|
|
@@ -1998,6 +2309,37 @@ class ValueTable {
|
|
|
1998
2309
|
}
|
|
1999
2310
|
return pieces.map((piece) => piece.text);
|
|
2000
2311
|
}
|
|
2312
|
+
/**
|
|
2313
|
+
* True when the hole stands for a runtime base URL the model can SEE
|
|
2314
|
+
* derives from configuration: an identifier bound to a module-scope
|
|
2315
|
+
* constant whose initializer reads `import.meta.env` / `process.env`,
|
|
2316
|
+
* directly or through a call to a function in the scanned set that
|
|
2317
|
+
* does (a `getEnv(key)` helper). Such a host is dropped by the
|
|
2318
|
+
* template model — never proven, never invented. Anything the model
|
|
2319
|
+
* cannot trace to a config read is false: an unknown hole must keep
|
|
2320
|
+
* the call unresolved.
|
|
2321
|
+
*/
|
|
2322
|
+
isRuntimeBaseHole(expression, file, bindings, depth = 0) {
|
|
2323
|
+
if (depth > MAX_BASE_HOPS)
|
|
2324
|
+
return false;
|
|
2325
|
+
const identifier = unwrapOwnExpression(expression);
|
|
2326
|
+
if (!ts.isIdentifier(identifier))
|
|
2327
|
+
return false;
|
|
2328
|
+
const bound = bindings?.get(identifier.text);
|
|
2329
|
+
if (bound !== undefined)
|
|
2330
|
+
return this.isRuntimeBaseHole(bound[0], bound[1], bindings, depth + 1);
|
|
2331
|
+
const constant = this.resolveConstantInitializer(identifier.text, file, new Set());
|
|
2332
|
+
if (constant === undefined)
|
|
2333
|
+
return false;
|
|
2334
|
+
const [initializer, origin] = constant;
|
|
2335
|
+
if (referencesRuntimeEnv(initializer))
|
|
2336
|
+
return true;
|
|
2337
|
+
const initCall = unwrapOwnExpression(initializer);
|
|
2338
|
+
if (!ts.isCallExpression(initCall) || !ts.isIdentifier(initCall.expression))
|
|
2339
|
+
return false;
|
|
2340
|
+
const declared = this.functionOf(initCall.expression.text, origin);
|
|
2341
|
+
return declared?.body !== undefined && referencesRuntimeEnv(declared.body);
|
|
2342
|
+
}
|
|
2001
2343
|
/** Path-absolute hole alternatives only. Query-shaped holes stay on the suffix model. */
|
|
2002
2344
|
pathHoleTargets(expr, file, bindings, seen) {
|
|
2003
2345
|
const targets = this.callTargets(expr, file, bindings, seen);
|
|
@@ -2382,13 +2724,23 @@ function extractJsxWrapper(node, file, config, table, calls, unresolved) {
|
|
|
2382
2724
|
const source = table.modelOf(file)?.source;
|
|
2383
2725
|
if (source === undefined)
|
|
2384
2726
|
return;
|
|
2727
|
+
const location = locationOf(file, source, node);
|
|
2385
2728
|
const bindings = new Map();
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2729
|
+
// Absent `parameter` means the first parameter — the original
|
|
2730
|
+
// single-parameter wrapper contract — the same fallback the direct
|
|
2731
|
+
// call path applies.
|
|
2732
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
2733
|
+
const paramName = wrapperConfig.parameter ?? (declared === undefined ? undefined : firstIdentifierParam(declared.fn));
|
|
2734
|
+
if (paramName !== undefined) {
|
|
2735
|
+
const attr = jsxAttributeExpression(node, paramName);
|
|
2736
|
+
if (attr !== undefined) {
|
|
2737
|
+
bindings.set(paramName, [attr, file]);
|
|
2738
|
+
if (resolveWrapperTargetThroughJsxProps(attr, node, paramName, wrapperConfig, file, config, table, bindings, calls, unresolved, location, [])) {
|
|
2739
|
+
return;
|
|
2740
|
+
}
|
|
2741
|
+
}
|
|
2390
2742
|
}
|
|
2391
|
-
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved,
|
|
2743
|
+
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, []);
|
|
2392
2744
|
}
|
|
2393
2745
|
function resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, reads) {
|
|
2394
2746
|
const declared = table.findFunction(wrapperConfig.name, file);
|
|
@@ -2443,10 +2795,21 @@ function wrapperBodyTargets(fn, fnFile, entry, config, table, bindings, depth, s
|
|
|
2443
2795
|
ts.forEachChild(node, visit);
|
|
2444
2796
|
return;
|
|
2445
2797
|
}
|
|
2446
|
-
const client = clientCallOf(node, config, fnFile, table);
|
|
2798
|
+
const client = clientCallOf(node, config, fnFile, table, bindings);
|
|
2447
2799
|
if (client !== undefined) {
|
|
2448
2800
|
saw = true;
|
|
2449
|
-
if (entry.methodFromBody
|
|
2801
|
+
if (entry.methodFromBody === true) {
|
|
2802
|
+
// The verb comes from the body: nothing provable means no call
|
|
2803
|
+
// fact — never an invented default.
|
|
2804
|
+
if (client.method === null) {
|
|
2805
|
+
failed = true;
|
|
2806
|
+
return;
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
else if (client.method !== null && client.method !== entry.method) {
|
|
2810
|
+
// The declaration governs, but a body that PROVES another verb
|
|
2811
|
+
// contradicts it; an unproven body (options spread of unknown
|
|
2812
|
+
// provenance) does not.
|
|
2450
2813
|
failed = true;
|
|
2451
2814
|
return;
|
|
2452
2815
|
}
|
|
@@ -2455,7 +2818,10 @@ function wrapperBodyTargets(fn, fnFile, entry, config, table, bindings, depth, s
|
|
|
2455
2818
|
failed = true;
|
|
2456
2819
|
return;
|
|
2457
2820
|
}
|
|
2458
|
-
|
|
2821
|
+
// Surviving the checks above, a proven body verb equals the
|
|
2822
|
+
// declaration (or governs via methodFromBody); only an unproven
|
|
2823
|
+
// body under a declared method reaches here as null.
|
|
2824
|
+
const method = client.method ?? entry.method;
|
|
2459
2825
|
for (const raw of targets)
|
|
2460
2826
|
found.push({ method, raw });
|
|
2461
2827
|
return;
|
|
@@ -2497,11 +2863,787 @@ function wrapperBodyTargets(fn, fnFile, entry, config, table, bindings, depth, s
|
|
|
2497
2863
|
return 'unresolved';
|
|
2498
2864
|
return found;
|
|
2499
2865
|
}
|
|
2500
|
-
|
|
2866
|
+
/**
|
|
2867
|
+
* The configured client symbol an `X.interceptors.response.use(...)`
|
|
2868
|
+
* call registers on, or null when the call is not such a registration
|
|
2869
|
+
* on `axios` or a configured client symbol. Request interceptors are
|
|
2870
|
+
* deliberately out of scope: a request-side handler edits the config
|
|
2871
|
+
* BEFORE the first send, so a re-send modeled there would record a call
|
|
2872
|
+
* the scan has never seen.
|
|
2873
|
+
*/
|
|
2874
|
+
function interceptorReplayClient(call, file, config) {
|
|
2875
|
+
const use = call.expression;
|
|
2876
|
+
if (!ts.isPropertyAccessExpression(use) || use.name.text !== 'use')
|
|
2877
|
+
return null;
|
|
2878
|
+
const response = use.expression;
|
|
2879
|
+
if (!ts.isPropertyAccessExpression(response) || response.name.text !== 'response')
|
|
2880
|
+
return null;
|
|
2881
|
+
const interceptors = response.expression;
|
|
2882
|
+
if (!ts.isPropertyAccessExpression(interceptors) || interceptors.name.text !== 'interceptors')
|
|
2883
|
+
return null;
|
|
2884
|
+
const client = interceptors.expression;
|
|
2885
|
+
if (!ts.isIdentifier(client))
|
|
2886
|
+
return null;
|
|
2887
|
+
if (client.text !== 'axios' && !clientSymbolActiveIn(config, client.text, file))
|
|
2888
|
+
return null;
|
|
2889
|
+
return client.text;
|
|
2890
|
+
}
|
|
2891
|
+
/** `error.config` for the handler's own error parameter — and nothing else. */
|
|
2892
|
+
function isOwnErrorConfig(expr, errorParamName) {
|
|
2893
|
+
if (errorParamName === undefined || expr === undefined)
|
|
2894
|
+
return false;
|
|
2895
|
+
// Real handlers type the config on the way through — PM's
|
|
2896
|
+
// `const config = error.config as RetriableConfig | undefined;` — so
|
|
2897
|
+
// the alias must be recognized through as/satisfies/non-null/parens.
|
|
2898
|
+
const peeled = unwrapOwnExpression(expr);
|
|
2899
|
+
return (ts.isPropertyAccessExpression(peeled) &&
|
|
2900
|
+
ts.isIdentifier(peeled.expression) &&
|
|
2901
|
+
peeled.expression.text === errorParamName &&
|
|
2902
|
+
peeled.name.text === 'config');
|
|
2903
|
+
}
|
|
2904
|
+
/**
|
|
2905
|
+
* Records the replay calls a response-error handler performs: calls on
|
|
2906
|
+
* the SAME client symbol whose single argument is the intercepted
|
|
2907
|
+
* error's own request config — `error.config`, a destructured
|
|
2908
|
+
* `{ config }` parameter binding, or a `const` bound to `error.config`.
|
|
2909
|
+
* Everything else inside the handler keeps today's model — a different
|
|
2910
|
+
* symbol, a different argument, a second argument, a verb call — because
|
|
2911
|
+
* only the re-send-the-original-request shape is a replay of a request
|
|
2912
|
+
* this scan has already recorded.
|
|
2913
|
+
*/
|
|
2914
|
+
function collectInterceptorReplays(call, file, config, replayCalls) {
|
|
2915
|
+
const clientName = interceptorReplayClient(call, file, config);
|
|
2916
|
+
if (clientName === null)
|
|
2917
|
+
return;
|
|
2918
|
+
const handler = call.arguments[1];
|
|
2919
|
+
if (handler === undefined || (!ts.isArrowFunction(handler) && !ts.isFunctionExpression(handler)))
|
|
2920
|
+
return;
|
|
2921
|
+
const first = handler.parameters[0];
|
|
2922
|
+
if (first === undefined)
|
|
2923
|
+
return;
|
|
2924
|
+
let errorParamName;
|
|
2925
|
+
const boundConfigNames = new Set();
|
|
2926
|
+
if (ts.isIdentifier(first.name)) {
|
|
2927
|
+
errorParamName = first.name.text;
|
|
2928
|
+
}
|
|
2929
|
+
else if (ts.isObjectBindingPattern(first.name)) {
|
|
2930
|
+
for (const element of first.name.elements) {
|
|
2931
|
+
if (element.dotDotDotToken !== undefined || !ts.isIdentifier(element.name))
|
|
2932
|
+
continue;
|
|
2933
|
+
const propertyName = element.propertyName === undefined
|
|
2934
|
+
? element.name.text
|
|
2935
|
+
: ts.isIdentifier(element.propertyName)
|
|
2936
|
+
? element.propertyName.text
|
|
2937
|
+
: undefined;
|
|
2938
|
+
if (propertyName === 'config')
|
|
2939
|
+
boundConfigNames.add(element.name.text);
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
else {
|
|
2943
|
+
return;
|
|
2944
|
+
}
|
|
2945
|
+
const configAliases = new Set();
|
|
2946
|
+
if (errorParamName !== undefined && handler.body !== undefined) {
|
|
2947
|
+
const collectAlias = (node) => {
|
|
2948
|
+
if (ts.isVariableDeclaration(node) &&
|
|
2949
|
+
ts.isIdentifier(node.name) &&
|
|
2950
|
+
isOwnErrorConfig(node.initializer, errorParamName)) {
|
|
2951
|
+
configAliases.add(node.name.text);
|
|
2952
|
+
}
|
|
2953
|
+
ts.forEachChild(node, collectAlias);
|
|
2954
|
+
};
|
|
2955
|
+
collectAlias(handler.body);
|
|
2956
|
+
}
|
|
2957
|
+
if (handler.body === undefined)
|
|
2958
|
+
return;
|
|
2959
|
+
const collectReplays = (node) => {
|
|
2960
|
+
if (ts.isCallExpression(node)) {
|
|
2961
|
+
const expression = node.expression;
|
|
2962
|
+
const argument = node.arguments[0];
|
|
2963
|
+
if (ts.isIdentifier(expression) &&
|
|
2964
|
+
expression.text === clientName &&
|
|
2965
|
+
node.arguments.length === 1 &&
|
|
2966
|
+
argument !== undefined &&
|
|
2967
|
+
(isOwnErrorConfig(argument, errorParamName) ||
|
|
2968
|
+
(ts.isIdentifier(argument) && (boundConfigNames.has(argument.text) || configAliases.has(argument.text))))) {
|
|
2969
|
+
replayCalls.add(node);
|
|
2970
|
+
return;
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
ts.forEachChild(node, collectReplays);
|
|
2974
|
+
};
|
|
2975
|
+
collectReplays(handler.body);
|
|
2976
|
+
}
|
|
2977
|
+
const jsxComponentIndexCache = new WeakMap();
|
|
2978
|
+
/**
|
|
2979
|
+
* Builds (once per scan) the component/usage index the JSX prop flow
|
|
2980
|
+
* resolves through: module-scope function-likes whose first parameter is
|
|
2981
|
+
* `props` or destructured, plus every JSX element with an identifier
|
|
2982
|
+
* tagName. The whole scanned set participates — usage sites are
|
|
2983
|
+
* dataflow inputs, and scan scoping narrows fact emission, not the
|
|
2984
|
+
* dataflow's ability to resolve product code.
|
|
2985
|
+
*/
|
|
2986
|
+
function jsxComponentIndex(table) {
|
|
2987
|
+
const cached = jsxComponentIndexCache.get(table);
|
|
2988
|
+
if (cached !== undefined)
|
|
2989
|
+
return cached;
|
|
2990
|
+
const index = { components: new Map(), usages: new Map() };
|
|
2991
|
+
for (const file of [...table.scannedFiles.keys()].sort()) {
|
|
2992
|
+
const model = table.modelOf(file);
|
|
2993
|
+
if (model === undefined)
|
|
2994
|
+
continue;
|
|
2995
|
+
for (const [name, fn] of model.functions) {
|
|
2996
|
+
if (index.components.has(name))
|
|
2997
|
+
continue;
|
|
2998
|
+
const first = fn.parameters[0];
|
|
2999
|
+
if (first === undefined)
|
|
3000
|
+
continue;
|
|
3001
|
+
if (!ts.isIdentifier(first.name) && !ts.isObjectBindingPattern(first.name))
|
|
3002
|
+
continue;
|
|
3003
|
+
index.components.set(name, { name, file, fn, source: model.source });
|
|
3004
|
+
}
|
|
3005
|
+
const collectUsages = (node) => {
|
|
3006
|
+
if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) {
|
|
3007
|
+
if (ts.isIdentifier(node.tagName)) {
|
|
3008
|
+
const sites = index.usages.get(node.tagName.text) ?? [];
|
|
3009
|
+
sites.push({ file, element: node });
|
|
3010
|
+
index.usages.set(node.tagName.text, sites);
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
ts.forEachChild(node, collectUsages);
|
|
3014
|
+
};
|
|
3015
|
+
collectUsages(model.source);
|
|
3016
|
+
}
|
|
3017
|
+
jsxComponentIndexCache.set(table, index);
|
|
3018
|
+
return index;
|
|
3019
|
+
}
|
|
3020
|
+
/**
|
|
3021
|
+
* The prop name `localName` binds inside a destructured first parameter,
|
|
3022
|
+
* or undefined. Renames count (`{ apiEndpoint: endpoint }` binds the
|
|
3023
|
+
* `apiEndpoint` prop under the local `endpoint`); rest elements and
|
|
3024
|
+
* non-identifier names bind nothing — an unbound name fails closed.
|
|
3025
|
+
*/
|
|
3026
|
+
function componentPropName(component, localName) {
|
|
3027
|
+
const first = component.fn.parameters[0];
|
|
3028
|
+
if (first === undefined || !ts.isObjectBindingPattern(first.name))
|
|
3029
|
+
return undefined;
|
|
3030
|
+
for (const element of first.name.elements) {
|
|
3031
|
+
if (!ts.isBindingElement(element) || element.dotDotDotToken !== undefined || !ts.isIdentifier(element.name))
|
|
3032
|
+
continue;
|
|
3033
|
+
const propName = element.propertyName === undefined
|
|
3034
|
+
? element.name.text
|
|
3035
|
+
: ts.isIdentifier(element.propertyName)
|
|
3036
|
+
? element.propertyName.text
|
|
3037
|
+
: undefined;
|
|
3038
|
+
if (propName !== undefined && element.name.text === localName)
|
|
3039
|
+
return propName;
|
|
3040
|
+
}
|
|
3041
|
+
return undefined;
|
|
3042
|
+
}
|
|
3043
|
+
/**
|
|
3044
|
+
* The component-prop reference an expression resolves to inside
|
|
3045
|
+
* `component`: a destructured prop (`apiEndpoint`), a `props.` access
|
|
3046
|
+
* (`props.apiEndpoint`), either with a member path (`duplicate.apiBasePath`,
|
|
3047
|
+
* `props.row.apiBasePath`). Undefined for anything else.
|
|
3048
|
+
*/
|
|
3049
|
+
function componentPropRef(expr, component) {
|
|
3050
|
+
const first = component.fn.parameters[0];
|
|
3051
|
+
if (first === undefined)
|
|
3052
|
+
return undefined;
|
|
3053
|
+
const takesPropsObject = ts.isIdentifier(first.name) && first.name.text === 'props';
|
|
3054
|
+
const segments = [];
|
|
3055
|
+
let current = expr;
|
|
3056
|
+
while (ts.isPropertyAccessExpression(current)) {
|
|
3057
|
+
segments.unshift(current.name.text);
|
|
3058
|
+
current = current.expression;
|
|
3059
|
+
}
|
|
3060
|
+
if (!ts.isIdentifier(current))
|
|
3061
|
+
return undefined;
|
|
3062
|
+
if (segments.length === 0) {
|
|
3063
|
+
const propName = componentPropName(component, current.text);
|
|
3064
|
+
return propName === undefined ? undefined : { propName, path: [] };
|
|
3065
|
+
}
|
|
3066
|
+
if (takesPropsObject && current.text === 'props') {
|
|
3067
|
+
const propName = segments[0];
|
|
3068
|
+
return propName === undefined ? undefined : { propName, path: segments.slice(1) };
|
|
3069
|
+
}
|
|
3070
|
+
const propName = componentPropName(component, current.text);
|
|
3071
|
+
return propName === undefined ? undefined : { propName, path: segments };
|
|
3072
|
+
}
|
|
3073
|
+
/** The innermost indexed component of `file` whose span contains `node`. */
|
|
3074
|
+
function innermostComponent(components, file, node) {
|
|
3075
|
+
let best;
|
|
3076
|
+
let bestStart = -1;
|
|
3077
|
+
for (const component of components.values()) {
|
|
3078
|
+
if (component.file !== file)
|
|
3079
|
+
continue;
|
|
3080
|
+
const start = component.fn.getStart(component.source);
|
|
3081
|
+
if (node.getStart(component.source) < start || node.getEnd() > component.fn.getEnd())
|
|
3082
|
+
continue;
|
|
3083
|
+
if (best === undefined || start > bestStart) {
|
|
3084
|
+
best = component;
|
|
3085
|
+
bestStart = start;
|
|
3086
|
+
}
|
|
3087
|
+
}
|
|
3088
|
+
return best;
|
|
3089
|
+
}
|
|
3090
|
+
/**
|
|
3091
|
+
* Every target a component prop provably carries, read off the
|
|
3092
|
+
* component's JSX usage sites in the scanned set. Each site must pass a
|
|
3093
|
+
* literal or statically resolvable value; a value may also forward
|
|
3094
|
+
* another component's prop unchanged, which resolves through THAT
|
|
3095
|
+
* component's sites — exactly one hop, then the value must be concrete.
|
|
3096
|
+
* A missing attribute, an unresolvable value, a second hop, or a
|
|
3097
|
+
* component without usage sites fails closed: a guessed target would
|
|
3098
|
+
* certify a route the server may never serve.
|
|
3099
|
+
*/
|
|
3100
|
+
function propTargetsThroughUsages(index, component, propName, depth, stack, table) {
|
|
3101
|
+
const stackKey = `${component.file}::${component.name}::${propName}`;
|
|
3102
|
+
if (stack.has(stackKey))
|
|
3103
|
+
return 'unresolved';
|
|
3104
|
+
const sites = index.usages.get(component.name);
|
|
3105
|
+
if (sites === undefined || sites.length === 0)
|
|
3106
|
+
return 'unresolved';
|
|
3107
|
+
const nextStack = new Set(stack);
|
|
3108
|
+
nextStack.add(stackKey);
|
|
3109
|
+
const targets = [];
|
|
3110
|
+
for (const site of sites) {
|
|
3111
|
+
const attr = jsxAttributeExpression(site.element, propName);
|
|
3112
|
+
if (attr === undefined)
|
|
3113
|
+
return 'unresolved';
|
|
3114
|
+
const enclosing = innermostComponent(index.components, site.file, site.element);
|
|
3115
|
+
if (enclosing !== undefined) {
|
|
3116
|
+
const forwarded = componentPropRef(attr, enclosing);
|
|
3117
|
+
if (forwarded !== undefined) {
|
|
3118
|
+
if (forwarded.path.length > 0 || depth >= 1)
|
|
3119
|
+
return 'unresolved';
|
|
3120
|
+
const deeper = propTargetsThroughUsages(index, enclosing, forwarded.propName, depth + 1, nextStack, table);
|
|
3121
|
+
if (deeper === 'unresolved')
|
|
3122
|
+
return 'unresolved';
|
|
3123
|
+
targets.push(...deeper);
|
|
3124
|
+
continue;
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
const direct = table.callTargets(attr, site.file);
|
|
3128
|
+
if (direct === undefined || direct.length === 0)
|
|
3129
|
+
return 'unresolved';
|
|
3130
|
+
targets.push(...direct);
|
|
3131
|
+
}
|
|
3132
|
+
return targets;
|
|
3133
|
+
}
|
|
3134
|
+
/** True for exactly the literal scalars a destructuring default may take: string, number, boolean, null, undefined. */
|
|
3135
|
+
function isLiteralScalar(expr) {
|
|
3136
|
+
const unwrapped = unwrapOwnExpression(expr);
|
|
3137
|
+
return unwrapped.kind === ts.SyntaxKind.NullKeyword
|
|
3138
|
+
|| unwrapped.kind === ts.SyntaxKind.TrueKeyword
|
|
3139
|
+
|| unwrapped.kind === ts.SyntaxKind.FalseKeyword
|
|
3140
|
+
|| ts.isStringLiteral(unwrapped)
|
|
3141
|
+
|| ts.isNoSubstitutionTemplateLiteral(unwrapped)
|
|
3142
|
+
|| ts.isNumericLiteral(unwrapped)
|
|
3143
|
+
|| (ts.isPrefixUnaryExpression(unwrapped)
|
|
3144
|
+
&& unwrapped.operator === ts.SyntaxKind.MinusToken
|
|
3145
|
+
&& ts.isNumericLiteral(unwrapped.operand))
|
|
3146
|
+
|| (ts.isIdentifier(unwrapped) && unwrapped.text === 'undefined');
|
|
3147
|
+
}
|
|
3148
|
+
/**
|
|
3149
|
+
* The literal initializer a destructured prop binds when its attribute
|
|
3150
|
+
* is omitted, or undefined. Renames count (`{ apiEndpoint: endpoint =
|
|
3151
|
+
* '/x/' }`); anything beyond a literal scalar — a call, an identifier,
|
|
3152
|
+
* a computed value — is not a provable default and fails closed, as
|
|
3153
|
+
* does a prop declared without an initializer.
|
|
3154
|
+
*/
|
|
3155
|
+
function componentPropDefault(component, propName) {
|
|
3156
|
+
const first = component.fn.parameters[0];
|
|
3157
|
+
if (first === undefined || !ts.isObjectBindingPattern(first.name))
|
|
3158
|
+
return undefined;
|
|
3159
|
+
for (const element of first.name.elements) {
|
|
3160
|
+
if (!ts.isBindingElement(element) || element.dotDotDotToken !== undefined || !ts.isIdentifier(element.name))
|
|
3161
|
+
continue;
|
|
3162
|
+
const bound = element.propertyName === undefined
|
|
3163
|
+
? element.name.text
|
|
3164
|
+
: ts.isIdentifier(element.propertyName)
|
|
3165
|
+
? element.propertyName.text
|
|
3166
|
+
: undefined;
|
|
3167
|
+
if (bound !== propName)
|
|
3168
|
+
continue;
|
|
3169
|
+
const initializer = element.initializer;
|
|
3170
|
+
if (initializer === undefined)
|
|
3171
|
+
return undefined;
|
|
3172
|
+
return isLiteralScalar(initializer) ? initializer : undefined;
|
|
3173
|
+
}
|
|
3174
|
+
return undefined;
|
|
3175
|
+
}
|
|
3176
|
+
/**
|
|
3177
|
+
* The argument's identifiers that name one of the component's
|
|
3178
|
+
* destructured props — the value references a per-usage binding must
|
|
3179
|
+
* cover. Property-access names (`x.detail`) and object-literal keys
|
|
3180
|
+
* are positions, not value references.
|
|
3181
|
+
*/
|
|
3182
|
+
function argumentPropLocals(argument, component) {
|
|
3183
|
+
const names = new Set();
|
|
3184
|
+
const visit = (node) => {
|
|
3185
|
+
if (ts.isIdentifier(node) && componentPropName(component, node.text) !== undefined && !isPositionalName(node)) {
|
|
3186
|
+
names.add(node.text);
|
|
3187
|
+
}
|
|
3188
|
+
node.forEachChild(visit);
|
|
3189
|
+
};
|
|
3190
|
+
visit(argument);
|
|
3191
|
+
return names;
|
|
3192
|
+
}
|
|
3193
|
+
/** True when `node` sits in a property-name position rather than a value position. */
|
|
3194
|
+
function isPositionalName(node) {
|
|
3195
|
+
const parent = node.parent;
|
|
3196
|
+
if (ts.isPropertyAccessExpression(parent) && parent.name === node)
|
|
3197
|
+
return true;
|
|
3198
|
+
if (ts.isPropertyAssignment(parent) && parent.name === node)
|
|
3199
|
+
return true;
|
|
3200
|
+
if (ts.isMethodDeclaration(parent) && parent.name === node)
|
|
3201
|
+
return true;
|
|
3202
|
+
return false;
|
|
3203
|
+
}
|
|
3204
|
+
/**
|
|
3205
|
+
* One usage site's binding of the referenced destructured props to
|
|
3206
|
+
* statically known values: the attribute the site passes, or — when
|
|
3207
|
+
* the site omits the attribute — the prop's literal destructuring
|
|
3208
|
+
* default (evaluated in the component's file). A prop whose value
|
|
3209
|
+
* cannot be proven at this site (omitted with no default or with a
|
|
3210
|
+
* non-literal default, attribute present without a value) binds to
|
|
3211
|
+
* `UNPROVEN_PROP_VALUE`: the evaluation fails only if it consults the
|
|
3212
|
+
* prop, so a guard the URL never reads cannot block a provable target.
|
|
3213
|
+
* Undefined when the site cannot bind the props AT ALL: a spread
|
|
3214
|
+
* attribute (`{...props}`) may carry any of them with unknown values.
|
|
3215
|
+
* Undefined fails closed at the caller.
|
|
3216
|
+
*/
|
|
3217
|
+
function usagePropBindings(component, site, referenced) {
|
|
3218
|
+
for (const attribute of site.element.attributes.properties) {
|
|
3219
|
+
if (ts.isJsxSpreadAttribute(attribute))
|
|
3220
|
+
return undefined;
|
|
3221
|
+
}
|
|
3222
|
+
const bindings = new Map();
|
|
3223
|
+
for (const localName of referenced) {
|
|
3224
|
+
const propName = componentPropName(component, localName);
|
|
3225
|
+
if (propName === undefined)
|
|
3226
|
+
return undefined;
|
|
3227
|
+
const attribute = jsxAttributeExpression(site.element, propName);
|
|
3228
|
+
if (attribute !== undefined) {
|
|
3229
|
+
bindings.set(localName, [attribute, site.file]);
|
|
3230
|
+
continue;
|
|
3231
|
+
}
|
|
3232
|
+
let presentWithoutValue = false;
|
|
3233
|
+
for (const attribute of site.element.attributes.properties) {
|
|
3234
|
+
if (ts.isJsxAttribute(attribute) && ts.isIdentifier(attribute.name) && attribute.name.text === propName) {
|
|
3235
|
+
presentWithoutValue = true;
|
|
3236
|
+
break;
|
|
3237
|
+
}
|
|
3238
|
+
}
|
|
3239
|
+
if (presentWithoutValue) {
|
|
3240
|
+
bindings.set(localName, [UNPROVEN_PROP_VALUE, component.file]);
|
|
3241
|
+
continue;
|
|
3242
|
+
}
|
|
3243
|
+
const initializer = componentPropDefault(component, propName);
|
|
3244
|
+
if (initializer === undefined) {
|
|
3245
|
+
bindings.set(localName, [UNPROVEN_PROP_VALUE, component.file]);
|
|
3246
|
+
continue;
|
|
3247
|
+
}
|
|
3248
|
+
bindings.set(localName, [initializer, component.file]);
|
|
3249
|
+
}
|
|
3250
|
+
return bindings;
|
|
3251
|
+
}
|
|
3252
|
+
/**
|
|
3253
|
+
* Decides a wrapper call whose endpoint argument DEPENDS on the host
|
|
3254
|
+
* component's props without being a bare prop reference — a ternary
|
|
3255
|
+
* guard like ``detail === null ? `${base}/x` : `${detail}/x` `` over a
|
|
3256
|
+
* destructured prop. Every JSX usage site of the component contributes
|
|
3257
|
+
* a value for each prop the argument references (the attribute passed,
|
|
3258
|
+
* or the prop's literal destructuring default when omitted), and the
|
|
3259
|
+
* argument is evaluated once per usage under those bindings: a
|
|
3260
|
+
* null/undefined default proves the guard and selects the matching
|
|
3261
|
+
* branch. The wrapper is then evaluated once per distinct resolved
|
|
3262
|
+
* target, emitting one call per distinct target. A usage that cannot
|
|
3263
|
+
* bind a referenced prop keeps today's unresolved finding, byte for
|
|
3264
|
+
* byte. Everything the resolver cannot itself prove — a host without
|
|
3265
|
+
* JSX usage sites (a hook), a usage-supplied value that still leaves
|
|
3266
|
+
* the argument unproven — defers: the caller falls through to the
|
|
3267
|
+
* plain configured-parameter model, which fails closed on its own.
|
|
3268
|
+
* Returns false when the argument references none of the component's
|
|
3269
|
+
* props — the caller keeps today's model.
|
|
3270
|
+
*/
|
|
3271
|
+
function resolveWrapperTargetThroughPropDefaults(argument, host, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads) {
|
|
3272
|
+
const referenced = argumentPropLocals(argument, host);
|
|
3273
|
+
if (referenced.size === 0)
|
|
3274
|
+
return false;
|
|
3275
|
+
const object = unwrapOwnExpression(argument);
|
|
3276
|
+
if (ts.isObjectLiteralExpression(object)) {
|
|
3277
|
+
return recordWrapperTargetsThroughObjectProps(object, host, wrapperConfig, file, config, table, calls, unresolved, location, reads);
|
|
3278
|
+
}
|
|
3279
|
+
const index = jsxComponentIndex(table);
|
|
3280
|
+
const sites = index.usages.get(host.name);
|
|
3281
|
+
if (sites === undefined || sites.length === 0) {
|
|
3282
|
+
// A host with no JSX usage sites (a hook called from other hooks)
|
|
3283
|
+
// has no per-usage values; the plain configured-parameter model may
|
|
3284
|
+
// still resolve the call. Defer rather than preempt it with a
|
|
3285
|
+
// finding the baseline did not emit.
|
|
3286
|
+
return false;
|
|
3287
|
+
}
|
|
3288
|
+
const targets = [];
|
|
3289
|
+
for (const site of sites) {
|
|
3290
|
+
const bindings = usagePropBindings(host, site, referenced);
|
|
3291
|
+
if (bindings === undefined) {
|
|
3292
|
+
unresolved.push({
|
|
3293
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3294
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3295
|
+
location,
|
|
3296
|
+
});
|
|
3297
|
+
return true;
|
|
3298
|
+
}
|
|
3299
|
+
const resolved = table.callTargets(argument, file, bindings);
|
|
3300
|
+
if (resolved === undefined || resolved.length === 0) {
|
|
3301
|
+
// The usage supplied values, but the argument still cannot be
|
|
3302
|
+
// proven under them. The plain configured-parameter model may
|
|
3303
|
+
// resolve the call more coarsely (an unbound hole becomes a
|
|
3304
|
+
// `${}` slot exactly as before this resolver existed). Defer.
|
|
3305
|
+
return false;
|
|
3306
|
+
}
|
|
3307
|
+
targets.push(...resolved);
|
|
3308
|
+
}
|
|
3309
|
+
return recordWrapperTargets(targets, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads);
|
|
3310
|
+
}
|
|
3311
|
+
/**
|
|
3312
|
+
* The hook-body bindings one config-object call argument carries: each
|
|
3313
|
+
* property binds the hook's destructured local to the property's value.
|
|
3314
|
+
* A value that NAMES a host prop resolves through that usage's prop
|
|
3315
|
+
* bindings (the passed attribute, or the literal destructuring
|
|
3316
|
+
* default); any other value binds as written and must resolve in the
|
|
3317
|
+
* call file. Undefined — fail closed — for shorthand properties whose
|
|
3318
|
+
* name is not a per-usage-known prop, for spread elements, and for
|
|
3319
|
+
* computed or method properties: what they carry cannot be proven.
|
|
3320
|
+
*/
|
|
3321
|
+
function objectUsageBindings(object, host, usage, callFile) {
|
|
3322
|
+
const bindings = new Map();
|
|
3323
|
+
for (const property of object.properties) {
|
|
3324
|
+
if (ts.isPropertyAssignment(property) && ts.isIdentifier(property.name)) {
|
|
3325
|
+
const value = unwrapOwnExpression(property.initializer);
|
|
3326
|
+
if (ts.isIdentifier(value) && componentPropName(host, value.text) !== undefined) {
|
|
3327
|
+
const bound = usage.get(value.text);
|
|
3328
|
+
if (bound === undefined)
|
|
3329
|
+
return undefined;
|
|
3330
|
+
bindings.set(property.name.text, bound);
|
|
3331
|
+
}
|
|
3332
|
+
else {
|
|
3333
|
+
bindings.set(property.name.text, [property.initializer, callFile]);
|
|
3334
|
+
}
|
|
3335
|
+
continue;
|
|
3336
|
+
}
|
|
3337
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
3338
|
+
const bound = usage.get(property.name.text);
|
|
3339
|
+
if (bound !== undefined) {
|
|
3340
|
+
bindings.set(property.name.text, bound);
|
|
3341
|
+
}
|
|
3342
|
+
else {
|
|
3343
|
+
bindings.set(property.name.text, [property.name, callFile]);
|
|
3344
|
+
}
|
|
3345
|
+
continue;
|
|
3346
|
+
}
|
|
3347
|
+
return undefined;
|
|
3348
|
+
}
|
|
3349
|
+
return bindings;
|
|
3350
|
+
}
|
|
3351
|
+
/**
|
|
3352
|
+
* Decides a wrapper call whose argument is a config OBJECT carrying the
|
|
3353
|
+
* host component's props into a destructured hook parameter —
|
|
3354
|
+
* `useSearchableDropdown({ endpoint: apiEndpoint, detailEndpoint })`.
|
|
3355
|
+
* Every JSX usage site of the component contributes the values it
|
|
3356
|
+
* passes and the literal destructuring defaults it omits; the hook
|
|
3357
|
+
* body is then evaluated once per usage under those bindings, so a
|
|
3358
|
+
* null/undefined default proves a guard inside the HOOK and selects
|
|
3359
|
+
* the matching branch. Raws are unioned across usages and recorded
|
|
3360
|
+
* once per distinct path per method. A usage that cannot bind a
|
|
3361
|
+
* referenced prop keeps today's unresolved finding, byte for byte.
|
|
3362
|
+
* Everything the resolver cannot itself prove — a host without JSX
|
|
3363
|
+
* usage sites (a hook), a hook body that stays unproven or a wrapper
|
|
3364
|
+
* with no modeled client call — defers: the caller falls through to
|
|
3365
|
+
* the plain configured-parameter model, which fails closed on its
|
|
3366
|
+
* own. Returns false when the object references none of the
|
|
3367
|
+
* component's props or the wrapper has no simple single-return client
|
|
3368
|
+
* call — the caller keeps today's model.
|
|
3369
|
+
*/
|
|
3370
|
+
function recordWrapperTargetsThroughObjectProps(object, host, wrapperConfig, file, config, table, calls, unresolved, location, reads) {
|
|
3371
|
+
const referenced = argumentPropLocals(object, host);
|
|
3372
|
+
if (referenced.size === 0)
|
|
3373
|
+
return false;
|
|
3374
|
+
const index = jsxComponentIndex(table);
|
|
3375
|
+
const sites = index.usages.get(host.name);
|
|
3376
|
+
if (sites === undefined || sites.length === 0) {
|
|
3377
|
+
// A host with no JSX usage sites (a hook called from other hooks)
|
|
3378
|
+
// has no per-usage values; the plain configured-parameter model may
|
|
3379
|
+
// still resolve the call. Defer rather than preempt it with a
|
|
3380
|
+
// finding the baseline did not emit.
|
|
3381
|
+
return false;
|
|
3382
|
+
}
|
|
3383
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
3384
|
+
if (declared === undefined)
|
|
3385
|
+
return false;
|
|
3386
|
+
const byMethod = new Map();
|
|
3387
|
+
for (const site of sites) {
|
|
3388
|
+
const usage = usagePropBindings(host, site, referenced);
|
|
3389
|
+
if (usage === undefined) {
|
|
3390
|
+
unresolved.push({
|
|
3391
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3392
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3393
|
+
location,
|
|
3394
|
+
});
|
|
3395
|
+
return true;
|
|
3396
|
+
}
|
|
3397
|
+
const bodyBindings = objectUsageBindings(object, host, usage, file);
|
|
3398
|
+
if (bodyBindings === undefined) {
|
|
3399
|
+
unresolved.push({
|
|
3400
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3401
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3402
|
+
location,
|
|
3403
|
+
});
|
|
3404
|
+
return true;
|
|
3405
|
+
}
|
|
3406
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bodyBindings, 0, new Set());
|
|
3407
|
+
if (body === 'unmodeled') {
|
|
3408
|
+
// The wrapper itself is not modeled - the plain extended-wrapper
|
|
3409
|
+
// model below emits exactly this finding with today's wording.
|
|
3410
|
+
// Defer so the wording and the fallback stay in one place.
|
|
3411
|
+
return false;
|
|
3412
|
+
}
|
|
3413
|
+
if (body === 'unresolved') {
|
|
3414
|
+
// The hook body cannot be proven under the per-usage values. The
|
|
3415
|
+
// plain configured-parameter model may still resolve the call
|
|
3416
|
+
// more coarsely (unbound holes become `${}` slots). Defer.
|
|
3417
|
+
return false;
|
|
3418
|
+
}
|
|
3419
|
+
for (const entry of body) {
|
|
3420
|
+
const raws = byMethod.get(entry.method) ?? [];
|
|
3421
|
+
raws.push(entry.raw);
|
|
3422
|
+
byMethod.set(entry.method, raws);
|
|
3423
|
+
}
|
|
3424
|
+
}
|
|
3425
|
+
for (const [method, raws] of byMethod) {
|
|
3426
|
+
recordRawTargets([...new Set(raws)], method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
3427
|
+
}
|
|
3428
|
+
return true;
|
|
3429
|
+
}
|
|
3430
|
+
/**
|
|
3431
|
+
* Decides a wrapper call whose POSITIONAL arguments carry the host
|
|
3432
|
+
* component's props — `useSearchableDropdown(apiEndpoint,
|
|
3433
|
+
* detailEndpoint)`, or a config object the single-argument model
|
|
3434
|
+
* cannot bind (`parameter`-extracted or destructured-parameter
|
|
3435
|
+
* shapes). Runs after the single-argument chain: it engages only the
|
|
3436
|
+
* calls that chain left unresolved. Every JSX usage site of the host
|
|
3437
|
+
* component contributes the values it passes and the literal
|
|
3438
|
+
* destructuring defaults it omits; each declared parameter binds its
|
|
3439
|
+
* call argument (a bare prop resolves through that usage's bindings),
|
|
3440
|
+
* the referenced prop locals bind for complex arguments, and the hook
|
|
3441
|
+
* body is evaluated once per usage. Raws are unioned across usages and
|
|
3442
|
+
* recorded once per distinct path per method. The resolver is a
|
|
3443
|
+
* precision upgrade only: every failure to bind or prove — a host
|
|
3444
|
+
* without JSX usage sites (a hook), a usage that cannot bind a
|
|
3445
|
+
* referenced prop, an unbindable argument, an unproven hook body —
|
|
3446
|
+
* defers to the plain configured-parameter model, which resolves or
|
|
3447
|
+
* fails closed exactly as before this resolver existed. Returns false
|
|
3448
|
+
* when no argument references one of the component's props — the
|
|
3449
|
+
* caller keeps today's model.
|
|
3450
|
+
*/
|
|
3451
|
+
function resolveWrapperTargetsThroughPositionalArgs(call, declared, wrapperConfig, file, config, table, calls, unresolved, location, reads) {
|
|
3452
|
+
const index = jsxComponentIndex(table);
|
|
3453
|
+
const host = innermostComponent(index.components, file, call);
|
|
3454
|
+
if (host === undefined)
|
|
3455
|
+
return false;
|
|
3456
|
+
const referenced = new Set();
|
|
3457
|
+
for (const argument of call.arguments) {
|
|
3458
|
+
for (const name of argumentPropLocals(argument, host))
|
|
3459
|
+
referenced.add(name);
|
|
3460
|
+
}
|
|
3461
|
+
if (referenced.size === 0)
|
|
3462
|
+
return false;
|
|
3463
|
+
const sites = index.usages.get(host.name);
|
|
3464
|
+
if (sites === undefined || sites.length === 0) {
|
|
3465
|
+
// A host with no JSX usage sites (a hook called from other hooks,
|
|
3466
|
+
// never rendered) has no per-usage values to bind - but the plain
|
|
3467
|
+
// configured-parameter model may still resolve the call. Defer;
|
|
3468
|
+
// claiming it here would turn every hook-to-hook call into a
|
|
3469
|
+
// finding the baseline did not emit.
|
|
3470
|
+
return false;
|
|
3471
|
+
}
|
|
3472
|
+
const byMethod = new Map();
|
|
3473
|
+
for (const site of sites) {
|
|
3474
|
+
const usage = usagePropBindings(host, site, referenced);
|
|
3475
|
+
if (usage === undefined) {
|
|
3476
|
+
unresolved.push({
|
|
3477
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3478
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3479
|
+
location,
|
|
3480
|
+
});
|
|
3481
|
+
return true;
|
|
3482
|
+
}
|
|
3483
|
+
const bodyBindings = new Map();
|
|
3484
|
+
for (const localName of referenced) {
|
|
3485
|
+
const bound = usage.get(localName);
|
|
3486
|
+
if (bound !== undefined)
|
|
3487
|
+
bodyBindings.set(localName, bound);
|
|
3488
|
+
}
|
|
3489
|
+
let unbindable = false;
|
|
3490
|
+
for (let paramIndex = 0; paramIndex < declared.fn.parameters.length; paramIndex += 1) {
|
|
3491
|
+
const parameter = declared.fn.parameters[paramIndex];
|
|
3492
|
+
const argument = call.arguments[paramIndex];
|
|
3493
|
+
if (parameter === undefined || argument === undefined)
|
|
3494
|
+
continue;
|
|
3495
|
+
if (ts.isIdentifier(parameter.name)) {
|
|
3496
|
+
const value = unwrapOwnExpression(argument);
|
|
3497
|
+
if (ts.isIdentifier(value) && componentPropName(host, value.text) !== undefined) {
|
|
3498
|
+
const bound = usage.get(value.text);
|
|
3499
|
+
if (bound === undefined) {
|
|
3500
|
+
unbindable = true;
|
|
3501
|
+
break;
|
|
3502
|
+
}
|
|
3503
|
+
bodyBindings.set(parameter.name.text, bound);
|
|
3504
|
+
}
|
|
3505
|
+
else {
|
|
3506
|
+
bodyBindings.set(parameter.name.text, [argument, file]);
|
|
3507
|
+
}
|
|
3508
|
+
continue;
|
|
3509
|
+
}
|
|
3510
|
+
if (ts.isObjectBindingPattern(parameter.name)) {
|
|
3511
|
+
const object = unwrapOwnExpression(argument);
|
|
3512
|
+
if (!ts.isObjectLiteralExpression(object)) {
|
|
3513
|
+
unbindable = true;
|
|
3514
|
+
break;
|
|
3515
|
+
}
|
|
3516
|
+
const objectBindings = objectUsageBindings(object, host, usage, file);
|
|
3517
|
+
if (objectBindings === undefined) {
|
|
3518
|
+
unbindable = true;
|
|
3519
|
+
break;
|
|
3520
|
+
}
|
|
3521
|
+
for (const [name, bound] of objectBindings)
|
|
3522
|
+
bodyBindings.set(name, bound);
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3525
|
+
if (unbindable) {
|
|
3526
|
+
// This call's arguments cannot be bound per usage. Defer: the
|
|
3527
|
+
// plain configured-parameter model decides, keeping today's
|
|
3528
|
+
// finding when it cannot resolve the call either.
|
|
3529
|
+
return false;
|
|
3530
|
+
}
|
|
3531
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bodyBindings, 0, new Set());
|
|
3532
|
+
if (body === 'unmodeled') {
|
|
3533
|
+
// The wrapper itself is not modeled - the plain extended-wrapper
|
|
3534
|
+
// model below emits exactly this finding with today's wording.
|
|
3535
|
+
// Defer so the wording and the fallback stay in one place.
|
|
3536
|
+
return false;
|
|
3537
|
+
}
|
|
3538
|
+
if (body === 'unresolved') {
|
|
3539
|
+
// The hook body cannot be proven under the per-usage values. The
|
|
3540
|
+
// plain configured-parameter model may still resolve the call
|
|
3541
|
+
// more coarsely (unbound holes become `${}` slots). Defer.
|
|
3542
|
+
return false;
|
|
3543
|
+
}
|
|
3544
|
+
for (const entry of body) {
|
|
3545
|
+
const raws = byMethod.get(entry.method) ?? [];
|
|
3546
|
+
raws.push(entry.raw);
|
|
3547
|
+
byMethod.set(entry.method, raws);
|
|
3548
|
+
}
|
|
3549
|
+
}
|
|
3550
|
+
for (const [method, raws] of byMethod) {
|
|
3551
|
+
recordRawTargets([...new Set(raws)], method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
3552
|
+
}
|
|
3553
|
+
return true;
|
|
3554
|
+
}
|
|
3555
|
+
/**
|
|
3556
|
+
* Emits the wrapper's calls for an already-resolved set of endpoint
|
|
3557
|
+
* targets: the declared wrapper body is evaluated once per distinct
|
|
3558
|
+
* target with the target bound to the wrapper's parameter, and the
|
|
3559
|
+
* raws are recorded per method. Returns false when the wrapper has no
|
|
3560
|
+
* simple single-return client call in the scanned set — the caller
|
|
3561
|
+
* keeps today's model. A body that cannot be evaluated keeps today's
|
|
3562
|
+
* unresolved finding.
|
|
3563
|
+
*/
|
|
3564
|
+
function recordWrapperTargets(targets, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads) {
|
|
3565
|
+
const declared = table.findFunction(wrapperConfig.name, file);
|
|
3566
|
+
if (declared === undefined)
|
|
3567
|
+
return false;
|
|
3568
|
+
const byMethod = new Map();
|
|
3569
|
+
for (const target of new Set(targets)) {
|
|
3570
|
+
const bindings = new Map(baseBindings);
|
|
3571
|
+
bindings.set(paramName, [ts.factory.createStringLiteral(target), file]);
|
|
3572
|
+
const body = wrapperBodyTargets(declared.fn, declared.file, wrapperConfig, config, table, bindings, 0, new Set());
|
|
3573
|
+
if (body === 'unmodeled' || body === 'unresolved') {
|
|
3574
|
+
unresolved.push({
|
|
3575
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3576
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3577
|
+
location,
|
|
3578
|
+
});
|
|
3579
|
+
return true;
|
|
3580
|
+
}
|
|
3581
|
+
for (const entry of body) {
|
|
3582
|
+
const raws = byMethod.get(entry.method) ?? [];
|
|
3583
|
+
raws.push(entry.raw);
|
|
3584
|
+
byMethod.set(entry.method, raws);
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
for (const [method, raws] of byMethod) {
|
|
3588
|
+
recordRawTargets([...new Set(raws)], method, wrapperConfig.name, config, calls, unresolved, location, reads);
|
|
3589
|
+
}
|
|
3590
|
+
return true;
|
|
3591
|
+
}
|
|
3592
|
+
/**
|
|
3593
|
+
* Decides a wrapper call whose endpoint argument is a prop of the
|
|
3594
|
+
* enclosing function component: the prop is resolved through every JSX
|
|
3595
|
+
* usage site of that component (one forwarding hop allowed) and the
|
|
3596
|
+
* wrapper is evaluated once per distinct resolved target, emitting one
|
|
3597
|
+
* call per distinct target. When the argument is not a bare
|
|
3598
|
+
* component-prop reference, the argument is resolved through the
|
|
3599
|
+
* component's destructuring defaults instead (see
|
|
3600
|
+
* resolveWrapperTargetThroughPropDefaults); when it references no
|
|
3601
|
+
* destructured prop either, the caller keeps today's model. A prop the
|
|
3602
|
+
* usage sites cannot resolve keeps today's unresolved finding, byte for
|
|
3603
|
+
* byte.
|
|
3604
|
+
*/
|
|
3605
|
+
function resolveWrapperTargetThroughJsxProps(argument, hostNode, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads) {
|
|
3606
|
+
const index = jsxComponentIndex(table);
|
|
3607
|
+
const host = innermostComponent(index.components, file, hostNode);
|
|
3608
|
+
if (host === undefined)
|
|
3609
|
+
return false;
|
|
3610
|
+
const ref = componentPropRef(unwrapOwnExpression(argument), host);
|
|
3611
|
+
if (ref === undefined || ref.path.length > 0) {
|
|
3612
|
+
return resolveWrapperTargetThroughPropDefaults(argument, host, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads);
|
|
3613
|
+
}
|
|
3614
|
+
const targets = propTargetsThroughUsages(index, host, ref.propName, 0, new Set(), table);
|
|
3615
|
+
if (targets === 'unresolved') {
|
|
3616
|
+
unresolved.push({
|
|
3617
|
+
code: FRONTEND_CALL_TARGET_UNRESOLVED,
|
|
3618
|
+
detail: `call target cannot be resolved statically (${wrapperConfig.name})`,
|
|
3619
|
+
location,
|
|
3620
|
+
});
|
|
3621
|
+
return true;
|
|
3622
|
+
}
|
|
3623
|
+
return recordWrapperTargets(targets, paramName, wrapperConfig, file, config, table, baseBindings, calls, unresolved, location, reads);
|
|
3624
|
+
}
|
|
3625
|
+
function clientCallOf(call, config, file, table, bindings) {
|
|
2501
3626
|
const expression = call.expression;
|
|
2502
3627
|
if (ts.isIdentifier(expression) && expression.text === 'fetch' && !table.shadowsGlobalFetch(expression)) {
|
|
2503
3628
|
const url = call.arguments[0];
|
|
2504
|
-
|
|
3629
|
+
if (url === undefined)
|
|
3630
|
+
return undefined;
|
|
3631
|
+
const optionsNode = call.arguments[1];
|
|
3632
|
+
if (optionsNode === undefined)
|
|
3633
|
+
return { url, method: 'GET' };
|
|
3634
|
+
if (ts.isObjectLiteralExpression(optionsNode) &&
|
|
3635
|
+
!optionsNode.properties.some(ts.isSpreadAssignment)) {
|
|
3636
|
+
// A plain literal: the direct-call scan decides its verb — the
|
|
3637
|
+
// wrapper-body model keeps today's GET default here.
|
|
3638
|
+
return { url, method: 'GET' };
|
|
3639
|
+
}
|
|
3640
|
+
// A spread-carrying literal or options built elsewhere: the verb is
|
|
3641
|
+
// whatever the CALL SITE's argument proves through the bounded
|
|
3642
|
+
// options model — `null` when nothing is proven (never a GET).
|
|
3643
|
+
const resolved = table.optionsMethod(optionsNode, file, bindings);
|
|
3644
|
+
if (resolved.kind === 'unproven')
|
|
3645
|
+
return { url, method: null };
|
|
3646
|
+
return { url, method: resolved.kind === 'proven' ? resolved.method : 'GET' };
|
|
2505
3647
|
}
|
|
2506
3648
|
if (!ts.isPropertyAccessExpression(expression) || !ts.isIdentifier(expression.expression))
|
|
2507
3649
|
return undefined;
|