From 25f14ae9b4c862a81f336355a1029e02cb7f8fe0 Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Sun, 19 Jul 2015 17:39:05 +0200 Subject: [PATCH] Fix: allow empty loop body in no-extra-semi (fixes #3075) --- lib/rules/no-extra-semi.js | 11 +++++++++-- tests/lib/rules/no-extra-semi.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-extra-semi.js b/lib/rules/no-extra-semi.js index a3897ef22750..994fe2c2f0e8 100644 --- a/lib/rules/no-extra-semi.js +++ b/lib/rules/no-extra-semi.js @@ -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. diff --git a/tests/lib/rules/no-extra-semi.js b/tests/lib/rules/no-extra-semi.js index 0342103a42dd..278b411f6933 100644 --- a/tests/lib/rules/no-extra-semi.js +++ b/tests/lib/rules/no-extra-semi.js @@ -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}}, @@ -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. {