Remove more unused variables
npm install remove-unused-varsRemove more unused variables. Highly experimental.
> [!WARNING]
>
> There's a reason linters don't fix the unused variables, even with a flag like --unsafe or --fix-dangerously.
Modern linters can fix a lot of issues automatically, but don't always remove all unused variables and types. Even when
using --unsafe or --fix-dangerously.
Use remove-unused-vars to remove everything the linter finds. Then run QA and review if there's anything that might
need reverting. Needless to say, Git's your friend here!
Don't use e.g. eslint --fix or biome lint --write when piping to remove-unused-vars, otherwise the positions to
remove things might not match up. Use the linter first to have it remove whatever it can, then proceed with the
command(s) below.
> [!TIP]
>
> Use this with Knip for a cruel code crusher experience.
``sh`
npm install -D remove-unused-vars
Pipe the JSON-formatted output of the linter to remove-unused-vars
Add to package.json#scripts, modify the commands below and make sure to use local linter configuration.
`sh`
eslint --rule 'no-unused-vars: error' --quiet -f json | remove-unused-vars
`sh`
eslint --rule 'no-unused-vars: off' --rule '@typescript-eslint/no-unused-vars: error' --quiet -f json | remove-unused-vars
`sh`
biome lint \
--only correctness/noUnusedVariables \
--only correctness/noUnusedImports \
--only correctness/noUnusedFunctionParameters \
--reporter json | remove-unused-vars
`sh`
oxlint -A all -D 'no-unused-vars' -D '@typescript-eslint/no-unused-vars' -f json | remove-unused-vars
Alternatively, provide a JSON file as the first argument to remove-unused-vars, for example:
`sh`
oxlint -A all -D '@typescript-eslint/no-unused-vars' -f json > unused-vars.json
remove-unused-vars unused-vars.json
Use something like this without installing remove-unused-vars:
`sh``
eslint -- --quiet --format json | npx -y remove-unused-vars