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

Adding “prefix” and “postfix” styles to Style/NegatedIf #4104

Merged
merged 1 commit into from
Mar 19, 2017
Merged

Adding “prefix” and “postfix” styles to Style/NegatedIf #4104

merged 1 commit into from
Mar 19, 2017

Conversation

brandonweiss
Copy link
Contributor

@brandonweiss brandonweiss commented Mar 9, 2017

[Fixes #3961]

Some people have a strong preference against using unless with an if
statement in multi-line/block form:

unless foo
  bar
end

But they think it still makes sense to use it in single-line/modifier
form:

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

@brandonweiss
Copy link
Contributor Author

@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 both style? Should I add a specific check for it rather than falling through if it's not prefix or postfix? Or is this OK because RuboCop will complain if you try to set something other than the supported styles?

@brandonweiss
Copy link
Contributor Author

Also, I guess I have to add the checks for the different styles to the autocorrect method as well?

@brandonweiss
Copy link
Contributor Author

@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 autocorrect block… what am I missing?

@brandonweiss
Copy link
Contributor Author

@Drenmi I pushed them up if you want to take a look…

# without else are considered.
# without else are considered. There are three different styles:
#
# both - enforces `unless` for `prefix` and `postfix` conditionals
Copy link
Collaborator

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.

Copy link
Contributor Author

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:
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 14, 2017

@brandonweiss ping :-)

@brandonweiss
Copy link
Contributor Author

@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
@brandonweiss
Copy link
Contributor Author

Updated!

@bbatsov bbatsov merged commit c332a94 into rubocop:master Mar 19, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 19, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Separate prefix-unless from postfix-unless - currently both covered by Style/NegatedIf
2 participants