2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview A rule to disallow calls to the Object constructor
|
|
|
|
* @author Matt DuVall <http://www.mattduvall.com/>
|
2024-08-21 06:34:30 +00:00
|
|
|
* @deprecated in ESLint v8.50.0
|
2024-08-09 12:04:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const astUtils = require("./utils/ast-utils");
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "suggestion",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Disallow `Object` constructors",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/no-new-object"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
deprecated: true,
|
|
|
|
|
|
|
|
replacedBy: [
|
|
|
|
"no-object-constructor"
|
|
|
|
],
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
schema: [],
|
|
|
|
|
|
|
|
messages: {
|
2024-08-21 06:34:30 +00:00
|
|
|
preferLiteral: "The object literal notation {} is preferable."
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
2024-08-21 06:34:30 +00:00
|
|
|
|
|
|
|
const sourceCode = context.sourceCode;
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
return {
|
|
|
|
NewExpression(node) {
|
|
|
|
const variable = astUtils.getVariableByName(
|
2024-08-21 06:34:30 +00:00
|
|
|
sourceCode.getScope(node),
|
2024-08-09 12:04:48 +00:00
|
|
|
node.callee.name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (variable && variable.identifiers.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.callee.name === "Object") {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: "preferLiteral"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|