Index

nonblock-statement-body-position

强制执行单行语句的位置

🔧 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

在编写 ifelsewhiledo-whilefor 语句时,主体可以是单个语句,而不是代码块。对于这些单个语句,强制其位置一致可能会很有用。

🌐 When writing if, else, while, do-while, and for statements, the body can be a single statement instead of a block. It can be useful to enforce a consistent location for these single statements.

例如,一些开发者避免编写这样的代码:

🌐 For example, some developers avoid writing code like this:

if (foo)
  bar();

如果另一个开发者试图将 baz(); 添加到 if 语句中,他们可能会错误地将代码更改为

🌐 If another developer attempts to add baz(); to the if statement, they might mistakenly change the code to

if (foo)
  bar();
  baz(); // this line is not in the `if` statement!

为避免此问题,可能需要所有单行 if 语句直接出现在条件语句之后,不要换行:

🌐 To avoid this issue, one might require all single-line if statements to appear directly after the conditional, without a linebreak:

if (foo) bar();

规则详情

🌐 Rule Details

此规则旨在为单行语句强制执行一致的位置。

🌐 This rule aims to enforce a consistent location for single-line statements.

请注意,此规则并不强制要求一般情况下使用单行语句。如果你想禁止使用单行语句,请改用 curly 规则。

🌐 Note that this rule does not enforce the usage of single-line statements in general. If you would like to disallow single-line statements, use the curly rule instead.

选项

🌐 Options

此规则接受字符串选项:

🌐 This rule accepts a string option:

  • "beside"(默认)不允许在单行语句前换行。
  • "below" 在单行语句前需要换行。
  • "any" 不强制单行语句的位置。

此外,该规则接受一个带有 "overrides" 键的可选对象选项。可用于指定覆盖默认设置的特定语句的位置。例如:

🌐 Additionally, the rule accepts an optional object option with an "overrides" key. This can be used to specify a location for particular statements that override the default. For example:

  • "beside", { "overrides": { "while": "below" } } 要求所有单行语句必须出现在其父语句的同一行,除非父语句是 while 语句,在这种情况下,单行语句不得与父语句在同一行。
  • "below", { "overrides": { "do": "any" } } 不允许所有单行语句出现在与其父语句同一行上,除非父语句是 do-while 语句,在这种情况下,不强制单行语句的位置。

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

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

在线运行
/* eslint nonblock-statement-body-position: ["error", "beside"] */

if (foo)
  bar();
else
  baz();

while (foo)
  bar();

for (let i = 1; i < foo; i++)
  bar();

do
  bar();
while (foo)

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

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

在线运行
/* eslint nonblock-statement-body-position: ["error", "beside"] */

if (foo) bar();
else baz();

while (foo) bar();

for (let i = 1; i < foo; i++) bar();

do bar(); while (foo)

if (foo) { // block statements are always allowed with this rule
  bar();
} else {
  baz();
}

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

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

在线运行
/* eslint nonblock-statement-body-position: ["error", "below"] */

if (foo) bar();
else baz();

while (foo) bar();

for (let i = 1; i < foo; i++) bar();

do bar(); while (foo)

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

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

在线运行
/* eslint nonblock-statement-body-position: ["error", "below"] */

if (foo)
  bar();
else
  baz();

while (foo)
  bar();

for (let i = 1; i < foo; i++)
  bar();

do
  bar();
while (foo)

if (foo) {
  // Although the second `if` statement is on the same line as the `else`, this is a very common
  // pattern, so it's not checked by this rule.
} else if (bar) {
}

使用 "beside", { "overrides": { "while": "below" } } 规则的此规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule with the "beside", { "overrides": { "while": "below" } } rule:

在线运行
/* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */

if (foo)
  bar();

while (foo) bar();

使用 "beside", { "overrides": { "while": "below" } } 规则的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "beside", { "overrides": { "while": "below" } } rule:

在线运行
/* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */

if (foo) bar();

while (foo)
  bar();

何时不使用

🌐 When Not To Use It

如果你不关心单行语句的位置是否一致,你不应该启用此规则。如果你在 curly 规则中使用 "all" 选项,也可以禁用此规则,因为这将完全禁止单行语句。

🌐 If you’re not concerned about consistent locations of single-line statements, you should not turn on this rule. You can also disable this rule if you’re using the "all" option for the curly rule, because this will disallow single-line statements entirely.

版本

此规则是在 ESLint v3.17.0 中引入。

进阶读物

资源