Skip to content

Commit

Permalink
Fix: allow empty loop body in no-extra-semi (fixes #3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Jul 19, 2015
1 parent 270ede8 commit 25f14ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rules/no-extra-semi.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ module.exports = function(context) {

return {
/**
* Reports this empty statement.
* Reports this empty statement, except if the parent node is a loop.
* @param {Node} node - A EmptyStatement node to be reported.
* @returns {void}
*/
"EmptyStatement": report,
"EmptyStatement": function(node) {
var parent = node.parent,
allowedParentTypes = ["ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement"];

if (allowedParentTypes.indexOf(parent.type) === -1) {
report(node);
}
},

/**
* Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-extra-semi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ eslintTester.addRuleTest("lib/rules/no-extra-semi", {
valid: [
"var x = 5;",
"function foo(){}",
"for(;;);",
"while(0);",
"do;while(0);",
"for(a in b);",
{ code: "for(a of b);", ecmaFeatures: { forOf: true } },

// Class body.
{code: "class A { }", ecmaFeatures: {classes: true}},
Expand All @@ -32,6 +37,11 @@ eslintTester.addRuleTest("lib/rules/no-extra-semi", {
invalid: [
{ code: "var x = 5;;", errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement"}] },
{ code: "function foo(){};", errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement"}] },
{ code: "for(;;);;", errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement" }] },
{ code: "while(0);;", errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement" }] },
{ code: "do;while(0);;", errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement" }] },
{ code: "for(a in b);;", errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement" }] },
{ code: "for(a of b);;", ecmaFeatures: { forOf: true }, errors: [{ message: "Unnecessary semicolon.", type: "EmptyStatement" }] },

// Class body.
{
Expand Down

0 comments on commit 25f14ae

Please sign in to comment.