47 lines
1.5 KiB
TypeScript
Executable file
47 lines
1.5 KiB
TypeScript
Executable file
import { Project } from 'ts-morph';
|
|
|
|
console.log("Starting unused import pruning for runboard-route-logic.tsx...");
|
|
|
|
const project = new Project({
|
|
tsConfigFilePath: 'C:/ScriptoriumAI/scriptoriumai-ui/tsconfig.json'
|
|
});
|
|
|
|
const sourceFile = project.getSourceFileOrThrow('C:/ScriptoriumAI/scriptoriumai-ui/src/utils/runboard-route-logic.tsx');
|
|
|
|
let removedCount = 0;
|
|
const importDeclarations = sourceFile.getImportDeclarations();
|
|
|
|
for (const importDecl of importDeclarations) {
|
|
const defaultImport = importDecl.getDefaultImport();
|
|
const namedImports = importDecl.getNamedImports();
|
|
let keepAny = false;
|
|
|
|
if (defaultImport) {
|
|
const refs = defaultImport.findReferencesAsNodes();
|
|
if (refs.length > 0) keepAny = true;
|
|
}
|
|
|
|
const unreferencedNamed = [];
|
|
for (const named of namedImports) {
|
|
const refs = named.getNameNode().findReferencesAsNodes();
|
|
// refs inherently contains the declaration itself, so length > 1 means it's used elsewhere
|
|
if (refs.length <= 1) {
|
|
unreferencedNamed.push(named);
|
|
removedCount++;
|
|
} else {
|
|
keepAny = true;
|
|
}
|
|
}
|
|
|
|
// Process removal precisely
|
|
for(const named of unreferencedNamed) {
|
|
named.remove();
|
|
}
|
|
|
|
if (!keepAny && namedImports.length === unreferencedNamed.length && !defaultImport) {
|
|
importDecl.remove();
|
|
}
|
|
}
|
|
|
|
sourceFile.saveSync();
|
|
console.log(`Pruned roughly ${removedCount} unused named imports.`);
|