VS Code - space before function parentheses
Solution 1:
- In VS Code open File -> Preferences -> Settings
- Add to your JSON config:
"javascript.format.insertSpaceBeforeFunctionParenthesis": true
function render () {
// some code here
}
"javascript.format.insertSpaceBeforeFunctionParenthesis": false
function render() {
// some code here
}
- Now you can continue using your auto format option
"editor.formatOnType": true
Solution 2:
I had opposite problem with anonymous functions. We use prettier extension. Auto-correct inserts a space before parenthesis. And then prettier complains about it.
var anonfunc = function() {
// Expected syntax.
}
var autocorrected = function () {
// Auto-correct inserts a space
}
There is similar code option, which solves my problem:
"javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false
By default it is true
. Took me some time, until I was tired correcting auto-correct.
Solution 3:
I had a similar issue with VSCode removing spaces after a constructor and ESLint complaining because there wasn't a space.
- Go to File -> Preferences -> Settings
- Search for
constructor
- Add a check next to
JavaScript › Format: Insert Space After Constructor
Solution 4:
I'm on the VSCode team. As of VSCode 1.8, this formatting option is not supported out of the box, but we are tracking the feature: https://github.com/Microsoft/vscode/issues/15386, https://github.com/Microsoft/TypeScript/issues/12234
As a workaround, try the following:
- Install the eslint extension:
ext install eslint
- Add
"eslint.autoFixOnSave": true
to your workspace or user settings -
In the root of your project, create an
.eslintrc.json
with:{ ... "rules": { ... "space-before-function-paren": "error" } }
The eslint extension can create a starter
.eslintrc.json
for you with thecreate .eslintrc.json
command.
This will automatically format functions to have a space after them when you save the file.