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

[Fix #116] Fix an incorrect autocorrect for Rails/Prensence #117

Merged

Conversation

koic
Copy link
Member

@koic koic commented Aug 27, 2019

Fixes #116 and adds regression tests for #115.

This PR fixes an incorrect autocorrect for Rails/Presence when else branch of ternary operator is not nil.

The following is a reproduction procedure.

# example.rb
a.blank? ? 1 : a
% bundle exec rubocop --only Rails/Presence -a
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Rails/Presence: Use a.presence instead of
a.blank? ? 1 : a.
a.blank? ? 1 : a
^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
-a.blank? ? 1 : a
+a.presence

This PR will auto-correct it to a.presence || 1.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the fix_an_incorrect_autocorrect_for_rails_presence branch from 4e875be to b32dc36 Compare August 27, 2019 08:31
@koic koic changed the title [#116] Fix an incorrect autocorrect for Rails/Prensence [Fix #116] Fix an incorrect autocorrect for Rails/Prensence Aug 27, 2019
@koic koic force-pushed the fix_an_incorrect_autocorrect_for_rails_presence branch 2 times, most recently from a0f162b to 27008a8 Compare August 27, 2019 13:57
Fixes rubocop#116 and adds regression tests for rubocop#115.

This PR fixes an incorrect autocorrect for `Rails/Presence` when
`else` branch of ternary operator is not nil.

The following is a reproduction procedure.

```ruby
# example.rb
a.blank? ? 1 : a
```

```console
% bundle exec rubocop --only Rails/Presence -a
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Rails/Presence: Use a.presence instead of
a.blank? ? 1 : a.
a.blank? ? 1 : a
^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
```

```diff
-a.blank? ? 1 : a
+a.presence
```

This PR will auto-correct it to `a.presence || 1`.
@koic koic force-pushed the fix_an_incorrect_autocorrect_for_rails_presence branch from 27008a8 to 3a0998c Compare August 31, 2019 07:01
@koic koic merged commit ba618ca into rubocop:master Sep 1, 2019
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.

blank? expression is incorrectly converted to presence expression
1 participant