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;
};