Skip to content

Commit

Permalink
[Fix rubocop#3696] modified empty_when lint to not warn when branches…
Browse files Browse the repository at this point in the history
… when body has comments
  • Loading branch information
kanth committed Nov 15, 2016
1 parent b7f224b commit 8b3f72e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@

* [#3662](https://github.com/bbatsov/rubocop/issues/3662): Fix the auto-correction of `Lint/UnneededSplatExpansion` when the splat expansion is inside of another array. ([@rrosenblum][])
* [#3699](https://github.com/bbatsov/rubocop/issues/3699): Fix false positive in `Style/VariableNumber` on variable names ending with an underscore. ([@bquorning][])
* [#3696](https://github.com/bbatsov/rubocop/issues/3696): Fix to avoid warning `when` branches without a body if it has comments. ([@hanumakanthvvn][])

## 0.45.0 (2016-10-31)

Expand Down Expand Up @@ -2492,3 +2493,4 @@
[@iGEL]: https://github.com/iGEL
[@tessi]: https://github.com/tessi
[@ivanovaleksey]: https://github.com/ivanovaleksey
[@hanumakanthvvn]: https://github.com/hanumakanthvvn
8 changes: 6 additions & 2 deletions lib/rubocop/cop/lint/empty_when.rb
Expand Up @@ -15,6 +15,10 @@ module Lint
class EmptyWhen < Cop
MSG = 'Avoid `when` branches without a body.'.freeze

def investigate(processed_source)
@processed_source = processed_source
end

def on_case(node)
_cond_node, *when_nodes, _else_node = *node

Expand All @@ -27,12 +31,12 @@ def on_case(node)

def check_when(when_node)
return unless empty_when_body?(when_node)

add_offense(when_node, when_node.source_range, MSG)
end

def empty_when_body?(when_node)
!when_node.to_a.last
node_comment = @processed_source[when_node.loc.first_line]
!(when_node.to_a.last || comment_line?(node_comment))
end
end
end
Expand Down
22 changes: 14 additions & 8 deletions spec/rubocop/cop/lint/empty_when_spec.rb
Expand Up @@ -44,26 +44,26 @@
it_behaves_like 'code with offense',
['case foo',
'when :bar then 1',
'when :baz # nothing',
'when :baz',
'end'].join("\n")

it_behaves_like 'code with offense',
['case foo',
'when :bar then 1',
'when :baz # nothing',
'else 3',
'else',
'end'].join("\n")

it_behaves_like 'code with offense',
['case foo',
'when :bar then 1',
'when :baz then # nothing',
'when :baz then',
'end'].join("\n")

it_behaves_like 'code with offense',
['case foo',
'when :bar then 1',
'when :baz then # nothing',
'when :baz then',
'else 3',
'end'].join("\n")

Expand All @@ -72,25 +72,22 @@
'when :bar',
' 1',
'when :baz',
' # nothing',
'end'].join("\n")

it_behaves_like 'code with offense',
['case foo',
'when :bar',
' 1',
'when :baz',
' # nothing',
' ',
'else',
' 3',
'end'].join("\n")

it_behaves_like 'code with offense',
['case',
'when :bar',
' 1',
'when :baz',
' # nothing',
'else',
' 3',
'end'].join("\n")
Expand Down Expand Up @@ -136,5 +133,14 @@
'else',
' 3',
'end'].join("\n")
it_behaves_like 'code without offense',
['case',
'when :bar',
' 1',
'when :baz',
' # nothing',
'else',
' 3',
'end'].join("\n")
end
end

0 comments on commit 8b3f72e

Please sign in to comment.