Skip to content

Commit

Permalink
fix: check for Object.create() without explicit return value in
Browse files Browse the repository at this point in the history
getter-return

Fixes: #16419
  • Loading branch information
hirasawayuki committed Oct 13, 2022
1 parent dd0c58f commit 44b2333
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rules/getter-return.js
Expand Up @@ -118,12 +118,13 @@ module.exports = {
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 calleePropName = astUtils.getStaticPropertyName(parent.parent.parent.parent.parent.callee);

return calleePropName === "defineProperties" || calleePropName === "create";
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/lib/rules/getter-return.js
Expand Up @@ -57,6 +57,14 @@ 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 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 },
Expand Down Expand Up @@ -220,11 +228,50 @@ 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 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" }] },

// Optional chaining
{
Expand All @@ -248,6 +295,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'" } }]
}
]
});

0 comments on commit 44b2333

Please sign in to comment.