rubocop 1.56.3 → 1.57.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +12 -0
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
  5. data/lib/rubocop/cli.rb +1 -1
  6. data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
  7. data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -22
  8. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  9. data/lib/rubocop/cop/layout/dot_position.rb +1 -5
  10. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +8 -0
  11. data/lib/rubocop/cop/layout/end_alignment.rb +7 -1
  12. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  13. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  14. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  15. data/lib/rubocop/cop/lint/debugger.rb +10 -1
  16. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  17. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  18. data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
  19. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +0 -1
  20. data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -0
  21. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
  22. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
  23. data/lib/rubocop/cop/lint/void.rb +29 -11
  24. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  25. data/lib/rubocop/cop/metrics/class_length.rb +8 -3
  26. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  27. data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
  28. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  29. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
  30. data/lib/rubocop/cop/style/class_equality_comparison.rb +5 -0
  31. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  32. data/lib/rubocop/cop/style/format_string.rb +24 -3
  33. data/lib/rubocop/cop/style/guard_clause.rb +26 -0
  34. data/lib/rubocop/cop/style/identical_conditional_branches.rb +25 -3
  35. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  36. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  37. data/lib/rubocop/cop/style/operator_method_call.rb +6 -0
  38. data/lib/rubocop/cop/style/redundant_begin.rb +9 -1
  39. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -9
  40. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
  41. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  42. data/lib/rubocop/cop/style/redundant_filter_chain.rb +22 -5
  43. data/lib/rubocop/cop/style/redundant_parentheses.rb +38 -14
  44. data/lib/rubocop/cop/style/return_nil.rb +6 -2
  45. data/lib/rubocop/cop/style/single_argument_dig.rb +2 -1
  46. data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
  47. data/lib/rubocop/formatter/html_formatter.rb +4 -2
  48. data/lib/rubocop/magic_comment.rb +12 -10
  49. data/lib/rubocop/server/cache.rb +1 -0
  50. data/lib/rubocop/version.rb +1 -1
  51. data/lib/rubocop.rb +1 -0
  52. metadata +6 -19
@@ -6,6 +6,12 @@ module RuboCop
6
6
  # Identifies usages of `any?`, `empty?` or `none?` predicate methods
7
7
  # chained to `select`/`filter`/`find_all` and change them to use predicate method instead.
8
8
  #
9
+ # @safety
10
+ # This cop's autocorrection is unsafe because `array.select.any?` evaluates all elements
11
+ # through the `select` method, while `array.any?` uses short-circuit evaluation.
12
+ # In other words, `array.select.any?` guarantees the evaluation of every element,
13
+ # but `array.any?` does not necessarily evaluate all of them.
14
+ #
9
15
  # @example
10
16
  # # bad
11
17
  # arr.select { |x| x > 1 }.any?
@@ -28,6 +34,9 @@ module RuboCop
28
34
  # # good
29
35
  # arr.select { |x| x > 1 }.many?
30
36
  #
37
+ # # good
38
+ # arr.select { |x| x > 1 }.present?
39
+ #
31
40
  # @example AllCops:ActiveSupportExtensionsEnabled: true
32
41
  # # bad
33
42
  # arr.select { |x| x > 1 }.many?
@@ -35,20 +44,26 @@ module RuboCop
35
44
  # # good
36
45
  # arr.many? { |x| x > 1 }
37
46
  #
47
+ # # bad
48
+ # arr.select { |x| x > 1 }.present?
49
+ #
50
+ # # good
51
+ # arr.any? { |x| x > 1 }
52
+ #
38
53
  class RedundantFilterChain < Base
39
54
  extend AutoCorrector
40
55
 
41
56
  MSG = 'Use `%<prefer>s` instead of `%<first_method>s.%<second_method>s`.'
42
57
 
43
- RAILS_METHODS = %i[many?].freeze
58
+ RAILS_METHODS = %i[many? present?].freeze
44
59
  RESTRICT_ON_SEND = (%i[any? empty? none? one?] + RAILS_METHODS).freeze
45
60
 
46
61
  # @!method select_predicate?(node)
47
62
  def_node_matcher :select_predicate?, <<~PATTERN
48
- (send
63
+ (call
49
64
  {
50
- (block $(send _ {:select :filter :find_all}) ...)
51
- $(send _ {:select :filter :find_all} block_pass_type?)
65
+ (block $(call _ {:select :filter :find_all}) ...)
66
+ $(call _ {:select :filter :find_all} block_pass_type?)
52
67
  }
53
68
  ${:#{RESTRICT_ON_SEND.join(' :')}})
54
69
  PATTERN
@@ -58,7 +73,8 @@ module RuboCop
58
73
  empty?: :none?,
59
74
  none?: :none?,
60
75
  one?: :one?,
61
- many?: :many?
76
+ many?: :many?,
77
+ present?: :any?
62
78
  }.freeze
63
79
  private_constant :REPLACEMENT_METHODS
64
80
 
@@ -71,6 +87,7 @@ module RuboCop
71
87
  register_offense(select_node, node)
72
88
  end
73
89
  end
90
+ alias on_csend on_send
74
91
 
75
92
  private
76
93
 
@@ -111,31 +111,49 @@ module RuboCop
111
111
 
112
112
  def first_arg_begins_with_hash_literal?(node)
113
113
  # Don't flag `method ({key: value})` or `method ({key: value}.method)`
114
- method_chain_begins_with_hash_literal?(node.children.first) &&
115
- first_argument?(node) &&
116
- !parentheses?(node.parent)
114
+ hash_literal = method_chain_begins_with_hash_literal(node.children.first)
115
+ if (root_method = node.each_ancestor(:send).to_a.last)
116
+ parenthesized = root_method.parenthesized_call?
117
+ end
118
+ hash_literal && first_argument?(node) && !parentheses?(hash_literal) && !parenthesized
117
119
  end
118
120
 
119
- def method_chain_begins_with_hash_literal?(node)
120
- return false if node.nil?
121
- return true if node.hash_type?
122
- return false unless node.send_type?
121
+ def method_chain_begins_with_hash_literal(node)
122
+ return if node.nil?
123
+ return node if node.hash_type?
124
+ return unless node.send_type?
123
125
 
124
- method_chain_begins_with_hash_literal?(node.children.first)
126
+ method_chain_begins_with_hash_literal(node.children.first)
125
127
  end
126
128
 
127
129
  def check(begin_node)
128
130
  node = begin_node.children.first
129
- return offense(begin_node, 'a keyword') if keyword_with_redundant_parentheses?(node)
130
- return offense(begin_node, 'a literal') if disallowed_literal?(begin_node, node)
131
- return offense(begin_node, 'a variable') if node.variable?
132
- return offense(begin_node, 'a constant') if node.const_type?
133
131
 
134
- return offense(begin_node, 'an interpolated expression') if interpolation?(begin_node)
132
+ if (message = find_offense_message(begin_node, node))
133
+ return offense(begin_node, message)
134
+ end
135
135
 
136
136
  check_send(begin_node, node) if node.call_type?
137
137
  end
138
138
 
139
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
140
+ def find_offense_message(begin_node, node)
141
+ return 'a keyword' if keyword_with_redundant_parentheses?(node)
142
+ return 'a literal' if disallowed_literal?(begin_node, node)
143
+ return 'a variable' if node.variable?
144
+ return 'a constant' if node.const_type?
145
+ return 'an interpolated expression' if interpolation?(begin_node)
146
+
147
+ return if begin_node.chained? || !begin_node.parent.nil?
148
+
149
+ if node.and_type? || node.or_type?
150
+ 'a logical expression'
151
+ elsif node.respond_to?(:comparison_method?) && node.comparison_method?
152
+ 'a comparison expression'
153
+ end
154
+ end
155
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
156
+
139
157
  # @!method interpolation?(node)
140
158
  def_node_matcher :interpolation?, '[^begin ^^dstr]'
141
159
 
@@ -215,7 +233,13 @@ module RuboCop
215
233
  end
216
234
 
217
235
  def first_argument?(node)
218
- first_send_argument?(node) || first_super_argument?(node) || first_yield_argument?(node)
236
+ if first_send_argument?(node) ||
237
+ first_super_argument?(node) ||
238
+ first_yield_argument?(node)
239
+ return true
240
+ end
241
+
242
+ node.each_ancestor.any? { |ancestor| first_argument?(ancestor) }
219
243
  end
220
244
 
221
245
  # @!method first_send_argument?(node)
@@ -3,9 +3,13 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Enforces consistency between 'return nil' and 'return'.
6
+ # Enforces consistency between `return nil` and `return`.
7
7
  #
8
- # Supported styles are: return, return_nil.
8
+ # This cop is disabled by default. Because there seems to be a perceived semantic difference
9
+ # between `return` and `return nil`. The former can be seen as just halting evaluation,
10
+ # while the latter might be used when the return value is of specific concern.
11
+ #
12
+ # Supported styles are `return` and `return_nil`.
9
13
  #
10
14
  # @example EnforcedStyle: return (default)
11
15
  # # bad
@@ -33,6 +33,7 @@ module RuboCop
33
33
 
34
34
  MSG = 'Use `%<receiver>s[%<argument>s]` instead of `%<original>s`.'
35
35
  RESTRICT_ON_SEND = %i[dig].freeze
36
+ IGNORED_ARGUMENT_TYPES = %i[block_pass forwarded_restarg forwarded_args hash].freeze
36
37
 
37
38
  # @!method single_argument_dig?(node)
38
39
  def_node_matcher :single_argument_dig?, <<~PATTERN
@@ -44,7 +45,7 @@ module RuboCop
44
45
 
45
46
  expression = single_argument_dig?(node)
46
47
  return unless expression
47
- return if expression.forwarded_args_type?
48
+ return if IGNORED_ARGUMENT_TYPES.include?(expression.type)
48
49
 
49
50
  receiver = node.receiver.source
50
51
  argument = expression.source
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for single-line `do`...`end` block.
7
+ #
8
+ # In practice a single line `do`...`end` is autocorrected when `EnforcedStyle: semantic`
9
+ # in `Style/BlockDelimiters`. The autocorrection maintains the `do` ... `end` syntax to
10
+ # preserve semantics and does not change it to `{`...`}` block.
11
+ #
12
+ # @example
13
+ #
14
+ # # bad
15
+ # foo do |arg| bar(arg) end
16
+ #
17
+ # # good
18
+ # foo do |arg|
19
+ # bar(arg)
20
+ # end
21
+ #
22
+ # # bad
23
+ # ->(arg) do bar(arg) end
24
+ #
25
+ # # good
26
+ # ->(arg) { bar(arg) }
27
+ #
28
+ class SingleLineDoEndBlock < Base
29
+ extend AutoCorrector
30
+
31
+ MSG = 'Prefer multiline `do`...`end` block.'
32
+
33
+ # rubocop:disable Metrics/AbcSize
34
+ def on_block(node)
35
+ return if !node.single_line? || node.braces?
36
+
37
+ add_offense(node) do |corrector|
38
+ corrector.insert_after(do_line(node), "\n")
39
+
40
+ node_body = node.body
41
+
42
+ if node_body.respond_to?(:heredoc?) && node_body.heredoc?
43
+ corrector.remove(node.loc.end)
44
+ corrector.insert_after(node_body.loc.heredoc_end, "\nend")
45
+ else
46
+ corrector.insert_before(node.loc.end, "\n")
47
+ end
48
+ end
49
+ end
50
+ # rubocop:enable Metrics/AbcSize
51
+ alias on_numblock on_block
52
+
53
+ private
54
+
55
+ def do_line(node)
56
+ if node.numblock_type? || node.arguments.children.empty? || node.send_node.lambda_literal?
57
+ node.loc.begin
58
+ else
59
+ node.arguments
60
+ end
61
+ end
62
+
63
+ def x(corrector, node); end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'base64'
4
3
  require 'cgi'
5
4
  require 'erb'
6
5
  require 'ostruct'
@@ -124,7 +123,10 @@ module RuboCop
124
123
 
125
124
  def base64_encoded_logo_image
126
125
  image = File.read(LOGO_IMAGE_PATH, binmode: true)
127
- Base64.encode64(image)
126
+
127
+ # `Base64.encode64` compatible:
128
+ # https://github.com/ruby/base64/blob/v0.1.1/lib/base64.rb#L27-L40
129
+ [image].pack('m')
128
130
  end
129
131
  end
130
132
  end
@@ -7,7 +7,7 @@ module RuboCop
7
7
  class MagicComment
8
8
  # IRB's pattern for matching magic comment tokens.
9
9
  # @see https://github.com/ruby/ruby/blob/b4a55c1/lib/irb/magic-file.rb#L5
10
- TOKEN = /[[:alnum:]\-_]+/.freeze
10
+ TOKEN = '(?<token>[[:alnum:]\-_]+)'
11
11
  KEYWORDS = {
12
12
  encoding: '(?:en)?coding',
13
13
  frozen_string_literal: 'frozen[_-]string[_-]literal',
@@ -129,7 +129,7 @@ module RuboCop
129
129
  # @return [String] if pattern matched
130
130
  # @return [nil] otherwise
131
131
  def extract(pattern)
132
- @comment[pattern, 1]
132
+ @comment[pattern, :token]
133
133
  end
134
134
 
135
135
  # Parent to Vim and Emacs magic comment handling.
@@ -157,10 +157,10 @@ module RuboCop
157
157
  # @return [String] extracted value if it is found
158
158
  # @return [nil] otherwise
159
159
  def match(keyword)
160
- pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*(#{TOKEN})\z/
160
+ pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*#{TOKEN}\z/
161
161
 
162
162
  tokens.each do |token|
163
- next unless (value = token[pattern, 1])
163
+ next unless (value = token[pattern, :token])
164
164
 
165
165
  return value.downcase
166
166
  end
@@ -188,7 +188,7 @@ module RuboCop
188
188
  # @see https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
189
189
  # @see https://github.com/ruby/ruby/blob/3f306dc/parse.y#L6873-L6892 Emacs handling in parse.y
190
190
  class EmacsComment < EditorComment
191
- REGEXP = /-\*-(.+)-\*-/.freeze
191
+ REGEXP = /-\*-(?<token>.+)-\*-/.freeze
192
192
  FORMAT = '# -*- %s -*-'
193
193
  SEPARATOR = ';'
194
194
  OPERATOR = ':'
@@ -216,7 +216,7 @@ module RuboCop
216
216
  #
217
217
  # comment.encoding # => 'ascii-8bit'
218
218
  class VimComment < EditorComment
219
- REGEXP = /#\s*vim:\s*(.+)/.freeze
219
+ REGEXP = /#\s*vim:\s*(?<token>.+)/.freeze
220
220
  FORMAT = '# vim: %s'
221
221
  SEPARATOR = ', '
222
222
  OPERATOR = '='
@@ -259,9 +259,11 @@ module RuboCop
259
259
  # comment2.frozen_string_literal # => nil
260
260
  # comment2.encoding # => 'utf-8'
261
261
  class SimpleComment < MagicComment
262
+ FSTRING_LITERAL_COMMENT = 'frozen_string_literal:\s*(true|false)'
263
+
262
264
  # Match `encoding` or `coding`
263
265
  def encoding
264
- extract(/\A\s*\#.*\b#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
266
+ extract(/\A\s*\#\s*(#{FSTRING_LITERAL_COMMENT})?\s*#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
265
267
  end
266
268
 
267
269
  # Rewrite the comment without a given token type
@@ -283,15 +285,15 @@ module RuboCop
283
285
  # Case-insensitive and dashes/underscores are acceptable.
284
286
  # @see https://github.com/ruby/ruby/blob/78b95b4/parse.y#L7134-L7138
285
287
  def extract_frozen_string_literal
286
- extract(/\A\s*#\s*#{KEYWORDS[:frozen_string_literal]}:\s*(#{TOKEN})\s*\z/io)
288
+ extract(/\A\s*#\s*#{KEYWORDS[:frozen_string_literal]}:\s*#{TOKEN}\s*\z/io)
287
289
  end
288
290
 
289
291
  def extract_shareable_constant_value
290
- extract(/\A\s*#\s*#{KEYWORDS[:shareable_constant_value]}:\s*(#{TOKEN})\s*\z/io)
292
+ extract(/\A\s*#\s*#{KEYWORDS[:shareable_constant_value]}:\s*#{TOKEN}\s*\z/io)
291
293
  end
292
294
 
293
295
  def extract_typed
294
- extract(/\A\s*#\s*#{KEYWORDS[:typed]}:\s*(#{TOKEN})\s*\z/io)
296
+ extract(/\A\s*#\s*#{KEYWORDS[:typed]}:\s*#{TOKEN}\s*\z/io)
295
297
  end
296
298
  end
297
299
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
3
4
  require 'pathname'
4
5
  require_relative '../cache_config'
5
6
  require_relative '../config_finder'
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.56.3'
6
+ STRING = '1.57.2'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
data/lib/rubocop.rb CHANGED
@@ -580,6 +580,7 @@ require_relative 'rubocop/cop/style/redundant_regexp_constructor'
580
580
  require_relative 'rubocop/cop/style/redundant_self_assignment'
581
581
  require_relative 'rubocop/cop/style/redundant_self_assignment_branch'
582
582
  require_relative 'rubocop/cop/style/require_order'
583
+ require_relative 'rubocop/cop/style/single_line_do_end_block'
583
584
  require_relative 'rubocop/cop/style/sole_nested_conditional'
584
585
  require_relative 'rubocop/cop/style/static_class'
585
586
  require_relative 'rubocop/cop/style/map_compact_with_conditional_block'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.56.3
4
+ version: 1.57.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,22 +10,8 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2023-09-11 00:00:00.000000000 Z
13
+ date: 2023-10-26 00:00:00.000000000 Z
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: base64
17
- requirement: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - "~>"
20
- - !ruby/object:Gem::Version
21
- version: 0.1.1
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
26
- - - "~>"
27
- - !ruby/object:Gem::Version
28
- version: 0.1.1
29
15
  - !ruby/object:Gem::Dependency
30
16
  name: json
31
17
  requirement: !ruby/object:Gem::Requirement
@@ -74,14 +60,14 @@ dependencies:
74
60
  requirements:
75
61
  - - ">="
76
62
  - !ruby/object:Gem::Version
77
- version: 3.2.2.3
63
+ version: 3.2.2.4
78
64
  type: :runtime
79
65
  prerelease: false
80
66
  version_requirements: !ruby/object:Gem::Requirement
81
67
  requirements:
82
68
  - - ">="
83
69
  - !ruby/object:Gem::Version
84
- version: 3.2.2.3
70
+ version: 3.2.2.4
85
71
  - !ruby/object:Gem::Dependency
86
72
  name: rainbow
87
73
  requirement: !ruby/object:Gem::Requirement
@@ -895,6 +881,7 @@ files:
895
881
  - lib/rubocop/cop/style/signal_exception.rb
896
882
  - lib/rubocop/cop/style/single_argument_dig.rb
897
883
  - lib/rubocop/cop/style/single_line_block_params.rb
884
+ - lib/rubocop/cop/style/single_line_do_end_block.rb
898
885
  - lib/rubocop/cop/style/single_line_methods.rb
899
886
  - lib/rubocop/cop/style/slicing_with_range.rb
900
887
  - lib/rubocop/cop/style/sole_nested_conditional.rb
@@ -1038,7 +1025,7 @@ metadata:
1038
1025
  homepage_uri: https://rubocop.org/
1039
1026
  changelog_uri: https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md
1040
1027
  source_code_uri: https://github.com/rubocop/rubocop/
1041
- documentation_uri: https://docs.rubocop.org/rubocop/1.56/
1028
+ documentation_uri: https://docs.rubocop.org/rubocop/1.57/
1042
1029
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1043
1030
  rubygems_mfa_required: 'true'
1044
1031
  post_install_message: