Index

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 中删除。