]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-new-object.js
import 8.3.0 source
[pve-eslint.git] / eslint / lib / rules / no-new-object.js
1 /**
2 * @fileoverview A rule to disallow calls to the Object constructor
3 * @author Matt DuVall <http://www.mattduvall.com/>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 module.exports = {
19 meta: {
20 type: "suggestion",
21
22 docs: {
23 description: "disallow `Object` constructors",
24 recommended: false,
25 url: "https://eslint.org/docs/rules/no-new-object"
26 },
27
28 schema: [],
29
30 messages: {
31 preferLiteral: "The object literal notation {} is preferrable."
32 }
33 },
34
35 create(context) {
36 return {
37 NewExpression(node) {
38 const variable = astUtils.getVariableByName(
39 context.getScope(),
40 node.callee.name
41 );
42
43 if (variable && variable.identifiers.length > 0) {
44 return;
45 }
46
47 if (node.callee.name === "Object") {
48 context.report({
49 node,
50 messageId: "preferLiteral"
51 });
52 }
53 }
54 };
55 }
56 };