function-paren-newline
在函数括号内强制使用一致的换行符
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
许多风格指南要求或禁止函数括号内的换行符。
🌐 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"要求在所有函数括号内使用换行符。"never"不允许在所有函数括号内换行。"multiline"(默认)要求如果函数的参数/实参之间有换行,则在函数括号内使用换行。否则,不允许换行。"multiline-arguments"的工作方式类似于multiline,但如果函数括号内只有一个参数/实参时,它允许换行。"consistent"要求每对括号使用一致的换行。如果一对括号中的一个括号内部有换行而另一个没有,它会报告错误。{ "minItems": value }要求如果函数括号内的参数/实参数量至少为value,则必须换行。否则,不允许换行。
示例配置:
🌐 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 中引入。