fix(javascript): keep keyed-collection reads out of endpoints
Static endpoint extraction read the first literal argument of any call
named get, post, put, patch, delete, or request as an HTTP request URL.
Reads of a provable keyed collection also became network endpoints in
application and web bundle analysis: `new URLSearchParams(...).get("q")`,
`response.headers.get("content-type")`, `new Map().get("key")`, and
`url.searchParams.get("page")`.
Skip these calls only when the receiver is a constructed Map, WeakMap,
Set, WeakSet, Headers, FormData, or URLSearchParams, or a platform
`headers`/`searchParams` property. Any other receiver may still be an
HTTP client with a base URL, so `api.get("users")` keeps its endpoint.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
8e8921d40c
commit
eb68761eb0
@@ -370,6 +370,43 @@ const isXhrMethodArgument = (node: t.Node | null | undefined): boolean => {
|
||||
);
|
||||
};
|
||||
|
||||
const KEYED_COLLECTION_CONSTRUCTORS = new Set([
|
||||
"FormData",
|
||||
"Headers",
|
||||
"Map",
|
||||
"Set",
|
||||
"URLSearchParams",
|
||||
"WeakMap",
|
||||
"WeakSet",
|
||||
]);
|
||||
|
||||
/**
|
||||
* Whether a get/delete receiver is provably a keyed collection rather than an
|
||||
* HTTP client: a constructed Map, Set, Headers, FormData, or URLSearchParams,
|
||||
* or the platform `headers`/`searchParams` properties of a request, response,
|
||||
* or URL. Other receivers stay possible clients.
|
||||
*/
|
||||
const isKeyedCollectionReceiver = (callee: t.Node): boolean => {
|
||||
if (!t.isMemberExpression(callee) && !t.isOptionalMemberExpression(callee))
|
||||
return false;
|
||||
const receiver = callee.object;
|
||||
if (t.isNewExpression(receiver))
|
||||
return (
|
||||
t.isIdentifier(receiver.callee) &&
|
||||
KEYED_COLLECTION_CONSTRUCTORS.has(receiver.callee.name)
|
||||
);
|
||||
if (
|
||||
!t.isMemberExpression(receiver) &&
|
||||
!t.isOptionalMemberExpression(receiver)
|
||||
)
|
||||
return false;
|
||||
const property = semanticStaticPropertyName(
|
||||
receiver.property,
|
||||
receiver.computed,
|
||||
);
|
||||
return property === "headers" || property === "searchParams";
|
||||
};
|
||||
|
||||
/** Select the literal URL argument for recognized network callees. */
|
||||
export const endpointArgument = (
|
||||
name: string,
|
||||
@@ -379,6 +416,7 @@ export const endpointArgument = (
|
||||
| t.JSXNamespacedName
|
||||
| t.ArgumentPlaceholder
|
||||
)[],
|
||||
callee: t.Node,
|
||||
): string | undefined => {
|
||||
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
|
||||
return stringValue(args[0]);
|
||||
@@ -390,7 +428,7 @@ export const endpointArgument = (
|
||||
(method) => name === method || name.endsWith(`.${method}`),
|
||||
)
|
||||
)
|
||||
return stringValue(args[0]);
|
||||
return isKeyedCollectionReceiver(callee) ? undefined : stringValue(args[0]);
|
||||
if (name.endsWith("loadURL")) return stringValue(args[0]);
|
||||
return undefined;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user