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 1 commit
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
7 changes: 7 additions & 0 deletions lib/rules/prefer-object-has-own.js
Expand Up @@ -87,6 +87,13 @@ module.exports = {
return null;
}

const tokenJustBeforeNode = sourceCode.getTokensBefore(node, 1)[0];
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

// for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
if (tokenJustBeforeNode && tokenJustBeforeNode.type === "Keyword" && !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
24 changes: 24 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,19 @@ 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: "Object['prototype']['hasOwnProperty']['call'](object, property);",
output: "Object.hasOwn(object, property);",
Expand Down