-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests shouldn't make code to be considered as used #23284
Comments
It would be nice to have further context here |
A component was added with a test, it was not used anywhere, it was not identified as dead code because it was imported by a test. If we stop using a bit of code and it happens to have a unit test then it will never get identified as dead code |
I played around with this a bit today, and there's definitely some stuff that can be safely removed (e.g. From what I can gather, import { Project, Node, ReferenceFindableNode, SourceFile } from "ts-morph";
import * as path from "node:path";
const root = path.resolve(path.join(__dirname, ".."));
const project = new Project({
tsConfigFilePath: path.join(root, "tsconfig.json"),
});
const SAFE: Record<string, string[]> = {
// list symbols that are externally depended upon, e.g.
"src/rageshake/rageshake": ["init", "flush", "cleanup", "tryInitStorage"],
};
for (const file of project.getSourceFiles()) {
file.forEachChild((child) => {
if (Node.isVariableStatement(child)) {
if (isExported(child)) child.getDeclarations().forEach(checkNode);
} else if (isExported(child)) checkNode(child);
});
}
function isExported(node: Node) {
return Node.isExportable(node) && node.isExported();
}
function checkNode(node: Node) {
if (Node.isInterfaceDeclaration(node) || Node.isTypeAliasDeclaration(node)) return;
if (!Node.isReferenceFindable(node)) return;
const file = node.getSourceFile();
const filePath = path.relative(root, file.getFilePath());
const symbName = Node.hasName(node) ? node.getName() : node.getText();
if (isSafe(node)) {
return;
}
if (isDeadCode(node, file))
console.log(`[${filePath}:${node.getStartLineNumber()}]: ${symbName} - ${node.getKindName()}`);
}
function isDeadCode(node: ReferenceFindableNode, _file: SourceFile): boolean {
return node.findReferencesAsNodes().length === 0;
}
function isSafe(node: Node): boolean {
const file = node.getSourceFile();
const filePath = path.relative(root, file.getFilePath());
const symbName = Node.hasName(node) ? node.getName() : node.getText();
// ts-morph likes to omit extensions, so check against both
return [filePath, filePath.replace(/\.tsx?$/, "")].some((p) => !!SAFE[p]?.includes(symbName));
} (Based on the same gist that I've skipped type-level information altogether, since it's elided from the final build, but there's fiddling to be done there too. I could try to add a feature to In order for something like this to be realistic, |
@clarkf thanks for digging into that, I'm working on noImplicitAny right now! |
The work on enabling |
No description provided.
The text was updated successfully, but these errors were encountered: