rubocop-performance 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +18 -7
- data/lib/rubocop/cop/performance/ancestors_include.rb +14 -13
- data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +7 -12
- 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 +13 -22
- data/lib/rubocop/cop/performance/delete_suffix.rb +13 -22
- data/lib/rubocop/cop/performance/detect.rb +29 -26
- data/lib/rubocop/cop/performance/double_start_end_with.rb +16 -24
- data/lib/rubocop/cop/performance/end_with.rb +8 -13
- 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 +25 -36
- data/lib/rubocop/cop/performance/open_struct.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +7 -6
- 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 +6 -16
- data/lib/rubocop/cop/performance/redundant_string_chars.rb +8 -12
- 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 +4 -10
- data/lib/rubocop/cop/performance/size.rb +6 -6
- data/lib/rubocop/cop/performance/sort_reverse.rb +6 -15
- data/lib/rubocop/cop/performance/squeeze.rb +6 -10
- data/lib/rubocop/cop/performance/start_with.rb +8 -13
- data/lib/rubocop/cop/performance/string_include.rb +9 -13
- data/lib/rubocop/cop/performance/string_replacement.rb +23 -27
- data/lib/rubocop/cop/performance/sum.rb +129 -0
- data/lib/rubocop/cop/performance/times_map.rb +11 -18
- data/lib/rubocop/cop/performance/unfreeze_string.rb +1 -1
- data/lib/rubocop/cop/performance/uri_default_parser.rb +6 -12
- data/lib/rubocop/cop/performance_cops.rb +2 -0
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +6 -4
@@ -17,7 +17,9 @@ module RuboCop
|
|
17
17
|
# Array.new(9) do |i|
|
18
18
|
# i.to_s
|
19
19
|
# end
|
20
|
-
class TimesMap <
|
20
|
+
class TimesMap < Base
|
21
|
+
extend AutoCorrector
|
22
|
+
|
21
23
|
MESSAGE = 'Use `Array.new(%<count>s)` with a block ' \
|
22
24
|
'instead of `.times.%<map_or_collect>s`'
|
23
25
|
MESSAGE_ONLY_IF = 'only if `%<count>s` is always 0 or more'
|
@@ -30,35 +32,26 @@ module RuboCop
|
|
30
32
|
check(node)
|
31
33
|
end
|
32
34
|
|
33
|
-
def autocorrect(node)
|
34
|
-
map_or_collect, count = times_map_call(node)
|
35
|
-
|
36
|
-
replacement =
|
37
|
-
"Array.new(#{count.source}" \
|
38
|
-
"#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
|
39
|
-
|
40
|
-
lambda do |corrector|
|
41
|
-
corrector.replace(map_or_collect.loc.expression, replacement)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
35
|
private
|
46
36
|
|
47
37
|
def check(node)
|
48
38
|
times_map_call(node) do |map_or_collect, count|
|
49
|
-
add_offense(node, message: message(map_or_collect, count))
|
39
|
+
add_offense(node, message: message(map_or_collect, count)) do |corrector|
|
40
|
+
replacement = "Array.new(#{count.source}" \
|
41
|
+
"#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
|
42
|
+
|
43
|
+
corrector.replace(map_or_collect.loc.expression, replacement)
|
44
|
+
end
|
50
45
|
end
|
51
46
|
end
|
52
47
|
|
53
48
|
def message(map_or_collect, count)
|
54
49
|
template = if count.literal?
|
55
|
-
MESSAGE
|
50
|
+
"#{MESSAGE}."
|
56
51
|
else
|
57
52
|
"#{MESSAGE} #{MESSAGE_ONLY_IF}."
|
58
53
|
end
|
59
|
-
format(template,
|
60
|
-
count: count.source,
|
61
|
-
map_or_collect: map_or_collect.method_name)
|
54
|
+
format(template, count: count.source, map_or_collect: map_or_collect.method_name)
|
62
55
|
end
|
63
56
|
|
64
57
|
def_node_matcher :times_map_call, <<~PATTERN
|
@@ -13,7 +13,9 @@ module RuboCop
|
|
13
13
|
# # good
|
14
14
|
# URI::DEFAULT_PARSER
|
15
15
|
#
|
16
|
-
class UriDefaultParser <
|
16
|
+
class UriDefaultParser < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
|
17
19
|
MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
|
18
20
|
'`%<double_colon>sURI::Parser.new`.'
|
19
21
|
|
@@ -28,17 +30,9 @@ module RuboCop
|
|
28
30
|
double_colon = captured_value ? '::' : ''
|
29
31
|
message = format(MSG, double_colon: double_colon)
|
30
32
|
|
31
|
-
add_offense(node, message: message)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
def autocorrect(node)
|
36
|
-
lambda do |corrector|
|
37
|
-
double_colon = uri_parser_new?(node) ? '::' : ''
|
38
|
-
|
39
|
-
corrector.replace(
|
40
|
-
node.loc.expression, "#{double_colon}URI::DEFAULT_PARSER"
|
41
|
-
)
|
33
|
+
add_offense(node, message: message) do |corrector|
|
34
|
+
corrector.replace(node.loc.expression, "#{double_colon}URI::DEFAULT_PARSER")
|
35
|
+
end
|
42
36
|
end
|
43
37
|
end
|
44
38
|
end
|
@@ -9,6 +9,7 @@ require_relative 'performance/bind_call'
|
|
9
9
|
require_relative 'performance/caller'
|
10
10
|
require_relative 'performance/case_when_splat'
|
11
11
|
require_relative 'performance/casecmp'
|
12
|
+
require_relative 'performance/collection_literal_in_loop'
|
12
13
|
require_relative 'performance/compare_with_block'
|
13
14
|
require_relative 'performance/count'
|
14
15
|
require_relative 'performance/delete_prefix'
|
@@ -36,6 +37,7 @@ require_relative 'performance/squeeze'
|
|
36
37
|
require_relative 'performance/start_with'
|
37
38
|
require_relative 'performance/string_include'
|
38
39
|
require_relative 'performance/string_replacement'
|
40
|
+
require_relative 'performance/sum'
|
39
41
|
require_relative 'performance/times_map'
|
40
42
|
require_relative 'performance/unfreeze_string'
|
41
43
|
require_relative 'performance/uri_default_parser'
|
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.8.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: 2020-
|
13
|
+
date: 2020-09-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -18,14 +18,14 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
21
|
+
version: 0.87.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: 0.
|
28
|
+
version: 0.87.0
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: simplecov
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- lib/rubocop/cop/performance/case_when_splat.rb
|
64
64
|
- lib/rubocop/cop/performance/casecmp.rb
|
65
65
|
- lib/rubocop/cop/performance/chain_array_allocation.rb
|
66
|
+
- lib/rubocop/cop/performance/collection_literal_in_loop.rb
|
66
67
|
- lib/rubocop/cop/performance/compare_with_block.rb
|
67
68
|
- lib/rubocop/cop/performance/count.rb
|
68
69
|
- lib/rubocop/cop/performance/delete_prefix.rb
|
@@ -90,6 +91,7 @@ files:
|
|
90
91
|
- lib/rubocop/cop/performance/start_with.rb
|
91
92
|
- lib/rubocop/cop/performance/string_include.rb
|
92
93
|
- lib/rubocop/cop/performance/string_replacement.rb
|
94
|
+
- lib/rubocop/cop/performance/sum.rb
|
93
95
|
- lib/rubocop/cop/performance/times_map.rb
|
94
96
|
- lib/rubocop/cop/performance/unfreeze_string.rb
|
95
97
|
- lib/rubocop/cop/performance/uri_default_parser.rb
|