Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
256 additions
and
6 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,89 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for places where multiple consecutive loops over the same data | ||
# can be combined into a single loop. It is very likely that combining them | ||
# will make the code more efficient and more concise. | ||
# | ||
# It is marked as unsafe, because the first loop might modify | ||
# a state that the second loop depends on; these two aren't combinable. | ||
# | ||
# @example | ||
# # bad | ||
# def method | ||
# items.each do |item| | ||
# do_something(item) | ||
# end | ||
# | ||
# items.each do |item| | ||
# do_something_else(item) | ||
# end | ||
# end | ||
# | ||
# # good | ||
# def method | ||
# items.each do |item| | ||
# do_something(item) | ||
# do_something_else(item) | ||
# end | ||
# end | ||
# | ||
# # bad | ||
# def method | ||
# for item in items do | ||
# do_something(item) | ||
# end | ||
# | ||
# for item in items do | ||
# do_something_else(item) | ||
# end | ||
# end | ||
# | ||
# # good | ||
# def method | ||
# for item in items do | ||
# do_something(item) | ||
# do_something_else(item) | ||
# end | ||
# end | ||
# | ||
class CombinableLoops < Base | ||
MSG = 'Combine this loop with the previous loop.' | ||
|
||
def on_block(node) | ||
return unless collection_looping_method?(node) | ||
|
||
sibling = left_sibling_of(node) | ||
add_offense(node) if same_collection_looping?(node, sibling) | ||
end | ||
|
||
def on_for(node) | ||
sibling = left_sibling_of(node) | ||
add_offense(node) if sibling&.for_type? && node.collection == sibling.collection | ||
end | ||
|
||
private | ||
|
||
def collection_looping_method?(node) | ||
method_name = node.send_node.method_name | ||
method_name.match?(/^each/) || method_name.match?(/_each$/) | ||
end | ||
|
||
def left_sibling_of(node) | ||
return unless node.parent&.begin_type? | ||
|
||
index = node.sibling_index - 1 | ||
node.parent.children[index] if index >= 0 | ||
end | ||
|
||
def same_collection_looping?(node, sibling) | ||
sibling&.block_type? && | ||
sibling.send_node.method?(node.method_name) && | ||
sibling.send_node.receiver == node.send_node.receiver | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::CombinableLoops do | ||
subject(:cop) { described_class.new } | ||
|
||
context 'when looping method' do | ||
it 'registers an offense when looping over the same data as previous loop' do | ||
expect_offense(<<~RUBY) | ||
items.each { |item| do_something(item) } | ||
items.each { |item| do_something_else(item, arg) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop. | ||
items.each_with_index { |item| do_something(item) } | ||
items.each_with_index { |item| do_something_else(item, arg) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop. | ||
items.reverse_each { |item| do_something(item) } | ||
items.reverse_each { |item| do_something_else(item, arg) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when the same loops are interleaved with some code' do | ||
expect_no_offenses(<<~RUBY) | ||
items.each { |item| do_something(item) } | ||
some_code | ||
items.each { |item| do_something_else(item, arg) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when the same loop method is used over different collections' do | ||
expect_no_offenses(<<~RUBY) | ||
items.each { |item| do_something(item) } | ||
bars.each { |bar| do_something(bar) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when different loop methods are used over the same collection' do | ||
expect_no_offenses(<<~RUBY) | ||
items.reverse_each { |item| do_something(item) } | ||
items.each { |item| do_something(item) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when each branch contains the same single loop over the same collection' do | ||
expect_no_offenses(<<~RUBY) | ||
if condition | ||
items.each { |item| do_something(item) } | ||
else | ||
items.each { |item| do_something_else(item, arg) } | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when for loop' do | ||
it 'registers an offense when looping over the same data as previous loop' do | ||
expect_offense(<<~RUBY) | ||
for item in items do do_something(item) end | ||
for item in items do do_something_else(item, arg) end | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when the same loops are interleaved with some code' do | ||
expect_no_offenses(<<~RUBY) | ||
for item in items do do_something(item) end | ||
some_code | ||
for item in items do do_something_else(item, arg) end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when the same loop method is used over different collections' do | ||
expect_no_offenses(<<~RUBY) | ||
for item in items do do_something(item) end | ||
for foo in foos do do_something(foo) end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when each branch contains the same single loop over the same collection' do | ||
expect_no_offenses(<<~RUBY) | ||
if condition | ||
for item in items do do_something(item) end | ||
else | ||
for item in items do do_something_else(item, arg) end | ||
end | ||
RUBY | ||
end | ||
end | ||
end |