1 | /**
|
2 | * @fileoverview A rule to disallow calls to the Object constructor
|
3 | * @author Matt DuVall <http://www.mattduvall.com/>
|
4 | */
|
5 |
|
6 | ;
|
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 | category: "Stylistic Issues",
|
25 | recommended: false,
|
26 | url: "https://eslint.org/docs/rules/no-new-object"
|
27 | },
|
28 |
|
29 | schema: [],
|
30 |
|
31 | messages: {
|
32 | preferLiteral: "The object literal notation {} is preferrable."
|
33 | }
|
34 | },
|
35 |
|
36 | create(context) {
|
37 | return {
|
38 | NewExpression(node) {
|
39 | const variable = astUtils.getVariableByName(
|
40 | context.getScope(),
|
41 | node.callee.name
|
42 | );
|
43 |
|
44 | if (variable && variable.identifiers.length > 0) {
|
45 | return;
|
46 | }
|
47 |
|
48 | if (node.callee.name === "Object") {
|
49 | context.report({
|
50 | node,
|
51 | messageId: "preferLiteral"
|
52 | });
|
53 | }
|
54 | }
|
55 | };
|
56 | }
|
57 | };
|