]> git.proxmox.com Git - pve-eslint.git/blame - eslint/lib/rules/no-new-symbol.js
import 8.4.0 source
[pve-eslint.git] / eslint / lib / rules / no-new-symbol.js
CommitLineData
eb39fafa
DC
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
34eeec05 12/** @type {import('../shared/types').Rule} */
eb39fafa
DC
13module.exports = {
14 meta: {
15 type: "problem",
16
17 docs: {
18 description: "disallow `new` operators with the `Symbol` object",
eb39fafa
DC
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;
ebb53d86 40 const parent = node.parent;
eb39fafa 41
ebb53d86 42 if (parent && parent.type === "NewExpression" && parent.callee === node) {
eb39fafa
DC
43 context.report({
44 node,
45 messageId: "noNewSymbol"
46 });
47 }
48 });
49 }
50 }
51 };
52
53 }
54};