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 #111] Fix an incorrect autocorrect for Rails/Presence #115

Merged

Conversation

koic
Copy link
Member

@koic koic commented Aug 24, 2019

Fixes #111.

This PR fixes an incorrect autocorrect for Rails/Presence when method arguments of else branch is not enclosed in parentheses.

The following is a reproduction procedure.

% cat example.rb
if value.present?
  value
else
  do_something value
end
% bundle exec rubocop -a --only Rails/Presence
Inspecting 1 file
E

Offenses:

example.rb:1:1: C: [Corrected] Rails/Presence: Use value.presence ||
do_something value instead of if value.present?
  value
else
  do_something value
end.
if value.present? ...
^^^^^^^^^^^^^^^^^
example.rb:1:32: E: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter,
under AllCops)
value.presence || do_something value
                               ^^^^^

1 file inspected, 2 offenses detected, 1 offense corrected
% cat example.rb
value.presence || do_something value

The auto-corrected code is a syntax error.

% ruby example.rb
example.rb:3: syntax error, unexpected local variable or method,
expecting `do' or '{' or '('
....presence || do_something value

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_incorrect_autocorrect_for_rails_presence branch from 4734c9a to de133ab Compare August 24, 2019 05:13
@koic koic changed the title [Fix #111] Fix incorrect autocorrect for Rails/Presence [Fix #111] Fix an incorrect autocorrect for Rails/Presence Aug 24, 2019
Fixes rubocop#111.

This PR fixes an incorrect autocorrect for `Rails/Presence` when method
arguments of `else` branch is not enclosed in parentheses.

The following is a reproduction procedure.

```console
% cat example.rb
if value.present?
  value
else
  do_something value
end
```

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

Offenses:

example.rb:1:1: C: [Corrected] Rails/Presence: Use value.presence ||
do_something value instead of if value.present?
  value
else
  do_something value
end.
if value.present? ...
^^^^^^^^^^^^^^^^^
example.rb:1:32: E: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter,
under AllCops)
value.presence || do_something value
                               ^^^^^

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

```console
% cat example.rb
value.presence || do_something value
```

The auto-corrected code is a syntax error.

```console
% ruby example.rb
example.rb:3: syntax error, unexpected local variable or method,
expecting `do' or '{' or '('
....presence || do_something value
```
@koic koic force-pushed the fix_incorrect_autocorrect_for_rails_presence branch from de133ab to ce2937d Compare August 26, 2019 05:11
@koic koic merged commit 18f1147 into rubocop:master Aug 26, 2019
@koic koic deleted the fix_incorrect_autocorrect_for_rails_presence branch August 26, 2019 05:22
koic added a commit to koic/rubocop-rails that referenced this pull request 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`.
koic added a commit to koic/rubocop-rails that referenced this pull request 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 pull request 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 pull request 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`.
koic added a commit to koic/rubocop-rails that referenced this pull request 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`.
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.

Not safe refactor when run with rubocop -R -a --safe
1 participant