-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/RedundantWithIndex
cop (#4708)
- Loading branch information
Showing
8 changed files
with
167 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for redundant `with_index`. | ||
# | ||
# @example | ||
# # bad | ||
# ary.each_with_index do |v| | ||
# v | ||
# end | ||
# | ||
# # good | ||
# ary.each do |v| | ||
# v | ||
# end | ||
# | ||
# # bad | ||
# ary.each.with_index do |v| | ||
# v | ||
# end | ||
# | ||
# # good | ||
# ary.each do |v| | ||
# v | ||
# end | ||
# | ||
class RedundantWithIndex < Cop | ||
MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'.freeze | ||
MSG_WITH_INDEX = 'Remove redundant `with_index`.'.freeze | ||
|
||
def_node_matcher :redundant_with_index?, <<-PATTERN | ||
(block | ||
$(send | ||
_ {:each_with_index :with_index}) | ||
(args | ||
(arg _)) | ||
...) | ||
PATTERN | ||
|
||
def on_block(node) | ||
redundant_with_index?(node) do |send| | ||
add_offense(node, with_index_range(send)) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
redundant_with_index?(node) do |send| | ||
if send.method_name == :each_with_index | ||
corrector.replace(send.loc.selector, 'each') | ||
else | ||
corrector.remove(send.loc.selector) | ||
corrector.remove(send.loc.dot) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(node) | ||
if node.method_name == :each_with_index | ||
MSG_EACH_WITH_INDEX | ||
else | ||
MSG_WITH_INDEX | ||
end | ||
end | ||
|
||
def with_index_range(send) | ||
range_between(send.loc.selector.begin_pos, send.loc.selector.end_pos) | ||
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::Lint::RedundantWithIndex do | ||
let(:config) { RuboCop::Config.new } | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense when using `ary.each_with_index { |v| v }`' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
ary.each_with_index { |v| v } | ||
^^^^^^^^^^^^^^^ Use `each` instead of `each_with_index`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `ary.each.with_index { |v| v }`' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
ary.each.with_index { |v| v } | ||
^^^^^^^^^^ Remove redundant `with_index`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `ary.each_with_object([]).with_index ' \ | ||
'{ |v| v }`' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
ary.each_with_object([]).with_index { |v| v } | ||
^^^^^^^^^^ Remove redundant `with_index`. | ||
RUBY | ||
end | ||
|
||
it 'autocorrects to ary.each from ary.each_with_index' do | ||
new_source = autocorrect_source('ary.each_with_index { |v| v }') | ||
|
||
expect(new_source).to eq 'ary.each { |v| v }' | ||
end | ||
|
||
it 'autocorrects to ary.each from ary.each.with_index' do | ||
new_source = autocorrect_source('ary.each.with_index { |v| v }') | ||
|
||
expect(new_source).to eq 'ary.each { |v| v }' | ||
end | ||
|
||
it 'autocorrects to ary.each from ary.each_with_object([]).with_index' do | ||
new_source = autocorrect_source('ary.each_with_object([]) { |v| v }') | ||
|
||
expect(new_source).to eq 'ary.each_with_object([]) { |v| v }' | ||
end | ||
|
||
it 'an index is used as a block argument' do | ||
expect_no_offenses('ary.each_with_index { |v, i| v; i }') | ||
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