VS Code - 函数括号前的空格

Yan*_*ich 30 visual-studio-code

有没有办法在VS代码中编辑函数时禁用删除括号前的空格?

让我们说我有一个功能

function render () {
    // some code here
}
Run Code Online (Sandbox Code Playgroud)

当我开始编辑它时,VS Code删除括号前的空格并将此代码转换为:

function render() {
    // some code here
}
Run Code Online (Sandbox Code Playgroud)

有没有办法禁用这种行为?

Ale*_*gle 33

  1. 在VS Code中打开文件 - >首选项 - >设置
  2. 添加到您的JSON配置:

"javascript.format.insertSpaceBeforeFunctionParenthesis": true

function render () {
    // some code here
}
Run Code Online (Sandbox Code Playgroud)

"javascript.format.insertSpaceBeforeFunctionParenthesis": false

function render() {
    // some code here
}
Run Code Online (Sandbox Code Playgroud)

  1. 现在,您可以继续使用自动格式选项 "editor.formatOnType": true

  • 对于打字稿,可以使用“`typescript.format.insertSpaceBeforeFunctionParenthesis”设置:true (3认同)

小智 26

我对匿名函数有相反的问题.我们使用更漂亮的扩展.自动更正在括号前插入空格.然后更漂亮的抱怨它.

var anonfunc = function() {
    // Expected syntax. 
}

var autocorrected = function () {
    // Auto-correct inserts a space
}
Run Code Online (Sandbox Code Playgroud)

有类似的代码选项,它解决了我的问题:

"javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false
Run Code Online (Sandbox Code Playgroud)

默认情况下是true.花了我一些时间,直到我累了纠正自动纠正.

  • 对于打字稿,您可以使用设置 `"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false` (2认同)

Nei*_*off 9

我在 VSCode 在构造函数和 ESLint 抱怨之后删除空格时遇到了类似的问题,因为没有空格。

  • 转到文件 -> 首选项 -> 设置
  • 搜索 constructor
  • 在旁边添加一个检查 JavaScript › Format: Insert Space After Constructor

在此处输入图片说明


mej*_*l57 7

就我而言,我想要 VS Code 的正常缩进/格式化行为,因此我禁用了 eslint 警告:

在 .eslintrc.js 文件中,我在规则中输入:

 'rules': {
    ....

    //disable rule of space before function parentheses 
    "space-before-function-paren": 0
  }
Run Code Online (Sandbox Code Playgroud)


Mat*_*ner 6

我在VSCode团队中。从VSCode 1.8开始,不支持立即使用此格式设置选项,但是我们正在跟踪该功能:https : //github.com/Microsoft/vscode/issues/15386,https : //github.com/Microsoft/TypeScript /问题/ 12234

作为一种解决方法,请尝试以下操作:

  • 安装eslint扩展名ext install eslint
  • 添加"eslint.autoFixOnSave": true到您的工作区或用户设置
  • 在项目的根目录中,创建一个.eslintrc.json带有:

    {
        ...
        "rules": {
            ...
            "space-before-function-paren": "error"
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    eslint扩展可以.eslintrc.json使用create .eslintrc.json命令为您创建启动器。

保存文件时,这将自动格式化函数以在其后留一个空格。


Yan*_*ich 2

我发现我"editor.formatOnType": true启用了设置。这就是让编辑器在您键入时自动设置代码格式的原因。禁用它有助于解决该问题。