curly

对所有控制语句强制执行一致的大括号样式

🔧 Fixable

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

当一个块只包含一个语句时,JavaScript 允许省略大括号。然而,许多人认为最好的做法是永远不要在块周围省略大括号,即使它们是可选的,因为它可能导致错误并降低代码清晰度。所以以下内容:

¥JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following:

if (foo) foo++;

可以改写为:

¥Can be rewritten as:

if (foo) {
    foo++;
}

然而,有些人更喜欢只在有多个语句要执行时才使用大括号。

¥There are, however, some who prefer to only use braces when there is more than one statement to be executed.

规则详情

¥Rule Details

此规则旨在通过确保将块语句封装在大括号中来防止错误并提高代码清晰度。它会在遇到省略大括号的块时触发警告。

¥This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces.

选项

¥Options

all

默认 "all" 选项的错误代码示例:

¥Examples of incorrect code for the default "all" option:

在线运行
/*eslint curly: "error"*/

if (foo) foo++;

while (bar)
    baz();

if (foo) {
    baz();
} else qux();

默认 "all" 选项的正确代码示例:

¥Examples of correct code for the default "all" option:

在线运行
/*eslint curly: "error"*/

if (foo) {
    foo++;
}

while (bar) {
    baz();
}

if (foo) {
    baz();
} else {
    qux();
}

multi

默认情况下,只要 ifelseforwhiledo 不使用块语句作为主体,此规则就会触发警告。但是,你可以指定仅当块中有多个语句时才应使用块语句,并在块中只有一个语句时触发警告。

¥By default, this rule warns whenever if, else, for, while, or do are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block.

"multi" 选项的错误代码示例:

¥Examples of incorrect code for the "multi" option:

在线运行
/*eslint curly: ["error", "multi"]*/

if (foo) {
    foo++;
}

if (foo) bar();
else {
    foo++;
}

while (true) {
    doSomething();
}

for (var i=0; i < items.length; i++) {
    doSomething();
}

"multi" 选项的正确代码示例:

¥Examples of correct code for the "multi" option:

在线运行
/*eslint curly: ["error", "multi"]*/

if (foo) foo++;

else foo();

while (true) {
    doSomething();
    doSomethingElse();
}

multi-line

或者,你可以放宽规则以允许无括号单行 ifelse ifelseforwhiledo,同时仍强制对其他实例使用大括号。

¥Alternatively, you can relax the rule to allow brace-less single-line if, else if, else, for, while, or do, while still enforcing the use of curly braces for other instances.

"multi-line" 选项的错误代码示例:

¥Examples of incorrect code for the "multi-line" option:

在线运行
/*eslint curly: ["error", "multi-line"]*/

if (foo)
  doSomething();
else
  doSomethingElse();

if (foo) foo(
  bar,
  baz);

"multi-line" 选项的正确代码示例:

¥Examples of correct code for the "multi-line" option:

在线运行
/*eslint curly: ["error", "multi-line"]*/

if (foo) foo++; else doSomething();

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

do something();
while (foo);

while (foo
  && bar) baz();

if (foo) {
    foo++;
}

if (foo) { foo++; }

while (true) {
    doSomething();
    doSomethingElse();
}

multi-or-nest

如果它们的主体仅包含一个单行语句,你可以使用另一种强制无括号 ifelse ifelseforwhiledo 的配置。并在所有其他情况下强制使用大括号。

¥You can use another configuration that forces brace-less if, else if, else, for, while, or do if their body contains only one single-line statement. And forces braces in all other cases.

"multi-or-nest" 选项的错误代码示例:

¥Examples of incorrect code for the "multi-or-nest" option:

在线运行
/*eslint curly: ["error", "multi-or-nest"]*/

if (!foo)
    foo = {
        bar: baz,
        qux: foo
    };

while (true)
  if(foo)
      doSomething();
  else
      doSomethingElse();

if (foo) {
    foo++;
}

while (true) {
    doSomething();
}

for (var i = 0; foo; i++) {
    doSomething();
}

"multi-or-nest" 选项的正确代码示例:

¥Examples of correct code for the "multi-or-nest" option:

在线运行
/*eslint curly: ["error", "multi-or-nest"]*/

if (!foo) {
    foo = {
        bar: baz,
        qux: foo
    };
}

while (true) {
  if(foo)
      doSomething();
  else
      doSomethingElse();
}

if (foo)
    foo++;

while (true)
    doSomething();

for (var i = 0; foo; i++)
    doSomething();

对于前面有注释的单行语句,大括号是允许的,但不是必需的。

¥For single-line statements preceded by a comment, braces are allowed but not required.

"multi-or-nest" 选项的附加正确代码示例:

¥Examples of additional correct code for the "multi-or-nest" option:

在线运行
/*eslint curly: ["error", "multi-or-nest"]*/

if (foo)
    // some comment
    bar();

if (foo) {
    // some comment
    bar();
}

consistent

使用任何 multi* 选项时,你可以添加一个选项以强制 ifelse ifelse 链的所有主体都带有或不带有大括号。

¥When using any of the multi* options, you can add an option to enforce all bodies of a if, else if and else chain to be with or without braces.

"multi", "consistent" 选项的错误代码示例:

¥Examples of incorrect code for the "multi", "consistent" options:

在线运行
/*eslint curly: ["error", "multi", "consistent"]*/

if (foo) {
    bar();
    baz();
} else
    buz();

if (foo)
    bar();
else if (faa)
    bor();
else {
    other();
    things();
}

if (true)
    foo();
else {
    baz();
}

if (foo) {
    foo++;
}

"multi", "consistent" 选项的正确代码示例:

¥Examples of correct code for the "multi", "consistent" options:

在线运行
/*eslint curly: ["error", "multi", "consistent"]*/

if (foo) {
    bar();
    baz();
} else {
    buz();
}

if (foo) {
    bar();
} else if (faa) {
    bor();
} else {
    other();
    things();
}

if (true)
    foo();
else
    baz();

if (foo)
    foo++;

何时不使用

¥When Not To Use It

如果你没有关于何时使用块语句以及何时不使用的严格约定,你可以安全地禁用此规则。

¥If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule.

版本

此规则是在 ESLint v0.0.2 中引入。

资源

ESLint 中文网
粤ICP备13048890号