Skip to content
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

Fix: no check for shadowed Object (fixes #12809) #13115

Merged
merged 1 commit into from Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/rules/no-new-object.md
Expand Up @@ -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:
Expand All @@ -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
Expand Down
18 changes: 15 additions & 3 deletions lib/rules/no-new-object.js
Expand Up @@ -5,6 +5,12 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -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,
Expand All @@ -40,6 +53,5 @@ module.exports = {
}
}
};

}
};
45 changes: 40 additions & 5 deletions tests/lib/rules/no-new-object.js
Expand Up @@ -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" }]
}
]
});