UNPKG

816 BJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag creation of function inside a loop
3 * @author Ilya Volodin
4 */
5
6//------------------------------------------------------------------------------
7// Rule Definition
8//------------------------------------------------------------------------------
9
10module.exports = function(context) {
11
12 "use strict";
13
14 function checkForLoops(node) {
15 var ancestors = context.getAncestors();
16
17 if (ancestors.some(function(ancestor) {
18 return ancestor.type === "ForStatement" || ancestor.type === "WhileStatement" || ancestor.type === "DoWhileStatement";
19 })) {
20 context.report(node, "Don't make functions within a loop");
21 }
22 }
23
24 return {
25 "FunctionExpression": checkForLoops,
26 "FunctionDeclaration": checkForLoops
27 };
28};