Skip to content

Commit

Permalink
[Fix rubocop#8499] Fix Style/IfUnlessModifier to prevent offense when…
Browse files Browse the repository at this point in the history
… first-line comment and code after end block
  • Loading branch information
Dmytro Savochkin committed Nov 6, 2020
1 parent daf4234 commit f115272
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#8499](https://github.com/rubocop-hq/rubocop/issues/8499): Fix `Style/IfUnlessModifier` and `Style/WhileUntilModifier` to prevent an offense if there are both first-line comment and code after `end` block. ([@dsavochkin][])

## 1.2.0 (2020-11-05)

### New features
Expand Down
21 changes: 18 additions & 3 deletions docs/modules/ROOT/pages/cops_style.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4485,14 +4485,18 @@ unless qux.empty?
Foo.do_something
end
do_something_in_a_method_with_a_long_name(arg) if long_condition
do_something_with_a_long_name(arg) if long_condition_that_prevents_code_fit_on_single_line
# good
do_stuff(bar) if condition
Foo.do_something unless qux.empty?
if long_condition
do_something_in_a_method_with_a_long_name(arg)
if long_condition_that_prevents_code_fit_on_single_line
do_something_with_a_long_name(arg)
end
if short_condition # a long comment that makes it too long if it were just a single line
do_something
end
----

Expand Down Expand Up @@ -11241,6 +11245,17 @@ end
x += 1 until x > 10
----

[source,ruby]
----
# bad
x += 100 while x < 500 # a long comment that makes code too long if it were a single line
# good
while x < 500 # a long comment that makes code too long if it were a single line
x += 100
end
----

=== References

* https://rubystyle.guide#while-as-a-modifier
Expand Down
13 changes: 9 additions & 4 deletions lib/rubocop/cop/mixin/statement_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def single_line_as_modifier?(node)
def non_eligible_node?(node)
node.modifier_form? ||
node.nonempty_line_count > 3 ||
processed_source.line_with_comment?(node.loc.last_line)
processed_source.line_with_comment?(node.loc.last_line) ||
(first_line_comment(node) && code_after(node))
end

def non_eligible_body?(body)
Expand All @@ -41,11 +42,9 @@ def modifier_fits_on_single_line?(node)

def length_in_modifier_form(node)
keyword_element = node.loc.keyword
end_element = node.loc.end
code_before = keyword_element.source_line[0...keyword_element.column]
code_after = end_element.source_line[end_element.last_column..-1]
expression = to_modifier_form(node)
line_length("#{code_before}#{expression}#{code_after}")
line_length("#{code_before}#{expression}#{code_after(node)}")
end

def to_modifier_form(node)
Expand All @@ -64,6 +63,12 @@ def first_line_comment(node)
comment_source unless comment_disables_cop?(comment_source)
end

def code_after(node)
end_element = node.loc.end
code = end_element.source_line[end_element.last_column..-1]
code unless code.empty?
end

def parenthesize?(node)
# Parenthesize corrected expression if changing to modifier-if form
# would change the meaning of the parent expression
Expand Down
10 changes: 7 additions & 3 deletions lib/rubocop/cop/style/if_unless_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ module Style
# Foo.do_something
# end
#
# do_something_in_a_method_with_a_long_name(arg) if long_condition
# do_something_with_a_long_name(arg) if long_condition_that_prevents_code_fit_on_single_line
#
# # good
# do_stuff(bar) if condition
# Foo.do_something unless qux.empty?
#
# if long_condition
# do_something_in_a_method_with_a_long_name(arg)
# if long_condition_that_prevents_code_fit_on_single_line
# do_something_with_a_long_name(arg)
# end
#
# if short_condition # a long comment that makes it too long if it were just a single line
# do_something
# end
class IfUnlessModifier < Base
include StatementModifier
Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/cop/style/while_until_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ module Style
#
# # good
# x += 1 until x > 10
#
# @example
# # bad
# x += 100 while x < 500 # a long comment that makes code too long if it were a single line
#
# # good
# while x < 500 # a long comment that makes code too long if it were a single line
# x += 100
# end
class WhileUntilModifier < Base
include StatementModifier
extend AutoCorrector
Expand Down
12 changes: 12 additions & 0 deletions spec/support/condition_modifier_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,17 @@
RUBY
end
end

context 'when there is a comment on the first line and some code after the end keyword' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
[
1, #{keyword} foo # bar
baz
end, 3
]
RUBY
end
end
end
end

0 comments on commit f115272

Please sign in to comment.