UNPKG

1.28 kBJavaScriptView Raw
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 docs: {
15 description: "disallow `new` operators with the `Symbol` object",
16 category: "ECMAScript 6",
17 recommended: true,
18 url: "https://eslint.org/docs/rules/no-new-symbol"
19 },
20
21 schema: []
22 },
23
24 create(context) {
25
26 return {
27 "Program:exit"() {
28 const globalScope = context.getScope();
29 const variable = globalScope.set.get("Symbol");
30
31 if (variable && variable.defs.length === 0) {
32 variable.references.forEach(ref => {
33 const node = ref.identifier;
34
35 if (node.parent && node.parent.type === "NewExpression") {
36 context.report({ node, message: "`Symbol` cannot be called as a constructor." });
37 }
38 });
39 }
40 }
41 };
42
43 }
44};