refactor(analysis): remove fixed result quotas

This commit is contained in:
morluto
2026-09-29 13:57:24 +08:00
parent 0e2738ffb6
commit 20152a9872
105 changed files with 605 additions and 1084 deletions
+26 -112
View File
@@ -4,7 +4,7 @@ import {
collectJavaScriptExports,
fingerprintJavaScriptAst,
} from "./javascriptAstFingerprint.js";
import { addBoundedFinding } from "./javascriptStaticAnalysisFindings.js";
import { addFindingOnce } from "./javascriptStaticAnalysisFindings.js";
import {
argumentValue,
calleeName,
@@ -38,59 +38,31 @@ export const inspectBundlerRegistration = (
if (runtime === undefined || !t.isArrayExpression(entry)) return;
const chunkIds = entry.elements[0];
const table = entry.elements[1];
const runtimeValue = runtimeMetadata(entry.elements[2], limits.maxFindings);
const runtimeValue = runtimeMetadata(entry.elements[2]);
if (!t.isArrayExpression(chunkIds) || !t.isObjectExpression(table)) return;
const recovered = recoverBundlerModules(source, table, accumulator, limits);
const chunkKeys = staticArrayValues(
chunkIds,
Math.min(256, limits.maxFindings),
);
if (chunkKeys.omitted > 0) {
accumulator.droppedFindings += chunkKeys.omitted;
accumulator.structuralTruncation = true;
}
const chunkKeys = staticArrayValues(chunkIds);
accumulator.unknownFindings += chunkKeys.unknown;
if (
runtimeValue.omittedEntryModuleKeys > 0 ||
runtimeValue.omittedAsyncChunkKeys > 0 ||
recovered.omittedAsyncChunkKeys > 0
) {
accumulator.droppedFindings +=
runtimeValue.omittedEntryModuleKeys +
runtimeValue.omittedAsyncChunkKeys +
recovered.omittedAsyncChunkKeys;
accumulator.structuralTruncation = true;
}
accumulator.unknownFindings +=
runtimeValue.unknownEntryModuleKeys +
runtimeValue.unknownAsyncChunkKeys +
recovered.unknownAsyncChunkKeys;
const asyncChunkKeys = boundedUniqueValues(
[...runtimeValue.asyncChunkKeys, ...recovered.asyncChunkKeys],
Math.min(256, limits.maxFindings),
);
if (asyncChunkKeys.omitted > 0) {
accumulator.droppedFindings += asyncChunkKeys.omitted;
accumulator.structuralTruncation = true;
}
const asyncChunkKeys = uniqueValues([
...runtimeValue.asyncChunkKeys,
...recovered.asyncChunkKeys,
]);
const registration = {
bundler: runtime.toLowerCase().includes("rspack")
? ("rspack" as const)
: ("webpack" as const),
runtime,
chunk_keys: chunkKeys.values,
omitted_chunk_keys: chunkKeys.omitted,
unknown_chunk_keys: chunkKeys.unknown,
runtime_require_name: runtimeValue.requireName,
runtime_module_cache_status: bundlerModuleCacheStatus(source),
entry_module_keys: runtimeValue.entryModuleKeys,
omitted_entry_module_keys: runtimeValue.omittedEntryModuleKeys,
unknown_entry_module_keys: runtimeValue.unknownEntryModuleKeys,
async_chunk_keys: asyncChunkKeys.values,
omitted_async_chunk_keys:
runtimeValue.omittedAsyncChunkKeys +
recovered.omittedAsyncChunkKeys +
asyncChunkKeys.omitted,
unknown_async_chunk_keys:
runtimeValue.unknownAsyncChunkKeys + recovered.unknownAsyncChunkKeys,
modules: recovered.modules.sort((left, right) =>
@@ -98,10 +70,9 @@ export const inspectBundlerRegistration = (
),
location: range(call),
};
addBoundedFinding(
addFindingOnce(
accumulator,
`registration\0${registrationKey(registration)}`,
limits.maxFindings,
() => accumulator.registrations.push(registration),
);
};
@@ -124,34 +95,26 @@ export const inspectEsbuildWrapper = (
if (wrapperKind === null || !t.isObjectExpression(table)) return;
const recovered = recoverBundlerModules(source, table, accumulator, limits);
if (recovered.modules.length === 0) return;
if (recovered.omittedAsyncChunkKeys > 0) {
accumulator.droppedFindings += recovered.omittedAsyncChunkKeys;
accumulator.structuralTruncation = true;
}
accumulator.unknownFindings += recovered.unknownAsyncChunkKeys;
const registration: JavaScriptBundlerRegistration = {
bundler: "esbuild",
runtime: `esbuild-${wrapperKind}`,
chunk_keys: [`${wrapperKind}@${String(call.start ?? 0)}`],
omitted_chunk_keys: 0,
unknown_chunk_keys: 0,
runtime_require_name: null,
runtime_module_cache_status: "not-observed",
entry_module_keys: [],
omitted_entry_module_keys: 0,
unknown_entry_module_keys: 0,
async_chunk_keys: recovered.asyncChunkKeys,
omitted_async_chunk_keys: recovered.omittedAsyncChunkKeys,
unknown_async_chunk_keys: recovered.unknownAsyncChunkKeys,
modules: recovered.modules.sort((left, right) =>
compareCodePoints(left.module_key, right.module_key),
),
location: range(call),
};
addBoundedFinding(
addFindingOnce(
accumulator,
`registration\0${registrationKey(registration)}`,
limits.maxFindings,
() => accumulator.registrations.push(registration),
);
};
@@ -159,7 +122,6 @@ export const inspectEsbuildWrapper = (
interface RecoveredBundlerModules {
readonly modules: JavaScriptBundlerModule[];
readonly asyncChunkKeys: readonly string[];
readonly omittedAsyncChunkKeys: number;
readonly unknownAsyncChunkKeys: number;
}
@@ -171,7 +133,6 @@ const recoverBundlerModules = (
): RecoveredBundlerModules => {
const modules: JavaScriptBundlerModule[] = [];
const asyncChunkKeys: string[] = [];
let omittedAsyncChunkKeys = 0;
let unknownAsyncChunkKeys = 0;
for (const property of table.properties) {
const recovered = recoverBundlerModule(
@@ -183,13 +144,11 @@ const recoverBundlerModules = (
if (recovered === null) continue;
modules.push(recovered.module);
asyncChunkKeys.push(...recovered.asyncChunkKeys);
omittedAsyncChunkKeys += recovered.omittedAsyncChunkKeys;
unknownAsyncChunkKeys += recovered.unknownAsyncChunkKeys;
}
return {
modules,
asyncChunkKeys,
omittedAsyncChunkKeys,
unknownAsyncChunkKeys,
};
};
@@ -202,34 +161,17 @@ const recoverBundlerModule = (
): {
readonly module: JavaScriptBundlerModule;
readonly asyncChunkKeys: readonly string[];
readonly omittedAsyncChunkKeys: number;
readonly unknownAsyncChunkKeys: number;
} | null => {
const factory = moduleFactory(property);
if (factory === undefined) return null;
if (accumulator.moduleCount >= limits.maxModules) {
accumulator.droppedFindings += 1;
return null;
}
accumulator.moduleCount += 1;
const key = modulePropertyName(property);
if (key.startsWith("[computed@") || key.startsWith("[unknown@"))
accumulator.unknownFindings += 1;
const fingerprint = fingerprintJavaScriptAst(
factory,
Math.min(limits.maxAstNodes, 100_000),
);
const exportsValue = collectJavaScriptExports(factory, 256);
const fingerprint = fingerprintJavaScriptAst(factory, limits.maxAstNodes);
const exportsValue = collectJavaScriptExports(factory);
const requireName = factoryRequireName(factory);
const asyncChunks = collectBundlerAsyncChunkKeys(
factory,
requireName,
limits.maxFindings,
);
if (fingerprint.truncated || exportsValue.truncated) {
accumulator.droppedFindings += 1;
accumulator.structuralTruncation = true;
}
const asyncChunks = collectBundlerAsyncChunkKeys(factory, requireName);
if (typeof factory.start === "number" && typeof factory.end === "number")
accumulator.modules.push({
start: factory.start,
@@ -255,11 +197,9 @@ const recoverBundlerModule = (
source_sha256: sha256Text(sourceSlice(source, factory)),
...structuralFingerprint,
exports: exportsValue.values,
exports_truncated: exportsValue.truncated,
location: range(factory),
},
asyncChunkKeys: asyncChunks.values,
omittedAsyncChunkKeys: asyncChunks.omitted,
unknownAsyncChunkKeys: asyncChunks.unknown,
};
};
@@ -267,16 +207,13 @@ const recoverBundlerModule = (
interface RuntimeMetadata {
readonly requireName: string | null;
readonly entryModuleKeys: readonly string[];
readonly omittedEntryModuleKeys: number;
readonly unknownEntryModuleKeys: number;
readonly asyncChunkKeys: readonly string[];
readonly omittedAsyncChunkKeys: number;
readonly unknownAsyncChunkKeys: number;
}
interface BoundedValues {
interface StaticValues {
readonly values: readonly string[];
readonly omitted: number;
readonly unknown: number;
}
@@ -285,43 +222,32 @@ type BundlerFunction =
| t.ArrowFunctionExpression
| t.ObjectMethod;
const runtimeMetadata = (
node: t.Node | null | undefined,
maximum: number,
): RuntimeMetadata => {
const runtimeMetadata = (node: t.Node | null | undefined): RuntimeMetadata => {
if (!isBundlerFunction(node))
return {
requireName: null,
entryModuleKeys: [],
omittedEntryModuleKeys: 0,
unknownEntryModuleKeys: node === null || node === undefined ? 0 : 1,
asyncChunkKeys: [],
omittedAsyncChunkKeys: 0,
unknownAsyncChunkKeys: 0,
};
const parameter = node.params[0];
const requireName = t.isIdentifier(parameter)
? parameter.name.slice(0, 4_096)
: null;
const requireName = t.isIdentifier(parameter) ? parameter.name : null;
if (requireName === null)
return {
requireName,
entryModuleKeys: [],
omittedEntryModuleKeys: 0,
unknownEntryModuleKeys: 1,
asyncChunkKeys: [],
omittedAsyncChunkKeys: 0,
unknownAsyncChunkKeys: 0,
};
const entries = collectBundlerEntryModuleKeys(node, requireName, maximum);
const asyncChunks = collectBundlerAsyncChunkKeys(node, requireName, maximum);
const entries = collectBundlerEntryModuleKeys(node, requireName);
const asyncChunks = collectBundlerAsyncChunkKeys(node, requireName);
return {
requireName,
entryModuleKeys: entries.values,
omittedEntryModuleKeys: entries.omitted,
unknownEntryModuleKeys: entries.unknown,
asyncChunkKeys: asyncChunks.values,
omittedAsyncChunkKeys: asyncChunks.omitted,
unknownAsyncChunkKeys: asyncChunks.unknown,
};
};
@@ -329,24 +255,20 @@ const runtimeMetadata = (
const collectBundlerEntryModuleKeys = (
factory: BundlerFunction,
requireName: string,
maximum: number,
): BoundedValues =>
collectBundlerCallArgumentValues(factory, requireName, maximum);
): StaticValues => collectBundlerCallArgumentValues(factory, requireName);
const collectBundlerAsyncChunkKeys = (
factory: BundlerFunction,
requireName: string | null,
maximum: number,
): BoundedValues =>
): StaticValues =>
requireName === null
? { values: [], omitted: 0, unknown: 0 }
: collectBundlerCallArgumentValues(factory, `${requireName}.e`, maximum);
? { values: [], unknown: 0 }
: collectBundlerCallArgumentValues(factory, `${requireName}.e`);
const collectBundlerCallArgumentValues = (
factory: BundlerFunction,
callee: string,
maximum: number,
): BoundedValues => {
): StaticValues => {
const values: string[] = [];
let unknown = 0;
t.traverseFast(factory, (node) => {
@@ -357,7 +279,7 @@ const collectBundlerCallArgumentValues = (
else values.push(value);
return undefined;
});
return boundedUniqueValues(values, Math.min(256, maximum), unknown);
return uniqueValues(values, unknown);
};
const isBundlerFunction = (
@@ -367,18 +289,10 @@ const isBundlerFunction = (
t.isArrowFunctionExpression(node) ||
t.isObjectMethod(node);
const boundedUniqueValues = (
values: readonly string[],
maximum: number,
unknown = 0,
): BoundedValues => {
const bounded = [
...new Set(values.map((value) => value.slice(0, 4_096))),
].sort(compareCodePoints);
const retained = bounded.slice(0, Math.max(0, maximum));
const uniqueValues = (values: readonly string[], unknown = 0): StaticValues => {
const unique = [...new Set(values)].sort(compareCodePoints);
return {
values: retained,
omitted: Math.max(0, bounded.length - retained.length),
values: unique,
unknown,
};
};