Let’s say there is an if statement like this:
if( a && b )
c();
Is there a way to auto-format it to having extra spaces around AND (&&
) / OR (||
) operators using VSCode’s default formatter (editor.formatOnSave
)?
if( a && b )
c();
Most formatting settings I found, except for this one.
2
Answers
VS Code’s default JavaScript and TypeScript formatter come from TypeScript. The closest thing to what you want that I’m aware of that providing is
javascript.format.insertSpaceBeforeAndAfterBinaryOperators
andtypescript.format.insertSpaceBeforeAndAfterBinaryOperators
. But I’m pretty sure that only makes one space and not two.If you want something relatively vanilla, you could use search and replace as a workaround. Use the search view’s replace feature, turn on regex mode, put
s*(&&|||)s*
in the search field, and put$1
in the replace field, and click the triple dots to open up the extra menu and put*.ts,*.js
in the "files to include" field.So use the Prittier extension. It will be easy for you. And also you can configure it from the VS Code setting.json according to the languages.