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 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
15 changes: 15 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 Down Expand Up @@ -87,6 +91,17 @@ module.exports = {
return null;
}

const tokenJustBeforeNode = sourceCode.getTokenBefore(node.callee, { includeComments: true });

// for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
if (
tokenJustBeforeNode &&
tokenJustBeforeNode.range[1] === node.callee.range[0] &&
!astUtils.canTokensBeAdjacent(tokenJustBeforeNode, "Object.hasOwn")
) {
return fixer.replaceText(node.callee, " Object.hasOwn");
}

return fixer.replaceText(node.callee, "Object.hasOwn");
}
});
Expand Down
112 changes: 112 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,107 @@ 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: "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: 64
}]
},
{
code: "Object['prototype']['hasOwnProperty']['call'](object, property);",
output: "Object.hasOwn(object, property);",
Expand Down