Skip to content

Commit

Permalink
fix: cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 11, 2021
1 parent 082a4eb commit 548eb55
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/rules/prefer-object-has-own.js
Expand Up @@ -12,6 +12,10 @@

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

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Checks if the given node is considered to be an access to a property of `Object.prototype`.
* @param {ASTNode} node `MemberExpression` node to evaluate.
Expand All @@ -36,6 +40,25 @@ function hasLeftHandObject(node) {
return false;
}

/**
* Checks if the given token is `await` Identefier, `of` Itentifier or a keyword token
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is is `await` Identefier, `of` Itentifier or a keyword token
*/
function isProblematicToken(token) {
const { type, value } = token;

if (
(type === "Keyword" && /^[a-z]*$/u.test(value)) ||

// AwaitExpression or ForOfStatement
(type === "Identifier" && (value === "of" || value === "await"))) {
return true;
}

return false;
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -87,10 +110,10 @@ module.exports = {
return null;
}

const tokenJustBeforeNode = sourceCode.getTokensBefore(node, 1)[0];
const tokenJustBeforeNode = sourceCode.getTokenBefore(node, { includeComments: true });

// for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
if (tokenJustBeforeNode && tokenJustBeforeNode.type === "Keyword" && !sourceCode.isSpaceBetween(node, tokenJustBeforeNode)) {
if (tokenJustBeforeNode && isProblematicToken(tokenJustBeforeNode) && !sourceCode.isSpaceBetween(node, tokenJustBeforeNode)) {
return fixer.replaceText(node.callee, " Object.hasOwn");
}

Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/prefer-object-has-own.js
Expand Up @@ -303,6 +303,39 @@ ruleTester.run("prefer-object-has-own", rule, {
endColumn: 62
}]
},
{
code: "function foo(){return/*comment*/{}.hasOwnProperty.call(object, property)}",
output: "function foo(){return/*comment*/Object.hasOwn(object, property)}",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 33,
endLine: 1,
endColumn: 73
}]
},
{
code: "async function foo(){return await{}.hasOwnProperty.call(object, property)}",
output: "async function foo(){return await Object.hasOwn(object, property)}",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 34,
endLine: 1,
endColumn: 74
}]
},
{
code: "async function foo(){return await/*comment*/{}.hasOwnProperty.call(object, property)}",
output: "async function foo(){return await/*comment*/Object.hasOwn(object, property)}",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 45,
endLine: 1,
endColumn: 85
}]
},
{
code: "Object['prototype']['hasOwnProperty']['call'](object, property);",
output: "Object.hasOwn(object, property);",
Expand Down

0 comments on commit 548eb55

Please sign in to comment.