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
Update: Add support for parens on left side for-loops (fixes: #8393) #8679
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,6 +255,22 @@ module.exports = { | |
!astUtils.canTokensBeAdjacent(tokenBeforeLeftParen, firstToken); | ||
} | ||
|
||
/** | ||
* Determines whether a node should be followed by an additional space when removing parens | ||
* @param {ASTNode} node node to evaluate; must be surrounded by parentheses | ||
* @returns {boolean} `true` if a space should be inserted after the node | ||
* @private | ||
*/ | ||
function requiresAfterSpace(node) { | ||
const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 }); | ||
const rightParenToken = nextTwoTokens[0]; | ||
const tokenAfterRightParen = nextTwoTokens[1]; | ||
|
||
return rightParenToken && tokenAfterRightParen && | ||
!sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) && | ||
tokenAfterRightParen.type === "Keyword"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking whether the rightmost token is a keyword, can you use something like this instead? const tokenBeforeRightParen = sourceCode.getLastToken(node);
!astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen) The reason a space is needed isn't because the following token is a keyword, it's because the two tokens will combine if the parens are removed. This makes a difference in cases like this: for ((foo['bar'])of baz); The |
||
} | ||
|
||
/** | ||
* Report the node | ||
* @param {ASTNode} node node to evaluate | ||
|
@@ -279,7 +295,7 @@ module.exports = { | |
return fixer.replaceTextRange([ | ||
leftParenToken.range[0], | ||
rightParenToken.range[1] | ||
], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource); | ||
], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresAfterSpace(node) ? " " : "")); | ||
} | ||
}); | ||
} | ||
|
@@ -488,12 +504,18 @@ module.exports = { | |
if (hasExcessParens(node.right)) { | ||
report(node.right); | ||
} | ||
if (hasExcessParens(node.left)) { | ||
report(node.left); | ||
} | ||
}, | ||
|
||
ForOfStatement(node) { | ||
if (hasExcessParens(node.right)) { | ||
report(node.right); | ||
} | ||
if (hasExcessParens(node.left)) { | ||
report(node.left); | ||
} | ||
}, | ||
|
||
ForStatement(node) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick:
requiresTrailingSpace
might be a little clearer for this function name