|
| 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 | + |
| 12 | +const { getVariableByName, isArrowToken } = require("./utils/ast-utils"); |
| 13 | + |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | +// Helpers |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests if a node appears at the beginning of an ancestor ExpressionStatement node. |
| 20 | + * @param {ASTNode} node The node to check. |
| 21 | + * @returns {boolean} Whether the node appears at the beginning of an ancestor ExpressionStatement node. |
| 22 | + */ |
| 23 | +function isStartOfExpressionStatement(node) { |
| 24 | + const start = node.range[0]; |
| 25 | + let ancestor = node; |
| 26 | + |
| 27 | + while ((ancestor = ancestor.parent) && ancestor.range[0] === start) { |
| 28 | + if (ancestor.type === "ExpressionStatement") { |
| 29 | + return true; |
| 30 | + } |
| 31 | + } |
| 32 | + return false; |
| 33 | +} |
| 34 | + |
| 35 | +//------------------------------------------------------------------------------ |
| 36 | +// Rule Definition |
| 37 | +//------------------------------------------------------------------------------ |
| 38 | + |
| 39 | +/** @type {import('../shared/types').Rule} */ |
| 40 | +module.exports = { |
| 41 | + meta: { |
| 42 | + type: "suggestion", |
| 43 | + |
| 44 | + docs: { |
| 45 | + description: "Disallow calls to the `Object` constructor without an argument", |
| 46 | + recommended: false, |
| 47 | + url: "https://eslint.org/docs/latest/rules/no-object-constructor" |
| 48 | + }, |
| 49 | + |
| 50 | + hasSuggestions: true, |
| 51 | + |
| 52 | + schema: [], |
| 53 | + |
| 54 | + messages: { |
| 55 | + preferLiteral: "The object literal notation {} is preferable.", |
| 56 | + useLiteral: "Replace with '{{replacement}}'." |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + create(context) { |
| 61 | + |
| 62 | + const sourceCode = context.sourceCode; |
| 63 | + |
| 64 | + /** |
| 65 | + * Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses. |
| 66 | + * @param {ASTNode} node The node to be replaced. |
| 67 | + * @returns {boolean} Whether or not parentheses around the object literal are required. |
| 68 | + */ |
| 69 | + function needsParentheses(node) { |
| 70 | + if (isStartOfExpressionStatement(node)) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + const prevToken = sourceCode.getTokenBefore(node); |
| 75 | + |
| 76 | + if (prevToken && isArrowToken(prevToken)) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Reports on nodes where the `Object` constructor is called without arguments. |
| 85 | + * @param {ASTNode} node The node to evaluate. |
| 86 | + * @returns {void} |
| 87 | + */ |
| 88 | + function check(node) { |
| 89 | + if (node.callee.type !== "Identifier" || node.callee.name !== "Object" || node.arguments.length) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const variable = getVariableByName(sourceCode.getScope(node), "Object"); |
| 94 | + |
| 95 | + if (variable && variable.identifiers.length === 0) { |
| 96 | + const replacement = needsParentheses(node) ? "({})" : "{}"; |
| 97 | + |
| 98 | + context.report({ |
| 99 | + node, |
| 100 | + messageId: "preferLiteral", |
| 101 | + suggest: [ |
| 102 | + { |
| 103 | + messageId: "useLiteral", |
| 104 | + data: { replacement }, |
| 105 | + fix: fixer => fixer.replaceText(node, replacement) |
| 106 | + } |
| 107 | + ] |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return { |
| 113 | + CallExpression: check, |
| 114 | + NewExpression: check |
| 115 | + }; |
| 116 | + |
| 117 | + } |
| 118 | +}; |
0 commit comments