From 1bfb387b76d53cde14ce0e73263361e48c45eaf8 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 1 Nov 2017 11:14:31 +0900 Subject: [PATCH] Make `Lint/RedundantWithIndex` cop aware of offset argument Follow up of https://github.com/bbatsov/rubocop/pull/4796#discussion_r141739242. Actually, it is only `with_index` method that accepts an offset argument. `with_with_index` method doesn't accept offset argument. However, I think that the same AST will be low maintenance cost. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/redundant_with_index.rb | 6 +++--- spec/rubocop/cop/lint/redundant_with_index_spec.rb | 13 +++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 465e48049ada..83a325a655b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [#4888](https://github.com/bbatsov/rubocop/pull/4888): Improve offense message of `Style/StderrPuts`. ([@jaredbeck][]) * [#4886](https://github.com/bbatsov/rubocop/issues/4886): Fix false offense for Style/CommentedKeyword. ([@michniewicz][]) +* [#4977](https://github.com/bbatsov/rubocop/pull/4977): Make `Lint/RedundantWithIndex` cop aware of offset argument. ([@koic][]) ### New features diff --git a/lib/rubocop/cop/lint/redundant_with_index.rb b/lib/rubocop/cop/lint/redundant_with_index.rb index 97497d19f90b..a072e4782c1b 100644 --- a/lib/rubocop/cop/lint/redundant_with_index.rb +++ b/lib/rubocop/cop/lint/redundant_with_index.rb @@ -33,7 +33,7 @@ class RedundantWithIndex < Cop def_node_matcher :redundant_with_index?, <<-PATTERN (block $(send - _ {:each_with_index :with_index}) + _ {:each_with_index :with_index} ...) (args (arg _)) ...) @@ -51,7 +51,7 @@ def autocorrect(node) if send.method_name == :each_with_index corrector.replace(send.loc.selector, 'each') else - corrector.remove(send.loc.selector) + corrector.remove(with_index_range(send)) corrector.remove(send.loc.dot) end end @@ -69,7 +69,7 @@ def message(node) end def with_index_range(send) - range_between(send.loc.selector.begin_pos, send.loc.selector.end_pos) + range_between(send.loc.selector.begin_pos, send.source.length) end end end diff --git a/spec/rubocop/cop/lint/redundant_with_index_spec.rb b/spec/rubocop/cop/lint/redundant_with_index_spec.rb index 54b3bce14a85..6611e17c1229 100644 --- a/spec/rubocop/cop/lint/redundant_with_index_spec.rb +++ b/spec/rubocop/cop/lint/redundant_with_index_spec.rb @@ -19,6 +19,13 @@ RUBY end + it 'registers an offense when using `ary.each.with_index(1) { |v| v }`' do + expect_offense(<<-RUBY.strip_indent) + ary.each.with_index(1) { |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) @@ -39,6 +46,12 @@ expect(new_source).to eq 'ary.each { |v| v }' end + it 'autocorrects to ary.each from ary.each.with_index(1)' do + new_source = autocorrect_source('ary.each.with_index(1) { |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 }')