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

Update: add fixer for no-extra-parens (fixes #6944) #6950

Merged
merged 1 commit into from Aug 26, 2016

Conversation

not-an-aardvark
Copy link
Member

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.

@mention-bot
Copy link

@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

@eslintbot
Copy link

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;
Copy link
Member

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[];

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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[].

@michaelficarra
Copy link
Member

You also have to watch out for +(+a), -(-a), and (0).a. They must be corrected to + +a, - -a, and 0..a (or 0 .a) respectively, not ++a, --a, or 0.a.

@not-an-aardvark
Copy link
Member Author

not-an-aardvark commented Aug 20, 2016

Good thinking with +(+a) and -(-a); I'll fix those and add tests for them. However, (0).a is currently not identified as an error by this rule, so it doesn't need to be addressed.

@michaelficarra
Copy link
Member

michaelficarra commented Aug 20, 2016

Hmm, that must be a bug then.

edit: Oh, I remember. We made a special exception for this case.

@not-an-aardvark
Copy link
Member Author

Based on #6864 it seems to be intended behavior, but it does seem a bit inconsistent to me.

@not-an-aardvark
Copy link
Member Author

not-an-aardvark commented Aug 20, 2016

By the way, couldn't +(+foo) (and maybe -(-foo)) simply be corrected to +foo?

(edit: changed 5 to foo)

@michaelficarra
Copy link
Member

michaelficarra commented Aug 20, 2016

For 5, sure, but in general, no. It's observable.

edit: Sorry, I was thinking of just reducing it to 5. Reducing to +5 is probably safe, yeah.

@eslintbot
Copy link

LGTM

@not-an-aardvark
Copy link
Member Author

I updated it to add a space rather than getting rid of the extra +, since we're not trying to minify peoples' code and they might be surprised if an operator disappears.

@nzakas nzakas added the do not merge This pull request should not be merged yet label Aug 21, 2016
@ilyavolodin ilyavolodin removed the do not merge This pull request should not be merged yet label Aug 26, 2016
@ilyavolodin
Copy link
Member

LGTM

@nzakas nzakas merged commit c210510 into eslint:master Aug 26, 2016
@not-an-aardvark not-an-aardvark deleted the issue6944 branch August 26, 2016 17:45
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants