Prevent deleting dead code during Auto Format on Save? - visual-studio-code

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:
Running Prettier (the file's formatter) from command line did not delete dead code.
Trying to disable all extensions didn't help either. The dead code is still deleted upon save.

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.

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

Related

YAML file formatting in VSCODE

I just started using VSCODE and faced with an annoyance every time I paste in YAML code in an existing YML file.
Basically the editor seems to auto format the document and in doing so messes up the significant spaces in the document. This causes builds in Azure Devops to break.
Although VS code formats the document nicely into collapsible regions, the formatting annoyance makes it hard to use.
Any help would be appreciated.
BEFORE:
AFTER:
I fixed this by changing editor.autoIndent settings for yaml and dockercompose language
"[yaml]": {
"editor.autoIndent": "advanced"
},
"[dockercompose]": {
"editor.autoIndent": "advanced"
}
In VsCode, press ctrl+shift+p (cmd+shift+p in Mac), and search for Preferences: Open User Settings (JSON). There I added this line:
"[yaml]": {
"editor.defaultFormatter": "redhat.vscode-yaml"
},
I picked it because it contains the word yaml, so I figured it must be yaml specific.
Anyhow, it seems to do a pretty good job for me.
Turn off the setting format on paste. This is a global setting, but plugins sometimes have their own, so if you're running a formatter like prettier, you'll need to see whether this is even an option with that plugin.
Looks like the problem is in the first line. Maybe when you copy the code, you're not copying the indentation on the first line.
One trick I use, is copying from the end of the preceding line, so the copied code starts with a newline, and then the paste is perfect.
Or just add the indentation on that first line, after pasting.

How to stop VS Code indenting oddly when moving block of code with alt-arrow

See https://youtu.be/mJDg4WnrsUo
When I move a block of code down it does this odd indenting whereby each move down a line also does an indent. When I move it up it doesn't show this behavior.
I can format the entire file afterwards, but is there a way to avoid this behavior?
Incidentally, I use vim bindings but I also tried disabling them and it didn't change this behavior.
This is caused by auto-indent. To disable it, set:
"editor.autoIndent": "keep"
From OP: Here's the screen where you do that, it is in 'settings' from the main menu:
However what you show should never happen so please file a bug report for your case

How to stop VSCode indenting when pushing existing code to a new line?

I have some code written, every time I press Enter to push it lower, it keeps indenting it.
Is there a way to stop this behavior?
The indent is caused by the onEnterRules of the full/advanced setting of the Editor: Auto Indent preference:
To avoid the problem, you could change the setting to keep or none, but be aware it changes your preference globally (for any file type where indenting would apply).
It looks like there might be a way to configure the onEnterRules per language, but I'm not entirely sure how to do that yet. Editing the language specific setting in JSON with the following config did not work for me:
{
"[scss]": {
"editor.autoIndent": "keep"
}
}

turn off "use spaces, do not use tabs" in a jslint extension for VSCode

I got a VSCode JSLint extension and I got its settings pointing to an .eslintrc file where I have the following specified for indentation:
{
...
"indent" : [1, "tab"]
...
}
The problem is, it's still putting the squiggly green lines where I have some tabs and I can't tell where anything's going wrong with any settings.
I have evidence the rc file is actually working because I was successfully able to change it from single to double-quotes. However it appears to completely ignore the indentation setting inside my VSCode.
You could simply disable the use_spaces rule. It's separate from the indent rule you changed. A bit over an oversight from JSLint.
There were quite a few complains about that rule, even here on SO. Quite a few people (not only on SO) suggest switching to JSHint instead. Personally I've only used ESLint and therefore don't know much about the differences, and I'd suggest checking those for yourself anyway.

Why is my node_modules folder greyed out after command 'npm install'?

I wonder why my node_modules folder is greyed out in my VS Code editor after I run the command npm install.
Here the picture of my folder:
Files/folders that are included in .gitignore are greyed out. Normally node_modules folder is included within .gitignore.
It's because your node_modules folder is referenced in your .gitignore file.
Visual Studio tells you that this folder is ignored by the version control by graying it out.
As was already pointed out, VSCode automatically ignores everything in .gitignore. However, it also explicitely ignores some folders by default. All of this can be configured. Here are the steps you might want to take:
Check out the official documentation on Workspace settings
Open the relevant settings (you can choose between User (global) and Workspace). I usually do this via CTRL (or Command on Mac) + SHIFT + P -> > settings, which will show you the relevant choices: . For each you can either choose the UI or the JSON variant.
Also make sure to check the default settings via Peferences: Open Default Settings (JSON). You will find that, as of now (May 2021), **/node_modules is also automatically excluded, independent of other ignore files. Currently the default settings look like this:
// Configure glob patterns for excluding files and folders in fulltext searches and quick open. Inherits all glob patterns from the `files.exclude` setting. Read more about glob patterns [here](https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options).
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/*.code-search": true
},
// Controls whether to use global `.gitignore` and `.ignore` files when searching for files.
"search.useGlobalIgnoreFiles": false,
// Controls whether to use `.gitignore` and `.ignore` files when searching for files.
"search.useIgnoreFiles": true,
Enabling searching node_modules
"search.exclude": {
"**/node_modules": false
},
"search.useIgnoreFiles": false
WARNING: This will slow down things a lot since node_modules takes forever to search through - at least during cold start, that is when things are not cached. (Note that it is difficult to estimate how their caching works since it keeps evolving over time.)
To mitigate this, you might want to add some additional search.exclude settings. Note you will have to explicitely list everything you don't want, because...
The big shortcoming of VSCode search
search.exclude does not support:
negative look-ahead/behind
mutually cascading queries
Things like this will not work!
// does NOT work! :((
"search.exclude": {
"**/node_modules/#types": false,
"**/node_modules": true
}
This is a MAJOR problem which has been heavily discussed in the almost famous issue #869 for 6 years already! Join in and add to the 100+ comments! :)
Update: As of 2021/8/14, the issue has been taken off the backlog queue and put in the On Deck queue (but that also might not mean much).