Eslint参考手册
规则 | Rules
no-lone-blocks
在 ES6 之前的 JavaScript 中,由花括号分隔的独立代码块不会创建新的作用域,也没有用处。例如,这些大括号无效foo
:
{
var foo = bar();
}
在 ES6 中,如果块级绑定(let
和const
),类声明或函数声明(以严格模式)存在,则代码块可能会创建新范围。在这些情况下块不被认为是多余的。
规则细节
此规则旨在消除脚本顶层或其他块中不必要的和可能混淆的块。
此规则的错误代码示例:
/*eslint no-lone-blocks: "error"*/
{}
if (foo) {
bar();
{
baz();
}
}
function bar() {
{
baz();
}
}
{
function foo() {}
}
{
aLabel: {
}
}
使用 es6 环境的此规则的正确代码示例:
/*eslint no-lone-blocks: "error"*/
/*eslint-env es6*/
while (foo) {
bar();
}
if (foo) {
if (bar) {
baz();
}
}
function bar() {
baz();
}
{
let x = 1;
}
{
const y = 1;
}
{
class Foo {}
}
aLabel: {
}
通过 ESLint 配置或代码中的指令使用es6环境和严格模式通过此规则的正确代码示例:"parserOptions": { "sourceType": "module" }"use strict"
/*eslint no-lone-blocks: "error"*/
/*eslint-env es6*/
"use strict";
{
function foo() {}
}
版本
这条规则是在 ESLint 0.4.0 中引入的。
资源
- Rule source
- Documentation source
规则 | Rules相关

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。
主页 | https://eslint.org/ |
源码 | https://github.com/eslint/eslint |
发布版本 | 4.12.0 |