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: improve autofix of prefer-object-has-own #15419

Merged
merged 7 commits into from Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 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,26 @@ function hasLeftHandObject(node) {
return false;
}

/**
* Checks if the given token is `await` Identefier, `of` Itentifier or a keyword token
* Inspired from eslint-plugin-unicorn/rules/fix/fix-space-around-keywords.js
* @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"))) {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

return false;
}

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

const tokenJustBeforeNode = sourceCode.getTokenBefore(node, { includeComments: true });
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

// for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
if (tokenJustBeforeNode && isProblematicToken(tokenJustBeforeNode) && !sourceCode.isSpaceBetween(node, tokenJustBeforeNode)) {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
return fixer.replaceText(node.callee, " Object.hasOwn");
}

return fixer.replaceText(node.callee, "Object.hasOwn");
}
});
Expand Down
101 changes: 101 additions & 0 deletions tests/lib/rules/prefer-object-has-own.js
Expand Up @@ -224,6 +224,17 @@ ruleTester.run("prefer-object-has-own", rule, {
endColumn: 61
}]
},
{
code: "const hasProperty={}.hasOwnProperty.call(object, property);",
output: "const hasProperty=Object.hasOwn(object, property);",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 19,
endLine: 1,
endColumn: 59
}]
},
{
code: "const hasProperty = (( {}.hasOwnProperty.call(object, property) ));",
output: "const hasProperty = (( Object.hasOwn(object, property) ));",
Expand Down Expand Up @@ -279,6 +290,96 @@ ruleTester.run("prefer-object-has-own", rule, {
endColumn: 63
}]
},

// https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
{
code: "function foo(){return{}.hasOwnProperty.call(object, property)}",
output: "function foo(){return Object.hasOwn(object, property)}",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 22,
endLine: 1,
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: "for (const x of{}.hasOwnProperty.call(object, property).toString());",
output: "for (const x of Object.hasOwn(object, property).toString());",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 16,
endLine: 1,
endColumn: 56
}]
},
{
code: "for (const x of/*comment*/{}.hasOwnProperty.call(object, property).toString());",
output: "for (const x of/*comment*/Object.hasOwn(object, property).toString());",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 27,
endLine: 1,
endColumn: 67
}]
},
{
code: "for (const x in{}.hasOwnProperty.call(object, property).toString());",
output: "for (const x in Object.hasOwn(object, property).toString());",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 16,
endLine: 1,
endColumn: 56
}]
},
{
code: "for (const x in/*comment*/{}.hasOwnProperty.call(object, property).toString());",
output: "for (const x in/*comment*/Object.hasOwn(object, property).toString());",
errors: [{
messageId: "useHasOwn",
line: 1,
column: 27,
endLine: 1,
endColumn: 67
}]
},
{
code: "Object['prototype']['hasOwnProperty']['call'](object, property);",
output: "Object.hasOwn(object, property);",
Expand Down