Skip to content

Commit

Permalink
fix: avoid unnecessary space in autofix output
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 13, 2021
1 parent 7e0c12d commit 767fb28
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
24 changes: 2 additions & 22 deletions lib/rules/prefer-object-has-own.js
Expand Up @@ -40,26 +40,6 @@ 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"))) {
return true;
}

return false;
}

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

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

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

Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/prefer-object-has-own.js
Expand Up @@ -380,6 +380,17 @@ ruleTester.run("prefer-object-has-own", rule, {
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

0 comments on commit 767fb28

Please sign in to comment.