Files
reverse-engineering-anything/scripts/verify/incur-import-guard.mjs
T
morluto ec1cf3a9bd refactor(cli): render command help with REA's own help owner (#11683)
* refactor(cli): render command help with REA's own help owner

Add src/cli/help.ts and move the three incur Help.formatCommand callers
(rea mcp add, rea mcp doctor, rea mcp --http) onto it. Option spelling and
the unwrapped field type move to src/cli/optionFields.ts, which completion
now reads instead of its own copies. A counted incur-import guard records
the names still imported from incur so the list can only shrink.

Refs #11654

* refactor(cli): share option spelling across CLI owners and count every incur import

The argv parser, strict positionals, contract command projection and the
command registry now read the flag spelling, declared field type and count
marker from src/cli/optionFields.ts instead of their own copies. The
incur-import guard also counts namespace, default, star, side-effect and
dynamic imports, so `import * as Incur from "incur"` cannot bring a removed
owner back.

* refactor(cli): format and filter CLI output without incur (#11684)

* refactor(cli): format and filter CLI output without incur

formatCliOutput encodes TOON (@toon-format/toon), JSON, YAML and JSON Lines
itself instead of calling incur's Formatter; YAML still loads on first use.
src/cli/outputFilter.ts owns the --filter-output grammar and returns a
Result: an unclosed slice is a parse failure, so output validation reads
the parser instead of a second bracket scanner, and the parsed paths
travel with the output arguments to the result writer and JSON Lines.
@toon-format/toon moves from devDependencies to dependencies because REA
now imports it at runtime.

Refs #11654

* test(cli): pin output encodings and filter grammar with literal cases

Literal expected outputs for empty values, TOON scalars, objects and rows,
JSON-text re-indentation, nested BigInt values, JSON Lines rows and YAML,
and literal parsed paths and unclosed-slice refusals for --filter-output,
so a change to either owner fails without comparing against itself.
2026-11-16 12:01:04 +08:00

113 lines
4.0 KiB
JavaScript

// Reads the bindings that import and re-export declarations take from incur:
// named (`import { Cli, type Help } from "incur"`, `export { z } from 'incur'`),
// default, namespace (`import * as Incur from "incur"`), star re-exports
// (`export * from "incur"`), side-effect imports and dynamic `import("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));
/**
* Read `{ a, type b, c as d }` starting at the `{` token.
* @returns {{ names: string[], end: number } | undefined} imported names and
* the index after `}`
*/
const namedBindings = (tokens, open) => {
const names = [];
let expectName = true;
let at = open + 1;
for (; at < tokens.length && tokens[at].text !== "}"; at += 1) {
const token = tokens[at];
if (token.text === ",") expectName = true;
// In `{ z as zod }` the imported name is z; in `{ type Cli }`, Cli.
else if (
expectName &&
token.kind === "id" &&
!(token.text === "type" && tokens[at + 1]?.kind === "id")
) {
names.push(token.text);
expectName = false;
}
}
return at < tokens.length ? { names, end: at + 1 } : undefined;
};
/**
* The bindings of the declaration whose `import` or `export` keyword is at
* `index`, or undefined when it does not take anything from incur.
* @returns {string[] | undefined}
*/
const incurBindings = (source, tokens, index) => {
let at = index + 1;
// `import("incur")` and `import "incur"` bind nothing by name.
if (tokens[index].text === "import" && tokens[at]?.text === "(")
return isIncurSpecifier(source, tokens[at + 1]) ? ["import()"] : undefined;
if (tokens[index].text === "import" && tokens[at]?.kind === "string")
return isIncurSpecifier(source, tokens[at]) ? ["import"] : undefined;
if (tokens[at]?.kind === "id" && tokens[at].text === "type") at += 1;
const names = [];
// A default import may precede a named or namespace clause.
if (
tokens[index].text === "import" &&
tokens[at]?.kind === "id" &&
tokens[at].text !== "from"
) {
names.push("default");
at += 1;
if (tokens[at]?.text !== ",") return finish(source, tokens, at, names);
at += 1;
}
if (tokens[at]?.text === "*") {
names.push("*");
at += 1;
if (tokens[at]?.kind === "id" && tokens[at].text === "as") at += 2;
} else if (tokens[at]?.text === "{") {
const named = namedBindings(tokens, at);
if (named === undefined) return undefined;
names.push(...named.names);
at = named.end;
} else if (names.length === 0) return undefined;
return finish(source, tokens, at, names);
};
const finish = (source, tokens, at, names) =>
tokens[at]?.text === "from" && isIncurSpecifier(source, tokens[at + 1])
? names
: undefined;
/**
* The bindings of each declaration that takes something from incur.
* @param {string} source
* @returns {string[][]}
*/
const incurDeclarations = (source) => {
const { tokens } = tokenize(source);
const declarations = [];
for (let index = 0; index < tokens.length; index += 1) {
const keyword = tokens[index];
if (
keyword.kind !== "id" ||
(keyword.text !== "import" && keyword.text !== "export")
)
continue;
const bindings = incurBindings(source, tokens, index);
if (bindings !== undefined) declarations.push(bindings);
}
return declarations;
};
/** Declarations that take zod's `z` from incur. @param {string} source */
export const incurZodImportCount = (source) =>
incurDeclarations(source).filter((names) => names.includes("z")).length;
/**
* Bindings taken from incur: one per named binding, and one for each default,
* namespace, star, side-effect or dynamic import. @param {string} source
*/
export const incurImportedNameCount = (source) =>
incurDeclarations(source).reduce((count, names) => count + names.length, 0);