-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…7576) * Deprecate `no-new-object` * Add rule `no-object-constructor` * Apply suggestions * mark no-new-object deprecated in v8.50.0 Co-authored-by: Francesco Trotta <github@fasttime.org> --------- Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
- Loading branch information
1 parent
48a44a7
commit 22a5582
Showing
12 changed files
with
323 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
title: no-object-constructor | ||
rule_type: suggestion | ||
related_rules: | ||
- no-array-constructor | ||
- no-new-wrappers | ||
--- | ||
|
||
Use of the `Object` constructor to construct a new empty object is generally discouraged in favor of object literal notation because of conciseness and because the `Object` global may be redefined. | ||
The exception is when the `Object` constructor is used to intentionally wrap a specified value which is passed as an argument. | ||
|
||
## Rule Details | ||
|
||
This rule disallows calling the `Object` constructor without an argument. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
:::incorrect | ||
|
||
```js | ||
/*eslint no-object-constructor: "error"*/ | ||
|
||
Object(); | ||
|
||
new Object(); | ||
``` | ||
|
||
::: | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
:::correct | ||
|
||
```js | ||
/*eslint no-object-constructor: "error"*/ | ||
|
||
Object("foo"); | ||
|
||
const obj = { a: 1, b: 2 }; | ||
|
||
const isObject = value => value === Object(value); | ||
|
||
const createObject = Object => new Object(); | ||
``` | ||
|
||
::: | ||
|
||
## When Not To Use It | ||
|
||
If you wish to allow the use of the `Object` constructor, you can safely turn this rule off. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* @fileoverview Rule to disallow calls to the `Object` constructor without an argument | ||
* @author Francesco Trotta | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const { getVariableByName, isArrowToken } = require("./utils/ast-utils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Tests if a node appears at the beginning of an ancestor ExpressionStatement node. | ||
* @param {ASTNode} node The node to check. | ||
* @returns {boolean} Whether the node appears at the beginning of an ancestor ExpressionStatement node. | ||
*/ | ||
function isStartOfExpressionStatement(node) { | ||
const start = node.range[0]; | ||
let ancestor = node; | ||
|
||
while ((ancestor = ancestor.parent) && ancestor.range[0] === start) { | ||
if (ancestor.type === "ExpressionStatement") { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('../shared/types').Rule} */ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
|
||
docs: { | ||
description: "Disallow calls to the `Object` constructor without an argument", | ||
recommended: false, | ||
url: "https://eslint.org/docs/latest/rules/no-object-constructor" | ||
}, | ||
|
||
hasSuggestions: true, | ||
|
||
schema: [], | ||
|
||
messages: { | ||
preferLiteral: "The object literal notation {} is preferable.", | ||
useLiteral: "Replace with '{{replacement}}'." | ||
} | ||
}, | ||
|
||
create(context) { | ||
|
||
const sourceCode = context.sourceCode; | ||
|
||
/** | ||
* Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses. | ||
* @param {ASTNode} node The node to be replaced. | ||
* @returns {boolean} Whether or not parentheses around the object literal are required. | ||
*/ | ||
function needsParentheses(node) { | ||
if (isStartOfExpressionStatement(node)) { | ||
return true; | ||
} | ||
|
||
const prevToken = sourceCode.getTokenBefore(node); | ||
|
||
if (prevToken && isArrowToken(prevToken)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Reports on nodes where the `Object` constructor is called without arguments. | ||
* @param {ASTNode} node The node to evaluate. | ||
* @returns {void} | ||
*/ | ||
function check(node) { | ||
if (node.callee.type !== "Identifier" || node.callee.name !== "Object" || node.arguments.length) { | ||
return; | ||
} | ||
|
||
const variable = getVariableByName(sourceCode.getScope(node), "Object"); | ||
|
||
if (variable && variable.identifiers.length === 0) { | ||
const replacement = needsParentheses(node) ? "({})" : "{}"; | ||
|
||
context.report({ | ||
node, | ||
messageId: "preferLiteral", | ||
suggest: [ | ||
{ | ||
messageId: "useLiteral", | ||
data: { replacement }, | ||
fix: fixer => fixer.replaceText(node, replacement) | ||
} | ||
] | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
CallExpression: check, | ||
NewExpression: check | ||
}; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.