Skip to content

Instantly share code, notes, and snippets.

@dsherret
Last active December 22, 2023 00:03
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsherret/0bae87310ce24866ae22425af80a9864 to your computer and use it in GitHub Desktop.
Save dsherret/0bae87310ce24866ae22425af80a9864 to your computer and use it in GitHub Desktop.
Searches files for any exported declarations that aren't used in other files.
// this could be improved... (ex. ignore interfaces/type aliases that describe a parameter type in the same file)
import { Project, TypeGuards, Node } from "ts-morph";
const project = new Project({ tsConfigFilePath: "tsconfig.json" });
for (const file of project.getSourceFiles()) {
file.forEachChild(child => {
if (TypeGuards.isVariableStatement(child)) {
if (isExported(child))
child.getDeclarations().forEach(checkNode);
}
else if (isExported(child))
checkNode(child);
});
}
function isExported(node: Node) {
return TypeGuards.isExportableNode(node) && node.isExported();
}
function checkNode(node: Node) {
if (!TypeGuards.isReferenceFindableNode(node))
return;
const file = node.getSourceFile();
if (node.findReferencesAsNodes().filter(n => n.getSourceFile() !== file).length === 0)
console.log(`[${file.getFilePath()}:${node.getStartLineNumber()}: ${TypeGuards.hasName(node) ? node.getName() : node.getText()}`);
}
@OliverJAsh
Copy link

Suggestion:

-for (const file of project.getDirectoryOrThrow("src").getDescendantSourceFiles()) {
+for (const file of project.getSourceFiles()) {

(My project doesn't have a src directory.) This seems to work OK?

Another idea: list exports which are only used in tests (using some heuristic for test files), and never used internally.

@dsherret
Copy link
Author

dsherret commented Nov 15, 2018

@OliverJAsh in v19 there's a helper method for excluding everything found in a node_modules folder: project.getSourceFiles().filter(s => !s.isInNodeModules()) (Edit: This is no longer required in v21 as files in the node_modules folder are not included unless explicitly added to the project)

By the way, the symbols hack isn't necessary anymore. In v19 it will always resolve dependencies when specifying a tsconfig.json in the constructor.

@zapo
Copy link

zapo commented Feb 9, 2019

This is super useful. Thank you guys for this snippet. We want to use that within our CI. Did you plan or be open to maintain it as a CLI node package ? I created a draft in https://github.com/zapo/unused-exports.ts that simply allows passing tsconfig in params, also include/exclude patterns for source files filtering and to return proper process exit code.

@OliverJAsh
Copy link

ts-simple-ast has been renamed to ts-morph, so the import will need updating. @dsherret Would you mind updating the gist?

@nadeesha
Copy link

nadeesha commented May 6, 2019

I've taken this idea a little bit further and built ts-prune: https://github.com/nadeesha/ts-prune

Thanks for the inspiration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment