-
-
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.
Implement Style/SlicingWithRange cop
Under Ruby >= 2.6, proposes to change ary[x..-1] to ary[x..]. Supports autocorrect.
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks that arrays are sliced with endless ranges instead of | ||
# `ary[start..-1]` on Ruby 2.6+. | ||
# | ||
# @example | ||
# # bad | ||
# items[1..-1] | ||
# | ||
# # good | ||
# items[1..] | ||
class SlicingWithRange < Cop | ||
extend TargetRubyVersion | ||
|
||
minimum_target_ruby_version 2.6 | ||
|
||
MSG = 'Prefer ary[n..] over ary[n..-1].' | ||
|
||
def_node_matcher :range_till_minus_one?, '(irange (int _) (int -1))' | ||
|
||
def on_send(node) | ||
return unless node.method?(:[]) && node.arguments.count == 1 | ||
return unless range_till_minus_one?(node.arguments.first) | ||
|
||
add_offense(node.arguments.first) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.remove(node.end) | ||
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::SlicingWithRange, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context '<= Ruby 2.5', :ruby25 do | ||
it 'reports no offense for array slicing with -1' do | ||
expect_no_offenses(<<~RUBY) | ||
ary[1..-1] | ||
RUBY | ||
end | ||
end | ||
|
||
context '>= Ruby 2.6', :ruby26 do | ||
it 'reports an offense for slicing to ..-1' do | ||
expect_offense(<<~RUBY) | ||
ary[1..-1] | ||
^^^^^ Prefer ary[n..] over ary[n..-1]. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
ary[1..] | ||
RUBY | ||
end | ||
|
||
it 'reports no offense for excluding end' do | ||
expect_no_offenses(<<~RUBY) | ||
ary[1...-1] | ||
RUBY | ||
end | ||
|
||
it 'reports no offense for other methods' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.push(1..-1) | ||
RUBY | ||
end | ||
|
||
it 'reports no offense for array with range inside' do | ||
expect_no_offenses(<<~RUBY) | ||
ranges = [1..-1] | ||
RUBY | ||
end | ||
end | ||
|
||
context '>= Ruby 2.7', :ruby27 do | ||
it 'reports no offense for startless' do | ||
expect_no_offenses(<<~RUBY) | ||
ary[..-1] | ||
RUBY | ||
end | ||
end | ||
end |