Index

implicit-arrow-linebreak

强制箭头函数体的位置

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

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.

Learn more

箭头函数体可以包含作为表达式的隐式返回,而不是块体。它可以用于强制隐式返回表达式的一致位置。

🌐 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"(默认)不允许在箭头函数体前有换行符。
  • "below" 在箭头函数体前需要换行。

使用默认 "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 中引入。

资源