-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
blank? expression is incorrectly converted to presence expression #116
Labels
bug
Something isn't working
Comments
I changed my code to use the expression "x.present? ? x : 1" hoping it would not auto-correct, but it changes that expression to "x.presence", which is incorrect. |
koic
added a commit
to koic/rubocop-rails
that referenced
this issue
Aug 27, 2019
Fixes rubocop#116 and adds regression test 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 is auto-corrected to `a.presence || 1`.
8 tasks
koic
added a commit
to koic/rubocop-rails
that referenced
this issue
Aug 27, 2019
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 is auto-corrected to `a.presence || 1`.
koic
added a commit
to koic/rubocop-rails
that referenced
this issue
Aug 27, 2019
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-corrected to `a.presence || 1`.
koic
added a commit
to koic/rubocop-rails
that referenced
this issue
Aug 27, 2019
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`.
Another example Using rubocop-rails (2.3.1)
|
koic
added a commit
to koic/rubocop-rails
that referenced
this issue
Aug 31, 2019
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
added a commit
that referenced
this issue
Sep 1, 2019
…ils_presence [Fix #116] Fix an incorrect autocorrect for `Rails/Prensence`
RuboCop Rails 2.3.2 has been released to resolve this issue. Thank you. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The expression "x.blank? 1 : x" is incorrectly corrected to "x.presence".
Expected behavior
Expected the line "puts [1, nil, 0, 3].map { |x| x.blank? ? 1 : x }.sum" to be unchanged.
Actual behavior
The line "puts [1, nil, 0, 3].map { |x| x.blank? ? 1 : x }.sum"
was changed to "puts [1, nil, 0, 3].map(&:presence).sum"
Steps to reproduce the problem
In my rails directory, I create the file "bug.rb"
with the line "puts [1, nil, 0, 3].map { |x| x.blank? ? 1 : x }.sum".
Ran "rails runner bug.rb".
It outputs "5", which is the correct and expected value.
Ran "rubocop --auto-correct", which changes the file bug.rb.
Ran "rails runner bug.rb".
It outputs a Traceback since the nil value was not converted to "1" like the original expression.
RuboCop version
bundle exec rubocop -V
0.74.0 (using Parser 2.6.3.0, running on ruby 2.6.3 x86_64-linux)
From my Gemfile.lock,
rubocop-performance is 1.4.1
rubocop-rails is 2.3.1
The text was updated successfully, but these errors were encountered: