Welcome to mirror list, hosted at ThFree Co, Russian Federation.

no-loop-func.js « rules « lib « eslint « tools - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec68611b72eb90f16f1315a076fe59a880adafca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * @fileoverview Rule to flag creation of function inside a loop
 * @author Ilya Volodin
 * @copyright 2013 Ilya Volodin. All rights reserved.
 */

"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
    var loopNodeTypes = [
        "ForStatement",
        "WhileStatement",
        "ForInStatement",
        "ForOfStatement",
        "DoWhileStatement"
    ];

    /**
     * Checks if the given node is a loop.
     * @param {ASTNode} node The AST node to check.
     * @returns {boolean} Whether or not the node is a loop.
     */
    function isLoop(node) {
        return loopNodeTypes.indexOf(node.type) > -1;
    }

    /**
     * Reports if the given node has an ancestor node which is a loop.
     * @param {ASTNode} node The AST node to check.
     * @returns {boolean} Whether or not the node is within a loop.
     */
    function checkForLoops(node) {
        var ancestors = context.getAncestors();

        if (ancestors.some(isLoop)) {
            context.report(node, "Don't make functions within a loop");
        }
    }

    return {
        "ArrowFunctionExpression": checkForLoops,
        "FunctionExpression": checkForLoops,
        "FunctionDeclaration": checkForLoops
    };
};