main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
2.1 KB
|
Line | |
---|
1 | /**
|
---|
2 | * @fileoverview Rule to disallow use of the new operator with global non-constructor functions
|
---|
3 | * @author Sosuke Suzuki
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 | // Helpers
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | const nonConstructorGlobalFunctionNames = ["Symbol", "BigInt"];
|
---|
13 |
|
---|
14 | //------------------------------------------------------------------------------
|
---|
15 | // Rule Definition
|
---|
16 | //------------------------------------------------------------------------------
|
---|
17 |
|
---|
18 | /** @type {import('../shared/types').Rule} */
|
---|
19 | module.exports = {
|
---|
20 | meta: {
|
---|
21 | type: "problem",
|
---|
22 |
|
---|
23 | docs: {
|
---|
24 | description: "Disallow `new` operators with global non-constructor functions",
|
---|
25 | recommended: false,
|
---|
26 | url: "https://eslint.org/docs/latest/rules/no-new-native-nonconstructor"
|
---|
27 | },
|
---|
28 |
|
---|
29 | schema: [],
|
---|
30 |
|
---|
31 | messages: {
|
---|
32 | noNewNonconstructor: "`{{name}}` cannot be called as a constructor."
|
---|
33 | }
|
---|
34 | },
|
---|
35 |
|
---|
36 | create(context) {
|
---|
37 |
|
---|
38 | const sourceCode = context.sourceCode;
|
---|
39 |
|
---|
40 | return {
|
---|
41 | "Program:exit"(node) {
|
---|
42 | const globalScope = sourceCode.getScope(node);
|
---|
43 |
|
---|
44 | for (const nonConstructorName of nonConstructorGlobalFunctionNames) {
|
---|
45 | const variable = globalScope.set.get(nonConstructorName);
|
---|
46 |
|
---|
47 | if (variable && variable.defs.length === 0) {
|
---|
48 | variable.references.forEach(ref => {
|
---|
49 | const idNode = ref.identifier;
|
---|
50 | const parent = idNode.parent;
|
---|
51 |
|
---|
52 | if (parent && parent.type === "NewExpression" && parent.callee === idNode) {
|
---|
53 | context.report({
|
---|
54 | node: idNode,
|
---|
55 | messageId: "noNewNonconstructor",
|
---|
56 | data: { name: nonConstructorName }
|
---|
57 | });
|
---|
58 | }
|
---|
59 | });
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | };
|
---|
64 |
|
---|
65 | }
|
---|
66 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.