summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-console.js
blob: 0daff2bb0752977c7da5746f3d33ee9281d23687 (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
64
65
/**
 * @fileoverview Rule to flag use of console object
 * @author Nicholas C. Zakas
 */

"use strict";

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

module.exports = {
    meta: {
        docs: {
            description: "disallow the use of `console`",
            category: "Possible Errors",
            recommended: true
        },

        schema: [
            {
                type: "object",
                properties: {
                    allow: {
                        type: "array",
                        items: {
                            type: "string"
                        },
                        minItems: 1,
                        uniqueItems: true
                    }
                },
                additionalProperties: false
            }
        ]
    },

    create: function(context) {

        return {

            MemberExpression: function(node) {

                if (node.object.name === "console") {
                    let blockConsole = true;

                    if (context.options.length > 0) {
                        const allowedProperties = context.options[0].allow;
                        const passedProperty = node.property.name;
                        const propertyIsAllowed = (allowedProperties.indexOf(passedProperty) > -1);

                        if (propertyIsAllowed) {
                            blockConsole = false;
                        }
                    }

                    if (blockConsole) {
                        context.report(node, "Unexpected console statement.");
                    }
                }
            }
        };

    }
};