Reputation: 3496
What is the magic configuration for allowing a file to be auto-formatted upon a Save operation, but stop / disable / prevent VSCode from deleting dead code?
Sometimes I want to deliberately throw an exception in the middle of a function, for debugging purposes but am forced to comment out all following code in order not to get it deleted.
What's worse is that sometime I save while the editor didn't yet recover from some error in the code, thinks the code still contains an error, and causes code deletions which should not happen in the first place. I found myself too many times pulling up git in order to restore good code which was wrongly deleted.
Is there a clear "do not delete dead code" option to switch on?
UPDATE:
Upvotes: 12
Views: 3010
Reputation: 1179
Based on the issue referenced in @NikoSams answer, you just need to add "allowUnreachableCode": true
to your tsconfig.json
and it won't automatically remove dead code; you don't need to completely disable fixes on file save.
It's not listed as a setting because the extension at fault is the built-in typescript extension, and it gets its "settings" from your tsconfig.json
, if present.
{
"compilerOptions": {
"allowUnreachableCode": true,
...
},
...
}
Upvotes: 1
Reputation: 4404
source.fixAll
activates also TS fixes, which are somehow too aggressive to use on save. Only enabling eslint fixes with source.fixAll.eslint
is a good compromise - it will still report dead code (no-unreachable) but not delete it.
https://github.com/microsoft/vscode/issues/109530
Upvotes: 10
Reputation: 3496
Case solved.
I can't pinpoint the exact package that's causing it but it seems to be related to either ESLint or Prettier.
Turns out that the project's Github repo contains a .vscode
directory with a settings.json that contains the following configuration:
{
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
I already saw posts saying to add "source.fixAll": false
to VSCode's general settings.json
file, but it had no effect when I did.
Setting the flag to false
did the trick
{
"editor.codeActionsOnSave": {
"source.fixAll": false
}
}
Note: Adding the above block to the general settings.json
had no effect as well. I had to modify the local .vscode/settings.json
file to get it to work.
Upvotes: 4