forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule: no-lone-blocks (fixes eslint#512)
Removed extra down-traversal in favor of looking up one level at ancestors and changed config from warning to error to incorporate PR feedback.
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @fileoverview Rule to flag blocks with no reason to exist | ||
* @author Brandon Mills | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
"use strict"; | ||
|
||
return { | ||
"BlockStatement": function (node) { | ||
// Check for any occurrence of BlockStatement > BlockStatement or | ||
// Program > BlockStatement | ||
var parent = context.getAncestors().pop(); | ||
if (parent.type === "BlockStatement" || parent.type === "Program") { | ||
context.report(node, "Block is nested inside another block."); | ||
} | ||
} | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] } | ||
] | ||
}); |