Skip to content

Commit

Permalink
New Rule: no-lone-blocks (fixes eslint#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
btmills committed Feb 1, 2014
1 parent 6fdd34d commit 7b21874
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"no-extend-native": 2,
"no-yoda": 2,
"no-path-concat": 0,
"no-lone-blocks": 1,

"brace-style": 0,
"block-scoped-var": 0,
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ These are rules designed to prevent you from making mistakes. They either prescr
* [no-floating-decimal](no-floating-decimal.md) - disallow the use of leading or trailing decimal points in numeric literals
* [no-implied-eval] - disallow use of `eval()`-like methods
* [no-iterator](no-iterator.md) - disallow usage of `__iterator__` property
* [no-lone-blocks](no-lone-blocks.md) - disallow unnecessary nested blocks
* [no-loop-func](no-loop-func.md) - disallow creation of functions within loops
* [no-multi-str](no-multi-str.md) - disallow use of multiline strings
* [no-native-reassign](no-native-reassign.md) - disallow reassignments of native objects
Expand Down
50 changes: 50 additions & 0 deletions docs/rules/no-lone-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Disallow Unnecessary Nested Blocks

In JavaScript, standalone code blocks delimited by curly braces do not create a new scope and have have no use. For example, these curly braces do nothing to `foo`:

```js
{
var foo = bar();
}
```

## Rule details

This rule aims to eliminate unnecessary and potentially confusing blocks at the top level of a script or within other blocks.

The following patterns are considered warnings:

```js
{}

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

function bar() {
{
baz();
}
}
```

The following patterns are not considered warnings:

```js
while (foo) {
bar();
}

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

functin bar() {
baz();
}
```
29 changes: 29 additions & 0 deletions lib/rules/no-lone-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @fileoverview Rule to flag blocks with no reason to exist
* @author Brandon Mills
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

"use strict";

function checkForLoners(node) {
// Check for any occurrence of BlockStatement > BlockStatement or
// Program > BlockStatement
node.body.forEach(function (stmt) {
if (stmt.type === "BlockStatement") {
context.report(stmt, "Block is nested inside another block.");
}
});
}

return {
"BlockStatement": checkForLoners,
"Program": checkForLoners
};

};
30 changes: 30 additions & 0 deletions tests/lib/rules/no-lone-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @fileoverview Tests for no-lone-blocks rule.
* @author Brandon Mills
*/

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslintTester = require("eslint-tester");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

eslintTester.addRuleTest("lib/rules/no-lone-blocks", {
valid: [
"if (foo) { if (bar) { baz(); } }",
"do { bar(); } while (foo)",
"function foo() { while (bar) { baz() } }"
],
invalid: [
{ code: "{}", errors: [{ message: "Block is nested inside another block.", type: "BlockStatement"}] },
{ code: "foo(); {} bar();", errors: [{ message: "Block is nested inside another block.", type: "BlockStatement"}] },
{ code: "if (foo) { bar(); {} baz(); }", errors: [{ message: "Block is nested inside another block.", type: "BlockStatement"}] },
{ code: "{ { } }", errors: [{ message: "Block is nested inside another block.", type: "BlockStatement"}, { message: "Block is nested inside another block.", type: "BlockStatement"}] },
{ code: "function foo() { bar(); {} baz(); }", errors: [{ message: "Block is nested inside another block.", type: "BlockStatement"}] },
{ code: "while (foo) { {} }", errors: [{ message: "Block is nested inside another block.", type: "BlockStatement"}] }
]
});

0 comments on commit 7b21874

Please sign in to comment.