enforce consistent line breaks inside function parentheses (function-paren-newline)

强制在函数括号内使用一致的换行 (function-paren-newline)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

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.

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

Example configurations:

示例配置:

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

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

选项 "always"错误 代码示例:

/* eslint function-paren-newline: ["error", "always"] */

function foo(bar, baz) {}

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

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

foo(bar, baz);

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

选项 "always"正确 代码示例:

/* eslint function-paren-newline: ["error", "always"] */

function foo(
  bar,
  baz
) {}

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

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

foo(
  bar,
  baz
);

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

选项 "never"错误 代码示例:

/* eslint function-paren-newline: ["error", "never"] */

function foo(
  bar,
  baz
) {}

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

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

foo(
  bar,
  baz
);

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

选项 "never"正确 代码示例:

/* eslint function-paren-newline: ["error", "never"] */

function foo(bar, baz) {}

function foo(bar,
             baz) {}

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

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

foo(bar, baz);

foo(bar,
  baz);

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

默认选项 "multiline"错误 代码示例:

/* eslint function-paren-newline: ["error", "multiline"] */

function foo(bar,
  baz
) {}

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

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

foo(bar,
  baz);

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

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

默认选项 "multiline"正确 代码示例:

/* eslint function-paren-newline: ["error", "multiline"] */

function foo(bar, baz) {}

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

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

foo(bar, baz, qux);

foo(
  bar,
  baz,
  qux
);

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

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

选项 "consistent"错误 代码示例:

/* eslint function-paren-newline: ["error", "consistent"] */

function foo(bar,
  baz
) {}

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

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

foo(
  bar,
  baz);

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

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

选项 "consistent"正确 代码示例:

/* eslint function-paren-newline: ["error", "consistent"] */

function foo(bar,
  baz) {}

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

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

foo(
  bar, baz
);

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

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

选项 "multiline-arguments"错误 代码示例:

/* eslint function-paren-newline: ["error", "multiline-arguments"] */

function foo(bar,
  baz
) {}

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

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

foo(
  bar,
  baz);

foo(
  bar, qux,
  baz
);

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

选项 "multiline-arguments"正确 代码示例:

/* eslint function-paren-newline: ["error", "multiline-arguments"] */

function foo(
  bar,
  baz
) {}

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

var foo = (
  bar
) => {};

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

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

选项 { "minItems": 3 }错误 代码示例:

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

function foo(
  bar,
  baz
) {}

function foo(bar, baz, qux) {}

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

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

foo(bar,
  baz);

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

选项 { "minItems": 3 }正确 代码示例:

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

function foo(bar, baz) {}

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

var foo = (
  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.

如果你不想在函数括号内强制使用一致的换行,不要开启此规则。

Version

This rule was introduced in ESLint 4.6.0.

该规则在 ESLint 4.6.0 中被引入。

Resources