-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Adding “prefix” and “postfix” styles to Style/NegatedIf #4104
Adding “prefix” and “postfix” styles to Style/NegatedIf #4104
Conversation
@bbatsov This is my first-time contributing to RuboCop and working with ASTs, so please let me know if I didn't do something right! One thing I wasn't sure about was how to handle the |
Also, I guess I have to add the checks for the different styles to the |
@Drenmi That's weird, I wrote some tests for autocorrect that I expected to fail and they all passed. It seems to magically do the right thing without modifying the |
@Drenmi I pushed them up if you want to take a look… |
lib/rubocop/cop/style/negated_if.rb
Outdated
# without else are considered. | ||
# without else are considered. There are three different styles: | ||
# | ||
# both - enforces `unless` for `prefix` and `postfix` conditionals |
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.
You should add # good
and # bad
and @example
annotations to the code examples.
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.
👍 How's that? There seems to be some inconsistency in the formatting between cops—I went with what I think looks good, but let me know which cop I should mimic if this isn't right.
@@ -861,6 +861,13 @@ Style/MultilineOperationIndentation: | |||
# But it can be overridden by setting this parameter | |||
IndentationWidth: ~ | |||
|
|||
Style/NegatedIf: | |||
EnforcedStyle: both | |||
SupportedStyles: |
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'd add some descriptions of the styles here.
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.
👍 Is this enough? There seems to be some inconsistency in how much of a description to give—some almost duplicate the documentation in the cop itself…
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'autocorrects by replacing if not with unless' do |
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.
None of those examples seem specific to the both
style.
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 not sure what you mean. Do you mean the three autocorrect tests at the end? Or all of the tests inside of the “both” block? Or do you mean it doesn't seem to be testing the “both” style because we're not specifically setting the EnforcedStyle
configuration to “both”?
What I did was I moved all the existing tests into the “both” block, and then added new tests specifically for “prefix” and “postfix”. My thinking was since “prefix” and “postfix” styles didn't previously exist, all the existing tests were inherently testing the “both” style.
].join("\n") | ||
end | ||
|
||
it 'does not autocorrect for postfix' do |
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.
Well, it if doesn't report offenses it won't auto-correct as well.
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.
Ah! So it's not necessary (or even possible) to test the not case?
@Drenmi So I guess that means the conditionals don't need to be added to the autocorrect
method, and that explains why the tests that I expected to fail passed, because if it only autocorrects when there's an offense, then the on_if
method acts like a guard.
expect(corrected).to eq 'bar unless foo' | ||
end | ||
|
||
it 'does not autocorrect for prefix' do |
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.
Same here.
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.
👍
@@ -95,6 +95,21 @@ | |||
|
|||
expect(cop.offenses).to be_empty | |||
end | |||
|
|||
it 'autocorrects by replacing if not with unless' do |
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.
Here probably there should be tests for both prefix
and post
auto-correction.
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.
👍
@brandonweiss ping :-) |
@bbatsov Sorry, I wasn't feeling well the last few days. I'm going to make these changes today! |
[Fixes #3961] Some people have a strong preference against using unless with an `if` statement in multi-line/block form: ```ruby unless foo bar end ``` But they think it still makes sense to use it in single-line/modifier form: ```ruby bar unless foo ``` I’ve added three supported styles: * `prefix` will only enforce using `unless` in the multi-line/block form * `postfix` will only enforce using `unless` in the single-line/modifier form * `both` will enforce using `unless` in both the `prefix` and `postfix` forms
Updated! |
Thanks! |
[Fixes #3961]
Some people have a strong preference against using unless with an
if
statement in multi-line/block form:
But they think it still makes sense to use it in single-line/modifier
form:
I’ve added three supported styles:
prefix
will only enforce usingunless
in the multi-line/block formpostfix
will only enforce usingunless
in thesingle-line/modifier form
both
will enforce usingunless
in both theprefix
andpostfix
forms