• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Rule to disallow use of the new operator with the `Symbol` object
3 * @author Alberto Rodríguez
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = {
13    meta: {
14        type: "problem",
15
16        docs: {
17            description: "disallow `new` operators with the `Symbol` object",
18            category: "ECMAScript 6",
19            recommended: true,
20            url: "https://eslint.org/docs/rules/no-new-symbol"
21        },
22
23        schema: [],
24
25        messages: {
26            noNewSymbol: "`Symbol` cannot be called as a constructor."
27        }
28    },
29
30    create(context) {
31
32        return {
33            "Program:exit"() {
34                const globalScope = context.getScope();
35                const variable = globalScope.set.get("Symbol");
36
37                if (variable && variable.defs.length === 0) {
38                    variable.references.forEach(ref => {
39                        const node = ref.identifier;
40                        const parent = node.parent;
41
42                        if (parent && parent.type === "NewExpression" && parent.callee === node) {
43                            context.report({
44                                node,
45                                messageId: "noNewSymbol"
46                            });
47                        }
48                    });
49                }
50            }
51        };
52
53    }
54};
55