Files
reverse-engineering-anything/scripts/lib/import-specifiers.mjs
T
gensokyo-dev e18594b247 refactor(composition): unify capability bindings and cleanup ownership (#8441)
Bind typed service creation, observed availability and retryable cleanup in one composition owner. Reuse selected CLI/MCP factories and canonical contract groups without copying provider operation inventories.

Closes #8429. Refs #740.

Validated paired real application workflows, actual local Codex CLI Terra/high use, complete Evidence and Unknown checkpoint parity, compiler-backed import boundaries, SDK schemas and final real mixed routing. Automated review findings were fixed at their producing owners. Existing exact-base CodeQL alerts are documented on the PR under the owner-authorized unrelated-check exception; no global alerts or security rules were changed.

Assisted-by: Suika (gensokyo-skills:suika-scatter-gather-planning)
2026-11-01 13:30:21 +08:00

64 lines
1.9 KiB
JavaScript

import { parse } from "@babel/parser";
import { traverseFast } from "@babel/types";
const literalValue = (node) => {
if (node?.type === "StringLiteral") return node.value;
if (node?.type === "TemplateLiteral" && node.expressions.length === 0)
return node.quasis[0]?.value.cooked ?? undefined;
return undefined;
};
/** Static string specifier on one import or re-export node, when it has one. */
export const importSpecifier = (node) => {
switch (node.type) {
case "ImportDeclaration":
case "ExportNamedDeclaration":
case "ExportAllDeclaration":
case "ImportExpression":
case "TSImportType":
return literalValue(node.source);
case "TSImportEqualsDeclaration":
return node.moduleReference.type === "TSExternalModuleReference"
? literalValue(node.moduleReference.expression)
: undefined;
default:
return undefined;
}
};
/** Whether TypeScript erases this module edge under verbatimModuleSyntax. */
const isTypeOnly = (node) => {
switch (node.type) {
case "TSImportType":
return true;
case "ImportDeclaration":
case "TSImportEqualsDeclaration":
return node.importKind === "type";
case "ExportNamedDeclaration":
case "ExportAllDeclaration":
return node.exportKind === "type";
default:
return false;
}
};
/**
* Static import and re-export specifiers in source order.
* Comments and string literals are not imports. Non-string dynamic imports
* are omitted. Callers decide which specifiers are in-repo edges.
*/
export const readImportSpecifiers = (source) => {
const specifiers = [];
const tree = parse(source, { sourceType: "module", plugins: ["typescript"] });
traverseFast(tree, (node) => {
const specifier = importSpecifier(node);
if (specifier === undefined) return;
specifiers.push({
specifier,
line: node.loc?.start.line ?? 1,
typeOnly: isTypeOnly(node),
});
});
return specifiers;
};