typescript로 데드코드 찾기 (tsconfig, ts-prune)

Tags
 

설치

yarn add ts-prune

설정

tsconfig.ts

{ "compilerOptions": { ...otherOptions, "noUnusedLocals": true, "noUnusedParameters": true, } }

package.json

package.jsonscript 란에 명령어 추가
{ ... "scripts": { "find-deadcode": "ts-prune | (! grep -v 'used in module')", } ... }

명령

yarn find-deadcode # 내가 친 명령어 (현재 프로젝트에서 작성된 부분만 참조하고자 그 외의 불필요한 부분들을 필터링함) yarn find-deadcode | grep -v -e "src/generated" -e "src/algocare_modules" # index.ts 파일에서 참조하지 않은 경우도 제외해서 보고 싶다면 yarn find-deadcode | grep -v -e "src/generated" -e "src/algocare_modules" -e ".*index.ts" # grep -Ev 'word1|word2' 이런 식으로도 한번에 제외 가능
 
notion image
명령어 결과 0 references인 파일들을 쭉 목록화 해주는 모습이다.
 
notion image
실제로 결과물로 나온 파일에 가보면 0 references으로 확인되는 것을 볼 수 있다.
어디에서도 현재 사용중이지 않은 파일이므로 제거해도 무방하다.

주의

notion image
notion image
notion image
참조가 없다고 감지되었으나, 실제로 해당 컴포넌트로 가보면 2 references로 사용중인 컴포넌트다.
탐지 결과 파일을 자세히 보면 reference가 없다고 하는 파일은 index.ts이다.
notion image
index.ts로부터 import 해서 사용한 것이 없다는 뜻이다. ( 해당 컴포넌트를 직접 import한 것만 있으므로 )
yarn find-deadcode | grep -v -e "src/generated" -e "src/algocare_modules" -e ".*index.ts"
그래서 index.ts로 끝나는 파일도 제외시켜서 보곤 한다.