From 7551f0c6fd12f0295cc7d6377bf1899c092e79d8 Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 1 Apr 2020 21:30:09 +0530 Subject: [PATCH] Fix: no check for shadowed Object (fixes #12809) (#13115) --- docs/rules/no-new-object.md | 5 +++- lib/rules/no-new-object.js | 18 ++++++++++--- tests/lib/rules/no-new-object.js | 45 ++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/docs/rules/no-new-object.md b/docs/rules/no-new-object.md index 5186682fc05..aef055c810b 100644 --- a/docs/rules/no-new-object.md +++ b/docs/rules/no-new-object.md @@ -27,7 +27,7 @@ Examples of **incorrect** code for this rule: var myObject = new Object(); -var myObject = new Object; +new Object(); ``` Examples of **correct** code for this rule: @@ -38,6 +38,9 @@ Examples of **correct** code for this rule: var myObject = new CustomObject(); var myObject = {}; + +var Object = function Object() {}; +new Object(); ``` ## When Not To Use It diff --git a/lib/rules/no-new-object.js b/lib/rules/no-new-object.js index f3e99c9bd13..e9f915db5ea 100644 --- a/lib/rules/no-new-object.js +++ b/lib/rules/no-new-object.js @@ -5,6 +5,12 @@ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const astUtils = require("./utils/ast-utils"); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -28,10 +34,17 @@ module.exports = { }, create(context) { - return { - NewExpression(node) { + const variable = astUtils.getVariableByName( + context.getScope(), + node.callee.name + ); + + if (variable && variable.identifiers.length > 0) { + return; + } + if (node.callee.name === "Object") { context.report({ node, @@ -40,6 +53,5 @@ module.exports = { } } }; - } }; diff --git a/tests/lib/rules/no-new-object.js b/tests/lib/rules/no-new-object.js index 667da7c4397..0e8f5eb9957 100644 --- a/tests/lib/rules/no-new-object.js +++ b/tests/lib/rules/no-new-object.js @@ -20,15 +20,50 @@ const ruleTester = new RuleTester(); ruleTester.run("no-new-object", rule, { valid: [ - "var foo = new foo.Object()" + "var myObject = {};", + "var myObject = new CustomObject();", + "var foo = new foo.Object()", + `var Object = function Object() {}; + new Object();`, + `var x = something ? MyClass : Object; + var y = new x();`, + { + code: ` + class Object { + constructor(){ + + } + } + new Object(); + `, + parserOptions: { ecmaVersion: 6 } + }, + { + code: ` + import { Object } from './' + new Object(); + `, + parserOptions: { ecmaVersion: 6, sourceType: "module" } + } ], invalid: [ { code: "var foo = new Object()", - errors: [{ - messageId: "preferLiteral", - type: "NewExpression" - }] + errors: [ + { + messageId: "preferLiteral", + type: "NewExpression" + } + ] + }, + { + code: "new Object();", + errors: [{ messageId: "preferLiteral", type: "NewExpression" }] + }, + { + code: "const a = new Object()", + parserOptions: { ecmaVersion: 6 }, + errors: [{ messageId: "preferLiteral", type: "NewExpression" }] } ] });