func-call-spacing

要求或不允许函数标识符和它们的调用之间有空格

🔧 Fixable

此规则报告的一些问题可通过 --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.

调用函数时,开发者可以在函数名称和调用它的括号之间插入可选的空格。以下成对的函数调用是等价的:

¥When calling a function, developers may insert optional whitespace between the function’s name and the parentheses that invoke it. The following pairs of function calls are equivalent:

alert('Hello');
alert ('Hello');

console.log(42);
console.log (42);

new Date();
new Date ();

规则详情

¥Rule Details

此规则要求或不允许函数名和调用它的左括号之间有空格。

¥This rule requires or disallows spaces between the function name and the opening parenthesis that calls it.

选项

¥Options

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

¥This rule has a string option:

  • "never"(默认)不允许函数名称和左括号之间有空格。

    ¥"never" (default) disallows space between the function name and the opening parenthesis.

  • "always" 要求函数名称和左括号之间有空格。

    ¥"always" requires space between the function name and the opening parenthesis.

此外,在 "always" 模式下,可以使用包含单个布尔 allowNewlines 属性的第二个对象选项。

¥Further, in "always" mode, a second object option is available that contains a single boolean allowNewlines property.

never

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

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

在线运行
/*eslint func-call-spacing: ["error", "never"]*/

fn ();

fn
();

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

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

在线运行
/*eslint func-call-spacing: ["error", "never"]*/

fn();

always

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

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

在线运行
/*eslint func-call-spacing: ["error", "always"]*/

fn();

fn
();

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

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

在线运行
/*eslint func-call-spacing: ["error", "always"]*/

fn ();

allowNewlines

默认情况下,"always" 不允许换行。要在 "always" 模式下允许换行,请将 allowNewlines 选项设置为 true。从来不需要换行符。

¥By default, "always" does not allow newlines. To permit newlines when in "always" mode, set the allowNewlines option to true. Newlines are never required.

启用了 allowNewlines 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with allowNewlines option enabled:

在线运行
/*eslint func-call-spacing: ["error", "always", { "allowNewlines": true }]*/

fn();

启用 allowNewlines 选项后此规则的正确代码示例:

¥Examples of correct code for this rule with the allowNewlines option enabled:

在线运行
/*eslint func-call-spacing: ["error", "always", { "allowNewlines": true }]*/

fn (); // Newlines are never required.

fn
();

何时不使用

¥When Not To Use It

如果你的项目不关心在函数调用中强制使用一致的间距样式,则可以安全地关闭此规则。

¥This rule can safely be turned off if your project does not care about enforcing a consistent style for spacing within function calls.

兼容性

¥Compatibility

版本

此规则是在 ESLint v3.3.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号