Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: allow continue instead of if wrap in guard-for-in (fixes #7567) #9796

Merged
merged 1 commit into from Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 35 additions & 7 deletions lib/rules/guard-for-in.js
Expand Up @@ -25,16 +25,44 @@ module.exports = {
return {

ForInStatement(node) {
const body = node.body;

/*
* If the for-in statement has {}, then the real body is the body
* of the BlockStatement. Otherwise, just use body as provided.
*/
const body = node.body.type === "BlockStatement" ? node.body.body[0] : node.body;
// empty statement
if (body.type === "EmptyStatement") {
return;
}

// if statement
if (body.type === "IfStatement") {
return;
}

// empty block
if (body.type === "BlockStatement" && body.body.length === 0) {
return;
}

if (body && body.type !== "IfStatement") {
context.report({ node, message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype." });
// block with just if statement
if (body.type === "BlockStatement" && body.body.length === 1 && body.body[0].type === "IfStatement") {
return;
}

// block that starts with if statement
if (body.type === "BlockStatement" && body.body.length >= 1 && body.body[0].type === "IfStatement") {
const i = body.body[0];

// ... whose consequent is a continue
if (i.consequent.type === "ContinueStatement") {
return;
}

// ... whose consequent is a block that contains only a continue
if (i.consequent.type === "BlockStatement" && i.consequent.body.length === 1 && i.consequent.body[0].type === "ContinueStatement") {
return;
}
}

context.report({ node, message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype." });
}
};

Expand Down
10 changes: 9 additions & 1 deletion tests/lib/rules/guard-for-in.js
Expand Up @@ -20,10 +20,18 @@ const ruleTester = new RuleTester();

ruleTester.run("guard-for-in", rule, {
valid: [
"for (var x in o);",
"for (var x in o) {}",
"for (var x in o) { if (x) {}}"
"for (var x in o) if (x) f();",
"for (var x in o) { if (x) { f(); } }",
"for (var x in o) { if (x) continue; f(); }",
"for (var x in o) { if (x) { continue; } f(); }"
],
invalid: [
{ code: "for (var x in o) { if (x) { f(); continue; } g(); }", errors: [{ message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", type: "ForInStatement" }] },
{ code: "for (var x in o) { if (x) { continue; f(); } g(); }", errors: [{ message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", type: "ForInStatement" }] },
{ code: "for (var x in o) { if (x) { f(); } g(); }", errors: [{ message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", type: "ForInStatement" }] },
{ code: "for (var x in o) { if (x) f(); g(); }", errors: [{ message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", type: "ForInStatement" }] },
{ code: "for (var x in o) { foo() }", errors: [{ message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", type: "ForInStatement" }] },
{ code: "for (var x in o) foo();", errors: [{ message: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", type: "ForInStatement" }] }
]
Expand Down