refactor(cli): import zod directly instead of through incur (#11655)

* refactor(cli): import zod directly instead of through incur

* fix(verify): count incur z imports from declarations, not comment or string text
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 10:37:43 +08:00
1 parent 832984c2cc
commit 07c5a54fee
54 files changed
+159 -50

No files matched your search

+13
View File
@@ -9,6 +9,7 @@ import {
} from "./counted-baseline.mjs";
import { localCompletenessEnumCount } from "./completeness-enum-guard.mjs";
import { countKeyedFirstMatches } from "./keyed-first-match.mjs";
import { incurZodImportCount } from "./incur-import-guard.mjs";
import { budgetArithmeticCount } from "./budget-arithmetic-guard.mjs";
import { localLimitErrorClassCount } from "./limit-class-guard.mjs";
import {
@@ -907,6 +908,18 @@ checkCounted(
[],
);
// Schemas import zod directly. incur's `z` is a re-export of the same package;
// importing it through the CLI framework ties schemas to a dependency REA is
// removing (#11654). Declarations are read from code tokens, so the text in a
// comment or string fixture does not count.
checkCounted(
'zod imported through incur (import { z } from "zod")',
"incur-zod-reexport",
[...files, ...testFiles],
(_content, file) => incurZodImportCount(file.source ?? file.content),
[],
);
// `find` on a parsed section, program-header or load-command table silently
// keeps the first of repeated declarations. Select keyed entries through
// src/domain/keyedCollection (`uniqueKeyed`, `allKeyed`, or
+1
View File
@@ -0,0 +1 @@
export function incurZodImportCount(source: string): number;
+40
View File
@@ -0,0 +1,40 @@
// Counts import and re-export declarations that take zod's `z` from incur
// (`import { z } from "incur"`, `import type { z as Zod } from 'incur'`,
// `export { z } from "incur"`). Declarations are read from code tokens, so the
// same text in a comment, string or template is not counted.
import { tokenize } from "./source-tokens.mjs";
const isIncurSpecifier = (source, token) =>
token?.kind === "string" &&
(source.startsWith('"incur"', token.start) ||
source.startsWith("'incur'", token.start));
/** @param {string} source */
export const incurZodImportCount = (source) => {
const { tokens } = tokenize(source);
let count = 0;
for (let index = 0; index < tokens.length; index += 1) {
const keyword = tokens[index];
if (
keyword.kind !== "id" ||
(keyword.text !== "import" && keyword.text !== "export")
)
continue;
let at = index + 1;
if (tokens[at]?.kind === "id" && tokens[at].text === "type") at += 1;
if (tokens[at]?.text !== "{") continue;
let importsZ = false;
for (at += 1; at < tokens.length && tokens[at].text !== "}"; at += 1)
// In `{ z as zod }` the imported name is z; in `{ zod as z }` it is not.
if (tokens[at].text === "z" && tokens[at - 1]?.text !== "as")
importsZ = true;
if (
importsZ &&
tokens[at + 1]?.text === "from" &&
isIncurSpecifier(source, tokens[at + 2])
)
count += 1;
index = at;
}
return count;
};
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import type { Logger } from "pino";
import { AdbDeviceAnalysisService } from "../application/adb/AdbDeviceAnalysisService.js";
+1 -1
View File
@@ -6,7 +6,7 @@ import {
undeliveredSourceClosure,
} from "../application/AnalysisEvidenceSources.js";
import type { AnalysisViewRequest } from "../domain/analysisView/analysisViewRequest.js";
import { z } from "incur";
import { z } from "zod";
import {
isImmutableEvidence,
parseEvidence,
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { inspectAnalysisView } from "../application/analysisView/AnalysisViewService.js";
import { ArtifactResourceScope } from "../resources/index.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { z } from "incur";
import type { z } from "zod";
import type { BrowserObservationPort } from "../ports/BrowserObservationPort.js";
import { createBrowserObservationProvider } from "../composition/browserObservation.js";
+2 -1
View File
@@ -1,4 +1,5 @@
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import type { FridaInstrumentationPort } from "../ports/frida/FridaInstrumentationPort.js";
+2 -1
View File
@@ -1,4 +1,5 @@
import { Cli, Completions, z } from "incur";
import { Cli, Completions } from "incur";
import { z } from "zod";
import {
CLI_COMPLETION_SHELLS,
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { invariantViolation } from "../domain/internalInvariant.js";
import type { Logger } from "pino";
+1 -1
View File
@@ -1,5 +1,5 @@
import { contractNamed } from "./contractCliDescription.js";
import { z } from "incur";
import { z } from "zod";
import { realpath, stat } from "node:fs/promises";
import { findAppBundleResourceRoot } from "../application/AppBundleExecutable.js";
+1 -1
View File
@@ -4,7 +4,7 @@ import { desktopViewPolicyInputSchema } from "../domain/desktop/desktopViewPolic
import { safeParseJson } from "../domain/safeJson.js";
import { createInterface } from "node:readline";
import { z } from "incur";
import { z } from "zod";
import type { Logger } from "pino";
import { captureDesktopApplication } from "../application/desktop/DesktopApplicationCapture.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
/**
* CLI argv keeps the domain field's inbound text. A service transform still
+2 -1
View File
@@ -1,6 +1,7 @@
import { projectCliAnalysisResult } from "./projectCliAnalysisResult.js";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { ELECTRON_TOOL_CONTRACTS } from "../contracts/javascript/electronToolContracts.js";
import { contractNamed } from "./contractCliDescription.js";
+1 -1
View File
@@ -8,7 +8,7 @@ import type {
} from "../application/environment/environmentExport.js";
import type { EnvironmentSource } from "../application/environment/environmentSource.js";
import type { ServedState } from "../application/environment/environmentState.js";
import { z } from "incur";
import { z } from "zod";
import type { Logger } from "pino";
import {
+2 -1
View File
@@ -1,6 +1,7 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { Errors, z, type MiddlewareHandler } from "incur";
import { Errors, type MiddlewareHandler } from "incur";
import { z } from "zod";
import {
parseCliOutputArguments,
+1 -1
View File
@@ -1,5 +1,5 @@
import { projectCliAnalysisResult } from "./projectCliAnalysisResult.js";
import { z } from "incur";
import { z } from "zod";
import { FridaInstrumentationService } from "../application/frida/FridaInstrumentationService.js";
import type { FridaInstrumentationPort } from "../ports/frida/FridaInstrumentationPort.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { ArtifactResourceScope } from "../resources/index.js";
import {
readGuidanceSection,
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import type { Logger } from "pino";
import { createIl2cppMetadataService } from "../composition/il2cpp.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import type { ContractCommandContext } from "./contractCommand.js";
import type { AnalysisViewRequest } from "../domain/analysisView/analysisViewRequest.js";
import { publishCliAnalysisEvidence } from "./analysisEvidenceDelivery.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { projectCliAnalysisResult } from "./projectCliAnalysisResult.js";
import type { z } from "incur";
import type { z } from "zod";
import type { JavaScriptRuntimeObservationPort } from "../ports/javascript/JavaScriptRuntimeObservationPort.js";
import {
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { jsonDocumentText } from "../domain/schemaConstraints.js";
+2 -1
View File
@@ -1,7 +1,8 @@
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import { Writable } from "node:stream";
import { Help, z } from "incur";
import { Help } from "incur";
import { z } from "zod";
import {
mcpClientRegistrationWritten,
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import type { Logger } from "pino";
import { matchNativeFunctionsFromBundles } from "../application/native/NativeFunctionMatchingService.js";
import { ArtifactResourceScope } from "../resources/index.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import type { CliInstance } from "./types.js";
import { visitRegisteredCommands } from "./visitRegisteredCommands.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { localPathStringSchema } from "../domain/localPath.js";
import {
+2 -1
View File
@@ -1,4 +1,5 @@
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import {
captureProcessScenarioFile,
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import type { Logger } from "pino";
import { createReleaseExposureService } from "../composition/releaseExposure.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { localPathStringSchema } from "../domain/localPath.js";
import type { Logger } from "pino";
import { OBJDUMP_PROVIDER_IDENTITY } from "../objdump/ObjdumpProviderIdentity.js";
+1 -1
View File
@@ -1,6 +1,6 @@
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import { withCommandCancellation } from "./commandCancellation.js";
import { z } from "incur";
import { z } from "zod";
import type { DoctorScope } from "../application/Doctor.js";
import {
+2 -1
View File
@@ -1,6 +1,7 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { Errors, Parser, z } from "incur";
import { Errors, Parser } from "incur";
import { z } from "zod";
import { splitIncurBuiltinArguments } from "../cliBuiltinArguments.js";
import type { CliInstance } from "./types.js";
+1 -1
View File
@@ -1,6 +1,6 @@
import { contractNamed } from "./contractCliDescription.js";
import { resolveJavaScriptMemberMutationAllowance } from "../config/javascriptMemberMutationAllowance.js";
import { z } from "incur";
import { z } from "zod";
import { cliDecimalField } from "./options.js";
import { requirePresence } from "../domain/schemaConstraints.js";
+2 -1
View File
@@ -2,7 +2,8 @@ import { withCommandCancellation } from "./cli/commandCancellation.js";
import { parseCliJsonInput } from "./cli/cliJsonInput.js";
import { analysisInputErrorFromIssues } from "./domain/inputIssueProjection.js";
import { bundleComparisonInputSchema } from "./domain/bundleComparison.js";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import {
compareEvidenceBundlesCommand,
+2 -1
View File
@@ -1,4 +1,5 @@
import { Help, z } from "incur";
import { Help } from "incur";
import { z } from "zod";
import type { HttpHostname } from "../domain/httpHostname.js";
import {
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "incur";
import { z } from "zod";
import { CLI_DOCUMENT_OUTPUT_FORMATS } from "./cliOutput.js";
@@ -3,7 +3,7 @@ import { join, resolve } from "node:path";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { expect } from "vitest";
import { z } from "incur";
import { z } from "zod";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
import { cliTest } from "../support/cli/cliFixture.js";
@@ -1,4 +1,5 @@
import { Parser, z } from "incur";
import { Parser } from "incur";
import { z } from "zod";
import { describe, expect, it } from "vitest";
import { createCli } from "../../../src/cli.js";
@@ -1,5 +1,6 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import { Parser, Schema, z } from "incur";
import { Parser, Schema } from "incur";
import { z } from "zod";
import { describe, expect, it } from "vitest";
import { createCli } from "../../../src/cli.js";
@@ -1,6 +1,7 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import type { AnySchema } from "ajv";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { expect, it } from "vitest";
import { createCli } from "../../../src/cli.js";
@@ -1,5 +1,6 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { resolve } from "node:path";
import { describe, expect, it, vi } from "vitest";
@@ -1,5 +1,6 @@
import { it, expect } from "vitest";
import { Parser, z } from "incur";
import { Parser } from "incur";
import { z } from "zod";
import {
splitCliTimingArguments,
normalizeCliArguments,
@@ -1,6 +1,7 @@
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import pino from "pino";
import { readFile, readdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
+2 -1
View File
@@ -1,5 +1,6 @@
import { resolve } from "node:path";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { expect, it } from "vitest";
import { createCliCompletions } from "../../../src/cli/completions.js";
@@ -1,4 +1,5 @@
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import pino from "pino";
import { expect, it } from "vitest";
@@ -1,4 +1,5 @@
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import pino from "pino";
import { expect, it } from "vitest";
@@ -1,7 +1,8 @@
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { Parser, z } from "incur";
import { Parser } from "incur";
import { z } from "zod";
import { decode } from "@toon-format/toon";
import { mcpRegistrationReportSchema } from "../../../src/contracts/mcpRegistration.js";
@@ -1,7 +1,8 @@
import { randomUUID } from "node:crypto";
import { Writable } from "node:stream";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { parse as parseYaml } from "yaml";
import { expect, it } from "vitest";
+2 -1
View File
@@ -1,6 +1,7 @@
import { Writable } from "node:stream";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { estimateTokenCount } from "tokenx";
import { describe, expect, it } from "vitest";
@@ -1,4 +1,5 @@
import { Cli, Errors, z } from "incur";
import { Cli, Errors } from "incur";
import { z } from "zod";
import { parseFragment, type DefaultTreeAdapterTypes } from "parse5";
import { estimateTokenCount } from "tokenx";
import { createMarkdownRenderer, disposeMdItInstance } from "vitepress";
@@ -1,7 +1,8 @@
import { join } from "node:path";
import { Ajv2020 } from "ajv/dist/2020.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { expect, it, vi } from "vitest";
import { createAnalysisExecution } from "../../../src/ports/AnalysisProvider.js";
import type { AdbDeviceAnalysisPort } from "../../../src/application/adb/AdbDeviceAnalysisPort.js";
@@ -1,6 +1,7 @@
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
import pino from "pino";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { afterEach, expect, it, vi } from "vitest";
import { rm } from "node:fs/promises";
+2 -1
View File
@@ -1,4 +1,5 @@
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import pino from "pino";
import { expect, it } from "vitest";
+2 -1
View File
@@ -1,7 +1,8 @@
import { EVIDENCE_FORMAT_REVISION } from "../../../src/domain/evidenceFormat.js";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Cli, z } from "incur";
import { Cli } from "incur";
import { z } from "zod";
import { expect, it } from "vitest";
import { createCli } from "../../../src/cli.js";
+29
View File
@@ -0,0 +1,29 @@
import { expect, it } from "vitest";
import { incurZodImportCount } from "../../scripts/verify/incur-import-guard.mjs";
it("counts declarations that take z from incur in every spelling", () => {
expect(incurZodImportCount('import { z } from "incur";')).toBe(1);
expect(incurZodImportCount("import type { z } from 'incur';")).toBe(1);
expect(incurZodImportCount('import { Cli, type z } from "incur";')).toBe(1);
expect(incurZodImportCount('import { z as zod } from "incur";')).toBe(1);
expect(incurZodImportCount('export { z } from "incur";')).toBe(1);
expect(
incurZodImportCount(
'import {\n Cli,\n z,\n} from "incur";\nconst a = 1;',
),
).toBe(1);
});
it("ignores other names, other modules and non-declaration text", () => {
expect(incurZodImportCount('import { Cli } from "incur";')).toBe(0);
expect(incurZodImportCount('import { schema as z } from "incur";')).toBe(0);
expect(incurZodImportCount('import { z } from "zod";')).toBe(0);
expect(incurZodImportCount('// import { z } from "incur";')).toBe(0);
expect(incurZodImportCount('/* import { z } from "incur"; */')).toBe(0);
expect(
incurZodImportCount("const fixture = 'import { z } from \"incur\";';"),
).toBe(0);
expect(
incurZodImportCount('const fixture = `\nimport { z } from "incur";\n`;'),
).toBe(0);
});