New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add rule no-object-constructor
, deprecate no-new-object
#17576
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule overview is partly borrowed from
no-array-constructor
. Does everything make sense?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.