Conversation
| @@ -0,0 +1,5 @@ | |||
| { | |||
There was a problem hiding this comment.
One problem I had writing this was that our codebase is split into multiple different projects with multiple tsconfig.json files that all depend on each other, and all the tools only operate on a single project at a time. Unfortunately just looking at one project you can't tell if a piece of code is unused or not, because it could be used from another project that depends on this one. To work around this I created a new find-deadcode-tsconfig.json file that covers the entire repository. I'm not totally sure this means the dead code detection is 100% accurate, and there could be edge cases since the different projects use different rules, but I think it's likely to be good enough.
(Side note: it can't be named tsconfig.json because that file already exists in that directory. I could put it in another directory, but I'm not sure which one.)
|
I did try out other tools but I found that
|
| "format": "prettier --write **/*.{ts,tsx} && eslint . --ext .ts,.tsx --fix", | ||
| "lint": "eslint . --ext .js,.ts,.tsx --max-warnings=0", | ||
| "lint:markdown": "markdownlint-cli2 \"../../**/*.{md,mdx}\" \"!**/node_modules/**\" \"!**/.vscode-test/**\" \"!**/build/cli/v*/**\"", | ||
| "find-deadcode": "ts-unused-exports find-deadcode-tsconfig.json --showLineNumber --excludePathsFromReport=$(find src test -type f '(' -name jest.config.ts -or -name 'index.tsx' -or -name 'index.ts' ')' -print0 | tr '\\0' ';')'gulpfile.ts/;src/stories/;test/vscode-tests/jest-runner-installed-extensions.ts'", |
There was a problem hiding this comment.
There are quite a few files that either have to have unused exported items by design (e.g. the index.tsx files or anything in src/stories/), and perhaps that we just don't care as much about so it's easier to ignore them to avoid jumping through hoops. Feel free to say if you think this list is not correct.
koesie10
left a comment
There was a problem hiding this comment.
LGTM, thanks for converting it to a script!
This PR installs
ts-unused-exportsand uses it to find dead code. This PR doesn't fix any existing violations, so the CI will fail until we fix those in #2632.The specific thing that this tools helps find is code that is exported but then not imported from anywhere else. This either means the code can be unexported if it's still used within its file, or it can be deleted.
By virtue of
typescript-eslint/no-unused-varswe already have detection of unexported code (including functions/types/variables) that is not used elsewhere within its file. Usingts-unused-exportsallows us to go one step further and look for unnecessary exports, and then once unexported our existing eslint rules will find any unused code.Checklist
ready-for-doc-reviewlabel there.