feat: clean

This commit is contained in:
TrèsAbhi committed 2026-03-20 14:48:38 -05:00
1 parent 80cc74a945
commit 950c99d3d7
1 file changed
+23 -43
@@ -1,6 +1,5 @@
--- ---
import { resolve } from "path"; import { resolve } from "path";
import { existsSync } from "fs";
import { readFile } from "fs/promises"; import { readFile } from "fs/promises";
import { Link } from "./Link"; import { Link } from "./Link";
import { api } from "../core/api/dynamic"; import { api } from "../core/api/dynamic";
@@ -10,7 +9,6 @@ import { ArrowBottomRightIcon } from "@radix-ui/react-icons";
import { Code } from "./Code"; import { Code } from "./Code";
import { import {
createProgram, createProgram,
createSourceFile,
forEachChild, forEachChild,
isEnumDeclaration, isEnumDeclaration,
isInterfaceDeclaration, isInterfaceDeclaration,
@@ -18,8 +16,6 @@ import {
isPropertySignature, isPropertySignature,
isTypeAliasDeclaration, isTypeAliasDeclaration,
isTypeReferenceNode, isTypeReferenceNode,
ScriptTarget,
TypeFormatFlags,
type InterfaceDeclaration, type InterfaceDeclaration,
type Node, type Node,
type TypeNode, type TypeNode,
@@ -39,31 +35,29 @@ const lang = "ts";
const name = "Avatar"; const name = "Avatar";
const path = resolve(`${roots[0]}/${value}.ts`); const path = resolve(`${roots[0]}/${value}.ts`);
const buffer = await readFile(path);
const program = createProgram([path], {}); const program = createProgram([path], {});
const source = program.getSourceFile(path); const source = program.getSourceFile(path);
if (!source) { if (source === undefined) {
throw new Error(`Could not find ${path}`); throw new Error(`Could not find ${path}`);
} }
const checker = program.getTypeChecker(); const checker = program.getTypeChecker();
const collected = new Map<string, string>(); const collected = new Map<string, string>();
let declaration: InterfaceDeclaration | undefined; let declaration: InterfaceDeclaration | undefined;
forEachChild(source, (node) => { forEachChild(source, (node) => {
if (!isInterfaceDeclaration(node) || node.name.text !== name) return; if (isInterfaceDeclaration(node) && node.name.text === name) {
declaration = node; declaration = node;
}
}); });
if (declaration === undefined) { if (declaration === undefined) {
throw new Error(`Could not find ${name} in ${path}`); throw new Error(`Could not find ${name} in ${path}`);
} }
function printNode(node: Node) { function collectNodes(node: Node) {
if (isInterfaceDeclaration(node)) { if (isInterfaceDeclaration(node)) {
const symbol = checker.getSymbolAtLocation(node.name); const symbol = checker.getSymbolAtLocation(node.name);
@@ -74,36 +68,16 @@ function printNode(node: Node) {
node.members.forEach((member) => { node.members.forEach((member) => {
if (isPropertySignature(member) && member.type) { if (isPropertySignature(member) && member.type) {
collectTypeDependencies(member.type); collectTypes(member.type);
}
if (isMethodSignature(member)) {
member.parameters.forEach((param) => {
if (param.type) {
collectTypeDependencies(param.type);
}
});
if (member.type) {
collectTypeDependencies(member.type);
}
} }
}); });
} }
} }
forEachChild(node, printNode); forEachChild(node, collectNodes);
} }
function isRelevantTypeDecl(node: Node) { function collectTypes(node: TypeNode) {
return (
isInterfaceDeclaration(node) ||
isTypeAliasDeclaration(node) ||
isEnumDeclaration(node)
);
}
function collectTypeDependencies(node: TypeNode) {
const type = checker.getTypeAtLocation(node); const type = checker.getTypeAtLocation(node);
const symbol = type.getSymbol(); const symbol = type.getSymbol();
@@ -115,29 +89,35 @@ function collectTypeDependencies(node: TypeNode) {
const declarations = symbol.getDeclarations(); const declarations = symbol.getDeclarations();
if (declarations) { if (declarations) {
declarations.forEach((decl) => { declarations.forEach((declaration) => {
if (!isRelevantTypeDecl(decl)) return; if (
!isInterfaceDeclaration(declaration) &&
!isTypeAliasDeclaration(declaration) &&
!isEnumDeclaration(declaration)
) {
return;
}
const name = (decl as any).name?.text; const name = declaration.name.text;
if (!name || collected.has(name)) return;
collected.set(name, decl.getText()); if (collected.has(name)) return;
forEachChild(decl, collectTypeDependenciesFromNode); collected.set(name, declaration.getText());
forEachChild(declaration, collectTypesFromNode);
}); });
} }
} }
} }
function collectTypeDependenciesFromNode(node: Node) { function collectTypesFromNode(node: Node) {
if (isTypeReferenceNode(node)) { if (isTypeReferenceNode(node)) {
collectTypeDependencies(node); collectTypes(node);
} }
forEachChild(node, collectTypeDependenciesFromNode); forEachChild(node, collectTypesFromNode);
} }
printNode(declaration); collectNodes(declaration);
let content = ""; let content = "";