-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This cop check the use of the `then` keyword in multi-line when statements. Its name comes from `Style/MultilineIfThen` cop. This cop is enabled by default because `Style/MultilineIfThen` cop is enabled by default.
- Loading branch information
1 parent
c804110
commit 203843f
Showing
9 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks uses of the `then` keyword | ||
# in multi-line when statements. | ||
# | ||
# @example | ||
# # bad | ||
# case foo | ||
# when bar then | ||
# end | ||
# | ||
# # good | ||
# case foo | ||
# when bar | ||
# end | ||
# | ||
# # good | ||
# case foo | ||
# when bar then do_something | ||
# end | ||
# | ||
class MultilineWhenThen < Cop | ||
include RangeHelp | ||
|
||
MSG = 'Do not use `then` for multiline `when` statement.' | ||
|
||
def on_when(node) | ||
# Without `then`, there's no offense | ||
return unless node.then? | ||
|
||
# Single line usage of `then` is not an offense | ||
return if !node.children.last.nil? && !node.multiline? && node.then? | ||
|
||
# With more than one statements after then, there's not offense | ||
return if node.children.last&.begin_type? | ||
|
||
add_offense(node, location: :begin) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.remove( | ||
range_with_surrounding_space( | ||
range: node.loc.begin, side: :left | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::MultilineWhenThen do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for empty when statement with then' do | ||
expect_offense(<<~RUBY) | ||
case foo | ||
when bar then | ||
^^^^ Do not use `then` for multiline `when` statement. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for multiline when statement with then' do | ||
expect_offense(<<~RUBY) | ||
case foo | ||
when bar then | ||
^^^^ Do not use `then` for multiline `when` statement. | ||
do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it "doesn't register an offense for singleline when statement with then" do | ||
expect_no_offenses(<<~RUBY) | ||
case foo | ||
when bar then do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it "doesn't register an offense for multiline when statement | ||
with then followed by other lines" do | ||
expect_no_offenses(<<~RUBY) | ||
case foo | ||
when bar then do_something | ||
do_another_thing | ||
end | ||
RUBY | ||
end | ||
|
||
it "doesn't register an offense for empty when statement without then" do | ||
expect_no_offenses(<<~RUBY) | ||
case foo | ||
when bar | ||
end | ||
RUBY | ||
end | ||
|
||
it "doesn't register an offense for multiline when statement without then" do | ||
expect_no_offenses(<<~RUBY) | ||
case foo | ||
when bar | ||
do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'autocorrects then in empty when' do | ||
new_source = autocorrect_source(<<~RUBY) | ||
case foo | ||
when bar then | ||
end | ||
RUBY | ||
expect(new_source).to eq(<<~RUBY) | ||
case foo | ||
when bar | ||
end | ||
RUBY | ||
end | ||
|
||
it 'autocorrects then in multiline when' do | ||
new_source = autocorrect_source(<<~RUBY) | ||
case foo | ||
when bar then | ||
do_something | ||
end | ||
RUBY | ||
expect(new_source).to eq(<<~RUBY) | ||
case foo | ||
when bar | ||
do_something | ||
end | ||
RUBY | ||
end | ||
end |