UNPKG

1.19 kBJavaScriptView 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 = {
13 meta: {
14 docs: {
15 description: "require `for-in` loops to include an `if` statement",
16 category: "Best Practices",
17 recommended: false
18 },
19
20 schema: []
21 },
22
23 create(context) {
24
25 return {
26
27 ForInStatement(node) {
28
29 /*
30 * If the for-in statement has {}, then the real body is the body
31 * of the BlockStatement. Otherwise, just use body as provided.
32 */
33 const body = node.body.type === "BlockStatement" ? node.body.body[0] : node.body;
34
35 if (body && body.type !== "IfStatement") {
36 context.report(node, "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.");
37 }
38 }
39 };
40
41 }
42};