rubocop-performance 1.6.1 → 1.9.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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -0
  3. data/config/default.yml +95 -8
  4. data/lib/rubocop/cop/mixin/regexp_metacharacter.rb +4 -4
  5. data/lib/rubocop/cop/mixin/sort_block.rb +28 -0
  6. data/lib/rubocop/cop/performance/ancestors_include.rb +49 -0
  7. data/lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb +74 -0
  8. data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +46 -0
  9. data/lib/rubocop/cop/performance/bind_call.rb +9 -18
  10. data/lib/rubocop/cop/performance/block_given_with_explicit_block.rb +52 -0
  11. data/lib/rubocop/cop/performance/caller.rb +14 -15
  12. data/lib/rubocop/cop/performance/case_when_splat.rb +18 -11
  13. data/lib/rubocop/cop/performance/casecmp.rb +13 -20
  14. data/lib/rubocop/cop/performance/chain_array_allocation.rb +4 -10
  15. data/lib/rubocop/cop/performance/collection_literal_in_loop.rb +140 -0
  16. data/lib/rubocop/cop/performance/compare_with_block.rb +10 -21
  17. data/lib/rubocop/cop/performance/constant_regexp.rb +68 -0
  18. data/lib/rubocop/cop/performance/count.rb +14 -16
  19. data/lib/rubocop/cop/performance/delete_prefix.rb +14 -22
  20. data/lib/rubocop/cop/performance/delete_suffix.rb +14 -22
  21. data/lib/rubocop/cop/performance/detect.rb +65 -32
  22. data/lib/rubocop/cop/performance/double_start_end_with.rb +16 -24
  23. data/lib/rubocop/cop/performance/end_with.rb +9 -13
  24. data/lib/rubocop/cop/performance/fixed_size.rb +2 -1
  25. data/lib/rubocop/cop/performance/flat_map.rb +21 -22
  26. data/lib/rubocop/cop/performance/inefficient_hash_search.rb +15 -14
  27. data/lib/rubocop/cop/performance/io_readlines.rb +112 -0
  28. data/lib/rubocop/cop/performance/method_object_as_block.rb +32 -0
  29. data/lib/rubocop/cop/performance/open_struct.rb +3 -2
  30. data/lib/rubocop/cop/performance/range_include.rb +15 -11
  31. data/lib/rubocop/cop/performance/redundant_block_call.rb +15 -10
  32. data/lib/rubocop/cop/performance/redundant_match.rb +12 -6
  33. data/lib/rubocop/cop/performance/redundant_merge.rb +19 -17
  34. data/lib/rubocop/cop/performance/redundant_sort_block.rb +43 -0
  35. data/lib/rubocop/cop/performance/redundant_string_chars.rb +129 -0
  36. data/lib/rubocop/cop/performance/regexp_match.rb +20 -20
  37. data/lib/rubocop/cop/performance/reverse_each.rb +10 -5
  38. data/lib/rubocop/cop/performance/reverse_first.rb +73 -0
  39. data/lib/rubocop/cop/performance/size.rb +42 -43
  40. data/lib/rubocop/cop/performance/sort_reverse.rb +45 -0
  41. data/lib/rubocop/cop/performance/squeeze.rb +67 -0
  42. data/lib/rubocop/cop/performance/start_with.rb +9 -13
  43. data/lib/rubocop/cop/performance/string_include.rb +56 -0
  44. data/lib/rubocop/cop/performance/string_replacement.rb +24 -27
  45. data/lib/rubocop/cop/performance/sum.rb +236 -0
  46. data/lib/rubocop/cop/performance/times_map.rb +12 -18
  47. data/lib/rubocop/cop/performance/unfreeze_string.rb +20 -2
  48. data/lib/rubocop/cop/performance/uri_default_parser.rb +7 -12
  49. data/lib/rubocop/cop/performance_cops.rb +16 -0
  50. data/lib/rubocop/performance/version.rb +6 -1
  51. metadata +35 -13
@@ -17,10 +17,13 @@ module RuboCop
17
17
  # Array.new(9) do |i|
18
18
  # i.to_s
19
19
  # end
20
- class TimesMap < Cop
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'
26
+ RESTRICT_ON_SEND = %i[map collect].freeze
24
27
 
25
28
  def on_send(node)
26
29
  check(node)
@@ -30,35 +33,26 @@ module RuboCop
30
33
  check(node)
31
34
  end
32
35
 
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
36
  private
46
37
 
47
38
  def check(node)
48
39
  times_map_call(node) do |map_or_collect, count|
49
- add_offense(node, message: message(map_or_collect, count))
40
+ add_offense(node, message: message(map_or_collect, count)) do |corrector|
41
+ replacement = "Array.new(#{count.source}" \
42
+ "#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
43
+
44
+ corrector.replace(map_or_collect.loc.expression, replacement)
45
+ end
50
46
  end
51
47
  end
52
48
 
53
49
  def message(map_or_collect, count)
54
50
  template = if count.literal?
55
- MESSAGE + '.'
51
+ "#{MESSAGE}."
56
52
  else
57
53
  "#{MESSAGE} #{MESSAGE_ONLY_IF}."
58
54
  end
59
- format(template,
60
- count: count.source,
61
- map_or_collect: map_or_collect.method_name)
55
+ format(template, count: count.source, map_or_collect: map_or_collect.method_name)
62
56
  end
63
57
 
64
58
  def_node_matcher :times_map_call, <<~PATTERN
@@ -10,6 +10,7 @@ module RuboCop
10
10
  # NOTE: `String.new` (without operator) is not exactly the same as `+''`.
11
11
  # These differ in encoding. `String.new.encoding` is always `ASCII-8BIT`.
12
12
  # However, `(+'').encoding` is the same as script encoding(e.g. `UTF-8`).
13
+ # Therefore, auto-correction is unsafe.
13
14
  # So, if you expect `ASCII-8BIT` encoding, disable this cop.
14
15
  #
15
16
  # @example
@@ -23,8 +24,11 @@ module RuboCop
23
24
  # # good
24
25
  # +'something'
25
26
  # +''
26
- class UnfreezeString < Cop
27
+ class UnfreezeString < Base
28
+ extend AutoCorrector
29
+
27
30
  MSG = 'Use unary plus to get an unfrozen string literal.'
31
+ RESTRICT_ON_SEND = %i[dup new].freeze
28
32
 
29
33
  def_node_matcher :dup_string?, <<~PATTERN
30
34
  (send {str dstr} :dup)
@@ -38,7 +42,21 @@ module RuboCop
38
42
  PATTERN
39
43
 
40
44
  def on_send(node)
41
- add_offense(node) if dup_string?(node) || string_new?(node)
45
+ return unless dup_string?(node) || string_new?(node)
46
+
47
+ add_offense(node) do |corrector|
48
+ corrector.replace(node, "+#{string_value(node)}")
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def string_value(node)
55
+ if node.receiver.source == 'String' && node.method?(:new)
56
+ node.arguments.empty? ? "''" : node.first_argument.source
57
+ else
58
+ node.receiver.source
59
+ end
42
60
  end
43
61
  end
44
62
  end
@@ -13,9 +13,12 @@ module RuboCop
13
13
  # # good
14
14
  # URI::DEFAULT_PARSER
15
15
  #
16
- class UriDefaultParser < Cop
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`.'
21
+ RESTRICT_ON_SEND = %i[new].freeze
19
22
 
20
23
  def_node_matcher :uri_parser_new?, <<~PATTERN
21
24
  (send
@@ -28,17 +31,9 @@ module RuboCop
28
31
  double_colon = captured_value ? '::' : ''
29
32
  message = format(MSG, double_colon: double_colon)
30
33
 
31
- add_offense(node, message: message)
32
- end
33
- end
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
- )
34
+ add_offense(node, message: message) do |corrector|
35
+ corrector.replace(node.loc.expression, "#{double_colon}URI::DEFAULT_PARSER")
36
+ end
42
37
  end
43
38
  end
44
39
  end
@@ -1,12 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'mixin/regexp_metacharacter'
4
+ require_relative 'mixin/sort_block'
4
5
 
6
+ require_relative 'performance/ancestors_include'
7
+ require_relative 'performance/array_semi_infinite_range_slice'
8
+ require_relative 'performance/big_decimal_with_numeric_argument'
5
9
  require_relative 'performance/bind_call'
10
+ require_relative 'performance/block_given_with_explicit_block'
6
11
  require_relative 'performance/caller'
7
12
  require_relative 'performance/case_when_splat'
8
13
  require_relative 'performance/casecmp'
14
+ require_relative 'performance/collection_literal_in_loop'
9
15
  require_relative 'performance/compare_with_block'
16
+ require_relative 'performance/constant_regexp'
10
17
  require_relative 'performance/count'
11
18
  require_relative 'performance/delete_prefix'
12
19
  require_relative 'performance/delete_suffix'
@@ -16,16 +23,25 @@ require_relative 'performance/end_with'
16
23
  require_relative 'performance/fixed_size'
17
24
  require_relative 'performance/flat_map'
18
25
  require_relative 'performance/inefficient_hash_search'
26
+ require_relative 'performance/method_object_as_block'
19
27
  require_relative 'performance/open_struct'
20
28
  require_relative 'performance/range_include'
29
+ require_relative 'performance/io_readlines'
21
30
  require_relative 'performance/redundant_block_call'
22
31
  require_relative 'performance/redundant_match'
23
32
  require_relative 'performance/redundant_merge'
33
+ require_relative 'performance/redundant_sort_block'
34
+ require_relative 'performance/redundant_string_chars'
24
35
  require_relative 'performance/regexp_match'
25
36
  require_relative 'performance/reverse_each'
37
+ require_relative 'performance/reverse_first'
26
38
  require_relative 'performance/size'
39
+ require_relative 'performance/sort_reverse'
40
+ require_relative 'performance/squeeze'
27
41
  require_relative 'performance/start_with'
42
+ require_relative 'performance/string_include'
28
43
  require_relative 'performance/string_replacement'
44
+ require_relative 'performance/sum'
29
45
  require_relative 'performance/times_map'
30
46
  require_relative 'performance/unfreeze_string'
31
47
  require_relative 'performance/uri_default_parser'
@@ -2,8 +2,13 @@
2
2
 
3
3
  module RuboCop
4
4
  module Performance
5
+ # This module holds the RuboCop Performance version information.
5
6
  module Version
6
- STRING = '1.6.1'
7
+ STRING = '1.9.0'
8
+
9
+ def self.document_version
10
+ STRING.match('\d+\.\d+').to_s
11
+ end
7
12
  end
8
13
  end
9
14
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-performance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-06-05 00:00:00.000000000 Z
13
+ date: 2020-11-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -18,28 +18,34 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 0.71.0
21
+ version: 0.90.0
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '2.0'
22
25
  type: :runtime
23
26
  prerelease: false
24
27
  version_requirements: !ruby/object:Gem::Requirement
25
28
  requirements:
26
29
  - - ">="
27
30
  - !ruby/object:Gem::Version
28
- version: 0.71.0
31
+ version: 0.90.0
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.0'
29
35
  - !ruby/object:Gem::Dependency
30
- name: simplecov
36
+ name: rubocop-ast
31
37
  requirement: !ruby/object:Gem::Requirement
32
38
  requirements:
33
39
  - - ">="
34
40
  - !ruby/object:Gem::Version
35
- version: '0'
36
- type: :development
41
+ version: 0.4.0
42
+ type: :runtime
37
43
  prerelease: false
38
44
  version_requirements: !ruby/object:Gem::Requirement
39
45
  requirements:
40
46
  - - ">="
41
47
  - !ruby/object:Gem::Version
42
- version: '0'
48
+ version: 0.4.0
43
49
  description: |
44
50
  A collection of RuboCop cops to check for performance optimizations
45
51
  in Ruby code.
@@ -55,12 +61,19 @@ files:
55
61
  - config/default.yml
56
62
  - lib/rubocop-performance.rb
57
63
  - lib/rubocop/cop/mixin/regexp_metacharacter.rb
64
+ - lib/rubocop/cop/mixin/sort_block.rb
65
+ - lib/rubocop/cop/performance/ancestors_include.rb
66
+ - lib/rubocop/cop/performance/array_semi_infinite_range_slice.rb
67
+ - lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
58
68
  - lib/rubocop/cop/performance/bind_call.rb
69
+ - lib/rubocop/cop/performance/block_given_with_explicit_block.rb
59
70
  - lib/rubocop/cop/performance/caller.rb
60
71
  - lib/rubocop/cop/performance/case_when_splat.rb
61
72
  - lib/rubocop/cop/performance/casecmp.rb
62
73
  - lib/rubocop/cop/performance/chain_array_allocation.rb
74
+ - lib/rubocop/cop/performance/collection_literal_in_loop.rb
63
75
  - lib/rubocop/cop/performance/compare_with_block.rb
76
+ - lib/rubocop/cop/performance/constant_regexp.rb
64
77
  - lib/rubocop/cop/performance/count.rb
65
78
  - lib/rubocop/cop/performance/delete_prefix.rb
66
79
  - lib/rubocop/cop/performance/delete_suffix.rb
@@ -70,16 +83,25 @@ files:
70
83
  - lib/rubocop/cop/performance/fixed_size.rb
71
84
  - lib/rubocop/cop/performance/flat_map.rb
72
85
  - lib/rubocop/cop/performance/inefficient_hash_search.rb
86
+ - lib/rubocop/cop/performance/io_readlines.rb
87
+ - lib/rubocop/cop/performance/method_object_as_block.rb
73
88
  - lib/rubocop/cop/performance/open_struct.rb
74
89
  - lib/rubocop/cop/performance/range_include.rb
75
90
  - lib/rubocop/cop/performance/redundant_block_call.rb
76
91
  - lib/rubocop/cop/performance/redundant_match.rb
77
92
  - lib/rubocop/cop/performance/redundant_merge.rb
93
+ - lib/rubocop/cop/performance/redundant_sort_block.rb
94
+ - lib/rubocop/cop/performance/redundant_string_chars.rb
78
95
  - lib/rubocop/cop/performance/regexp_match.rb
79
96
  - lib/rubocop/cop/performance/reverse_each.rb
97
+ - lib/rubocop/cop/performance/reverse_first.rb
80
98
  - lib/rubocop/cop/performance/size.rb
99
+ - lib/rubocop/cop/performance/sort_reverse.rb
100
+ - lib/rubocop/cop/performance/squeeze.rb
81
101
  - lib/rubocop/cop/performance/start_with.rb
102
+ - lib/rubocop/cop/performance/string_include.rb
82
103
  - lib/rubocop/cop/performance/string_replacement.rb
104
+ - lib/rubocop/cop/performance/sum.rb
83
105
  - lib/rubocop/cop/performance/times_map.rb
84
106
  - lib/rubocop/cop/performance/unfreeze_string.rb
85
107
  - lib/rubocop/cop/performance/uri_default_parser.rb
@@ -94,9 +116,9 @@ metadata:
94
116
  homepage_uri: https://docs.rubocop.org/rubocop-performance/
95
117
  changelog_uri: https://github.com/rubocop-hq/rubocop-performance/blob/master/CHANGELOG.md
96
118
  source_code_uri: https://github.com/rubocop-hq/rubocop-performance/
97
- documentation_uri: https://docs.rubocop.org/rubocop-performance/
119
+ documentation_uri: https://docs.rubocop.org/rubocop-performance/1.9/
98
120
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop-performance/issues
99
- post_install_message:
121
+ post_install_message:
100
122
  rdoc_options: []
101
123
  require_paths:
102
124
  - lib
@@ -111,8 +133,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
133
  - !ruby/object:Gem::Version
112
134
  version: '0'
113
135
  requirements: []
114
- rubygems_version: 3.1.3
115
- signing_key:
136
+ rubygems_version: 3.2.0.rc.2
137
+ signing_key:
116
138
  specification_version: 4
117
139
  summary: Automatic performance checking tool for Ruby code.
118
140
  test_files: []