Skip to content

Instantly share code, notes, and snippets.

@jaynetics
Created May 4, 2023 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaynetics/e0bc3211a55cb252eb05028e8a226446 to your computer and use it in GitHub Desktop.
Save jaynetics/e0bc3211a55cb252eb05028e8a226446 to your computer and use it in GitHub Desktop.
find possibly unused modules in a rails app
code = Dir['{app,config,lib}/**/*.{rb,slim,erb,rake}'].map { File.read _1 }.join; 1
defs = code.scan(/(?:class|module) \K[A-Z][\w:]+(?=\n)/); 1
refs = code.scan(/(?<!class |module )\b#{Regexp.union(defs + defs.map { |d| d.split('::').last })}\b/); 1
unused = defs.uniq.sort.select { |d| refs.count(d) == 0 && refs.count(d.split('::').last) == 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment