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 fixer for no-extra-parens (fixes #6944) #6950
Conversation
@not-an-aardvark, thanks for your PR! By analyzing the annotation information on this pull request, we identified @michaelficarra, @nzakas and @mysticatea to be potential reviewers |
LGTM |
const precedingToken = sourceCode.getTokenBefore(leftParenToken); | ||
|
||
// If the parens are preceded by a keyword (e.g. `typeof(0)`), a space should be inserted (`typeof 0`) | ||
const isPrecededByKeyword = precedingToken && precedingToken.type === "Keyword" && precedingToken.end === leftParenToken.start; |
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.
This is only necessary if the parenthesised expression starts with an IdentifierPart
. You can use esutils.isIdentifierPart
for this test.
edit: An example test case:
typeof([]);
should be corrected to
typeof[];
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.
That fix works, but I wonder if it could just always output a space after keywords, and then let the keyword-spacing rule clean up if the spacing is wrong.
On the other hand, I suppose it's better to modify the code as little as possible.
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.
I'm okay with either way.
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.
Fixed; it now only adds a space if it's necessary to prevent an error, so typeof([])
will get fixed to typeof[]
.
You also have to watch out for |
Good thinking with |
Hmm, that must be a bug then. edit: Oh, I remember. We made a special exception for this case. |
Based on #6864 it seems to be intended behavior, but it does seem a bit inconsistent to me. |
By the way, couldn't (edit: changed |
For edit: Sorry, I was thinking of just reducing it to |
185da9e
to
a9294e0
Compare
LGTM |
I updated it to add a space rather than getting rid of the extra |
LGTM |
What issue does this pull request address?
#6944
What changes did you make? (Give an overview)
This adds an autofixer to
no-extra-parens
, allowing it to be fixed automatically with the--fix
option.Is there anything you'd like reviewers to focus on?
#6944 has not been marked as accepted yet, so this PR shouldn't be merged until it is.
It's probably worth double-checking this logic to make sure there are no other special cases that need to be addressed.