From a5bf4b9a032d692c8e404ba8cfa5a9b9104b2399 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 6 Jun 2019 22:57:20 +0800 Subject: [PATCH] New: Let `no-new-object` pass with arguments (fixes #11810) --- docs/rules/no-new-object.md | 23 +++++++++++++++++++++++ lib/rules/no-new-object.js | 17 +++++++++++++++-- tests/lib/rules/no-new-object.js | 11 +++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/docs/rules/no-new-object.md b/docs/rules/no-new-object.md index 5186682fc058..7628954c3291 100644 --- a/docs/rules/no-new-object.md +++ b/docs/rules/no-new-object.md @@ -40,6 +40,29 @@ var myObject = new CustomObject(); var myObject = {}; ``` +## Options + +This rule has a single `allowWithArguments` option. + +### allowWithArguments + +One can coerce primitives into objects by passing `new Object()` an argument. +This option allows for this usage. + +Examples of **incorrect** code for the `{ "allowWithArguments": true }` option: + +```js +/*eslint no-new-object: [2, { "allowWithArguments": true }]*/ +var myObject = new Object(); +``` + +Examples of **correct** code for the `{ "allowWithArguments": true }` option: + +```js +/*eslint no-new-object: [2, { "allowWithArguments": true }]*/ +var myObject = new Object(BigInt('1')); +``` + ## When Not To Use It If you wish to allow the use of the `Object` constructor, you can safely turn this rule off. diff --git a/lib/rules/no-new-object.js b/lib/rules/no-new-object.js index f5cc28664f4e..2844a695925f 100644 --- a/lib/rules/no-new-object.js +++ b/lib/rules/no-new-object.js @@ -20,15 +20,28 @@ module.exports = { url: "https://eslint.org/docs/rules/no-new-object" }, - schema: [] + schema: [ + { + additionalProperties: false, + properties: { + allowWithArguments: { + type: "boolean" + } + }, + type: "object" + } + ] }, create(context) { + const { allowWithArguments } = context.options[0] || {}; return { NewExpression(node) { - if (node.callee.name === "Object") { + if (node.callee.name === "Object" && ( + !allowWithArguments || !node.arguments.length + )) { context.report({ node, message: "The object literal notation {} is preferrable." }); } } diff --git a/tests/lib/rules/no-new-object.js b/tests/lib/rules/no-new-object.js index b0fb22e370ba..6b0cee12cd76 100644 --- a/tests/lib/rules/no-new-object.js +++ b/tests/lib/rules/no-new-object.js @@ -20,9 +20,16 @@ const ruleTester = new RuleTester(); ruleTester.run("no-new-object", rule, { valid: [ - "var foo = new foo.Object()" + "var foo = new foo.Object()", + { + code: "var foo = new Object(BigInt('1n'))", + options: [ + { allowWithArguments: true } + ] + } ], invalid: [ - { code: "var foo = new Object()", errors: [{ message: "The object literal notation {} is preferrable.", type: "NewExpression" }] } + { code: "var foo = new Object()", errors: [{ message: "The object literal notation {} is preferrable.", type: "NewExpression" }] }, + { code: "var foo = new Object(BigInt('1n'))", errors: [{ message: "The object literal notation {} is preferrable.", type: "NewExpression" }] } ] });