@gate-forge/pack-http 0.15.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/dist/client-calls.d.ts.map +1 -1
- package/dist/client-calls.js +934 -12
- 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,6 +653,14 @@ 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. */
|
|
@@ -809,10 +819,19 @@ export function scanClientCalls(file, sourceText, config, scannedFiles) {
|
|
|
809
819
|
const unresolved = [];
|
|
810
820
|
const wrapperSpans = modeledWrapperSpans(model, config, file);
|
|
811
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();
|
|
812
828
|
const visit = (node) => {
|
|
813
829
|
if (!insideWrapper(node)) {
|
|
814
830
|
if (ts.isCallExpression(node)) {
|
|
815
|
-
|
|
831
|
+
collectInterceptorReplays(node, file, config, replayCalls);
|
|
832
|
+
if (!replayCalls.has(node)) {
|
|
833
|
+
extractCall(node, file, config, table, model, calls, unresolved);
|
|
834
|
+
}
|
|
816
835
|
}
|
|
817
836
|
else if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) {
|
|
818
837
|
extractJsxWrapper(node, file, config, table, calls, unresolved);
|
|
@@ -910,10 +929,43 @@ function extractCall(call, file, config, table, model, calls, unresolved) {
|
|
|
910
929
|
const bindings = new Map();
|
|
911
930
|
const declared = table.findFunction(wrapperConfig.name, file);
|
|
912
931
|
const paramName = wrapperConfig.parameter ?? (declared === undefined ? undefined : firstIdentifierParam(declared.fn));
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
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
|
+
}
|
|
917
969
|
}
|
|
918
970
|
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, reads);
|
|
919
971
|
}
|
|
@@ -1919,7 +1971,8 @@ class ValueTable {
|
|
|
1919
1971
|
return value;
|
|
1920
1972
|
}
|
|
1921
1973
|
conditionalTargets(node, file, bindings, seen) {
|
|
1922
|
-
const proven = this.
|
|
1974
|
+
const proven = this.proveEquality(node.condition, file, bindings, seen)
|
|
1975
|
+
?? this.proveStartsWith(node.condition, file, bindings, seen);
|
|
1923
1976
|
if (proven === true)
|
|
1924
1977
|
return this.callTargets(node.whenTrue, file, bindings, seen);
|
|
1925
1978
|
if (proven === false)
|
|
@@ -1952,6 +2005,106 @@ class ValueTable {
|
|
|
1952
2005
|
return undefined;
|
|
1953
2006
|
return (receiver[0] ?? '').startsWith(probe.text);
|
|
1954
2007
|
}
|
|
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
|
+
}
|
|
1955
2108
|
expandTemplate(node, file, bindings, seen) {
|
|
1956
2109
|
let pieces = [{
|
|
1957
2110
|
text: node.head.text,
|
|
@@ -2382,13 +2535,23 @@ function extractJsxWrapper(node, file, config, table, calls, unresolved) {
|
|
|
2382
2535
|
const source = table.modelOf(file)?.source;
|
|
2383
2536
|
if (source === undefined)
|
|
2384
2537
|
return;
|
|
2538
|
+
const location = locationOf(file, source, node);
|
|
2385
2539
|
const bindings = new Map();
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
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
|
+
}
|
|
2390
2553
|
}
|
|
2391
|
-
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved,
|
|
2554
|
+
resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, []);
|
|
2392
2555
|
}
|
|
2393
2556
|
function resolveExtendedWrapper(wrapperConfig, file, config, table, bindings, calls, unresolved, location, reads) {
|
|
2394
2557
|
const declared = table.findFunction(wrapperConfig.name, file);
|
|
@@ -2497,6 +2660,765 @@ function wrapperBodyTargets(fn, fnFile, entry, config, table, bindings, depth, s
|
|
|
2497
2660
|
return 'unresolved';
|
|
2498
2661
|
return found;
|
|
2499
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
|
+
}
|
|
2500
3422
|
function clientCallOf(call, config, file, table) {
|
|
2501
3423
|
const expression = call.expression;
|
|
2502
3424
|
if (ts.isIdentifier(expression) && expression.text === 'fetch' && !table.shadowsGlobalFetch(expression)) {
|