In your settings.json add the following
xxxxxxxxxx
{
"editor.formatOnSave": true,
// to do most of the basic formatting you need also add the following
// add as required
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
}
To just add via VSCode settings:
Go to File > Preferences > Settings
Go to Text Editor > Formatting or search for "editor.formatOnSave" Format on save option.
Check the Editor: Format On Save option.