UNPKG

949 BJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag for-in loops without if statements inside
3 * @author Nicholas C. Zakas
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = function(context) {
13
14 return {
15
16 "ForInStatement": function(node) {
17
18 /*
19 * If the for-in statement has {}, then the real body is the body
20 * of the BlockStatement. Otherwise, just use body as provided.
21 */
22 var body = node.body.type === "BlockStatement" ? node.body.body[0] : node.body;
23
24 if (body && body.type !== "IfStatement") {
25 context.report(node, "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.");
26 }
27 }
28 };
29
30};
31
32module.exports.schema = [];