rubocop-performance 1.15.0 → 1.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/performance/count.rb +38 -2
- data/lib/rubocop/cop/performance/map_compact.rb +5 -1
- data/lib/rubocop/cop/performance/redundant_string_chars.rb +7 -3
- data/lib/rubocop/cop/performance/regexp_match.rb +2 -2
- data/lib/rubocop/cop/performance/string_include.rb +8 -5
- data/lib/rubocop/cop/performance/sum.rb +3 -0
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ab8b6af1dd52db3d8f00efc6428d397474a9c06b9d950b2aa7afa29b2d7feb8e
|
|
4
|
+
data.tar.gz: 80102c530b212f682d6e48d1eff740446821a37757b2a94726669f41fd6a6978
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 36f622e84aec25aae177cbfee56b9c530c6843cbc547e960947d21ab46cc8df87774aa717b3daa75a41a823ef7c8836c7b6acd0c0f45415fc4b216a6901ef617
|
|
7
|
+
data.tar.gz: c46677e2f07fbd77f24690218b377b308299a8558c664cf21e75009a011638da6aaece36e89ba2c2b42b003ba310233d20bcac6fed5d1ae7d1f5d3f48ada47e0
|
|
@@ -79,12 +79,11 @@ module RuboCop
|
|
|
79
79
|
def autocorrect(corrector, node, selector_node, selector)
|
|
80
80
|
selector_loc = selector_node.loc.selector
|
|
81
81
|
|
|
82
|
-
return if selector == :reject
|
|
83
|
-
|
|
84
82
|
range = source_starting_at(node) { |n| n.loc.dot.begin_pos }
|
|
85
83
|
|
|
86
84
|
corrector.remove(range)
|
|
87
85
|
corrector.replace(selector_loc, 'count')
|
|
86
|
+
negate_reject(corrector, node) if selector == :reject
|
|
88
87
|
end
|
|
89
88
|
|
|
90
89
|
def eligible_node?(node)
|
|
@@ -100,6 +99,43 @@ module RuboCop
|
|
|
100
99
|
|
|
101
100
|
range_between(begin_pos, node.source_range.end_pos)
|
|
102
101
|
end
|
|
102
|
+
|
|
103
|
+
def negate_reject(corrector, node)
|
|
104
|
+
if node.receiver.send_type?
|
|
105
|
+
negate_block_pass_reject(corrector, node)
|
|
106
|
+
else
|
|
107
|
+
negate_block_reject(corrector, node)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def negate_block_pass_reject(corrector, node)
|
|
112
|
+
corrector.replace(
|
|
113
|
+
node.receiver.loc.expression.with(begin_pos: node.receiver.loc.begin.begin_pos),
|
|
114
|
+
negate_block_pass_as_inline_block(node.receiver)
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def negate_block_reject(corrector, node)
|
|
119
|
+
target =
|
|
120
|
+
if node.receiver.body.begin_type?
|
|
121
|
+
node.receiver.body.children.last
|
|
122
|
+
else
|
|
123
|
+
node.receiver.body
|
|
124
|
+
end
|
|
125
|
+
corrector.replace(target, negate_expression(target))
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def negate_expression(node)
|
|
129
|
+
"!(#{node.source})"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def negate_block_pass_as_inline_block(node)
|
|
133
|
+
if node.last_argument.children.first.sym_type?
|
|
134
|
+
" { |element| !element.#{node.last_argument.children.first.value} }"
|
|
135
|
+
else
|
|
136
|
+
" { !#{node.last_argument.children.first.source}.call }"
|
|
137
|
+
end
|
|
138
|
+
end
|
|
103
139
|
end
|
|
104
140
|
end
|
|
105
141
|
end
|
|
@@ -67,7 +67,7 @@ module RuboCop
|
|
|
67
67
|
def remove_compact_method(corrector, map_node, compact_node, chained_method)
|
|
68
68
|
compact_method_range = compact_node.loc.selector
|
|
69
69
|
|
|
70
|
-
if compact_node.multiline? && chained_method&.loc.respond_to?(:selector) && chained_method
|
|
70
|
+
if compact_node.multiline? && chained_method&.loc.respond_to?(:selector) && use_dot?(chained_method) &&
|
|
71
71
|
!map_method_and_compact_method_on_same_line?(map_node, compact_node) &&
|
|
72
72
|
!invoke_method_after_map_compact_on_same_line?(compact_node, chained_method)
|
|
73
73
|
compact_method_range = compact_method_with_final_newline_range(compact_method_range)
|
|
@@ -78,6 +78,10 @@ module RuboCop
|
|
|
78
78
|
corrector.remove(compact_method_range)
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
+
def use_dot?(node)
|
|
82
|
+
node.respond_to?(:dot?) && node.dot?
|
|
83
|
+
end
|
|
84
|
+
|
|
81
85
|
def map_method_and_compact_method_on_same_line?(map_node, compact_node)
|
|
82
86
|
compact_node.loc.selector.line == map_node.loc.selector.line
|
|
83
87
|
end
|
|
@@ -9,6 +9,7 @@ module RuboCop
|
|
|
9
9
|
# # bad
|
|
10
10
|
# str.chars[0..2]
|
|
11
11
|
# str.chars.slice(0..2)
|
|
12
|
+
# str.chars.last
|
|
12
13
|
#
|
|
13
14
|
# # good
|
|
14
15
|
# str[0..2].chars
|
|
@@ -20,6 +21,7 @@ module RuboCop
|
|
|
20
21
|
# # good
|
|
21
22
|
# str[0]
|
|
22
23
|
# str[0...2].chars
|
|
24
|
+
# str[-1]
|
|
23
25
|
#
|
|
24
26
|
# # bad
|
|
25
27
|
# str.chars.take(2)
|
|
@@ -33,9 +35,8 @@ module RuboCop
|
|
|
33
35
|
# str.size
|
|
34
36
|
# str.empty?
|
|
35
37
|
#
|
|
36
|
-
# # For example, if the receiver is
|
|
38
|
+
# # For example, if the receiver is an empty string, it will be incompatible.
|
|
37
39
|
# # If a negative value is specified for the receiver, `nil` is returned.
|
|
38
|
-
# str.chars.last # Incompatible with `str[-1]`.
|
|
39
40
|
# str.chars.last(2) # Incompatible with `str[-2..-1].chars`.
|
|
40
41
|
# str.chars.drop(2) # Incompatible with `str[2..-1].chars`.
|
|
41
42
|
#
|
|
@@ -44,7 +45,7 @@ module RuboCop
|
|
|
44
45
|
extend AutoCorrector
|
|
45
46
|
|
|
46
47
|
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
|
|
47
|
-
RESTRICT_ON_SEND = %i[[] slice first take length size empty?].freeze
|
|
48
|
+
RESTRICT_ON_SEND = %i[[] slice first last take length size empty?].freeze
|
|
48
49
|
|
|
49
50
|
def_node_matcher :redundant_chars_call?, <<~PATTERN
|
|
50
51
|
(send $(send _ :chars) $_ $...)
|
|
@@ -52,6 +53,7 @@ module RuboCop
|
|
|
52
53
|
|
|
53
54
|
def on_send(node)
|
|
54
55
|
return unless (receiver, method, args = redundant_chars_call?(node))
|
|
56
|
+
return if method == :last && !args.empty?
|
|
55
57
|
|
|
56
58
|
range = offense_range(receiver, node)
|
|
57
59
|
message = build_message(method, args)
|
|
@@ -86,6 +88,8 @@ module RuboCop
|
|
|
86
88
|
"[#{build_call_args(args)}].chars"
|
|
87
89
|
when :[], :first
|
|
88
90
|
build_good_method_for_brackets_or_first_method(method, args)
|
|
91
|
+
when :last
|
|
92
|
+
'[-1]'
|
|
89
93
|
when :take
|
|
90
94
|
"[0...#{args.first.source}].chars"
|
|
91
95
|
else
|
|
@@ -124,8 +124,8 @@ module RuboCop
|
|
|
124
124
|
|
|
125
125
|
def_node_search :last_matches, <<~PATTERN
|
|
126
126
|
{
|
|
127
|
-
(send (const nil? :Regexp) :last_match)
|
|
128
|
-
(send (const nil? :Regexp) :last_match _)
|
|
127
|
+
(send (const {nil? cbase} :Regexp) :last_match)
|
|
128
|
+
(send (const {nil? cbase} :Regexp) :last_match _)
|
|
129
129
|
({back_ref nth_ref} _)
|
|
130
130
|
(gvar #match_gvar?)
|
|
131
131
|
}
|
|
@@ -22,11 +22,11 @@ module RuboCop
|
|
|
22
22
|
class StringInclude < Base
|
|
23
23
|
extend AutoCorrector
|
|
24
24
|
|
|
25
|
-
MSG = 'Use
|
|
26
|
-
RESTRICT_ON_SEND = %i[match =~ match?].freeze
|
|
25
|
+
MSG = 'Use `%<negation>sString#include?` instead of a regex match with literal-only pattern.'
|
|
26
|
+
RESTRICT_ON_SEND = %i[match =~ !~ match?].freeze
|
|
27
27
|
|
|
28
28
|
def_node_matcher :redundant_regex?, <<~PATTERN
|
|
29
|
-
{(send $!nil? {:match :=~ :match?} (regexp (str $#literal?) (regopt)))
|
|
29
|
+
{(send $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt)))
|
|
30
30
|
(send (regexp (str $#literal?) (regopt)) {:match :match?} $str)
|
|
31
31
|
(match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)}
|
|
32
32
|
PATTERN
|
|
@@ -34,11 +34,14 @@ module RuboCop
|
|
|
34
34
|
def on_send(node)
|
|
35
35
|
return unless (receiver, regex_str = redundant_regex?(node))
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
negation = node.send_type? && node.method?(:!~)
|
|
38
|
+
message = format(MSG, negation: ('!' if negation))
|
|
39
|
+
|
|
40
|
+
add_offense(node, message: message) do |corrector|
|
|
38
41
|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
|
|
39
42
|
regex_str = interpret_string_escapes(regex_str)
|
|
40
43
|
|
|
41
|
-
new_source = "#{receiver.source}.include?(#{to_string_literal(regex_str)})"
|
|
44
|
+
new_source = "#{'!' if negation}#{receiver.source}.include?(#{to_string_literal(regex_str)})"
|
|
42
45
|
|
|
43
46
|
corrector.replace(node.source_range, new_source)
|
|
44
47
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-performance
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.15.
|
|
4
|
+
version: 1.15.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bozhidar Batsov
|
|
@@ -10,7 +10,7 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date: 2022-
|
|
13
|
+
date: 2022-12-25 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: rubocop
|
|
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
141
141
|
- !ruby/object:Gem::Version
|
|
142
142
|
version: '0'
|
|
143
143
|
requirements: []
|
|
144
|
-
rubygems_version: 3.
|
|
144
|
+
rubygems_version: 3.4.0.dev
|
|
145
145
|
signing_key:
|
|
146
146
|
specification_version: 4
|
|
147
147
|
summary: Automatic performance checking tool for Ruby code.
|