]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | module.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; | |
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 | }; |