space-before-function-parentheses

在函数定义中打开括号之前强制保持一致的间距。

¥Enforces consistent spacing before opening parenthesis in function definitions.

格式化函数时,函数名或 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.

这条规则有一个参数。如果是默认选项 "always",则所有命名函数和匿名函数都必须在函数括号前有空格。如果是 "never",那么所有命名函数和匿名函数在函数括号之前不能有空格。如果你想要命名函数和匿名函数的不同间距,你可以将配置对象作为规则参数传递以分别配置它们(例如 {"anonymous": "always", "named": "never"})。

¥This rule takes one argument. If it is "always", which is the default option, all named functions and anonymous functions must have space before function parentheses. If "never" then all named functions and anonymous functions must not have space before function parentheses. If you want different spacing for named and anonymous functions you can pass a configuration object as the rule argument to configure those separately (e. g. {"anonymous": "always", "named": "never"}).

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

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

function foo() {
    // ...
}

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

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

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

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

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

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

function foo () {
    // ...
}

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

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

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

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

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

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

function foo () {
    // ...
}

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

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

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

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

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

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

function foo() {
    // ...
}

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

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

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

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

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

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

function foo () {
    // ...
}

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

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

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

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

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

function foo() {
    // ...
}

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

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

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

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

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

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:

function foo () {
    // ...
}

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

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.15.0 中引入,并在 v1.0.0-rc-1 中删除。