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

feat: check for Object.create() without explicit return value in getter return #16420

Merged
merged 1 commit into from Oct 21, 2022
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
22 changes: 14 additions & 8 deletions lib/rules/getter-return.js
Expand Up @@ -112,18 +112,24 @@ module.exports = {
}
if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") {

// Object.defineProperty()
if (parent.parent.parent.type === "CallExpression" &&
astUtils.getStaticPropertyName(parent.parent.parent.callee) === "defineProperty") {
return true;
// Object.defineProperty() or Reflect.defineProperty()
if (parent.parent.parent.type === "CallExpression") {
const callNode = parent.parent.parent.callee;

if (astUtils.isSpecificMemberAccess(callNode, "Object", "defineProperty") ||
astUtils.isSpecificMemberAccess(callNode, "Reflect", "defineProperty")) {
return true;
}
}

// Object.defineProperties()
// Object.defineProperties() or Object.create()
if (parent.parent.parent.type === "Property" &&
parent.parent.parent.parent.type === "ObjectExpression" &&
parent.parent.parent.parent.parent.type === "CallExpression" &&
astUtils.getStaticPropertyName(parent.parent.parent.parent.parent.callee) === "defineProperties") {
return true;
parent.parent.parent.parent.parent.type === "CallExpression") {
const callNode = parent.parent.parent.parent.parent.callee;

return astUtils.isSpecificMemberAccess(callNode, "Object", "defineProperties") ||
astUtils.isSpecificMemberAccess(callNode, "Object", "create");
}
}
}
Expand Down
83 changes: 82 additions & 1 deletion tests/lib/rules/getter-return.js
Expand Up @@ -57,11 +57,27 @@ ruleTester.run("getter-return", rule, {
"Object.defineProperties(foo, { bar: { get: function () {return true;}} });",
"Object.defineProperties(foo, { bar: { get: function () { ~function (){ return true; }(); return true;}} });",

/*
* test reflect.defineProperty(s)
* option: {allowImplicit: false}
*/
"Reflect.defineProperty(foo, \"bar\", { get: function () {return true;}});",
"Reflect.defineProperty(foo, \"bar\", { get: function () { ~function (){ return true; }();return true;}});",

/*
* test object.create(s)
* option: {allowImplicit: false}
*/
"Object.create(foo, { bar: { get() {return true;} } });",
"Object.create(foo, { bar: { get: function () {return true;} } });",
"Object.create(foo, { bar: { get: () => {return true;} } });",

// option: {allowImplicit: true}
{ code: "Object.defineProperty(foo, \"bar\", { get: function () {return true;}});", options },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){return;}});", options },
{ code: "Object.defineProperties(foo, { bar: { get: function () {return true;}} });", options },
{ code: "Object.defineProperties(foo, { bar: { get: function () {return;}} });", options },
{ code: "Reflect.defineProperty(foo, \"bar\", { get: function () {return true;}});", options },

// not getter.
"var get = function(){};",
Expand All @@ -73,7 +89,10 @@ ruleTester.run("getter-return", rule, {
"var foo = { bar: function(){return true;} };",
"var foo = { get: function () {} }",
"var foo = { get: () => {}};",
"class C { get; foo() {} }"
"class C { get; foo() {} }",
"foo.defineProperty(null, { get() {} });",
"foo.defineProperties(null, { bar: { get() {} } });",
"foo.create(null, { bar: { get() {} } });"
],

invalid: [
Expand Down Expand Up @@ -220,11 +239,67 @@ ruleTester.run("getter-return", rule, {
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){if(bar) {return true;}}});", errors: [{ messageId: "expectedAlways" }] },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){ ~function () { return true; }()}});", errors: [{ messageId: "expected" }] },

/*
* test reflect.defineProperty(s)
* option: {allowImplicit: false}
*/
{
code: "Reflect.defineProperty(foo, 'bar', { get: function (){}});",
errors: [{
messageId: "expected",
data: { name: "method 'get'" },
line: 1,
column: 38,
endLine: 1,
endColumn: 52
}]
},

/*
* test object.create(s)
* option: {allowImplicit: false}
*/
{
code: "Object.create(foo, { bar: { get: function() {} } })",
errors: [{
messageId: "expected",
data: { name: "method 'get'" },
line: 1,
column: 29,
endLine: 1,
endColumn: 42
}]
},
{
code: "Object.create(foo, { bar: { get() {} } })",
errors: [{
messageId: "expected",
data: { name: "method 'get'" },
line: 1,
column: 29,
endLine: 1,
endColumn: 32
}]
},
{
code: "Object.create(foo, { bar: { get: () => {} } })",
errors: [{
messageId: "expected",
data: { name: "method 'get'" },
line: 1,
column: 29,
endLine: 1,
endColumn: 34
}]
},

// option: {allowImplicit: true}
{ code: "Object.defineProperties(foo, { bar: { get: function () {}} });", options, errors: [{ messageId: "expected" }] },
{ code: "Object.defineProperties(foo, { bar: { get: function (){if(bar) {return true;}}}});", options, errors: [{ messageId: "expectedAlways" }] },
{ code: "Object.defineProperties(foo, { bar: { get: function () {~function () { return true; }()}} });", options, errors: [{ messageId: "expected" }] },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){}});", options, errors: [{ messageId: "expected" }] },
{ code: "Object.create(foo, { bar: { get: function (){} } });", options, errors: [{ messageId: "expected" }] },
{ code: "Reflect.defineProperty(foo, \"bar\", { get: function (){}});", options, errors: [{ messageId: "expected" }] },

// Optional chaining
{
Expand All @@ -248,6 +323,12 @@ ruleTester.run("getter-return", rule, {
options,
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "expected", data: { name: "method 'get'" } }]
},
{
code: "(Object?.create)(foo, { bar: { get: function (){} } });",
options,
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "expected", data: { name: "method 'get'" } }]
}
]
});