Skip to content
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

Finding unused classes #36

Open
lukepighetti opened this issue Feb 19, 2021 · 1 comment
Open

Finding unused classes #36

lukepighetti opened this issue Feb 19, 2021 · 1 comment

Comments

@lukepighetti
Copy link

lukepighetti commented Feb 19, 2021

I'm just poking around with dart_codemod and I was wondering if someone would be willing to suggest/discuss a strategy for finding unused classes.

My idea was to visit every class declaration and get the name of the class, then search the rest of the AST for any use of the constructor. I tried to walk the tree and wasn't successful.

void main(List<String> arguments) {
  runInteractiveCodemod(
    filePathsFromGlob(Glob('**.dart', recursive: true)),
    PrintEveryCommentInProject(),
    args: arguments,
  );
}

class PrintEveryCommentInProject extends GeneralizingAstVisitor
    with AstVisitingSuggestorMixin {
  @override
  dynamic visitDeclaration(Declaration node) {
    print(node.documentationComment);

    node.visitChildren(this);
  }
}
@evanweible-wf
Copy link
Contributor

Hi @lukepighetti! I think you'd need a fully resolved AST (or maybe an ElementVisitor) so that you can determine where each class is located in addition to its name, otherwise you could have classes that are distinct but named the same.

Either way, I'd probably start by writing a visitor that implements visitClassDeclaration to collect all of the classes, and then pass that info to another visitor that implements visitTypeName to determine which classes are unused. I haven't tested that idea, so there might be another visit method that would be better.. I can never quite keep all the analyzer APIs straight in my head :)

Couple other things to keep in mind:

  • Any class that is publicly exported from a package is technically "used" even if it doesn't have any usages in its own package.
  • Depending on what you want to do once you've found the unused classes, you might not even need this package - you might be able to get by with just package:analyzer

Hopefully that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants