rubocop 1.75.1 → 1.75.4

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 (81) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +29 -18
  3. data/lib/rubocop/config_validator.rb +6 -6
  4. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -1
  5. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +1 -0
  6. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
  7. data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
  8. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  9. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  10. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
  11. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  12. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
  13. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -4
  14. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  15. data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -5
  16. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  17. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -3
  18. data/lib/rubocop/cop/lint/literal_as_condition.rb +25 -11
  19. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
  20. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  21. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +2 -2
  22. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
  23. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +7 -4
  24. data/lib/rubocop/cop/lint/return_in_void_context.rb +7 -2
  25. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  26. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  27. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  28. data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
  29. data/lib/rubocop/cop/message_annotator.rb +7 -3
  30. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  31. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
  32. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  33. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  34. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +0 -1
  35. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
  36. data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -4
  37. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  38. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  39. data/lib/rubocop/cop/style/arguments_forwarding.rb +5 -5
  40. data/lib/rubocop/cop/style/class_and_module_children.rb +19 -3
  41. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
  42. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  43. data/lib/rubocop/cop/style/comparable_between.rb +2 -2
  44. data/lib/rubocop/cop/style/conditional_assignment.rb +16 -4
  45. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  46. data/lib/rubocop/cop/style/empty_literal.rb +4 -0
  47. data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
  48. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  49. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
  50. data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
  51. data/lib/rubocop/cop/style/hash_fetch_chain.rb +0 -1
  52. data/lib/rubocop/cop/style/hash_syntax.rb +3 -0
  53. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  54. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  55. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  56. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  57. data/lib/rubocop/cop/style/lambda_call.rb +7 -2
  58. data/lib/rubocop/cop/style/map_into_array.rb +3 -1
  59. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +1 -1
  60. data/lib/rubocop/cop/style/redundant_condition.rb +13 -1
  61. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +1 -1
  62. data/lib/rubocop/cop/style/redundant_line_continuation.rb +0 -3
  63. data/lib/rubocop/cop/style/redundant_parentheses.rb +20 -3
  64. data/lib/rubocop/cop/style/return_nil.rb +2 -2
  65. data/lib/rubocop/cop/style/safe_navigation.rb +18 -3
  66. data/lib/rubocop/cop/style/super_arguments.rb +1 -2
  67. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +7 -1
  68. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
  69. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
  70. data/lib/rubocop/cop/util.rb +1 -1
  71. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  72. data/lib/rubocop/cops_documentation_generator.rb +6 -2
  73. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  74. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  75. data/lib/rubocop/magic_comment.rb +8 -0
  76. data/lib/rubocop/rspec/cop_helper.rb +2 -2
  77. data/lib/rubocop/rspec/shared_contexts.rb +1 -2
  78. data/lib/rubocop/server/cache.rb +13 -10
  79. data/lib/rubocop/target_finder.rb +6 -2
  80. data/lib/rubocop/version.rb +1 -1
  81. metadata +5 -5
@@ -48,7 +48,7 @@ module RuboCop
48
48
 
49
49
  args = process_args(node.arguments)
50
50
  return extract_breakable_node_from_elements(node, args, max)
51
- elsif node.type?(:def, :defs)
51
+ elsif node.any_def_type?
52
52
  return extract_breakable_node_from_elements(node, node.arguments, max)
53
53
  elsif node.type?(:array, :hash)
54
54
  return extract_breakable_node_from_elements(node, node.children, max)
@@ -220,7 +220,7 @@ module RuboCop
220
220
 
221
221
  # @api private
222
222
  def already_on_multiple_lines?(node)
223
- return node.first_line != node.last_argument.last_line if node.type?(:def, :defs)
223
+ return node.first_line != node.last_argument.last_line if node.any_def_type?
224
224
 
225
225
  !node.single_line?
226
226
  end
@@ -40,7 +40,7 @@ module RuboCop
40
40
  end
41
41
 
42
42
  def safe_to_split?(node)
43
- node.each_descendant(:if, :case, :kwbegin, :def, :defs).none? &&
43
+ node.each_descendant(:if, :case, :kwbegin, :any_def).none? &&
44
44
  node.each_descendant(:dstr, :str).none? { |n| n.heredoc? || n.value.include?("\n") } &&
45
45
  node.each_descendant(:begin, :sym).none? { |b| !b.single_line? }
46
46
  end
@@ -19,7 +19,7 @@ module RuboCop
19
19
 
20
20
  # @!method non_public_modifier?(node)
21
21
  def_node_matcher :non_public_modifier?, <<~PATTERN
22
- (send nil? {:private :protected :private_class_method} ({def defs} ...))
22
+ (send nil? {:private :protected :private_class_method} (any_def ...))
23
23
  PATTERN
24
24
  end
25
25
  end
@@ -21,7 +21,7 @@ module RuboCop
21
21
 
22
22
  # @!method empty_line_required?(node)
23
23
  def_node_matcher :empty_line_required?,
24
- '{def defs class module (send nil? {:private :protected :public})}'
24
+ '{any_def class module (send nil? {:private :protected :public})}'
25
25
 
26
26
  def check(node, body, adjusted_first_line: nil)
27
27
  return if valid_body_style?(body)
@@ -6,7 +6,6 @@ module RuboCop
6
6
  module FrozenStringLiteral
7
7
  module_function
8
8
 
9
- FROZEN_STRING_LITERAL_REGEXP = /#\s*frozen[-_]?string[-_]?literal:/i.freeze
10
9
  FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
11
10
  FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze
12
11
 
@@ -17,6 +17,8 @@ module RuboCop
17
17
 
18
18
  lhs = left_hand_side(node.receiver)
19
19
  rhs = right_hand_side(node)
20
+ return unless rhs
21
+
20
22
  range = offending_range(node, lhs, rhs, style)
21
23
  check(range, node, lhs, rhs)
22
24
  end
@@ -107,7 +107,7 @@ module RuboCop
107
107
  # of the argument is not considered multiline, even if the argument
108
108
  # itself might span multiple lines.
109
109
  def allowed_multiline_argument?(node)
110
- elements(node).one? && !Util.begins_its_line?(node.loc.end)
110
+ elements(node).one? && (!node.loc.end || !Util.begins_its_line?(node.loc.end))
111
111
  end
112
112
 
113
113
  def elements(node)
@@ -136,9 +136,9 @@ module RuboCop
136
136
  end
137
137
 
138
138
  def last_item_precedes_newline?(node)
139
- after_last_item =
140
- range_between(node.children.last.source_range.end_pos, node.loc.end.begin_pos)
141
- after_last_item.source =~ /\A,?\s*\n/
139
+ after_last_item = node.children.last.source_range.end.join(node.loc.end.begin)
140
+
141
+ after_last_item.source.start_with?(/,?\s*(#.*)?\n/)
142
142
  end
143
143
 
144
144
  def avoid_comma(kind, comma_begin_pos, extra_info)
@@ -242,7 +242,7 @@ module RuboCop
242
242
  def find_definition(node)
243
243
  # Methods can be defined in a `def` or `defs`,
244
244
  # or dynamically via a `block` node.
245
- node.each_ancestor(:def, :defs, :block).each do |ancestor|
245
+ node.each_ancestor(:any_def, :block).each do |ancestor|
246
246
  method_node, method_name = method_definition?(ancestor)
247
247
  return [method_node, method_name] if method_node
248
248
  end
@@ -105,7 +105,7 @@ module RuboCop
105
105
  end
106
106
 
107
107
  def register_forbidden_name(node)
108
- if node.type?(:def, :defs)
108
+ if node.any_def_type?
109
109
  name_node = node.loc.name
110
110
  method_name = node.method_name
111
111
  else
@@ -16,16 +16,16 @@ module RuboCop
16
16
  #
17
17
  # In Ruby 3.2, anonymous args/kwargs forwarding has been added.
18
18
  #
19
- # This cop also identifies places where `use_args(*args)`/`use_kwargs(**kwargs)` can be
20
- # replaced by `use_args(*)`/`use_kwargs(**)`; if desired, this functionality can be disabled
21
- # by setting `UseAnonymousForwarding: false`.
19
+ # This cop also identifies places where `+use_args(*args)+`/`+use_kwargs(**kwargs)+` can be
20
+ # replaced by `+use_args(*)+`/`+use_kwargs(**)+`; if desired, this functionality can be
21
+ # disabled by setting `UseAnonymousForwarding: false`.
22
22
  #
23
23
  # And this cop has `RedundantRestArgumentNames`, `RedundantKeywordRestArgumentNames`,
24
24
  # and `RedundantBlockArgumentNames` options. This configuration is a list of redundant names
25
25
  # that are sufficient for anonymizing meaningless naming.
26
26
  #
27
27
  # Meaningless names that are commonly used can be anonymized by default:
28
- # e.g., `*args`, `**options`, `&block`, and so on.
28
+ # e.g., `+*args+`, `+**options+`, `&block`, and so on.
29
29
  #
30
30
  # Names not on this list are likely to be meaningful and are allowed by default.
31
31
  #
@@ -146,7 +146,7 @@ module RuboCop
146
146
  minimum_target_ruby_version 2.7
147
147
 
148
148
  FORWARDING_LVAR_TYPES = %i[splat kwsplat block_pass].freeze
149
- ADDITIONAL_ARG_TYPES = %i[lvar arg].freeze
149
+ ADDITIONAL_ARG_TYPES = %i[lvar arg optarg].freeze
150
150
 
151
151
  FORWARDING_MSG = 'Use shorthand syntax `...` for arguments forwarding.'
152
152
  ARGS_MSG = 'Use anonymous positional arguments forwarding (`*`).'
@@ -131,21 +131,27 @@ module RuboCop
131
131
  "#{node.body.children.first.const_name}"
132
132
  end
133
133
 
134
+ # rubocop:disable Metrics/AbcSize
134
135
  def remove_end(corrector, body)
135
- remove_begin_pos = body.loc.end.begin_pos - leading_spaces(body).size
136
+ remove_begin_pos = if same_line?(body.loc.name, body.loc.end)
137
+ body.loc.name.end_pos
138
+ else
139
+ body.loc.end.begin_pos - leading_spaces(body).size
140
+ end
136
141
  adjustment = processed_source.raw_source[remove_begin_pos] == ';' ? 0 : 1
137
142
  range = range_between(remove_begin_pos, body.loc.end.end_pos + adjustment)
138
143
 
139
144
  corrector.remove(range)
140
145
  end
146
+ # rubocop:enable Metrics/AbcSize
141
147
 
142
148
  def unindent(corrector, node)
143
149
  return unless node.body.children.last
144
150
 
145
151
  last_child_leading_spaces = leading_spaces(node.body.children.last)
146
- return if leading_spaces(node).size == last_child_leading_spaces.size
152
+ return if spaces_size(leading_spaces(node)) == spaces_size(last_child_leading_spaces)
147
153
 
148
- column_delta = configured_indentation_width - last_child_leading_spaces.size
154
+ column_delta = configured_indentation_width - spaces_size(last_child_leading_spaces)
149
155
  return if column_delta.zero?
150
156
 
151
157
  AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
@@ -155,6 +161,16 @@ module RuboCop
155
161
  node.source_range.source_line[/\A\s*/]
156
162
  end
157
163
 
164
+ def spaces_size(spaces_string)
165
+ mapping = { "\t" => tab_indentation_width }
166
+ spaces_string.chars.sum { |character| mapping.fetch(character, 1) }
167
+ end
168
+
169
+ def tab_indentation_width
170
+ config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
171
+ configured_indentation_width
172
+ end
173
+
158
174
  def check_style(node, body, style)
159
175
  return if node.identifier.namespace&.cbase_type?
160
176
 
@@ -68,7 +68,7 @@ module RuboCop
68
68
  PATTERN
69
69
 
70
70
  def on_send(node)
71
- def_node = node.each_ancestor(:def, :defs).first
71
+ def_node = node.each_ancestor(:any_def).first
72
72
  return if def_node &&
73
73
  (allowed_method?(def_node.method_name) ||
74
74
  matches_allowed_pattern?(def_node.method_name))
@@ -58,7 +58,7 @@ module RuboCop
58
58
  REGEXP = /(?<keyword>\S+).*#/.freeze
59
59
 
60
60
  SUBCLASS_DEFINITION = /\A\s*class\s+(\w|::)+\s*<\s*(\w|::)+/.freeze
61
- METHOD_DEFINITION = /\A\s*def\s/.freeze
61
+ METHOD_OR_END_DEFINITIONS = /\A\s*(def\s|end)/.freeze
62
62
 
63
63
  STEEP_REGEXP = /#\ssteep:ignore(\s|\z)/.freeze
64
64
 
@@ -102,7 +102,7 @@ module RuboCop
102
102
  case line
103
103
  when SUBCLASS_DEFINITION
104
104
  comment.text.start_with?(/#\[.+\]/)
105
- when METHOD_DEFINITION
105
+ when METHOD_OR_END_DEFINITIONS
106
106
  comment.text.start_with?('#:')
107
107
  else
108
108
  false
@@ -61,8 +61,8 @@ module RuboCop
61
61
 
62
62
  def register_offense(node, min_and_value, max_and_value)
63
63
  value = (min_and_value & max_and_value).first
64
- min = min_and_value.find { _1 != value }
65
- max = max_and_value.find { _1 != value }
64
+ min = min_and_value.find { _1 != value } || value
65
+ max = max_and_value.find { _1 != value } || value
66
66
 
67
67
  prefer = "#{value.source}.between?(#{min.source}, #{max.source})"
68
68
  add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
@@ -314,6 +314,7 @@ module RuboCop
314
314
 
315
315
  def assignment_node(node)
316
316
  assignment = node.send_type? ? node.last_argument : node.expression
317
+ return unless assignment
317
318
 
318
319
  # ignore pseudo-assignments without rhs in for nodes
319
320
  return if node.parent&.for_type?
@@ -436,19 +437,28 @@ module RuboCop
436
437
  # Helper module to provide common methods to ConditionalAssignment
437
438
  # correctors
438
439
  module ConditionalCorrectorHelper
440
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
439
441
  def remove_whitespace_in_branches(corrector, branch, condition, column)
440
442
  branch.each_node do |child|
441
443
  next if child.source_range.nil?
444
+ next if child.parent.dstr_type?
442
445
 
443
446
  white_space = white_space_range(child, column)
444
447
  corrector.remove(white_space) if white_space.source.strip.empty?
445
448
  end
446
449
 
447
- [condition.loc.else, condition.loc.end].each do |loc|
448
- next unless loc
449
-
450
- corrector.remove_preceding(loc, loc.column - column)
450
+ if condition.loc.else && !same_line?(condition.else_branch, condition)
451
+ corrector.remove_preceding(condition.loc.else, condition.loc.else.column - column)
451
452
  end
453
+
454
+ return unless condition.loc.end && !same_line?(condition.loc.end, condition)
455
+
456
+ corrector.remove_preceding(condition.loc.end, condition.loc.end.column - column)
457
+ end
458
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
459
+
460
+ def same_line?(node1, node2)
461
+ RuboCop::Cop::Util.same_line?(node1, node2)
452
462
  end
453
463
 
454
464
  def white_space_range(node, column)
@@ -595,6 +605,8 @@ module RuboCop
595
605
 
596
606
  return unless (branch_else = branch.parent.loc.else)
597
607
 
608
+ return if same_line?(branch_else, condition)
609
+
598
610
  corrector.remove_preceding(branch_else, branch_else.column - column)
599
611
  end
600
612
  end
@@ -102,7 +102,7 @@ module RuboCop
102
102
 
103
103
  def find_def_node_from_ascendant(node)
104
104
  return unless (parent = node.parent)
105
- return parent if parent.type?(:def, :defs)
105
+ return parent if parent.any_def_type?
106
106
  return node.parent.child_nodes.first if define_method?(parent)
107
107
 
108
108
  find_def_node_from_ascendant(node.parent)
@@ -6,6 +6,10 @@ module RuboCop
6
6
  # Checks for the use of a method, the result of which
7
7
  # would be a literal, like an empty array, hash, or string.
8
8
  #
9
+ # NOTE: When frozen string literals are enabled, `String.new`
10
+ # isn't corrected to an empty string since the former is
11
+ # mutable and the latter would be frozen.
12
+ #
9
13
  # @example
10
14
  # # bad
11
15
  # a = Array.new
@@ -4,12 +4,12 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Ensures that eval methods (`eval`, `instance_eval`, `class_eval`
7
- # and `module_eval`) are given filename and line number values (`\_\_FILE\_\_`
8
- # and `\_\_LINE\_\_`). This data is used to ensure that any errors raised
7
+ # and `module_eval`) are given filename and line number values (`+__FILE__+`
8
+ # and `+__LINE__+`). This data is used to ensure that any errors raised
9
9
  # within the evaluated code will be given the correct identification
10
10
  # in a backtrace.
11
11
  #
12
- # The cop also checks that the line number given relative to `\_\_LINE\_\_` is
12
+ # The cop also checks that the line number given relative to `+__LINE__+` is
13
13
  # correct.
14
14
  #
15
15
  # This cop will autocorrect incorrect or missing filename and line number
@@ -65,7 +65,7 @@ module RuboCop
65
65
  yielding_block?(block_node) do |send_node, block_args, yield_args|
66
66
  return unless yielding_arguments?(block_args, yield_args)
67
67
 
68
- def_node = block_node.each_ancestor(:def, :defs).first
68
+ def_node = block_node.each_ancestor(:any_def).first
69
69
  # if `yield` is being called outside of a method context, ignore
70
70
  # this is not a valid ruby pattern, but can happen in haml or erb,
71
71
  # so this can cause crashes in haml_lint
@@ -151,7 +151,7 @@ module RuboCop
151
151
  end
152
152
 
153
153
  def build_new_arguments_for_zsuper(node)
154
- def_node = node.each_ancestor(:def, :defs).first
154
+ def_node = node.each_ancestor(:any_def).first
155
155
  def_node.arguments.map do |arg|
156
156
  arg.optarg_type? ? arg.node_parts[0] : arg.source
157
157
  end
@@ -151,7 +151,7 @@ module RuboCop
151
151
 
152
152
  def frozen_string_literal_comment(processed_source)
153
153
  processed_source.tokens.find do |token|
154
- token.text.start_with?(FROZEN_STRING_LITERAL_REGEXP)
154
+ MagicComment.parse(token.text).frozen_string_literal_specified?
155
155
  end
156
156
  end
157
157
 
@@ -189,8 +189,9 @@ module RuboCop
189
189
 
190
190
  def enable_comment(corrector)
191
191
  comment = frozen_string_literal_comment(processed_source)
192
+ replacement = MagicComment.parse(comment.text).new_frozen_string_literal(true)
192
193
 
193
- corrector.replace(line_range(comment.line), FROZEN_STRING_LITERAL_ENABLED)
194
+ corrector.replace(line_range(comment.line), replacement)
194
195
  end
195
196
 
196
197
  def insert_comment(corrector)
@@ -8,6 +8,9 @@ module RuboCop
8
8
  # reassign (possibly to redirect some stream) constants in Ruby, you'll get
9
9
  # an interpreter warning if you do so.
10
10
  #
11
+ # Additionally, `$stdout/$stderr/$stdin` can safely be accessed in a Ractor because they
12
+ # are ractor-local, while `STDOUT/STDERR/STDIN` will raise `Ractor::IsolationError`.
13
+ #
11
14
  # @safety
12
15
  # Autocorrection is unsafe because `STDOUT` and `$stdout` may point to different
13
16
  # objects, for example.
@@ -40,7 +40,6 @@ module RuboCop
40
40
  class HashFetchChain < Base
41
41
  extend AutoCorrector
42
42
  extend TargetRubyVersion
43
- include IgnoredNode
44
43
 
45
44
  MSG = 'Use `%<replacement>s` instead.'
46
45
  RESTRICT_ON_SEND = %i[fetch].freeze
@@ -76,6 +76,9 @@ module RuboCop
76
76
  # # good
77
77
  # {foo:, bar:}
78
78
  #
79
+ # # good - allowed to mix syntaxes
80
+ # {foo:, bar: baz}
81
+ #
79
82
  # @example EnforcedShorthandSyntax: never
80
83
  #
81
84
  # # bad
@@ -3,8 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Looks for uses of `\_.each_with_object({}) {...}`,
7
- # `\_.map {...}.to_h`, and `Hash[\_.map {...}]` that are actually just
6
+ # Looks for uses of `+_.each_with_object({}) {...}+`,
7
+ # `+_.map {...}.to_h+`, and `+Hash[_.map {...}]+` that are actually just
8
8
  # transforming the keys of a hash, and tries to use a simpler & faster
9
9
  # call to `transform_keys` instead.
10
10
  # It should only be enabled on Ruby version 2.5 or newer.
@@ -3,8 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Looks for uses of `\_.each_with_object({}) {...}`,
7
- # `\_.map {...}.to_h`, and `Hash[\_.map {...}]` that are actually just
6
+ # Looks for uses of `+_.each_with_object({}) {...}+`,
7
+ # `+_.map {...}.to_h+`, and `+Hash[_.map {...}]+` that are actually just
8
8
  # transforming the values of a hash, and tries to use a simpler & faster
9
9
  # call to `transform_values` instead.
10
10
  #
@@ -10,7 +10,7 @@ module RuboCop
10
10
  # `nonzero?` method is allowed by default.
11
11
  # These are customizable with `AllowedMethods` option.
12
12
  #
13
- # This cop targets only `if`s with a single `elsif` or `else` branch. The following
13
+ # This cop targets only ``if``s with a single `elsif` or `else` branch. The following
14
14
  # code will be allowed, because it has two `elsif` branches:
15
15
  #
16
16
  # [source,ruby]
@@ -42,7 +42,7 @@ module RuboCop
42
42
  return if kwarg_nodes.empty?
43
43
 
44
44
  add_offense(node) do |corrector|
45
- defining_node = node.each_ancestor(:def, :defs, :block).first
45
+ defining_node = node.each_ancestor(:any_def, :block).first
46
46
  next if processed_source.contains_comment?(arguments_range(defining_node))
47
47
  next unless node.parent.find(&:kwoptarg_type?) == node
48
48
 
@@ -54,9 +54,14 @@ module RuboCop
54
54
 
55
55
  def prefer(node)
56
56
  receiver = node.receiver.source
57
- arguments = node.arguments.map(&:source).join(', ')
58
57
  dot = node.loc.dot.source
59
- method = explicit_style? ? "call(#{arguments})" : "(#{arguments})"
58
+ call_arguments = if node.arguments.empty?
59
+ ''
60
+ else
61
+ arguments = node.arguments.map(&:source).join(', ')
62
+ "(#{arguments})"
63
+ end
64
+ method = explicit_style? ? "call#{call_arguments}" : "(#{arguments})"
60
65
 
61
66
  "#{receiver}#{dot}#{method}"
62
67
  end
@@ -212,9 +212,11 @@ module RuboCop
212
212
  end
213
213
 
214
214
  def correct_push_node(corrector, push_node)
215
+ arg_node = push_node.first_argument
215
216
  range = push_node.source_range
216
- arg_range = push_node.first_argument.source_range
217
+ arg_range = arg_node.source_range
217
218
 
219
+ corrector.wrap(arg_node, '{ ', ' }') if arg_node.hash_type? && !arg_node.braces?
218
220
  corrector.remove(range_between(range.begin_pos, arg_range.begin_pos))
219
221
  corrector.remove(range_between(arg_range.end_pos, range.end_pos))
220
222
  end
@@ -49,7 +49,7 @@ module RuboCop
49
49
 
50
50
  def inside_endless_method_def?(node)
51
51
  # parens are required around arguments inside an endless method
52
- node.each_ancestor(:def, :defs).any?(&:endless?) && node.arguments.any?
52
+ node.each_ancestor(:any_def).any?(&:endless?) && node.arguments.any?
53
53
  end
54
54
 
55
55
  def require_parentheses_for_hash_value_omission?(node) # rubocop:disable Metrics/PerceivedComplexity
@@ -230,6 +230,14 @@ module RuboCop
230
230
  node.type?(:kwsplat, :forwarded_kwrestarg)
231
231
  end
232
232
 
233
+ def wrap_arguments_with_parens(condition)
234
+ method = condition.source_range.begin.join(condition.loc.selector.end)
235
+ arguments = condition.first_argument.source_range.begin.join(condition.source_range.end)
236
+
237
+ "#{method.source}(#{arguments.source})"
238
+ end
239
+
240
+ # rubocop:disable Metrics/AbcSize
233
241
  def if_source(if_branch, arithmetic_operation)
234
242
  if branches_have_method?(if_branch.parent) && if_branch.parenthesized?
235
243
  if_branch.source.delete_suffix(')')
@@ -238,11 +246,15 @@ module RuboCop
238
246
 
239
247
  "#{if_branch.receiver.source} #{if_branch.method_name} (#{argument_source}"
240
248
  elsif if_branch.true_type?
241
- if_branch.parent.condition.source
249
+ condition = if_branch.parent.condition
250
+ return condition.source if condition.arguments.empty?
251
+
252
+ wrap_arguments_with_parens(condition)
242
253
  else
243
254
  if_branch.source
244
255
  end
245
256
  end
257
+ # rubocop:enable Metrics/AbcSize
246
258
 
247
259
  def else_source(else_branch, arithmetic_operation) # rubocop:disable Metrics/AbcSize
248
260
  if arithmetic_operation
@@ -20,7 +20,7 @@ module RuboCop
20
20
 
21
21
  MSG = 'Remove the redundant current directory path.'
22
22
  RESTRICT_ON_SEND = %i[require_relative].freeze
23
- CURRENT_DIRECTORY_PREFIX = %r{./+}.freeze
23
+ CURRENT_DIRECTORY_PREFIX = %r{\./+}.freeze
24
24
  REDUNDANT_CURRENT_DIRECTORY_PREFIX = /\A#{CURRENT_DIRECTORY_PREFIX}/.freeze
25
25
 
26
26
  def on_send(node)
@@ -181,9 +181,7 @@ module RuboCop
181
181
  ARGUMENT_TYPES.include?(next_token.type)
182
182
  end
183
183
 
184
- # rubocop:disable Metrics/AbcSize
185
184
  def argument_newline?(node)
186
- node = node.to_a.last if node.assignment?
187
185
  return false if node.parenthesized_call?
188
186
 
189
187
  node = node.children.first if node.root? && node.begin_type?
@@ -196,7 +194,6 @@ module RuboCop
196
194
  node.loc.selector.line != node.first_argument.loc.line
197
195
  end
198
196
  end
199
- # rubocop:enable Metrics/AbcSize
200
197
 
201
198
  def find_node_for_line(last_line)
202
199
  processed_source.ast.each_node do |node|
@@ -78,7 +78,7 @@ module RuboCop
78
78
  ancestor = node.ancestors.first
79
79
  return false unless ancestor
80
80
 
81
- !ancestor.type?(:begin, :def, :any_block)
81
+ !ancestor.type?(:begin, :any_def, :any_block)
82
82
  end
83
83
 
84
84
  def allowed_ternary?(node)
@@ -98,7 +98,7 @@ module RuboCop
98
98
  return false unless node.type?(:send, :super, :yield)
99
99
 
100
100
  node.arguments.one? && !node.parenthesized? &&
101
- !node.arithmetic_operation? && node.first_argument.begin_type?
101
+ !node.operator_method? && node.first_argument.begin_type?
102
102
  end
103
103
 
104
104
  def multiline_control_flow_statements?(node)
@@ -134,7 +134,9 @@ module RuboCop
134
134
  node = begin_node.children.first
135
135
 
136
136
  if (message = find_offense_message(begin_node, node))
137
- begin_node = begin_node.parent if node.range_type?
137
+ if node.range_type? && !argument_of_parenthesized_method_call?(begin_node)
138
+ begin_node = begin_node.parent
139
+ end
138
140
 
139
141
  return offense(begin_node, message)
140
142
  end
@@ -155,6 +157,7 @@ module RuboCop
155
157
  return 'an expression'
156
158
  end
157
159
  return 'an interpolated expression' if interpolation?(begin_node)
160
+ return 'a method argument' if argument_of_parenthesized_method_call?(begin_node)
158
161
 
159
162
  return if begin_node.chained?
160
163
 
@@ -177,6 +180,20 @@ module RuboCop
177
180
  # @!method interpolation?(node)
178
181
  def_node_matcher :interpolation?, '[^begin ^^dstr]'
179
182
 
183
+ def argument_of_parenthesized_method_call?(begin_node)
184
+ node = begin_node.children.first
185
+ return false if node.basic_conditional? || method_call_parentheses_required?(node)
186
+ return false unless (parent = begin_node.parent)
187
+
188
+ parent.call_type? && parent.parenthesized? && parent.receiver != begin_node
189
+ end
190
+
191
+ def method_call_parentheses_required?(node)
192
+ return false unless node.call_type?
193
+
194
+ (node.receiver.nil? || node.loc.dot) && node.arguments.any?
195
+ end
196
+
180
197
  def allow_in_multiline_conditions?
181
198
  !!config.for_enabled_cop('Style/ParenthesesAroundCondition')['AllowInMultilineConditions']
182
199
  end
@@ -47,7 +47,7 @@ module RuboCop
47
47
 
48
48
  def on_return(node)
49
49
  # Check Lint/NonLocalExitFromIterator first before this cop
50
- node.each_ancestor(:block, :def, :defs) do |n|
50
+ node.each_ancestor(:block, :any_def) do |n|
51
51
  break if scoped_node?(n)
52
52
 
53
53
  send_node, args_node, _body_node = *n
@@ -83,7 +83,7 @@ module RuboCop
83
83
  end
84
84
 
85
85
  def scoped_node?(node)
86
- node.type?(:def, :defs) || node.lambda?
86
+ node.any_def_type? || node.lambda?
87
87
  end
88
88
 
89
89
  # @!method chained_send?(node)