rubocop-performance 1.17.1 → 1.18.0
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/compare_with_block.rb +19 -10
- data/lib/rubocop/cop/performance/constant_regexp.rb +4 -0
- data/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb +1 -1
- data/lib/rubocop/cop/performance/regexp_match.rb +4 -0
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4926be80598b4880037ffd36eef32c9025051af7f0398f69a34439c135c9d87
|
4
|
+
data.tar.gz: ba3d738da529cbc70c33e8c5d4baa4c6c8013a5498c834f2e28dc87a8811c15a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b9a64379554a660c30bffea5a0fc8a8d36a9e31efef6ccb4576f4df432d2f270d91d76b0e7e7638740e6cf83a50b94b4dfe37d93e5366d0171b8a0f4e5bff50
|
7
|
+
data.tar.gz: d27b9db647eb1a53f3e80b0ec20cd43b37b0244127a28f7fbdacefcf9477205a7f4d373c0a27d882fcb43f902d4ab398ad73ea3f51cdd21e5d422cd419d621e9
|
@@ -5,35 +5,42 @@ module RuboCop
|
|
5
5
|
module Performance
|
6
6
|
# Identifies places where `sort { |a, b| a.foo <=> b.foo }`
|
7
7
|
# can be replaced by `sort_by(&:foo)`.
|
8
|
-
# This cop also checks `max` and `
|
8
|
+
# This cop also checks `sort!`, `min`, `max` and `minmax` methods.
|
9
9
|
#
|
10
10
|
# @example
|
11
11
|
# # bad
|
12
|
-
# array.sort
|
13
|
-
# array.
|
14
|
-
# array.
|
15
|
-
# array.
|
12
|
+
# array.sort { |a, b| a.foo <=> b.foo }
|
13
|
+
# array.sort! { |a, b| a.foo <=> b.foo }
|
14
|
+
# array.max { |a, b| a.foo <=> b.foo }
|
15
|
+
# array.min { |a, b| a.foo <=> b.foo }
|
16
|
+
# array.minmax { |a, b| a.foo <=> b.foo }
|
17
|
+
# array.sort { |a, b| a[:foo] <=> b[:foo] }
|
16
18
|
#
|
17
19
|
# # good
|
18
20
|
# array.sort_by(&:foo)
|
21
|
+
# array.sort_by!(&:foo)
|
19
22
|
# array.sort_by { |v| v.foo }
|
20
23
|
# array.sort_by do |var|
|
21
24
|
# var.foo
|
22
25
|
# end
|
23
26
|
# array.max_by(&:foo)
|
24
27
|
# array.min_by(&:foo)
|
28
|
+
# array.minmax_by(&:foo)
|
25
29
|
# array.sort_by { |a| a[:foo] }
|
26
30
|
class CompareWithBlock < Base
|
27
31
|
include RangeHelp
|
28
32
|
extend AutoCorrector
|
29
33
|
|
30
|
-
MSG = 'Use `%<
|
34
|
+
MSG = 'Use `%<replacement_method>s%<instead>s` instead of ' \
|
31
35
|
'`%<compare_method>s { |%<var_a>s, %<var_b>s| %<str_a>s ' \
|
32
36
|
'<=> %<str_b>s }`.'
|
33
37
|
|
38
|
+
REPLACEMENT = { sort: :sort_by, sort!: :sort_by!, min: :min_by, max: :max_by, minmax: :minmax_by }.freeze
|
39
|
+
private_constant :REPLACEMENT
|
40
|
+
|
34
41
|
def_node_matcher :compare?, <<~PATTERN
|
35
42
|
(block
|
36
|
-
$(send _ {:sort :min :max})
|
43
|
+
$(send _ {:sort :sort! :min :max :minmax})
|
37
44
|
(args (arg $_a) (arg $_b))
|
38
45
|
$send)
|
39
46
|
PATTERN
|
@@ -54,9 +61,9 @@ module RuboCop
|
|
54
61
|
|
55
62
|
add_offense(range, message: message(send, method, var_a, var_b, args_a)) do |corrector|
|
56
63
|
replacement = if method == :[]
|
57
|
-
"#{send.method_name}
|
64
|
+
"#{REPLACEMENT[send.method_name]} { |a| a[#{args_a.first.source}] }"
|
58
65
|
else
|
59
|
-
"#{send.method_name}
|
66
|
+
"#{REPLACEMENT[send.method_name]}(&:#{method})"
|
60
67
|
end
|
61
68
|
corrector.replace(range, replacement)
|
62
69
|
end
|
@@ -82,7 +89,8 @@ module RuboCop
|
|
82
89
|
|
83
90
|
# rubocop:disable Metrics/MethodLength
|
84
91
|
def message(send, method, var_a, var_b, args)
|
85
|
-
compare_method
|
92
|
+
compare_method = send.method_name
|
93
|
+
replacement_method = REPLACEMENT[compare_method]
|
86
94
|
if method == :[]
|
87
95
|
key = args.first
|
88
96
|
instead = " { |a| a[#{key.source}] }"
|
@@ -94,6 +102,7 @@ module RuboCop
|
|
94
102
|
str_b = "#{var_b}.#{method}"
|
95
103
|
end
|
96
104
|
format(MSG, compare_method: compare_method,
|
105
|
+
replacement_method: replacement_method,
|
97
106
|
instead: instead,
|
98
107
|
var_a: var_a,
|
99
108
|
var_b: var_b,
|
@@ -38,6 +38,10 @@ module RuboCop
|
|
38
38
|
|
39
39
|
MSG = 'Extract this regexp into a constant, memoize it, or append an `/o` option to its options.'
|
40
40
|
|
41
|
+
def self.autocorrect_incompatible_with
|
42
|
+
[RegexpMatch]
|
43
|
+
end
|
44
|
+
|
41
45
|
def on_regexp(node)
|
42
46
|
return if within_allowed_assignment?(node) || !include_interpolated_const?(node) || node.single_interpolation?
|
43
47
|
|
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.
|
4
|
+
version: 1.18.0
|
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: 2023-
|
13
|
+
date: 2023-05-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -123,7 +123,7 @@ metadata:
|
|
123
123
|
homepage_uri: https://docs.rubocop.org/rubocop-performance/
|
124
124
|
changelog_uri: https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md
|
125
125
|
source_code_uri: https://github.com/rubocop/rubocop-performance/
|
126
|
-
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.
|
126
|
+
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.18/
|
127
127
|
bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
|
128
128
|
rubygems_mfa_required: 'true'
|
129
129
|
post_install_message:
|
@@ -134,14 +134,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
134
|
requirements:
|
135
135
|
- - ">="
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
version: 2.
|
137
|
+
version: 2.7.0
|
138
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
139
|
requirements:
|
140
140
|
- - ">="
|
141
141
|
- !ruby/object:Gem::Version
|
142
142
|
version: '0'
|
143
143
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
144
|
+
rubygems_version: 3.5.0.dev
|
145
145
|
signing_key:
|
146
146
|
specification_version: 4
|
147
147
|
summary: Automatic performance checking tool for Ruby code.
|