function-paren-newline

在函数括号内强制使用一致的换行符

🔧 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.

许多风格指南要求或禁止函数括号内的换行符。

¥Many style guides require or disallow newlines inside of function parentheses.

规则详情

¥Rule Details

此规则在函数参数或参数的括号内强制执行一致的换行符。

¥This rule enforces consistent line breaks inside parentheses of function parameters or arguments.

选项

¥Options

此规则有一个选项,可以是字符串或对象。

¥This rule has a single option, which can either be a string or an object.

  • "always" 要求所有函数括号内都换行。

    ¥"always" requires line breaks inside all function parentheses.

  • "never" 不允许在所有函数括号内换行。

    ¥"never" disallows line breaks inside all function parentheses.

  • 如果任何参数/参数之间有换行符,则 "multiline"(默认)要求函数括号内有换行符。否则,它不允许换行。

    ¥"multiline" (default) requires linebreaks inside function parentheses if any of the parameters/arguments have a line break between them. Otherwise, it disallows linebreaks.

  • "multiline-arguments" 的工作方式与 multiline 类似,但如果只有一个参数/参数,则允许在函数括号内换行。

    ¥"multiline-arguments" works like multiline but allows linebreaks inside function parentheses if there is only one parameter/argument.

  • "consistent" 要求每对括号一致地使用换行符。如果一对括号中的一个括号内有换行符,而另一个括号内没有,则会报告错误。

    ¥"consistent" requires consistent usage of linebreaks for each pair of parentheses. It reports an error if one parenthesis in the pair has a linebreak inside it and the other parenthesis does not.

  • 如果参数/参数的数量至少为 value,则 { "minItems": value } 需要在函数括号内使用换行符。否则,它不允许换行。

    ¥{ "minItems": value } requires linebreaks inside function parentheses if the number of parameters/arguments is at least value. Otherwise, it disallows linebreaks.

示例配置:

¥Example configurations:

{
  "rules": {
    "function-paren-newline": ["error", "never"]
  }
}
{
  "rules": {
    "function-paren-newline": ["error", { "minItems": 3 }]
  }
}

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

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

在线运行
/* eslint function-paren-newline: ["error", "always"] */

function foo(bar, baz) {}

var qux = function(bar, baz) {};

var qux = (bar, baz) => {};

foo(bar, baz);

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

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

在线运行
/* eslint function-paren-newline: ["error", "always"] */

function foo(
  bar,
  baz
) {}

var qux = function(
  bar, baz
) {};

var qux = (
  bar,
  baz
) => {};

foo(
  bar,
  baz
);

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

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

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

function foo(
  bar,
  baz
) {}

var qux = function(
  bar, baz
) {};

var qux = (
  bar,
  baz
) => {};

foo(
  bar,
  baz
);

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

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

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

function foo(bar, baz) {}

function qux(bar,
             baz) {}

var foobar = function(bar, baz) {};

var foobar = (bar, baz) => {};

foo(bar, baz);

foo(bar,
  baz);

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

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

在线运行
/* eslint function-paren-newline: ["error", "multiline"] */

function foo(bar,
  baz
) {}

var qux = function(
  bar, baz
) {};

var qux = (
  bar,
  baz) => {};

foo(bar,
  baz);

foo(
  function() {
    return baz;
  }
);

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

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

在线运行
/* eslint function-paren-newline: ["error", "multiline"] */

function foo(bar, baz) {}

var foobar = function(
  bar,
  baz
) {};

var foobar = (bar, baz) => {};

foo(bar, baz, qux);

foo(
  bar,
  baz,
  qux
);

foo(function() {
  return baz;
});

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

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

在线运行
/* eslint function-paren-newline: ["error", "consistent"] */

function foo(bar,
  baz
) {}

var qux = function(bar,
  baz
) {};

var qux = (
  bar,
  baz) => {};

foo(
  bar,
  baz);

foo(
  function() {
    return baz;
  });

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

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

在线运行
/* eslint function-paren-newline: ["error", "consistent"] */

function foo(bar,
  baz) {}

var qux = function(bar, baz) {};

var qux = (
  bar,
  baz
) => {};

foo(
  bar, baz
);

foo(
  function() {
    return baz;
  }
);

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

¥Examples of incorrect code for this rule with the "multiline-arguments" option:

在线运行
/* eslint function-paren-newline: ["error", "multiline-arguments"] */

function foo(bar,
  baz
) {}

var foobar = function(bar,
  baz
) {};

var foobar = (
  bar,
  baz) => {};

foo(
  bar,
  baz);

foo(
  bar, qux,
  baz
);

具有一致 "multiline-arguments" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the consistent "multiline-arguments" option:

在线运行
/* eslint function-paren-newline: ["error", "multiline-arguments"] */

function foo(
  bar,
  baz
) {}

var qux = function(bar, baz) {};

var qux = (
  bar
) => {};

foo(
  function() {
    return baz;
  }
);

使用 { "minItems": 3 } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "minItems": 3 } option:

在线运行
/* eslint function-paren-newline: ["error", { "minItems": 3 }] */

function foo(
  bar,
  baz
) {}

function foobar(bar, baz, qux) {}

var barbaz = function(
  bar, baz
) {};

var barbaz = (
  bar,
  baz
) => {};

foo(
  bar,
  baz
);

使用 { "minItems": 3 } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "minItems": 3 } option:

在线运行
/* eslint function-paren-newline: ["error", { "minItems": 3 }] */

function foo(bar, baz) {}

var foobar = function(
  bar,
  baz,
  qux
) {};

var foobar = (
  bar, baz, qux
) => {};

foo(bar, baz);

foo(
  bar, baz, qux
);

何时不使用

¥When Not To Use It

如果不想在函数括号内强制执行一致的换行符,请不要打开此规则。

¥If don’t want to enforce consistent linebreaks inside function parentheses, do not turn on this rule.

版本

此规则是在 ESLint v4.6.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号