implicit-arrow-linebreak

强制箭头函数体的位置

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

箭头函数体可以包含作为表达式而不是块体的隐式返回。为隐式返回的表达式强制执行一致的位置可能很有用。

¥An arrow function body can contain an implicit return as an expression instead of a block body. It can be useful to enforce a consistent location for the implicitly returned expression.

规则详情

¥Rule Details

此规则旨在为包含隐式返回的箭头函数强制执行一致的位置。

¥This rule aims to enforce a consistent location for an arrow function containing an implicit return.

选项

¥Options

此规则接受字符串选项:

¥This rule accepts a string option:

  • "beside"(默认)不允许在箭头函数体之前有换行符。

    ¥"beside" (default) disallows a newline before an arrow function body.

  • "below" 需要在箭头函数体之前有一个换行符。

    ¥"below" requires a newline before an arrow function body.

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

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

在线运行
/* eslint implicit-arrow-linebreak: ["error", "beside"] */

(foo) =>
  bar;

(foo) =>
  (bar);

(foo) =>
  bar =>
    baz;

(foo) =>
(
  bar()
);

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

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

在线运行
/* eslint implicit-arrow-linebreak: ["error", "beside"] */

(foo) => bar;

(foo) => (bar);

(foo) => bar => baz;

(foo) => (
  bar()
);

// functions with block bodies allowed with this rule using any style
// to enforce a consistent location for this case, see the rule: `brace-style`
(foo) => {
  return bar();
}

(foo) =>
{
  return bar();
}

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

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

在线运行
/* eslint implicit-arrow-linebreak: ["error", "below"] */

(foo) => bar;

(foo) => (bar);

(foo) => bar => baz;

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

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

在线运行
/* eslint implicit-arrow-linebreak: ["error", "below"] */

(foo) =>
  bar;

(foo) =>
  (bar);

(foo) =>
  bar =>
    baz;

何时不使用

¥When Not To Use It

如果你不关心隐式返回的箭头函数表达式的位置一致,则不应打开此规则。

¥If you’re not concerned about consistent locations of implicitly returned arrow function expressions, you should not turn on this rule.

如果你对 arrow-body-style 使用 "always" 选项,你也可以禁用此规则,因为这将禁用在箭头函数中使用隐式返回。

¥You can also disable this rule if you are using the "always" option for the arrow-body-style, since this will disable the use of implicit returns in arrow functions.

版本

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

资源

ESLint 中文网
粤ICP备13048890号