rubocop-performance 1.18.0 → 1.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/config/default.yml +24 -17
- data/lib/rubocop/cop/mixin/sort_block.rb +2 -2
- data/lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb +2 -1
- data/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +4 -0
- data/lib/rubocop/cop/performance/casecmp.rb +6 -0
- data/lib/rubocop/cop/performance/chain_array_allocation.rb +11 -4
- data/lib/rubocop/cop/performance/count.rb +5 -5
- data/lib/rubocop/cop/performance/delete_prefix.rb +5 -2
- data/lib/rubocop/cop/performance/delete_suffix.rb +5 -2
- data/lib/rubocop/cop/performance/detect.rb +3 -2
- data/lib/rubocop/cop/performance/end_with.rb +6 -3
- data/lib/rubocop/cop/performance/fixed_size.rb +4 -3
- data/lib/rubocop/cop/performance/flat_map.rb +4 -3
- data/lib/rubocop/cop/performance/inefficient_hash_search.rb +16 -10
- data/lib/rubocop/cop/performance/map_compact.rb +5 -4
- data/lib/rubocop/cop/performance/map_method_chain.rb +89 -0
- data/lib/rubocop/cop/performance/range_include.rb +8 -6
- data/lib/rubocop/cop/performance/redundant_block_call.rb +2 -0
- data/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_match.rb +30 -1
- data/lib/rubocop/cop/performance/redundant_merge.rb +5 -3
- data/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb +2 -1
- data/lib/rubocop/cop/performance/regexp_match.rb +1 -8
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
- data/lib/rubocop/cop/performance/reverse_first.rb +6 -13
- data/lib/rubocop/cop/performance/select_map.rb +3 -6
- data/lib/rubocop/cop/performance/size.rb +4 -3
- data/lib/rubocop/cop/performance/sort_reverse.rb +5 -4
- data/lib/rubocop/cop/performance/squeeze.rb +5 -2
- data/lib/rubocop/cop/performance/start_with.rb +6 -3
- data/lib/rubocop/cop/performance/string_identifier_argument.rb +58 -10
- data/lib/rubocop/cop/performance/string_include.rb +11 -5
- data/lib/rubocop/cop/performance/string_replacement.rb +2 -1
- data/lib/rubocop/cop/performance/sum.rb +11 -9
- data/lib/rubocop/cop/performance/times_map.rb +14 -2
- data/lib/rubocop/cop/performance/unfreeze_string.rb +6 -3
- data/lib/rubocop/cop/performance/uri_default_parser.rb +1 -1
- data/lib/rubocop/cop/performance_cops.rb +1 -0
- data/lib/rubocop/performance/version.rb +1 -1
- data/lib/rubocop-performance.rb +8 -0
- metadata +15 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c00ee718f283199f3c4f53b54071998b356f7239edcd3c84015bac94ad1b05dc
|
4
|
+
data.tar.gz: a5e7d011cd090fd5d30eb0ef9e12acabb93a993cc3acb46129da46c37294e1ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 711efd2eae8c54dc1b567e198360038fb16eee00c2e6a61db7dbcfb063f219618e36809ac5679cc531e8fe84245ea1f66e6e8eb9fbed5cbb9dcc22f57c10dab6
|
7
|
+
data.tar.gz: 5618f678392ffb6f93b34388e3ef060c017f7e70b53d992ebe48d2bb49b7ec5570adc7b21f5f22f0511cddc4fb1dead044904d7bcf40949191ee6ed8b884565f
|
data/README.md
CHANGED
data/config/default.yml
CHANGED
@@ -6,7 +6,7 @@ Performance:
|
|
6
6
|
|
7
7
|
Performance/AncestorsInclude:
|
8
8
|
Description: 'Use `A <= B` instead of `A.ancestors.include?(B)`.'
|
9
|
-
Reference: 'https://github.com/
|
9
|
+
Reference: 'https://github.com/fastruby/fast-ruby#ancestorsinclude-vs--code'
|
10
10
|
Enabled: 'pending'
|
11
11
|
Safe: false
|
12
12
|
VersionAdded: '1.7'
|
@@ -54,10 +54,11 @@ Performance/CaseWhenSplat:
|
|
54
54
|
Performance/Casecmp:
|
55
55
|
Description: >-
|
56
56
|
Use `casecmp` rather than `downcase ==`, `upcase ==`, `== downcase`, or `== upcase`..
|
57
|
-
Reference: 'https://github.com/
|
58
|
-
Enabled:
|
57
|
+
Reference: 'https://github.com/fastruby/fast-ruby#stringcasecmp-vs--stringcasecmp-vs-stringdowncase---code'
|
58
|
+
Enabled: false
|
59
59
|
Safe: false
|
60
60
|
VersionAdded: '0.36'
|
61
|
+
VersionChanged: '1.21'
|
61
62
|
|
62
63
|
Performance/ChainArrayAllocation:
|
63
64
|
Description: >-
|
@@ -122,7 +123,7 @@ Performance/Detect:
|
|
122
123
|
Description: >-
|
123
124
|
Use `detect` instead of `select.first`, `find_all.first`, `filter.first`,
|
124
125
|
`select.last`, `find_all.last`, and `filter.last`.
|
125
|
-
Reference: 'https://github.com/
|
126
|
+
Reference: 'https://github.com/fastruby/fast-ruby#enumerabledetect-vs-enumerableselectfirst-code'
|
126
127
|
# This cop has known compatibility issues with `ActiveRecord` and other
|
127
128
|
# frameworks. `ActiveRecord` does not implement a `detect` method and `find`
|
128
129
|
# has its own meaning. Correcting `ActiveRecord` methods with this cop
|
@@ -145,7 +146,7 @@ Performance/DoubleStartEndWith:
|
|
145
146
|
|
146
147
|
Performance/EndWith:
|
147
148
|
Description: 'Use `end_with?` instead of a regex match anchored to the end of a string.'
|
148
|
-
Reference: 'https://github.com/
|
149
|
+
Reference: 'https://github.com/fastruby/fast-ruby#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end'
|
149
150
|
# This will change to a new method call which isn't guaranteed to be on the
|
150
151
|
# object. Switching these methods has to be done with knowledge of the types
|
151
152
|
# of the variables which rubocop doesn't have.
|
@@ -165,7 +166,7 @@ Performance/FlatMap:
|
|
165
166
|
Use `Enumerable#flat_map`
|
166
167
|
instead of `Enumerable#map...Array#flatten(1)`
|
167
168
|
or `Enumerable#collect..Array#flatten(1)`.
|
168
|
-
Reference: 'https://github.com/
|
169
|
+
Reference: 'https://github.com/fastruby/fast-ruby#enumerablemaparrayflatten-vs-enumerableflat_map-code'
|
169
170
|
Enabled: true
|
170
171
|
VersionAdded: '0.30'
|
171
172
|
EnabledForFlattenWithoutParams: false
|
@@ -176,7 +177,7 @@ Performance/FlatMap:
|
|
176
177
|
|
177
178
|
Performance/InefficientHashSearch:
|
178
179
|
Description: 'Use `key?` or `value?` instead of `keys.include?` or `values.include?`.'
|
179
|
-
Reference: 'https://github.com/
|
180
|
+
Reference: 'https://github.com/fastruby/fast-ruby#hashkey-instead-of-hashkeysinclude-code'
|
180
181
|
Enabled: true
|
181
182
|
VersionAdded: '0.56'
|
182
183
|
Safe: false
|
@@ -193,9 +194,15 @@ Performance/MapCompact:
|
|
193
194
|
SafeAutoCorrect: false
|
194
195
|
VersionAdded: '1.11'
|
195
196
|
|
197
|
+
Performance/MapMethodChain:
|
198
|
+
Description: 'Checks if the `map` method is used in a chain.'
|
199
|
+
Enabled: pending
|
200
|
+
Safe: false
|
201
|
+
VersionAdded: '1.19'
|
202
|
+
|
196
203
|
Performance/MethodObjectAsBlock:
|
197
204
|
Description: 'Use block explicitly instead of block-passing a method object.'
|
198
|
-
Reference: 'https://github.com/
|
205
|
+
Reference: 'https://github.com/fastruby/fast-ruby#normal-way-to-apply-method-vs-method-code'
|
199
206
|
Enabled: pending
|
200
207
|
VersionAdded: '1.9'
|
201
208
|
|
@@ -207,7 +214,7 @@ Performance/OpenStruct:
|
|
207
214
|
|
208
215
|
Performance/RangeInclude:
|
209
216
|
Description: 'Use `Range#cover?` instead of `Range#include?` (or `Range#member?`).'
|
210
|
-
Reference: 'https://github.com/
|
217
|
+
Reference: 'https://github.com/fastruby/fast-ruby#cover-vs-include-code'
|
211
218
|
Enabled: true
|
212
219
|
VersionAdded: '0.36'
|
213
220
|
VersionChanged: '1.7'
|
@@ -215,7 +222,7 @@ Performance/RangeInclude:
|
|
215
222
|
|
216
223
|
Performance/RedundantBlockCall:
|
217
224
|
Description: 'Use `yield` instead of `block.call`.'
|
218
|
-
Reference: 'https://github.com/
|
225
|
+
Reference: 'https://github.com/fastruby/fast-ruby#proccall-and-block-arguments-vs-yieldcode'
|
219
226
|
Enabled: true
|
220
227
|
VersionAdded: '0.36'
|
221
228
|
|
@@ -238,7 +245,7 @@ Performance/RedundantMatch:
|
|
238
245
|
|
239
246
|
Performance/RedundantMerge:
|
240
247
|
Description: 'Use Hash#[]=, rather than Hash#merge! with a single key-value pair.'
|
241
|
-
Reference: 'https://github.com/
|
248
|
+
Reference: 'https://github.com/fastruby/fast-ruby#hashmerge-vs-hash-code'
|
242
249
|
Enabled: true
|
243
250
|
Safe: false
|
244
251
|
VersionAdded: '0.36'
|
@@ -265,13 +272,13 @@ Performance/RegexpMatch:
|
|
265
272
|
Description: >-
|
266
273
|
Use `match?` instead of `Regexp#match`, `String#match`, `Symbol#match`,
|
267
274
|
`Regexp#===`, or `=~` when `MatchData` is not used.
|
268
|
-
Reference: 'https://github.com/
|
275
|
+
Reference: 'https://github.com/fastruby/fast-ruby#regexp-vs-regexpmatch-vs-regexpmatch-vs-stringmatch-vs-string-vs-stringmatch-code-'
|
269
276
|
Enabled: true
|
270
277
|
VersionAdded: '0.47'
|
271
278
|
|
272
279
|
Performance/ReverseEach:
|
273
280
|
Description: 'Use `reverse_each` instead of `reverse.each`.'
|
274
|
-
Reference: 'https://github.com/
|
281
|
+
Reference: 'https://github.com/fastruby/fast-ruby#enumerablereverseeach-vs-enumerablereverse_each-code'
|
275
282
|
Enabled: true
|
276
283
|
VersionAdded: '0.30'
|
277
284
|
|
@@ -289,7 +296,7 @@ Performance/Size:
|
|
289
296
|
Description: >-
|
290
297
|
Use `size` instead of `count` for counting
|
291
298
|
the number of elements in `Array` and `Hash`.
|
292
|
-
Reference: 'https://github.com/
|
299
|
+
Reference: 'https://github.com/fastruby/fast-ruby#arraylength-vs-arraysize-vs-arraycount-code'
|
293
300
|
Enabled: true
|
294
301
|
VersionAdded: '0.30'
|
295
302
|
|
@@ -300,13 +307,13 @@ Performance/SortReverse:
|
|
300
307
|
|
301
308
|
Performance/Squeeze:
|
302
309
|
Description: "Use `squeeze('a')` instead of `gsub(/a+/, 'a')`."
|
303
|
-
Reference: 'https://github.com/
|
310
|
+
Reference: 'https://github.com/fastruby/fast-ruby#remove-extra-spaces-or-other-contiguous-characters-code'
|
304
311
|
Enabled: 'pending'
|
305
312
|
VersionAdded: '1.7'
|
306
313
|
|
307
314
|
Performance/StartWith:
|
308
315
|
Description: 'Use `start_with?` instead of a regex match anchored to the beginning of a string.'
|
309
|
-
Reference: 'https://github.com/
|
316
|
+
Reference: 'https://github.com/fastruby/fast-ruby#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end'
|
310
317
|
# This will change to a new method call which isn't guaranteed to be on the
|
311
318
|
# object. Switching these methods has to be done with knowledge of the types
|
312
319
|
# of the variables which rubocop doesn't have.
|
@@ -333,7 +340,7 @@ Performance/StringReplacement:
|
|
333
340
|
Use `tr` instead of `gsub` when you are replacing the same
|
334
341
|
number of characters. Use `delete` instead of `gsub` when
|
335
342
|
you are deleting characters.
|
336
|
-
Reference: 'https://github.com/
|
343
|
+
Reference: 'https://github.com/fastruby/fast-ruby#stringgsub-vs-stringtr-code'
|
337
344
|
Enabled: true
|
338
345
|
VersionAdded: '0.33'
|
339
346
|
|
@@ -9,14 +9,14 @@ module RuboCop
|
|
9
9
|
|
10
10
|
def_node_matcher :sort_with_block?, <<~PATTERN
|
11
11
|
(block
|
12
|
-
$(
|
12
|
+
$(call _ :sort)
|
13
13
|
(args (arg $_a) (arg $_b))
|
14
14
|
$send)
|
15
15
|
PATTERN
|
16
16
|
|
17
17
|
def_node_matcher :sort_with_numblock?, <<~PATTERN
|
18
18
|
(numblock
|
19
|
-
$(
|
19
|
+
$(call _ :sort)
|
20
20
|
$_arg_count
|
21
21
|
$send)
|
22
22
|
PATTERN
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
39
39
|
RESTRICT_ON_SEND = SLICE_METHODS
|
40
40
|
|
41
41
|
def_node_matcher :endless_range_slice?, <<~PATTERN
|
42
|
-
(
|
42
|
+
(call $!{str dstr xstr} $%SLICE_METHODS $#endless_range?)
|
43
43
|
PATTERN
|
44
44
|
|
45
45
|
def_node_matcher :endless_range?, <<~PATTERN
|
@@ -59,6 +59,7 @@ module RuboCop
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
62
|
+
alias on_csend on_send
|
62
63
|
|
63
64
|
private
|
64
65
|
|
@@ -6,6 +6,12 @@ module RuboCop
|
|
6
6
|
# Identifies places where a case-insensitive string comparison
|
7
7
|
# can better be implemented using `casecmp`.
|
8
8
|
#
|
9
|
+
# This cop is disabled by default because `String#casecmp` only works with
|
10
|
+
# ASCII characters. See https://github.com/rubocop/rubocop/issues/9753.
|
11
|
+
#
|
12
|
+
# If you are working only with ASCII characters, then this cop can be
|
13
|
+
# safely enabled.
|
14
|
+
#
|
9
15
|
# @safety
|
10
16
|
# This cop is unsafe because `String#casecmp` and `String#casecmp?` behave
|
11
17
|
# differently when using Non-ASCII characters.
|
@@ -19,8 +19,6 @@ module RuboCop
|
|
19
19
|
# array.map! { |x| x.downcase }
|
20
20
|
# array
|
21
21
|
class ChainArrayAllocation < Base
|
22
|
-
include RangeHelp
|
23
|
-
|
24
22
|
# These methods return a new array but only sometimes. They must be
|
25
23
|
# called with an argument. For example:
|
26
24
|
#
|
@@ -54,7 +52,7 @@ module RuboCop
|
|
54
52
|
def_node_matcher :chain_array_allocation?, <<~PATTERN
|
55
53
|
(send {
|
56
54
|
(send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar send})
|
57
|
-
(block (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
|
55
|
+
({block numblock} (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
|
58
56
|
(send _ $%RETURNS_NEW_ARRAY ...)
|
59
57
|
} $%HAS_MUTATION_ALTERNATIVE ...)
|
60
58
|
PATTERN
|
@@ -62,12 +60,21 @@ module RuboCop
|
|
62
60
|
def on_send(node)
|
63
61
|
chain_array_allocation?(node) do |fm, sm|
|
64
62
|
return if node.each_descendant(:send).any? { |descendant| descendant.method?(:lazy) }
|
63
|
+
return if node.method?(:select) && !enumerable_select_method?(node.receiver)
|
65
64
|
|
66
|
-
range =
|
65
|
+
range = node.loc.selector.begin.join(node.source_range.end)
|
67
66
|
|
68
67
|
add_offense(range, message: format(MSG, method: fm, second_method: sm))
|
69
68
|
end
|
70
69
|
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def enumerable_select_method?(node)
|
74
|
+
# NOTE: `QueryMethods#select` in Rails accepts positional arguments, whereas `Enumerable#select` does not.
|
75
|
+
# This difference can be utilized to reduce the knowledge requirements related to `select`.
|
76
|
+
(node.block_type? || node.numblock_type?) && node.send_node.arguments.empty?
|
77
|
+
end
|
71
78
|
end
|
72
79
|
end
|
73
80
|
end
|
@@ -9,8 +9,7 @@ module RuboCop
|
|
9
9
|
#
|
10
10
|
# @safety
|
11
11
|
# This cop is unsafe because it has known compatibility issues with `ActiveRecord` and other
|
12
|
-
# frameworks.
|
13
|
-
# `ActiveRecord` will ignore the block that is passed to `count`.
|
12
|
+
# frameworks. Before Rails 5.1, `ActiveRecord` will ignore the block that is passed to `count`.
|
14
13
|
# Other methods, such as `select`, will convert the association to an
|
15
14
|
# array and then run the block on the array. A simple work around to
|
16
15
|
# make `count` work with a block is to call `to_a.count {...}`.
|
@@ -55,8 +54,8 @@ module RuboCop
|
|
55
54
|
|
56
55
|
def_node_matcher :count_candidate?, <<~PATTERN
|
57
56
|
{
|
58
|
-
(
|
59
|
-
(
|
57
|
+
(call (block $(call _ ${:select :filter :find_all :reject}) ...) ${:count :length :size})
|
58
|
+
(call $(call _ ${:select :filter :find_all :reject} (:block_pass _)) ${:count :length :size})
|
60
59
|
}
|
61
60
|
PATTERN
|
62
61
|
|
@@ -73,6 +72,7 @@ module RuboCop
|
|
73
72
|
end
|
74
73
|
end
|
75
74
|
end
|
75
|
+
alias on_csend on_send
|
76
76
|
|
77
77
|
private
|
78
78
|
|
@@ -101,7 +101,7 @@ module RuboCop
|
|
101
101
|
end
|
102
102
|
|
103
103
|
def negate_reject(corrector, node)
|
104
|
-
if node.receiver.
|
104
|
+
if node.receiver.call_type?
|
105
105
|
negate_block_pass_reject(corrector, node)
|
106
106
|
else
|
107
107
|
negate_block_reject(corrector, node)
|
@@ -64,9 +64,10 @@ module RuboCop
|
|
64
64
|
}.freeze
|
65
65
|
|
66
66
|
def_node_matcher :delete_prefix_candidate?, <<~PATTERN
|
67
|
-
(
|
67
|
+
(call $!nil? ${:gsub :gsub! :sub :sub!} (regexp (str $#literal_at_start?) (regopt)) (str $_))
|
68
68
|
PATTERN
|
69
69
|
|
70
|
+
# rubocop:disable Metrics/AbcSize
|
70
71
|
def on_send(node)
|
71
72
|
return unless (receiver, bad_method, regexp_str, replace_string = delete_prefix_candidate?(node))
|
72
73
|
return unless replace_string.empty?
|
@@ -80,11 +81,13 @@ module RuboCop
|
|
80
81
|
regexp_str = interpret_string_escapes(regexp_str)
|
81
82
|
string_literal = to_string_literal(regexp_str)
|
82
83
|
|
83
|
-
new_code = "#{receiver.source}
|
84
|
+
new_code = "#{receiver.source}#{node.loc.dot.source}#{good_method}(#{string_literal})"
|
84
85
|
|
85
86
|
corrector.replace(node, new_code)
|
86
87
|
end
|
87
88
|
end
|
89
|
+
# rubocop:enable Metrics/AbcSize
|
90
|
+
alias on_csend on_send
|
88
91
|
end
|
89
92
|
end
|
90
93
|
end
|
@@ -64,9 +64,10 @@ module RuboCop
|
|
64
64
|
}.freeze
|
65
65
|
|
66
66
|
def_node_matcher :delete_suffix_candidate?, <<~PATTERN
|
67
|
-
(
|
67
|
+
(call $!nil? ${:gsub :gsub! :sub :sub!} (regexp (str $#literal_at_end?) (regopt)) (str $_))
|
68
68
|
PATTERN
|
69
69
|
|
70
|
+
# rubocop:disable Metrics/AbcSize
|
70
71
|
def on_send(node)
|
71
72
|
return unless (receiver, bad_method, regexp_str, replace_string = delete_suffix_candidate?(node))
|
72
73
|
return unless replace_string.empty?
|
@@ -80,11 +81,13 @@ module RuboCop
|
|
80
81
|
regexp_str = interpret_string_escapes(regexp_str)
|
81
82
|
string_literal = to_string_literal(regexp_str)
|
82
83
|
|
83
|
-
new_code = "#{receiver.source}
|
84
|
+
new_code = "#{receiver.source}#{node.loc.dot.source}#{good_method}(#{string_literal})"
|
84
85
|
|
85
86
|
corrector.replace(node, new_code)
|
86
87
|
end
|
87
88
|
end
|
89
|
+
# rubocop:enable Metrics/AbcSize
|
90
|
+
alias on_csend on_send
|
88
91
|
end
|
89
92
|
end
|
90
93
|
end
|
@@ -40,9 +40,9 @@ module RuboCop
|
|
40
40
|
|
41
41
|
def_node_matcher :detect_candidate?, <<~PATTERN
|
42
42
|
{
|
43
|
-
(send $(block (
|
43
|
+
(send $(block (call _ %CANDIDATE_METHODS) ...) ${:first :last} $...)
|
44
44
|
(send $(block (send _ %CANDIDATE_METHODS) ...) $:[] (int ${0 -1}))
|
45
|
-
(send $(
|
45
|
+
(send $(call _ %CANDIDATE_METHODS ...) ${:first :last} $...)
|
46
46
|
(send $(send _ %CANDIDATE_METHODS ...) $:[] (int ${0 -1}))
|
47
47
|
}
|
48
48
|
PATTERN
|
@@ -63,6 +63,7 @@ module RuboCop
|
|
63
63
|
register_offense(node, receiver, second_method, index)
|
64
64
|
end
|
65
65
|
end
|
66
|
+
alias on_csend on_send
|
66
67
|
|
67
68
|
private
|
68
69
|
|
@@ -54,9 +54,10 @@ module RuboCop
|
|
54
54
|
RESTRICT_ON_SEND = %i[match =~ match?].freeze
|
55
55
|
|
56
56
|
def_node_matcher :redundant_regex?, <<~PATTERN
|
57
|
-
{(
|
57
|
+
{(call $!nil? {:match :=~ :match?} (regexp (str $#literal_at_end?) (regopt)))
|
58
58
|
(send (regexp (str $#literal_at_end?) (regopt)) {:match :match?} $_)
|
59
|
-
(match-with-lvasgn (regexp (str $#literal_at_end?) (regopt)) $_)
|
59
|
+
({send match-with-lvasgn} (regexp (str $#literal_at_end?) (regopt)) $_)
|
60
|
+
(send (regexp (str $#literal_at_end?) (regopt)) :=~ $_)}
|
60
61
|
PATTERN
|
61
62
|
|
62
63
|
def on_send(node)
|
@@ -66,12 +67,14 @@ module RuboCop
|
|
66
67
|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
|
67
68
|
regex_str = drop_end_metacharacter(regex_str)
|
68
69
|
regex_str = interpret_string_escapes(regex_str)
|
70
|
+
dot = node.loc.dot ? node.loc.dot.source : '.'
|
69
71
|
|
70
|
-
new_source = "#{receiver.source}
|
72
|
+
new_source = "#{receiver.source}#{dot}end_with?(#{to_string_literal(regex_str)})"
|
71
73
|
|
72
74
|
corrector.replace(node, new_source)
|
73
75
|
end
|
74
76
|
end
|
77
|
+
alias on_csend on_send
|
75
78
|
alias on_match_with_lvasgn on_send
|
76
79
|
end
|
77
80
|
end
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
50
50
|
RESTRICT_ON_SEND = %i[count length size].freeze
|
51
51
|
|
52
52
|
def_node_matcher :counter, <<~MATCHER
|
53
|
-
(
|
53
|
+
(call ${array hash str sym} {:count :length :size} $...)
|
54
54
|
MATCHER
|
55
55
|
|
56
56
|
def on_send(node)
|
@@ -62,6 +62,7 @@ module RuboCop
|
|
62
62
|
add_offense(node)
|
63
63
|
end
|
64
64
|
end
|
65
|
+
alias on_csend on_send
|
65
66
|
|
66
67
|
private
|
67
68
|
|
@@ -78,13 +79,13 @@ module RuboCop
|
|
78
79
|
end
|
79
80
|
|
80
81
|
def contains_splat?(node)
|
81
|
-
return unless node.array_type?
|
82
|
+
return false unless node.array_type?
|
82
83
|
|
83
84
|
node.each_child_node(:splat).any?
|
84
85
|
end
|
85
86
|
|
86
87
|
def contains_double_splat?(node)
|
87
|
-
return unless node.hash_type?
|
88
|
+
return false unless node.hash_type?
|
88
89
|
|
89
90
|
node.each_child_node(:kwsplat).any?
|
90
91
|
end
|
@@ -26,10 +26,10 @@ module RuboCop
|
|
26
26
|
'multiple levels.'
|
27
27
|
|
28
28
|
def_node_matcher :flat_map_candidate?, <<~PATTERN
|
29
|
-
(
|
29
|
+
(call
|
30
30
|
{
|
31
|
-
$(block (
|
32
|
-
$(
|
31
|
+
$(block (call _ ${:collect :map}) ...)
|
32
|
+
$(call _ ${:collect :map} (block_pass _))
|
33
33
|
}
|
34
34
|
${:flatten :flatten!}
|
35
35
|
$...
|
@@ -46,6 +46,7 @@ module RuboCop
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
|
+
alias on_csend on_send
|
49
50
|
|
50
51
|
private
|
51
52
|
|
@@ -45,7 +45,7 @@ module RuboCop
|
|
45
45
|
RESTRICT_ON_SEND = %i[include?].freeze
|
46
46
|
|
47
47
|
def_node_matcher :inefficient_include?, <<~PATTERN
|
48
|
-
(
|
48
|
+
(call (call $_ {:keys :values}) :include? _)
|
49
49
|
PATTERN
|
50
50
|
|
51
51
|
def on_send(node)
|
@@ -56,21 +56,23 @@ module RuboCop
|
|
56
56
|
add_offense(node, message: message) do |corrector|
|
57
57
|
# Replace `keys.include?` or `values.include?` with the appropriate
|
58
58
|
# `key?`/`value?` method.
|
59
|
-
corrector.replace(
|
60
|
-
node,
|
61
|
-
"#{autocorrect_hash_expression(node)}.#{autocorrect_method(node)}(#{autocorrect_argument(node)})"
|
62
|
-
)
|
59
|
+
corrector.replace(node, replacement(node))
|
63
60
|
end
|
64
61
|
end
|
65
62
|
end
|
63
|
+
alias on_csend on_send
|
66
64
|
|
67
65
|
private
|
68
66
|
|
69
67
|
def message(node)
|
70
|
-
"Use `##{
|
68
|
+
"Use `##{correct_method(node)}` instead of `##{current_method(node)}.include?`."
|
71
69
|
end
|
72
70
|
|
73
|
-
def
|
71
|
+
def replacement(node)
|
72
|
+
"#{correct_hash_expression(node)}#{correct_dot(node)}#{correct_method(node)}(#{correct_argument(node)})"
|
73
|
+
end
|
74
|
+
|
75
|
+
def correct_method(node)
|
74
76
|
case current_method(node)
|
75
77
|
when :keys then use_long_method ? 'has_key?' : 'key?'
|
76
78
|
when :values then use_long_method ? 'has_value?' : 'value?'
|
@@ -86,13 +88,17 @@ module RuboCop
|
|
86
88
|
preferred_config && preferred_config['EnforcedStyle'] == 'long' && preferred_config['Enabled']
|
87
89
|
end
|
88
90
|
|
89
|
-
def
|
90
|
-
node.
|
91
|
+
def correct_argument(node)
|
92
|
+
node.first_argument.source
|
91
93
|
end
|
92
94
|
|
93
|
-
def
|
95
|
+
def correct_hash_expression(node)
|
94
96
|
node.receiver.receiver.source
|
95
97
|
end
|
98
|
+
|
99
|
+
def correct_dot(node)
|
100
|
+
node.receiver.loc.dot.source
|
101
|
+
end
|
96
102
|
end
|
97
103
|
end
|
98
104
|
end
|
@@ -39,13 +39,13 @@ module RuboCop
|
|
39
39
|
|
40
40
|
def_node_matcher :map_compact, <<~PATTERN
|
41
41
|
{
|
42
|
-
(
|
43
|
-
$(
|
42
|
+
(call
|
43
|
+
$(call _ {:map :collect}
|
44
44
|
(block_pass
|
45
45
|
(sym _))) _)
|
46
|
-
(
|
46
|
+
(call
|
47
47
|
(block
|
48
|
-
$(
|
48
|
+
$(call _ {:map :collect})
|
49
49
|
(args ...) _) _)
|
50
50
|
}
|
51
51
|
PATTERN
|
@@ -61,6 +61,7 @@ module RuboCop
|
|
61
61
|
remove_compact_method(corrector, map_node, node, node.parent)
|
62
62
|
end
|
63
63
|
end
|
64
|
+
alias on_csend on_send
|
64
65
|
|
65
66
|
private
|
66
67
|
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Performance
|
6
|
+
# Checks if the map method is used in a chain.
|
7
|
+
#
|
8
|
+
# Autocorrection is not supported because an appropriate block variable name cannot be determined automatically.
|
9
|
+
#
|
10
|
+
# @safety
|
11
|
+
# This cop is unsafe because false positives occur if the number of times the first method is executed
|
12
|
+
# affects the return value of subsequent methods.
|
13
|
+
#
|
14
|
+
# [source,ruby]
|
15
|
+
# ----
|
16
|
+
# class X
|
17
|
+
# def initialize
|
18
|
+
# @@num = 0
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# def foo
|
22
|
+
# @@num += 1
|
23
|
+
# self
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# def bar
|
27
|
+
# @@num * 2
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# [X.new, X.new].map(&:foo).map(&:bar) # => [4, 4]
|
32
|
+
# [X.new, X.new].map { |x| x.foo.bar } # => [2, 4]
|
33
|
+
# ----
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
#
|
37
|
+
# # bad
|
38
|
+
# array.map(&:foo).map(&:bar)
|
39
|
+
#
|
40
|
+
# # good
|
41
|
+
# array.map { |item| item.foo.bar }
|
42
|
+
#
|
43
|
+
class MapMethodChain < Base
|
44
|
+
include IgnoredNode
|
45
|
+
|
46
|
+
MSG = 'Use `%<method_name>s { |x| x.%<map_args>s }` instead of `%<method_name>s` method chain.'
|
47
|
+
RESTRICT_ON_SEND = %i[map collect].freeze
|
48
|
+
|
49
|
+
def_node_matcher :block_pass_with_symbol_arg?, <<~PATTERN
|
50
|
+
(:block_pass (:sym $_))
|
51
|
+
PATTERN
|
52
|
+
|
53
|
+
def on_send(node)
|
54
|
+
return if part_of_ignored_node?(node)
|
55
|
+
return unless (map_arg = block_pass_with_symbol_arg?(node.first_argument))
|
56
|
+
|
57
|
+
map_args = [map_arg]
|
58
|
+
|
59
|
+
return unless (begin_of_chained_map_method = find_begin_of_chained_map_method(node, map_args))
|
60
|
+
|
61
|
+
range = begin_of_chained_map_method.loc.selector.begin.join(node.source_range.end)
|
62
|
+
message = format(MSG, method_name: begin_of_chained_map_method.method_name, map_args: map_args.join('.'))
|
63
|
+
|
64
|
+
add_offense(range, message: message)
|
65
|
+
|
66
|
+
ignore_node(node)
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
72
|
+
def find_begin_of_chained_map_method(node, map_args)
|
73
|
+
return unless (chained_map_method = node.receiver)
|
74
|
+
return if !chained_map_method.call_type? || !RESTRICT_ON_SEND.include?(chained_map_method.method_name)
|
75
|
+
return unless (map_arg = block_pass_with_symbol_arg?(chained_map_method.first_argument))
|
76
|
+
|
77
|
+
map_args.unshift(map_arg)
|
78
|
+
|
79
|
+
receiver = chained_map_method.receiver
|
80
|
+
|
81
|
+
return chained_map_method unless receiver&.call_type? && block_pass_with_symbol_arg?(receiver.first_argument)
|
82
|
+
|
83
|
+
find_begin_of_chained_map_method(chained_map_method, map_args)
|
84
|
+
end
|
85
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|