rubocop-performance 1.6.0 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +1 -1
- data/config/default.yml +77 -10
- data/lib/rubocop/cop/mixin/regexp_metacharacter.rb +39 -4
- data/lib/rubocop/cop/mixin/sort_block.rb +28 -0
- data/lib/rubocop/cop/performance/ancestors_include.rb +48 -0
- data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +45 -0
- data/lib/rubocop/cop/performance/bind_call.rb +8 -18
- data/lib/rubocop/cop/performance/caller.rb +3 -2
- data/lib/rubocop/cop/performance/case_when_splat.rb +18 -11
- data/lib/rubocop/cop/performance/casecmp.rb +12 -20
- data/lib/rubocop/cop/performance/chain_array_allocation.rb +4 -10
- data/lib/rubocop/cop/performance/collection_literal_in_loop.rb +140 -0
- data/lib/rubocop/cop/performance/compare_with_block.rb +10 -21
- data/lib/rubocop/cop/performance/count.rb +13 -16
- data/lib/rubocop/cop/performance/delete_prefix.rb +43 -28
- data/lib/rubocop/cop/performance/delete_suffix.rb +43 -28
- data/lib/rubocop/cop/performance/detect.rb +63 -31
- data/lib/rubocop/cop/performance/double_start_end_with.rb +16 -24
- data/lib/rubocop/cop/performance/end_with.rb +29 -17
- data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
- data/lib/rubocop/cop/performance/flat_map.rb +20 -22
- data/lib/rubocop/cop/performance/inefficient_hash_search.rb +13 -14
- data/lib/rubocop/cop/performance/io_readlines.rb +116 -0
- data/lib/rubocop/cop/performance/open_struct.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +14 -11
- data/lib/rubocop/cop/performance/redundant_block_call.rb +11 -6
- data/lib/rubocop/cop/performance/redundant_match.rb +11 -6
- data/lib/rubocop/cop/performance/redundant_merge.rb +18 -17
- data/lib/rubocop/cop/performance/redundant_sort_block.rb +43 -0
- data/lib/rubocop/cop/performance/redundant_string_chars.rb +133 -0
- data/lib/rubocop/cop/performance/regexp_match.rb +20 -20
- data/lib/rubocop/cop/performance/reverse_each.rb +9 -5
- data/lib/rubocop/cop/performance/reverse_first.rb +72 -0
- data/lib/rubocop/cop/performance/size.rb +41 -43
- data/lib/rubocop/cop/performance/sort_reverse.rb +45 -0
- data/lib/rubocop/cop/performance/squeeze.rb +66 -0
- data/lib/rubocop/cop/performance/start_with.rb +29 -17
- data/lib/rubocop/cop/performance/string_include.rb +55 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +23 -27
- data/lib/rubocop/cop/performance/sum.rb +134 -0
- data/lib/rubocop/cop/performance/times_map.rb +11 -18
- data/lib/rubocop/cop/performance/unfreeze_string.rb +2 -2
- data/lib/rubocop/cop/performance/uri_default_parser.rb +6 -12
- data/lib/rubocop/cop/performance_cops.rb +12 -0
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +33 -8
@@ -36,7 +36,9 @@ module RuboCop
|
|
36
36
|
# { a: 1, b: 2 }.has_value?('garbage')
|
37
37
|
# h = { a: 1, b: 2 }; h.value?(nil)
|
38
38
|
#
|
39
|
-
class InefficientHashSearch <
|
39
|
+
class InefficientHashSearch < Base
|
40
|
+
extend AutoCorrector
|
41
|
+
|
40
42
|
def_node_matcher :inefficient_include?, <<~PATTERN
|
41
43
|
(send (send $_ {:keys :values}) :include? _)
|
42
44
|
PATTERN
|
@@ -45,19 +47,16 @@ module RuboCop
|
|
45
47
|
inefficient_include?(node) do |receiver|
|
46
48
|
return if receiver.nil?
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
"#{autocorrect_hash_expression(node)}."\
|
59
|
-
"#{autocorrect_method(node)}(#{autocorrect_argument(node)})"
|
60
|
-
)
|
50
|
+
message = message(node)
|
51
|
+
add_offense(node, message: message) do |corrector|
|
52
|
+
# Replace `keys.include?` or `values.include?` with the appropriate
|
53
|
+
# `key?`/`value?` method.
|
54
|
+
corrector.replace(
|
55
|
+
node.loc.expression,
|
56
|
+
"#{autocorrect_hash_expression(node)}."\
|
57
|
+
"#{autocorrect_method(node)}(#{autocorrect_argument(node)})"
|
58
|
+
)
|
59
|
+
end
|
61
60
|
end
|
62
61
|
end
|
63
62
|
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Performance
|
6
|
+
# This cop identifies places where inefficient `readlines` method
|
7
|
+
# can be replaced by `each_line` to avoid fully loading file content into memory.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# File.readlines('testfile').each { |l| puts l }
|
13
|
+
# IO.readlines('testfile', chomp: true).each { |l| puts l }
|
14
|
+
#
|
15
|
+
# conn.readlines(10).map { |l| l.size }
|
16
|
+
# file.readlines.find { |l| l.start_with?('#') }
|
17
|
+
# file.readlines.each { |l| puts l }
|
18
|
+
#
|
19
|
+
# # good
|
20
|
+
# File.open('testfile', 'r').each_line { |l| puts l }
|
21
|
+
# IO.open('testfile').each_line(chomp: true) { |l| puts l }
|
22
|
+
#
|
23
|
+
# conn.each_line(10).map { |l| l.size }
|
24
|
+
# file.each_line.find { |l| l.start_with?('#') }
|
25
|
+
# file.each_line { |l| puts l }
|
26
|
+
#
|
27
|
+
class IoReadlines < Base
|
28
|
+
include RangeHelp
|
29
|
+
extend AutoCorrector
|
30
|
+
|
31
|
+
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
32
|
+
ENUMERABLE_METHODS = (Enumerable.instance_methods + [:each]).freeze
|
33
|
+
|
34
|
+
def_node_matcher :readlines_on_class?, <<~PATTERN
|
35
|
+
$(send $(send (const nil? {:IO :File}) :readlines ...) #enumerable_method?)
|
36
|
+
PATTERN
|
37
|
+
|
38
|
+
def_node_matcher :readlines_on_instance?, <<~PATTERN
|
39
|
+
$(send $(send ${nil? !const_type?} :readlines ...) #enumerable_method? ...)
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def on_send(node)
|
43
|
+
return unless (captured_values = readlines_on_class?(node) || readlines_on_instance?(node))
|
44
|
+
|
45
|
+
enumerable_call, readlines_call, receiver = *captured_values
|
46
|
+
|
47
|
+
range = offense_range(enumerable_call, readlines_call)
|
48
|
+
good_method = build_good_method(enumerable_call)
|
49
|
+
bad_method = build_bad_method(enumerable_call)
|
50
|
+
|
51
|
+
add_offense(range, message: format(MSG, good: good_method, bad: bad_method)) do |corrector|
|
52
|
+
autocorrect(corrector, enumerable_call, readlines_call, receiver)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def enumerable_method?(node)
|
59
|
+
ENUMERABLE_METHODS.include?(node.to_sym)
|
60
|
+
end
|
61
|
+
|
62
|
+
def autocorrect(corrector, enumerable_call, readlines_call, receiver)
|
63
|
+
# We cannot safely correct `.readlines` method called on IO/File classes
|
64
|
+
# due to its signature and we are not sure with implicit receiver
|
65
|
+
# if it is called in the context of some instance or mentioned class.
|
66
|
+
return if receiver.nil?
|
67
|
+
|
68
|
+
range = correction_range(enumerable_call, readlines_call)
|
69
|
+
|
70
|
+
if readlines_call.arguments?
|
71
|
+
call_args = build_call_args(readlines_call.arguments)
|
72
|
+
replacement = "each_line(#{call_args})"
|
73
|
+
else
|
74
|
+
replacement = 'each_line'
|
75
|
+
end
|
76
|
+
|
77
|
+
corrector.replace(range, replacement)
|
78
|
+
end
|
79
|
+
|
80
|
+
def offense_range(enumerable_call, readlines_call)
|
81
|
+
readlines_pos = readlines_call.loc.selector.begin_pos
|
82
|
+
enumerable_pos = enumerable_call.loc.selector.end_pos
|
83
|
+
range_between(readlines_pos, enumerable_pos)
|
84
|
+
end
|
85
|
+
|
86
|
+
def build_good_method(enumerable_call)
|
87
|
+
if enumerable_call.method?(:each)
|
88
|
+
'each_line'
|
89
|
+
else
|
90
|
+
"each_line.#{enumerable_call.method_name}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def build_bad_method(enumerable_call)
|
95
|
+
"readlines.#{enumerable_call.method_name}"
|
96
|
+
end
|
97
|
+
|
98
|
+
def correction_range(enumerable_call, readlines_call)
|
99
|
+
begin_pos = readlines_call.loc.selector.begin_pos
|
100
|
+
|
101
|
+
end_pos = if enumerable_call.method?(:each)
|
102
|
+
enumerable_call.loc.expression.end_pos
|
103
|
+
else
|
104
|
+
enumerable_call.loc.dot.begin_pos
|
105
|
+
end
|
106
|
+
|
107
|
+
range_between(begin_pos, end_pos)
|
108
|
+
end
|
109
|
+
|
110
|
+
def build_call_args(call_args_node)
|
111
|
+
call_args_node.map(&:source).join(', ')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -27,7 +27,7 @@ module RuboCop
|
|
27
27
|
# end
|
28
28
|
# end
|
29
29
|
#
|
30
|
-
class OpenStruct <
|
30
|
+
class OpenStruct < Base
|
31
31
|
MSG = 'Consider using `Struct` over `OpenStruct` ' \
|
32
32
|
'to optimize the performance.'
|
33
33
|
|
@@ -37,7 +37,7 @@ module RuboCop
|
|
37
37
|
|
38
38
|
def on_send(node)
|
39
39
|
open_struct(node) do
|
40
|
-
add_offense(node
|
40
|
+
add_offense(node.loc.selector)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -3,18 +3,19 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Performance
|
6
|
-
# This cop identifies uses of `Range#include?`, which iterates over each
|
6
|
+
# This cop identifies uses of `Range#include?` and `Range#member?`, which iterates over each
|
7
7
|
# item in a `Range` to see if a specified item is there. In contrast,
|
8
8
|
# `Range#cover?` simply compares the target item with the beginning and
|
9
9
|
# end points of the `Range`. In a great majority of cases, this is what
|
10
10
|
# is wanted.
|
11
11
|
#
|
12
|
-
# This cop is `Safe: false` by default because `Range#include?` and
|
12
|
+
# This cop is `Safe: false` by default because `Range#include?` (or `Range#member?`) and
|
13
13
|
# `Range#cover?` are not equivalent behaviour.
|
14
14
|
#
|
15
15
|
# @example
|
16
16
|
# # bad
|
17
17
|
# ('a'..'z').include?('b') # => true
|
18
|
+
# ('a'..'z').member?('b') # => true
|
18
19
|
#
|
19
20
|
# # good
|
20
21
|
# ('a'..'z').cover?('b') # => true
|
@@ -23,8 +24,10 @@ module RuboCop
|
|
23
24
|
# # the desired result:
|
24
25
|
#
|
25
26
|
# ('a'..'z').cover?('yellow') # => true
|
26
|
-
class RangeInclude <
|
27
|
-
|
27
|
+
class RangeInclude < Base
|
28
|
+
extend AutoCorrector
|
29
|
+
|
30
|
+
MSG = 'Use `Range#cover?` instead of `Range#%<bad_method>s`.'
|
28
31
|
|
29
32
|
# TODO: If we traced out assignments of variables to their uses, we
|
30
33
|
# might pick up on a few more instances of this issue
|
@@ -32,17 +35,17 @@ module RuboCop
|
|
32
35
|
# (We don't even catch it if the Range is in double parens)
|
33
36
|
|
34
37
|
def_node_matcher :range_include, <<~PATTERN
|
35
|
-
(send {irange erange (begin {irange erange})} :include? ...)
|
38
|
+
(send {irange erange (begin {irange erange})} ${:include? :member?} ...)
|
36
39
|
PATTERN
|
37
40
|
|
38
41
|
def on_send(node)
|
39
|
-
|
40
|
-
|
41
|
-
add_offense(node, location: :selector)
|
42
|
-
end
|
42
|
+
range_include(node) do |bad_method|
|
43
|
+
message = format(MSG, bad_method: bad_method)
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
add_offense(node.loc.selector, message: message) do |corrector|
|
46
|
+
corrector.replace(node.loc.selector, 'cover?')
|
47
|
+
end
|
48
|
+
end
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
@@ -22,7 +22,9 @@ module RuboCop
|
|
22
22
|
# def another
|
23
23
|
# yield 1, 2, 3
|
24
24
|
# end
|
25
|
-
class RedundantBlockCall <
|
25
|
+
class RedundantBlockCall < Base
|
26
|
+
extend AutoCorrector
|
27
|
+
|
26
28
|
MSG = 'Use `yield` instead of `%<argname>s.call`.'
|
27
29
|
YIELD = 'yield'
|
28
30
|
OPEN_PAREN = '('
|
@@ -47,13 +49,17 @@ module RuboCop
|
|
47
49
|
next unless body
|
48
50
|
|
49
51
|
calls_to_report(argname, body).each do |blockcall|
|
50
|
-
add_offense(blockcall, message: format(MSG, argname: argname))
|
52
|
+
add_offense(blockcall, message: format(MSG, argname: argname)) do |corrector|
|
53
|
+
autocorrect(corrector, blockcall)
|
54
|
+
end
|
51
55
|
end
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
59
|
+
private
|
60
|
+
|
55
61
|
# offenses are registered on the `block.call` nodes
|
56
|
-
def autocorrect(node)
|
62
|
+
def autocorrect(corrector, node)
|
57
63
|
_receiver, _method, *args = *node
|
58
64
|
new_source = String.new(YIELD)
|
59
65
|
unless args.empty?
|
@@ -67,10 +73,9 @@ module RuboCop
|
|
67
73
|
end
|
68
74
|
|
69
75
|
new_source << CLOSE_PAREN if parentheses?(node) && !args.empty?
|
70
|
-
->(corrector) { corrector.replace(node.source_range, new_source) }
|
71
|
-
end
|
72
76
|
|
73
|
-
|
77
|
+
corrector.replace(node.source_range, new_source)
|
78
|
+
end
|
74
79
|
|
75
80
|
def calls_to_report(argname, body)
|
76
81
|
return [] if blockarg_assigned?(body, argname)
|
@@ -17,7 +17,9 @@ module RuboCop
|
|
17
17
|
# # good
|
18
18
|
# method(str =~ /regex/)
|
19
19
|
# return value unless regex =~ 'str'
|
20
|
-
class RedundantMatch <
|
20
|
+
class RedundantMatch < Base
|
21
|
+
extend AutoCorrector
|
22
|
+
|
21
23
|
MSG = 'Use `=~` in places where the `MatchData` returned by ' \
|
22
24
|
'`#match` will not be used.'
|
23
25
|
|
@@ -37,18 +39,21 @@ module RuboCop
|
|
37
39
|
(!node.value_used? || only_truthiness_matters?(node)) &&
|
38
40
|
!(node.parent && node.parent.block_type?)
|
39
41
|
|
40
|
-
add_offense(node)
|
42
|
+
add_offense(node) do |corrector|
|
43
|
+
autocorrect(corrector, node)
|
44
|
+
end
|
41
45
|
end
|
42
46
|
|
43
|
-
|
47
|
+
private
|
48
|
+
|
49
|
+
def autocorrect(corrector, node)
|
44
50
|
# Regexp#match can take a second argument, but this cop doesn't
|
45
51
|
# register an offense in that case
|
46
52
|
return unless node.first_argument.regexp_type?
|
47
53
|
|
48
|
-
new_source =
|
49
|
-
node.receiver.source + ' =~ ' + node.first_argument.source
|
54
|
+
new_source = "#{node.receiver.source} =~ #{node.first_argument.source}"
|
50
55
|
|
51
|
-
|
56
|
+
corrector.replace(node.source_range, new_source)
|
52
57
|
end
|
53
58
|
end
|
54
59
|
end
|
@@ -24,7 +24,9 @@ module RuboCop
|
|
24
24
|
# # good
|
25
25
|
# hash[:a] = 1
|
26
26
|
# hash[:b] = 2
|
27
|
-
class RedundantMerge <
|
27
|
+
class RedundantMerge < Base
|
28
|
+
extend AutoCorrector
|
29
|
+
|
28
30
|
AREF_ASGN = '%<receiver>s[%<key>s] = %<value>s'
|
29
31
|
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
|
30
32
|
|
@@ -44,18 +46,17 @@ module RuboCop
|
|
44
46
|
|
45
47
|
def on_send(node)
|
46
48
|
each_redundant_merge(node) do |redundant_merge_node|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
correct_single_element(node, new_source)
|
49
|
+
message = message(node)
|
50
|
+
add_offense(redundant_merge_node, message: message) do |corrector|
|
51
|
+
redundant_merge_candidate(node) do |receiver, pairs|
|
52
|
+
new_source = to_assignments(receiver, pairs).join("\n")
|
53
|
+
|
54
|
+
if node.parent && pairs.size > 1
|
55
|
+
correct_multiple_elements(corrector, node, node.parent, new_source)
|
56
|
+
else
|
57
|
+
correct_single_element(corrector, node, new_source)
|
58
|
+
end
|
59
|
+
end
|
59
60
|
end
|
60
61
|
end
|
61
62
|
end
|
@@ -98,7 +99,7 @@ module RuboCop
|
|
98
99
|
!EachWithObjectInspector.new(node, receiver).value_used?
|
99
100
|
end
|
100
101
|
|
101
|
-
def correct_multiple_elements(node, parent, new_source)
|
102
|
+
def correct_multiple_elements(corrector, node, parent, new_source)
|
102
103
|
if modifier_flow_control?(parent)
|
103
104
|
new_source = rewrite_with_modifier(node, parent, new_source)
|
104
105
|
node = parent
|
@@ -107,11 +108,11 @@ module RuboCop
|
|
107
108
|
new_source.gsub!(/\n/, padding)
|
108
109
|
end
|
109
110
|
|
110
|
-
|
111
|
+
corrector.replace(node.source_range, new_source)
|
111
112
|
end
|
112
113
|
|
113
|
-
def correct_single_element(node, new_source)
|
114
|
-
|
114
|
+
def correct_single_element(corrector, node, new_source)
|
115
|
+
corrector.replace(node.source_range, new_source)
|
115
116
|
end
|
116
117
|
|
117
118
|
def to_assignments(receiver, pairs)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Performance
|
6
|
+
# This cop identifies places where `sort { |a, b| a <=> b }`
|
7
|
+
# can be replaced with `sort`.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# array.sort { |a, b| a <=> b }
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# array.sort
|
15
|
+
#
|
16
|
+
class RedundantSortBlock < Base
|
17
|
+
include SortBlock
|
18
|
+
extend AutoCorrector
|
19
|
+
|
20
|
+
MSG = 'Use `sort` instead of `%<bad_method>s`.'
|
21
|
+
|
22
|
+
def on_block(node)
|
23
|
+
return unless (send, var_a, var_b, body = sort_with_block?(node))
|
24
|
+
|
25
|
+
replaceable_body?(body, var_a, var_b) do
|
26
|
+
range = sort_range(send, node)
|
27
|
+
|
28
|
+
add_offense(range, message: message(var_a, var_b)) do |corrector|
|
29
|
+
corrector.replace(range, 'sort')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def message(var_a, var_b)
|
37
|
+
bad_method = "sort { |#{var_a}, #{var_b}| #{var_a} <=> #{var_b} }"
|
38
|
+
format(MSG, bad_method: bad_method)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Performance
|
6
|
+
# This cop checks for redundant `String#chars`.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# str.chars[0..2]
|
11
|
+
# str.chars.slice(0..2)
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# str[0..2].chars
|
15
|
+
#
|
16
|
+
# # bad
|
17
|
+
# str.chars.first
|
18
|
+
# str.chars.first(2)
|
19
|
+
# str.chars.last
|
20
|
+
# str.chars.last(2)
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# str[0]
|
24
|
+
# str[0...2].chars
|
25
|
+
# str[-1]
|
26
|
+
# str[-2..-1].chars
|
27
|
+
#
|
28
|
+
# # bad
|
29
|
+
# str.chars.take(2)
|
30
|
+
# str.chars.drop(2)
|
31
|
+
# str.chars.length
|
32
|
+
# str.chars.size
|
33
|
+
# str.chars.empty?
|
34
|
+
#
|
35
|
+
# # good
|
36
|
+
# str[0...2].chars
|
37
|
+
# str[2..-1].chars
|
38
|
+
# str.length
|
39
|
+
# str.size
|
40
|
+
# str.empty?
|
41
|
+
#
|
42
|
+
class RedundantStringChars < Base
|
43
|
+
include RangeHelp
|
44
|
+
extend AutoCorrector
|
45
|
+
|
46
|
+
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
|
47
|
+
REPLACEABLE_METHODS = %i[[] slice first last take drop length size empty?].freeze
|
48
|
+
|
49
|
+
def_node_matcher :redundant_chars_call?, <<~PATTERN
|
50
|
+
(send $(send _ :chars) $#replaceable_method? $...)
|
51
|
+
PATTERN
|
52
|
+
|
53
|
+
def on_send(node)
|
54
|
+
return unless (receiver, method, args = redundant_chars_call?(node))
|
55
|
+
|
56
|
+
range = offense_range(receiver, node)
|
57
|
+
message = build_message(method, args)
|
58
|
+
|
59
|
+
add_offense(range, message: message) do |corrector|
|
60
|
+
range = correction_range(receiver, node)
|
61
|
+
replacement = build_good_method(method, args)
|
62
|
+
|
63
|
+
corrector.replace(range, replacement)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def replaceable_method?(method_name)
|
70
|
+
REPLACEABLE_METHODS.include?(method_name)
|
71
|
+
end
|
72
|
+
|
73
|
+
def offense_range(receiver, node)
|
74
|
+
range_between(receiver.loc.selector.begin_pos, node.loc.expression.end_pos)
|
75
|
+
end
|
76
|
+
|
77
|
+
def correction_range(receiver, node)
|
78
|
+
range_between(receiver.loc.dot.begin_pos, node.loc.expression.end_pos)
|
79
|
+
end
|
80
|
+
|
81
|
+
def build_message(method, args)
|
82
|
+
good_method = build_good_method(method, args)
|
83
|
+
bad_method = build_bad_method(method, args)
|
84
|
+
format(MSG, good_method: good_method, bad_method: bad_method)
|
85
|
+
end
|
86
|
+
|
87
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
88
|
+
def build_good_method(method, args)
|
89
|
+
case method
|
90
|
+
when :[], :slice
|
91
|
+
"[#{build_call_args(args)}].chars"
|
92
|
+
when :first
|
93
|
+
if args.any?
|
94
|
+
"[0...#{args.first.source}].chars"
|
95
|
+
else
|
96
|
+
'[0]'
|
97
|
+
end
|
98
|
+
when :last
|
99
|
+
if args.any?
|
100
|
+
"[-#{args.first.source}..-1].chars"
|
101
|
+
else
|
102
|
+
'[-1]'
|
103
|
+
end
|
104
|
+
when :take
|
105
|
+
"[0...#{args.first.source}].chars"
|
106
|
+
when :drop
|
107
|
+
"[#{args.first.source}..-1].chars"
|
108
|
+
else
|
109
|
+
".#{method}"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
113
|
+
|
114
|
+
def build_bad_method(method, args)
|
115
|
+
case method
|
116
|
+
when :[]
|
117
|
+
"chars[#{build_call_args(args)}]"
|
118
|
+
else
|
119
|
+
if args.any?
|
120
|
+
"chars.#{method}(#{build_call_args(args)})"
|
121
|
+
else
|
122
|
+
"chars.#{method}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def build_call_args(call_args_node)
|
128
|
+
call_args_node.map(&:source).join(', ')
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|