diff --git a/lib/rules/getter-return.js b/lib/rules/getter-return.js index 5209ab1504bd..dd145a746900 100644 --- a/lib/rules/getter-return.js +++ b/lib/rules/getter-return.js @@ -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"; } } } diff --git a/tests/lib/rules/getter-return.js b/tests/lib/rules/getter-return.js index 92032c066fbc..5f602c587e23 100644 --- a/tests/lib/rules/getter-return.js +++ b/tests/lib/rules/getter-return.js @@ -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 }, @@ -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 { @@ -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'" } }] } ] });