]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-new-symbol.js
import 8.3.0 source
[pve-eslint.git] / eslint / lib / rules / no-new-symbol.js
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
12 module.exports = {
13 meta: {
14 type: "problem",
15
16 docs: {
17 description: "disallow `new` operators with the `Symbol` object",
18 recommended: true,
19 url: "https://eslint.org/docs/rules/no-new-symbol"
20 },
21
22 schema: [],
23
24 messages: {
25 noNewSymbol: "`Symbol` cannot be called as a constructor."
26 }
27 },
28
29 create(context) {
30
31 return {
32 "Program:exit"() {
33 const globalScope = context.getScope();
34 const variable = globalScope.set.get("Symbol");
35
36 if (variable && variable.defs.length === 0) {
37 variable.references.forEach(ref => {
38 const node = ref.identifier;
39 const parent = node.parent;
40
41 if (parent && parent.type === "NewExpression" && parent.callee === node) {
42 context.report({
43 node,
44 messageId: "noNewSymbol"
45 });
46 }
47 });
48 }
49 }
50 };
51
52 }
53 };