find dead JavaScript code?

后端 未结 8 897
醉酒成梦
醉酒成梦 2020-12-05 05:51

We are refactoring a legacy web app and as a result are \"killing\" quite a lot of JavaScript code but we\'re afraid of deleting what we think is dead code due to not being

8条回答
  •  抹茶落季
    2020-12-05 06:54

    I hate this problem, and that there are no good tools for solving it, despite the parse-heavy javascript ecosystem. As mentioned in another answer, deadfile is pretty neat, but I couldn't make it work for my codebase, which uses absolute imports from a src directory. The following bash was good enough to get an idea of whether any files weren't imported anywhere (I found some!), which was easily hand-verifiable.

    for f in $(find src -name '*.js' | grep -E 'src/(app|modules|components).*\.js$' | grep -v '.test.js'); do
      f=${f/src\//};
      f=${f/\/index.js/};
      f=${f/.js/};
    
      echo "$f imported in"$(grep -rl "$f" src | wc -l)" files"
    done
    

    I didn't care about tests/resources, hence the app|modules|components bit. The string replacements could be cleaned up significantly too, but hopefully this will be useful to someone.

提交回复
热议问题