summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-shadow-restricted-names.js
blob: 54dc1e58733c869b97f642d4492ae4adb44e6b9e (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1)
 * @author Michael Ficarra
 */
"use strict";

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

module.exports = {
    meta: {
        docs: {
            description: "disallow identifiers from shadowing restricted names",
            category: "Variables",
            recommended: false
        },

        schema: []
    },

    create: function(context) {

        const RESTRICTED = ["undefined", "NaN", "Infinity", "arguments", "eval"];

        /**
         * Check if the node name is present inside the restricted list
         * @param {ASTNode} id id to evaluate
         * @returns {void}
         * @private
         */
        function checkForViolation(id) {
            if (RESTRICTED.indexOf(id.name) > -1) {
                context.report(id, "Shadowing of global property '" + id.name + "'.");
            }
        }

        return {
            VariableDeclarator: function(node) {
                checkForViolation(node.id);
            },
            ArrowFunctionExpression: function(node) {
                [].map.call(node.params, checkForViolation);
            },
            FunctionExpression: function(node) {
                if (node.id) {
                    checkForViolation(node.id);
                }
                [].map.call(node.params, checkForViolation);
            },
            FunctionDeclaration: function(node) {
                if (node.id) {
                    checkForViolation(node.id);
                    [].map.call(node.params, checkForViolation);
                }
            },
            CatchClause: function(node) {
                checkForViolation(node.param);
            }
        };

    }
};