space-before-function-paren
在 function
定义左括号之前强制执行一致的间距
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
格式化函数时,函数名或 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
(默认)需要一个空格,后跟(
参数。¥
always
(default) requires a space followed by the(
of arguments. -
never
不允许任何空格后跟(
参数。¥
never
disallows any space followed by the(
of arguments.
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 () {}
)。¥
anonymous
is for anonymous function expressions (e.g.function () {}
). -
named
用于命名函数表达式(例如function foo () {}
)。¥
named
is for named function expressions (e.g.function foo () {}
). -
asyncArrow
用于异步箭头函数表达式(例如async () => {}
)。¥
asyncArrow
is for async arrow function expressions (e.g.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 中引入。