source: imaps-frontend/node_modules/eslint/lib/rules/no-object-constructor.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @fileoverview Rule to disallow calls to the `Object` constructor without an argument
3 * @author Francesco Trotta
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const {
13 getVariableByName,
14 isArrowToken,
15 isStartOfExpressionStatement,
16 needsPrecedingSemicolon
17} = require("./utils/ast-utils");
18
19//------------------------------------------------------------------------------
20// Rule Definition
21//------------------------------------------------------------------------------
22
23/** @type {import('../shared/types').Rule} */
24module.exports = {
25 meta: {
26 type: "suggestion",
27
28 docs: {
29 description: "Disallow calls to the `Object` constructor without an argument",
30 recommended: false,
31 url: "https://eslint.org/docs/latest/rules/no-object-constructor"
32 },
33
34 hasSuggestions: true,
35
36 schema: [],
37
38 messages: {
39 preferLiteral: "The object literal notation {} is preferable.",
40 useLiteral: "Replace with '{{replacement}}'.",
41 useLiteralAfterSemicolon: "Replace with '{{replacement}}', add preceding semicolon."
42 }
43 },
44
45 create(context) {
46
47 const sourceCode = context.sourceCode;
48
49 /**
50 * Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses.
51 * @param {ASTNode} node The node to be replaced.
52 * @returns {boolean} Whether or not parentheses around the object literal are required.
53 */
54 function needsParentheses(node) {
55 if (isStartOfExpressionStatement(node)) {
56 return true;
57 }
58
59 const prevToken = sourceCode.getTokenBefore(node);
60
61 if (prevToken && isArrowToken(prevToken)) {
62 return true;
63 }
64
65 return false;
66 }
67
68 /**
69 * Reports on nodes where the `Object` constructor is called without arguments.
70 * @param {ASTNode} node The node to evaluate.
71 * @returns {void}
72 */
73 function check(node) {
74 if (node.callee.type !== "Identifier" || node.callee.name !== "Object" || node.arguments.length) {
75 return;
76 }
77
78 const variable = getVariableByName(sourceCode.getScope(node), "Object");
79
80 if (variable && variable.identifiers.length === 0) {
81 let replacement;
82 let fixText;
83 let messageId = "useLiteral";
84
85 if (needsParentheses(node)) {
86 replacement = "({})";
87 if (needsPrecedingSemicolon(sourceCode, node)) {
88 fixText = ";({})";
89 messageId = "useLiteralAfterSemicolon";
90 } else {
91 fixText = "({})";
92 }
93 } else {
94 replacement = fixText = "{}";
95 }
96
97 context.report({
98 node,
99 messageId: "preferLiteral",
100 suggest: [
101 {
102 messageId,
103 data: { replacement },
104 fix: fixer => fixer.replaceText(node, fixText)
105 }
106 ]
107 });
108 }
109 }
110
111 return {
112 CallExpression: check,
113 NewExpression: check
114 };
115
116 }
117};
Note: See TracBrowser for help on using the repository browser.