forked from rubocop/rubocop-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_semi_infinite_range_slice.rb
79 lines (68 loc) · 2.36 KB
/
array_semi_infinite_range_slice.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# This cop identifies places where slicing arrays with semi-infinite ranges
# can be replaced by `Array#take` and `Array#drop`.
# This cop was created due to a mistake in microbenchmark and hence is disabled by default.
# Refer https://github.com/rubocop/rubocop-performance/pull/175#issuecomment-731892717
#
# @safety
# This cop is unsafe for string slices because strings do not have `#take` and `#drop` methods.
#
# @example
# # bad
# array[..2]
# array[...2]
# array[2..]
# array[2...]
# array.slice(..2)
#
# # good
# array.take(3)
# array.take(2)
# array.drop(2)
# array.drop(2)
# array.take(3)
#
class ArraySemiInfiniteRangeSlice < Base
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion
minimum_target_ruby_version 2.7
MSG = 'Use `%<prefer>s` instead of `%<current>s` with semi-infinite range.'
SLICE_METHODS = Set[:[], :slice].freeze
RESTRICT_ON_SEND = SLICE_METHODS
def_node_matcher :endless_range_slice?, <<~PATTERN
(send $_ $%SLICE_METHODS $#endless_range?)
PATTERN
def_node_matcher :endless_range?, <<~PATTERN
{
({irange erange} nil? (int positive?))
({irange erange} (int positive?) nil?)
}
PATTERN
def on_send(node)
endless_range_slice?(node) do |receiver, method_name, range_node|
prefer = range_node.begin ? :drop : :take
message = format(MSG, prefer: prefer, current: method_name)
add_offense(node, message: message) do |corrector|
corrector.replace(node, correction(receiver, range_node))
end
end
end
private
def correction(receiver, range_node)
method_call = if range_node.begin
"drop(#{range_node.begin.value})"
elsif range_node.irange_type?
"take(#{range_node.end.value + 1})"
else
"take(#{range_node.end.value})"
end
"#{receiver.source}.#{method_call}"
end
end
end
end
end