Index

space-before-function-paren

function 定义左括号之前强制执行一致的间距

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

在格式化函数时,允许在函数名或 function 关键字与左括号之间使用空格。命名函数还要求在 function 关键字与函数名之间使用空格,但匿名函数则不需要空格。例如:

🌐 When formatting a function, whitespace is allowed between the function name or function keyword and the opening paren. Named functions also require a space between the function keyword and the function name, but anonymous functions require no whitespace. For example:

function withoutSpace(x) {
    // ...
}

function withSpace (x) {
    // ...
}

var anonymousWithoutSpace = function() {};

var anonymousWithSpace = function () {};

样式指南可能要求在用于匿名函数的 function 关键字后加一个空格,而其他指南则规定不要空格。类似地,函数名后的空格可能是必需的,也可能不是必需的。

🌐 Style guides may require a space after the function keyword for anonymous functions, while others specify no whitespace. Similarly, the space after a function name may or may not be required.

规则详情

🌐 Rule Details

此规则旨在在函数括号之前强制执行一致的间距,因此,只要空格与指定的首选项不匹配,就会触发警告。

🌐 This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn’t match the preferences specified.

选项

🌐 Options

此规则有一个字符串选项或一个对象选项:

🌐 This rule has a string option or an object option:

{
    "space-before-function-paren": ["error", "always"],
    // or
    "space-before-function-paren": ["error", {
        "anonymous": "always",
        "named": "always",
        "asyncArrow": "always"
    }],
}
  • always(默认)需要一个空格,然后是参数的 (
  • never 不允许在参数的 ( 前有任何空格。

string 选项不检查异步箭头函数表达式的向后兼容性。

🌐 The string option does not check async arrow function expressions for backward compatibility.

你也可以为每种类型的功能使用单独的选项。以下每个选项都可以设置为 "always""never""ignore"。默认值是 "always"

🌐 You can also use a separate option for each type of function. Each of the following options can be set to "always", "never", or "ignore". The default is "always".

  • anonymous 用于匿名函数表达式(例如 function () {})。
  • named 用于具名函数表达式(例如 function foo () {})。
  • asyncArrow 用于异步箭头函数表达式(例如 async () => {})。

“always”

使用默认 "always" 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default "always" option:

在线运行
/*eslint space-before-function-paren: "error"*/

function foo() {
    // ...
}

var bar = function() {
    // ...
};

var bar = function foo() {
    // ...
};

class Foo {
    constructor() {
        // ...
    }
}

var baz = {
    bar() {
        // ...
    }
};

var baz = async() => 1

使用默认 "always" 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default "always" option:

在线运行
/*eslint space-before-function-paren: "error"*/

function foo () {
    // ...
}

var bar = function () {
    // ...
};

var bar = function foo () {
    // ...
};

class Foo {
    constructor () {
        // ...
    }
}

var baz = {
    bar () {
        // ...
    }
};

var baz = async () => 1

“never”

使用 "never" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint space-before-function-paren: ["error", "never"]*/

function foo () {
    // ...
}

var bar = function () {
    // ...
};

var bar = function foo () {
    // ...
};

class Foo {
    constructor () {
        // ...
    }
}

var baz = {
    bar () {
        // ...
    }
};

var baz = async () => 1

使用 "never" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "never" option:

在线运行
/*eslint space-before-function-paren: ["error", "never"]*/

function foo() {
    // ...
}

var bar = function() {
    // ...
};

var bar = function foo() {
    // ...
};

class Foo {
    constructor() {
        // ...
    }
}

var baz = {
    bar() {
        // ...
    }
};

var baz = async() => 1

{"anonymous": "always", "named": "never", "asyncArrow": "always"}

使用 {"anonymous": "always", "named": "never", "asyncArrow": "always"} 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the {"anonymous": "always", "named": "never", "asyncArrow": "always"} option:

在线运行
/*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/

function foo () {
    // ...
}

var bar = function() {
    // ...
};

class Foo {
    constructor () {
        // ...
    }
}

var baz = {
    bar () {
        // ...
    }
};

var baz = async(a) => await a

使用 {"anonymous": "always", "named": "never", "asyncArrow": "always"} 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the {"anonymous": "always", "named": "never", "asyncArrow": "always"} option:

在线运行
/*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/

function foo() {
    // ...
}

var bar = function () {
    // ...
};

class Foo {
    constructor() {
        // ...
    }
}

var baz = {
    bar() {
        // ...
    }
};

var baz = async (a) => await a

{"anonymous": "never", "named": "always"}

使用 {"anonymous": "never", "named": "always"} 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the {"anonymous": "never", "named": "always"} option:

在线运行
/*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/

function foo() {
    // ...
}

var bar = function () {
    // ...
};

class Foo {
    constructor() {
        // ...
    }
}

var baz = {
    bar() {
        // ...
    }
};

使用 {"anonymous": "never", "named": "always"} 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the {"anonymous": "never", "named": "always"} option:

在线运行
/*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/

function foo () {
    // ...
}

var bar = function() {
    // ...
};

class Foo {
    constructor () {
        // ...
    }
}

var baz = {
    bar () {
        // ...
    }
};

{"anonymous": "ignore", "named": "always"}

使用 {"anonymous": "ignore", "named": "always"} 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the {"anonymous": "ignore", "named": "always"} option:

在线运行
/*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/

function foo() {
    // ...
}

class Foo {
    constructor() {
        // ...
    }
}

var baz = {
    bar() {
        // ...
    }
};

使用 {"anonymous": "ignore", "named": "always"} 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the {"anonymous": "ignore", "named": "always"} option:

在线运行
/*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/

var bar = function() {
    // ...
};

var bar = function () {
    // ...
};

function foo () {
    // ...
}

class Foo {
    constructor () {
        // ...
    }
}

var baz = {
    bar () {
        // ...
    }
};

何时不使用

🌐 When Not To Use It

如果你不关心函数括号前间距的一致性,你可以关闭此规则。

🌐 You can turn this rule off if you are not concerned with the consistency of spacing before function parenthesis.

版本

此规则是在 ESLint v0.18.0 中引入。

资源