nonblock-statement-body-position
强制执行单行语句的位置
            此规则报告的一些问题可通过 --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.
在编写 if、else、while、do-while 和 for 语句时,主体可以是单个语句而不是块。为这些单个语句强制执行一致的位置可能很有用。
¥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"(默认)不允许在单行语句之前出现换行符。¥
"beside"(default) disallows a newline before a single-line statement. - 
"below"在单行语句之前需要换行符。¥
"below"requires a newline before a single-line statement. - 
"any"不强制单行语句的位置。¥
"any"does not enforce the position of a single-line statement. 
此外,该规则接受带有 "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语句,在这种情况下,单行语句不得位于同一行。¥
"beside", { "overrides": { "while": "below" } }requires all single-line statements to appear on the same line as their parent, unless the parent is awhilestatement, in which case the single-line statement must not be on the same line. - 
"below", { "overrides": { "do": "any" } }不允许所有单行语句与其父语句出现在同一行,除非父语句是do-while语句,在这种情况下,不强制执行单行语句的位置。¥
"below", { "overrides": { "do": "any" } }disallows all single-line statements from appearing on the same line as their parent, unless the parent is ado-whilestatement, in which case the position of the single-line statement is not enforced. 
使用默认 "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 中引入。