rubocop-performance 1.5.2 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +5 -1
  4. data/config/default.yml +96 -13
  5. data/lib/rubocop/cop/mixin/regexp_metacharacter.rb +76 -0
  6. data/lib/rubocop/cop/mixin/sort_block.rb +28 -0
  7. data/lib/rubocop/cop/performance/ancestors_include.rb +48 -0
  8. data/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb +45 -0
  9. data/lib/rubocop/cop/performance/bind_call.rb +77 -0
  10. data/lib/rubocop/cop/performance/caller.rb +5 -4
  11. data/lib/rubocop/cop/performance/case_when_splat.rb +18 -11
  12. data/lib/rubocop/cop/performance/casecmp.rb +17 -23
  13. data/lib/rubocop/cop/performance/chain_array_allocation.rb +5 -11
  14. data/lib/rubocop/cop/performance/collection_literal_in_loop.rb +140 -0
  15. data/lib/rubocop/cop/performance/compare_with_block.rb +12 -23
  16. data/lib/rubocop/cop/performance/count.rb +14 -17
  17. data/lib/rubocop/cop/performance/delete_prefix.rb +87 -0
  18. data/lib/rubocop/cop/performance/delete_suffix.rb +87 -0
  19. data/lib/rubocop/cop/performance/detect.rb +30 -27
  20. data/lib/rubocop/cop/performance/double_start_end_with.rb +18 -26
  21. data/lib/rubocop/cop/performance/end_with.rb +38 -25
  22. data/lib/rubocop/cop/performance/fixed_size.rb +2 -2
  23. data/lib/rubocop/cop/performance/flat_map.rb +21 -23
  24. data/lib/rubocop/cop/performance/inefficient_hash_search.rb +14 -15
  25. data/lib/rubocop/cop/performance/io_readlines.rb +116 -0
  26. data/lib/rubocop/cop/performance/open_struct.rb +3 -3
  27. data/lib/rubocop/cop/performance/range_include.rb +15 -12
  28. data/lib/rubocop/cop/performance/redundant_block_call.rb +14 -9
  29. data/lib/rubocop/cop/performance/redundant_match.rb +13 -8
  30. data/lib/rubocop/cop/performance/redundant_merge.rb +36 -23
  31. data/lib/rubocop/cop/performance/redundant_sort_block.rb +43 -0
  32. data/lib/rubocop/cop/performance/redundant_string_chars.rb +133 -0
  33. data/lib/rubocop/cop/performance/regexp_match.rb +32 -32
  34. data/lib/rubocop/cop/performance/reverse_each.rb +10 -5
  35. data/lib/rubocop/cop/performance/reverse_first.rb +72 -0
  36. data/lib/rubocop/cop/performance/size.rb +41 -43
  37. data/lib/rubocop/cop/performance/sort_reverse.rb +45 -0
  38. data/lib/rubocop/cop/performance/squeeze.rb +66 -0
  39. data/lib/rubocop/cop/performance/start_with.rb +38 -28
  40. data/lib/rubocop/cop/performance/string_include.rb +55 -0
  41. data/lib/rubocop/cop/performance/string_replacement.rb +25 -36
  42. data/lib/rubocop/cop/performance/sum.rb +129 -0
  43. data/lib/rubocop/cop/performance/times_map.rb +12 -19
  44. data/lib/rubocop/cop/performance/unfreeze_string.rb +4 -8
  45. data/lib/rubocop/cop/performance/uri_default_parser.rb +7 -13
  46. data/lib/rubocop/cop/performance_cops.rb +17 -0
  47. data/lib/rubocop/performance/inject.rb +1 -1
  48. data/lib/rubocop/performance/version.rb +1 -1
  49. metadata +27 -11
@@ -17,7 +17,9 @@ 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'
@@ -30,38 +32,29 @@ 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
- def_node_matcher :times_map_call, <<-PATTERN
57
+ def_node_matcher :times_map_call, <<~PATTERN
65
58
  {(block $(send (send $!nil? :times) {:map :collect}) ...)
66
59
  $(send (send $!nil? :times) {:map :collect} (block_pass ...))}
67
60
  PATTERN
@@ -7,7 +7,7 @@ module RuboCop
7
7
  # literal instead of `String#dup` and `String.new`.
8
8
  # Unary plus operator is faster than `String#dup`.
9
9
  #
10
- # Note: `String.new` (without operator) is not exactly the same as `+''`.
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
13
  # So, if you expect `ASCII-8BIT` encoding, disable this cop.
@@ -23,18 +23,14 @@ module RuboCop
23
23
  # # good
24
24
  # +'something'
25
25
  # +''
26
- class UnfreezeString < Cop
27
- extend TargetRubyVersion
28
-
29
- minimum_target_ruby_version 2.3
30
-
26
+ class UnfreezeString < Base
31
27
  MSG = 'Use unary plus to get an unfrozen string literal.'
32
28
 
33
- def_node_matcher :dup_string?, <<-PATTERN
29
+ def_node_matcher :dup_string?, <<~PATTERN
34
30
  (send {str dstr} :dup)
35
31
  PATTERN
36
32
 
37
- def_node_matcher :string_new?, <<-PATTERN
33
+ def_node_matcher :string_new?, <<~PATTERN
38
34
  {
39
35
  (send (const nil? :String) :new {str dstr})
40
36
  (send (const nil? :String) :new)
@@ -13,11 +13,13 @@ 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`.'
19
21
 
20
- def_node_matcher :uri_parser_new?, <<-PATTERN
22
+ def_node_matcher :uri_parser_new?, <<~PATTERN
21
23
  (send
22
24
  (const
23
25
  (const ${nil? cbase} :URI) :Parser) :new)
@@ -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
- 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
- )
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
@@ -1,10 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'mixin/regexp_metacharacter'
4
+ require_relative 'mixin/sort_block'
5
+
6
+ require_relative 'performance/ancestors_include'
7
+ require_relative 'performance/big_decimal_with_numeric_argument'
8
+ require_relative 'performance/bind_call'
3
9
  require_relative 'performance/caller'
4
10
  require_relative 'performance/case_when_splat'
5
11
  require_relative 'performance/casecmp'
12
+ require_relative 'performance/collection_literal_in_loop'
6
13
  require_relative 'performance/compare_with_block'
7
14
  require_relative 'performance/count'
15
+ require_relative 'performance/delete_prefix'
16
+ require_relative 'performance/delete_suffix'
8
17
  require_relative 'performance/detect'
9
18
  require_relative 'performance/double_start_end_with'
10
19
  require_relative 'performance/end_with'
@@ -13,14 +22,22 @@ require_relative 'performance/flat_map'
13
22
  require_relative 'performance/inefficient_hash_search'
14
23
  require_relative 'performance/open_struct'
15
24
  require_relative 'performance/range_include'
25
+ require_relative 'performance/io_readlines'
16
26
  require_relative 'performance/redundant_block_call'
17
27
  require_relative 'performance/redundant_match'
18
28
  require_relative 'performance/redundant_merge'
29
+ require_relative 'performance/redundant_sort_block'
30
+ require_relative 'performance/redundant_string_chars'
19
31
  require_relative 'performance/regexp_match'
20
32
  require_relative 'performance/reverse_each'
33
+ require_relative 'performance/reverse_first'
21
34
  require_relative 'performance/size'
35
+ require_relative 'performance/sort_reverse'
36
+ require_relative 'performance/squeeze'
22
37
  require_relative 'performance/start_with'
38
+ require_relative 'performance/string_include'
23
39
  require_relative 'performance/string_replacement'
40
+ require_relative 'performance/sum'
24
41
  require_relative 'performance/times_map'
25
42
  require_relative 'performance/unfreeze_string'
26
43
  require_relative 'performance/uri_default_parser'
@@ -8,7 +8,7 @@ module RuboCop
8
8
  def self.defaults!
9
9
  path = CONFIG_DEFAULT.to_s
10
10
  hash = ConfigLoader.send(:load_yaml_configuration, path)
11
- config = Config.new(hash, path)
11
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
12
12
  puts "configuration from #{path}" if ConfigLoader.debug?
13
13
  config = ConfigLoader.merge_with_default(config, path)
14
14
  ConfigLoader.instance_variable_set(:@default_configuration, config)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Performance
5
5
  module Version
6
- STRING = '1.5.2'
6
+ STRING = '1.8.0'
7
7
  end
8
8
  end
9
9
  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.5.2
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: 2019-12-25 00:00:00.000000000 Z
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.71.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.71.0
28
+ version: 0.87.0
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: simplecov
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -40,9 +40,9 @@ dependencies:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
- description: |2
44
- A collection of RuboCop cops to check for performance optimizations
45
- in Ruby code.
43
+ description: |
44
+ A collection of RuboCop cops to check for performance optimizations
45
+ in Ruby code.
46
46
  email: rubocop@googlegroups.com
47
47
  executables: []
48
48
  extensions: []
@@ -54,28 +54,44 @@ files:
54
54
  - README.md
55
55
  - config/default.yml
56
56
  - lib/rubocop-performance.rb
57
+ - lib/rubocop/cop/mixin/regexp_metacharacter.rb
58
+ - lib/rubocop/cop/mixin/sort_block.rb
59
+ - lib/rubocop/cop/performance/ancestors_include.rb
60
+ - lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
61
+ - lib/rubocop/cop/performance/bind_call.rb
57
62
  - lib/rubocop/cop/performance/caller.rb
58
63
  - lib/rubocop/cop/performance/case_when_splat.rb
59
64
  - lib/rubocop/cop/performance/casecmp.rb
60
65
  - lib/rubocop/cop/performance/chain_array_allocation.rb
66
+ - lib/rubocop/cop/performance/collection_literal_in_loop.rb
61
67
  - lib/rubocop/cop/performance/compare_with_block.rb
62
68
  - lib/rubocop/cop/performance/count.rb
69
+ - lib/rubocop/cop/performance/delete_prefix.rb
70
+ - lib/rubocop/cop/performance/delete_suffix.rb
63
71
  - lib/rubocop/cop/performance/detect.rb
64
72
  - lib/rubocop/cop/performance/double_start_end_with.rb
65
73
  - lib/rubocop/cop/performance/end_with.rb
66
74
  - lib/rubocop/cop/performance/fixed_size.rb
67
75
  - lib/rubocop/cop/performance/flat_map.rb
68
76
  - lib/rubocop/cop/performance/inefficient_hash_search.rb
77
+ - lib/rubocop/cop/performance/io_readlines.rb
69
78
  - lib/rubocop/cop/performance/open_struct.rb
70
79
  - lib/rubocop/cop/performance/range_include.rb
71
80
  - lib/rubocop/cop/performance/redundant_block_call.rb
72
81
  - lib/rubocop/cop/performance/redundant_match.rb
73
82
  - lib/rubocop/cop/performance/redundant_merge.rb
83
+ - lib/rubocop/cop/performance/redundant_sort_block.rb
84
+ - lib/rubocop/cop/performance/redundant_string_chars.rb
74
85
  - lib/rubocop/cop/performance/regexp_match.rb
75
86
  - lib/rubocop/cop/performance/reverse_each.rb
87
+ - lib/rubocop/cop/performance/reverse_first.rb
76
88
  - lib/rubocop/cop/performance/size.rb
89
+ - lib/rubocop/cop/performance/sort_reverse.rb
90
+ - lib/rubocop/cop/performance/squeeze.rb
77
91
  - lib/rubocop/cop/performance/start_with.rb
92
+ - lib/rubocop/cop/performance/string_include.rb
78
93
  - lib/rubocop/cop/performance/string_replacement.rb
94
+ - lib/rubocop/cop/performance/sum.rb
79
95
  - lib/rubocop/cop/performance/times_map.rb
80
96
  - lib/rubocop/cop/performance/unfreeze_string.rb
81
97
  - lib/rubocop/cop/performance/uri_default_parser.rb
@@ -87,10 +103,10 @@ homepage: https://github.com/rubocop-hq/rubocop-performance
87
103
  licenses:
88
104
  - MIT
89
105
  metadata:
90
- homepage_uri: https://docs.rubocop.org/projects/performance
106
+ homepage_uri: https://docs.rubocop.org/rubocop-performance/
91
107
  changelog_uri: https://github.com/rubocop-hq/rubocop-performance/blob/master/CHANGELOG.md
92
108
  source_code_uri: https://github.com/rubocop-hq/rubocop-performance/
93
- documentation_uri: https://docs.rubocop.org/projects/performance
109
+ documentation_uri: https://docs.rubocop.org/rubocop-performance/
94
110
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop-performance/issues
95
111
  post_install_message:
96
112
  rdoc_options: []
@@ -100,14 +116,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
116
  requirements:
101
117
  - - ">="
102
118
  - !ruby/object:Gem::Version
103
- version: 2.3.0
119
+ version: 2.4.0
104
120
  required_rubygems_version: !ruby/object:Gem::Requirement
105
121
  requirements:
106
122
  - - ">="
107
123
  - !ruby/object:Gem::Version
108
124
  version: '0'
109
125
  requirements: []
110
- rubygems_version: 3.1.2
126
+ rubygems_version: 3.1.4
111
127
  signing_key:
112
128
  specification_version: 4
113
129
  summary: Automatic performance checking tool for Ruby code.