rubocop 1.87.0 → 1.88.2

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 (193) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +80 -72
  3. data/config/obsoletion.yml +21 -1
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +6 -0
  5. data/lib/rubocop/cop/base.rb +30 -10
  6. data/lib/rubocop/cop/bundler/gem_comment.rb +5 -3
  7. data/lib/rubocop/cop/commissioner.rb +13 -11
  8. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -1
  10. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +8 -1
  11. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  12. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +12 -2
  13. data/lib/rubocop/cop/gemspec/require_mfa.rb +4 -1
  14. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +5 -3
  15. data/lib/rubocop/cop/layout/block_alignment.rb +58 -4
  16. data/lib/rubocop/cop/layout/class_structure.rb +7 -3
  17. data/lib/rubocop/cop/layout/comment_indentation.rb +9 -2
  18. data/lib/rubocop/cop/layout/condition_position.rb +13 -3
  19. data/lib/rubocop/cop/layout/else_alignment.rb +1 -14
  20. data/lib/rubocop/cop/layout/empty_comment.rb +8 -10
  21. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +14 -1
  22. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +13 -14
  23. data/lib/rubocop/cop/layout/indentation_width.rb +28 -0
  24. data/lib/rubocop/cop/layout/line_length.rb +10 -5
  25. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +6 -1
  26. data/lib/rubocop/cop/layout/space_around_operators.rb +6 -2
  27. data/lib/rubocop/cop/lint/ambiguous_assignment.rb +1 -11
  28. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +1 -10
  29. data/lib/rubocop/cop/lint/assignment_in_condition.rb +13 -1
  30. data/lib/rubocop/cop/lint/circular_argument_reference.rb +1 -3
  31. data/lib/rubocop/cop/lint/debugger.rb +13 -2
  32. data/lib/rubocop/cop/lint/deprecated_constants.rb +1 -7
  33. data/lib/rubocop/cop/lint/empty_block.rb +3 -3
  34. data/lib/rubocop/cop/lint/ensure_return.rb +19 -1
  35. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -1
  36. data/lib/rubocop/cop/lint/float_comparison.rb +1 -0
  37. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -1
  38. data/lib/rubocop/cop/lint/interpolation_check.rb +18 -3
  39. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  40. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +11 -1
  41. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +8 -11
  42. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +4 -4
  43. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +16 -0
  44. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
  45. data/lib/rubocop/cop/lint/number_conversion.rb +13 -4
  46. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +10 -0
  47. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +7 -7
  48. data/lib/rubocop/cop/lint/raise_exception.rb +1 -1
  49. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  50. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -1
  51. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +4 -1
  52. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +15 -4
  53. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +14 -7
  54. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +4 -0
  55. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +7 -0
  56. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  57. data/lib/rubocop/cop/lint/redundant_with_object.rb +5 -0
  58. data/lib/rubocop/cop/lint/refinement_import_methods.rb +8 -1
  59. data/lib/rubocop/cop/lint/regexp_as_condition.rb +9 -1
  60. data/lib/rubocop/cop/lint/require_parentheses.rb +13 -4
  61. data/lib/rubocop/cop/lint/require_range_parentheses.rb +2 -1
  62. data/lib/rubocop/cop/lint/require_relative_self_path.rb +5 -5
  63. data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
  64. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  65. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  66. data/lib/rubocop/cop/lint/script_permission.rb +5 -1
  67. data/lib/rubocop/cop/lint/self_assignment.rb +24 -1
  68. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -1
  69. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +14 -0
  70. data/lib/rubocop/cop/lint/shared_mutable_default.rb +3 -1
  71. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +12 -0
  72. data/lib/rubocop/cop/lint/symbol_conversion.rb +21 -4
  73. data/lib/rubocop/cop/lint/to_enum_arguments.rb +35 -2
  74. data/lib/rubocop/cop/lint/to_json.rb +8 -1
  75. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  76. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -1
  77. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +35 -9
  78. data/lib/rubocop/cop/lint/unreachable_code.rb +9 -4
  79. data/lib/rubocop/cop/lint/useless_assignment.rb +10 -5
  80. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +10 -10
  81. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +7 -3
  82. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -1
  83. data/lib/rubocop/cop/lint/useless_times.rb +22 -1
  84. data/lib/rubocop/cop/lint/void.rb +2 -2
  85. data/lib/rubocop/cop/metrics/collection_literal_length.rb +1 -1
  86. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  87. data/lib/rubocop/cop/metrics/perceived_complexity.rb +38 -7
  88. data/lib/rubocop/cop/mixin/allowed_methods.rb +5 -5
  89. data/lib/rubocop/cop/mixin/allowed_pattern.rb +5 -1
  90. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +5 -1
  91. data/lib/rubocop/cop/mixin/hash_subset.rb +8 -0
  92. data/lib/rubocop/cop/mixin/hash_transform_method.rb +4 -0
  93. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  94. data/lib/rubocop/cop/naming/file_name.rb +4 -3
  95. data/lib/rubocop/cop/naming/inclusive_language.rb +8 -2
  96. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +9 -0
  97. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +9 -3
  98. data/lib/rubocop/cop/security/io_methods.rb +1 -1
  99. data/lib/rubocop/cop/security/marshal_load.rb +1 -1
  100. data/lib/rubocop/cop/style/accessor_grouping.rb +11 -1
  101. data/lib/rubocop/cop/style/alias.rb +1 -1
  102. data/lib/rubocop/cop/style/and_or.rb +1 -1
  103. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  104. data/lib/rubocop/cop/style/array_first_last.rb +12 -1
  105. data/lib/rubocop/cop/style/array_intersect.rb +8 -4
  106. data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +3 -0
  107. data/lib/rubocop/cop/style/block_delimiters.rb +16 -2
  108. data/lib/rubocop/cop/style/case_equality.rb +14 -2
  109. data/lib/rubocop/cop/style/class_equality_comparison.rb +21 -13
  110. data/lib/rubocop/cop/style/class_methods_definitions.rb +11 -5
  111. data/lib/rubocop/cop/style/collection_compact.rb +1 -1
  112. data/lib/rubocop/cop/style/colon_method_call.rb +13 -6
  113. data/lib/rubocop/cop/style/combinable_loops.rb +5 -0
  114. data/lib/rubocop/cop/style/comparable_clamp.rb +12 -1
  115. data/lib/rubocop/cop/style/concat_array_literals.rb +5 -1
  116. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -1
  117. data/lib/rubocop/cop/style/constant_visibility.rb +4 -1
  118. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  119. data/lib/rubocop/cop/style/date_time.rb +2 -2
  120. data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -2
  121. data/lib/rubocop/cop/style/dig_chain.rb +5 -0
  122. data/lib/rubocop/cop/style/dir_empty.rb +4 -0
  123. data/lib/rubocop/cop/style/documentation_method.rb +5 -1
  124. data/lib/rubocop/cop/style/empty_case_condition.rb +12 -2
  125. data/lib/rubocop/cop/style/empty_class_definition.rb +8 -1
  126. data/lib/rubocop/cop/style/empty_heredoc.rb +4 -0
  127. data/lib/rubocop/cop/style/empty_literal.rb +7 -2
  128. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +30 -20
  129. data/lib/rubocop/cop/style/env_home.rb +4 -0
  130. data/lib/rubocop/cop/style/even_odd.rb +11 -1
  131. data/lib/rubocop/cop/style/exact_regexp_match.rb +8 -1
  132. data/lib/rubocop/cop/style/fetch_env_var.rb +1 -1
  133. data/lib/rubocop/cop/style/file_null.rb +4 -2
  134. data/lib/rubocop/cop/style/file_write.rb +17 -14
  135. data/lib/rubocop/cop/style/format_string.rb +13 -1
  136. data/lib/rubocop/cop/style/hash_conversion.rb +14 -6
  137. data/lib/rubocop/cop/style/hash_lookup_method.rb +2 -6
  138. data/lib/rubocop/cop/style/hash_slice.rb +16 -0
  139. data/lib/rubocop/cop/style/hash_syntax.rb +2 -0
  140. data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -4
  141. data/lib/rubocop/cop/style/if_unless_modifier.rb +5 -5
  142. data/lib/rubocop/cop/style/if_with_semicolon.rb +9 -1
  143. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  144. data/lib/rubocop/cop/style/invertible_unless_condition.rb +25 -5
  145. data/lib/rubocop/cop/style/keyword_arguments_merging.rb +4 -0
  146. data/lib/rubocop/cop/style/keyword_parameters_order.rb +7 -3
  147. data/lib/rubocop/cop/style/lambda.rb +7 -1
  148. data/lib/rubocop/cop/style/lambda_call.rb +11 -0
  149. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +11 -0
  150. data/lib/rubocop/cop/style/map_into_array.rb +1 -1
  151. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -5
  152. data/lib/rubocop/cop/style/method_def_parentheses.rb +5 -1
  153. data/lib/rubocop/cop/style/min_max_comparison.rb +3 -0
  154. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +10 -7
  155. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  156. data/lib/rubocop/cop/style/module_function.rb +4 -2
  157. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  158. data/lib/rubocop/cop/style/multiline_memoization.rb +7 -1
  159. data/lib/rubocop/cop/style/multiline_method_signature.rb +11 -4
  160. data/lib/rubocop/cop/style/mutable_constant.rb +105 -11
  161. data/lib/rubocop/cop/style/nil_lambda.rb +8 -0
  162. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  163. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  164. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  165. data/lib/rubocop/cop/style/optional_arguments.rb +1 -0
  166. data/lib/rubocop/cop/style/parallel_assignment.rb +19 -3
  167. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  168. data/lib/rubocop/cop/style/perl_backrefs.rb +5 -3
  169. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +13 -1
  170. data/lib/rubocop/cop/style/redundant_exception.rb +6 -0
  171. data/lib/rubocop/cop/style/redundant_filter_chain.rb +1 -1
  172. data/lib/rubocop/cop/style/redundant_format.rb +29 -0
  173. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +6 -1
  174. data/lib/rubocop/cop/style/redundant_line_continuation.rb +11 -3
  175. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -4
  176. data/lib/rubocop/cop/style/redundant_self.rb +9 -0
  177. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +23 -4
  178. data/lib/rubocop/cop/style/semicolon.rb +20 -5
  179. data/lib/rubocop/cop/style/single_line_do_end_block.rb +17 -4
  180. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -0
  181. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -1
  182. data/lib/rubocop/cop/style/ternary_parentheses.rb +11 -0
  183. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -8
  184. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  185. data/lib/rubocop/cop/style/while_until_do.rb +7 -0
  186. data/lib/rubocop/cop/style/word_array.rb +1 -0
  187. data/lib/rubocop/cop/style/zero_length_predicate.rb +6 -3
  188. data/lib/rubocop/cop/team.rb +18 -4
  189. data/lib/rubocop/formatter/disabled_config_formatter.rb +14 -7
  190. data/lib/rubocop/runner.rb +55 -13
  191. data/lib/rubocop/server/core.rb +6 -0
  192. data/lib/rubocop/version.rb +1 -1
  193. metadata +3 -3
@@ -331,12 +331,11 @@ module RuboCop
331
331
  # @api private
332
332
  def self.callbacks_needed
333
333
  @callbacks_needed ||= public_instance_methods.select do |m|
334
- # OPTIMIZE: Check method existence first to make fewer `start_with?` calls.
335
- # At the time of writing this comment, this excludes 98 of ~104 methods.
336
- # `start_with?` with two string arguments instead of a regex is faster
337
- # in this specific case as well.
338
- !Base.method_defined?(m) && # exclude standard "callbacks" like 'on_begin_investigation'
339
- m.start_with?('on_', 'after_')
334
+ # OPTIMIZE: `start_with?` with two string arguments instead of a regex
335
+ # is faster in this specific case.
336
+ m.start_with?('on_', 'after_') &&
337
+ # exclude standard "callbacks" like 'on_new_investigation' unless refined
338
+ instance_method(m).owner != Base
340
339
  end
341
340
  end
342
341
  # rubocop:enable Layout/ClassStructure
@@ -498,8 +497,23 @@ module RuboCop
498
497
  patterns = cop_config[parameter]
499
498
  return default_result unless patterns
500
499
 
501
- patterns = FilePatterns.from(patterns)
502
- patterns.match?(config.path_relative_to_config(file)) || patterns.match?(file)
500
+ file_patterns = FilePatterns.from(patterns)
501
+ relative_file_path = config.path_relative_to_config(file)
502
+ return true if file_patterns.match?(relative_file_path)
503
+
504
+ if parameter == 'Include' && !relative_file_path.start_with?('..')
505
+ matches_absolute_include_pattern?(patterns, file)
506
+ else
507
+ file_patterns.match?(file)
508
+ end
509
+ end
510
+
511
+ def matches_absolute_include_pattern?(patterns, file)
512
+ absolute_file_path = absolute?(file) ? file : File.expand_path(file)
513
+ patterns.any? do |pattern|
514
+ (absolute?(pattern.to_s) || pattern.to_s.start_with?('..')) &&
515
+ match_path?(pattern, absolute_file_path)
516
+ end
503
517
  end
504
518
 
505
519
  def enabled_line?(line_number)
@@ -530,15 +544,21 @@ module RuboCop
530
544
  end
531
545
 
532
546
  def range_for_original(range)
547
+ buffer = @current_original.buffer
548
+ return range if @current_offset.zero? && range.source_buffer.equal?(buffer)
549
+
533
550
  ::Parser::Source::Range.new(
534
- @current_original.buffer,
551
+ buffer,
535
552
  range.begin_pos + @current_offset,
536
553
  range.end_pos + @current_offset
537
554
  )
538
555
  end
539
556
 
540
557
  def target_satisfies_all_gem_version_requirements?
541
- self.class.gem_requirements.all? do |gem_name, version_req|
558
+ gem_requirements = self.class.gem_requirements
559
+ return true if gem_requirements.empty?
560
+
561
+ gem_requirements.all? do |gem_name, version_req|
542
562
  all_gem_versions_in_target = @config.gem_versions_in_target
543
563
  next false unless all_gem_versions_in_target
544
564
 
@@ -128,8 +128,8 @@ module RuboCop
128
128
  end
129
129
 
130
130
  def ignored_gem?(node)
131
- ignored_gems = Array(cop_config['IgnoredGems'])
132
- ignored_gems.include?(node.first_argument.value)
131
+ allowed_gems = Array(cop_config['AllowedGems'])
132
+ allowed_gems.include?(node.first_argument.value)
133
133
  end
134
134
 
135
135
  def checked_options_present?(node)
@@ -163,7 +163,9 @@ module RuboCop
163
163
  def gem_options(node)
164
164
  return [] unless node.last_argument&.hash_type?
165
165
 
166
- node.last_argument.keys.map(&:value)
166
+ # Only literal keys carry an option name to check; a non-literal key
167
+ # (e.g. a variable or method call) has no `value` and must be skipped.
168
+ node.last_argument.keys.filter_map { |key| key.value if key.type?(:sym, :str) }
167
169
  end
168
170
  end
169
171
  end
@@ -64,14 +64,16 @@ module RuboCop
64
64
  r = '#' unless RESTRICTED_CALLBACKS.include?(method_name) # has Restricted?
65
65
  c = '#' if NO_CHILD_NODES.include?(node_type) # has Children?
66
66
 
67
+ # The `after_` calls are guarded because barely any cop defines an `after_`
68
+ # callback, and the guard is cheaper than the method call it avoids.
67
69
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
68
- def on_#{node_type}(node) # def on_send(node)
69
- trigger_responding_cops(:on_#{node_type}, node) # trigger_responding_cops(:on_send, node)
70
- #{r} trigger_restricted_cops(:on_#{node_type}, node) # trigger_restricted_cops(:on_send, node)
71
- #{c} super(node) # super(node)
72
- #{c} trigger_responding_cops(:after_#{node_type}, node) # trigger_responding_cops(:after_send, node)
73
- #{c}#{r} trigger_restricted_cops(:after_#{node_type}, node) # trigger_restricted_cops(:after_send, node)
74
- end # end
70
+ def on_#{node_type}(node) # def on_send(node)
71
+ trigger_responding_cops(:on_#{node_type}, node) # trigger_responding_cops(:on_send, node)
72
+ #{r} trigger_restricted_cops(:on_#{node_type}, node) # trigger_restricted_cops(:on_send, node)
73
+ #{c} super(node) # super(node)
74
+ #{c} trigger_responding_cops(:after_#{node_type}, node) if @callbacks[:after_#{node_type}] # trigger_responding_cops(:after_send, node) if @callbacks[:after_send]
75
+ #{c}#{r} trigger_restricted_cops(:after_#{node_type}, node) unless @restricted_map[:after_#{node_type}].empty? # trigger_restricted_cops(:after_send, node) unless @restricted_map[:after_send].empty?
76
+ end # end
75
77
  RUBY
76
78
  end
77
79
 
@@ -81,13 +83,13 @@ module RuboCop
81
83
 
82
84
  begin_investigation(processed_source, offset: offset, original: original)
83
85
  if processed_source.valid_syntax?
84
- invoke(:on_new_investigation, @cops)
86
+ invoke(:on_new_investigation, @callbacks[:on_new_investigation])
85
87
  invoke_with_argument(:investigate, @forces, processed_source)
86
88
 
87
89
  walk(processed_source.ast) unless @cops.empty?
88
- invoke(:on_investigation_end, @cops)
90
+ invoke(:on_investigation_end, @callbacks[:on_investigation_end])
89
91
  else
90
- invoke(:on_other_file, @cops)
92
+ invoke(:on_other_file, @callbacks[:on_other_file])
91
93
  end
92
94
  reports = @cops.map { |cop| cop.send(:complete_investigation) }
93
95
  InvestigationReport.new(processed_source, reports, @errors)
@@ -157,7 +159,7 @@ module RuboCop
157
159
  end
158
160
 
159
161
  def invoke(callback, cops)
160
- cops.each { |cop| with_cop_error_handling(cop) { cop.send(callback) } }
162
+ cops&.each { |cop| with_cop_error_handling(cop) { cop.send(callback) } }
161
163
  end
162
164
 
163
165
  def invoke_with_argument(callback, cops, arg)
@@ -27,7 +27,7 @@ module RuboCop
27
27
  if block_node.arguments?
28
28
  format(CORRECTION_WITH_ARGUMENTS,
29
29
  collection: collection_node.source,
30
- variables: argument_node.children.first.source)
30
+ variables: argument_node.children.map(&:source).join(', '))
31
31
  else
32
32
  format(CORRECTION_WITHOUT_ARGUMENTS, enumerable: collection_node.source)
33
33
  end
@@ -86,7 +86,13 @@ module RuboCop
86
86
  end
87
87
 
88
88
  def lambda_arg_string
89
- arguments.children.map(&:source).join(', ')
89
+ # Block-local (shadow) arguments are separated from regular arguments by a
90
+ # `;`; joining everything with `,` would turn them into extra parameters
91
+ # and change the lambda's arity.
92
+ regular, shadow = arguments.children.partition { |arg| !arg.shadowarg_type? }
93
+ arg_string = regular.map(&:source).join(', ')
94
+ arg_string += "; #{shadow.map(&:source).join(', ')}" unless shadow.empty?
95
+ arg_string
90
96
  end
91
97
 
92
98
  def needs_separating_space?
@@ -18,7 +18,14 @@ module RuboCop
18
18
  current_range = declaration_with_comment(node)
19
19
  previous_range = declaration_with_comment(previous_declaration)
20
20
 
21
- ->(corrector) { corrector.swap(current_range, previous_range) }
21
+ lambda do |corrector|
22
+ if current_range.source.end_with?("\n")
23
+ corrector.swap(current_range, previous_range)
24
+ else
25
+ corrector.insert_before(previous_range, "#{current_range.source}\n")
26
+ corrector.remove(current_range)
27
+ end
28
+ end
22
29
  end
23
30
 
24
31
  private
@@ -95,7 +95,7 @@ module RuboCop
95
95
  private
96
96
 
97
97
  def forbidden_gem?(gem_name)
98
- !cop_config['AllowedGems'].include?(gem_name)
98
+ !Array(cop_config['AllowedGems']).include?(gem_name)
99
99
  end
100
100
 
101
101
  def message(_range)
@@ -83,18 +83,28 @@ module RuboCop
83
83
  def duplicated_assignment_method_nodes
84
84
  assignment_method_declarations(processed_source.ast)
85
85
  .select(&:assignment_method?)
86
- .group_by(&:method_name)
86
+ .group_by { |node| [enclosing_specification(node), node.method_name] }
87
87
  .values
88
88
  .select { |nodes| nodes.size > 1 }
89
89
  end
90
90
 
91
91
  def duplicated_indexed_assignment_method_nodes
92
92
  indexed_assignment_method_declarations(processed_source.ast)
93
- .group_by { |node| [node.children.first.method_name, node.first_argument] }
93
+ .group_by { |node| indexed_assignment_key(node) }
94
94
  .values
95
95
  .select { |nodes| nodes.size > 1 }
96
96
  end
97
97
 
98
+ def indexed_assignment_key(node)
99
+ [enclosing_specification(node), node.children.first.method_name, node.first_argument]
100
+ end
101
+
102
+ # Assignments in separate `Gem::Specification.new` blocks are not duplicates of
103
+ # one another, so the enclosing specification is part of the grouping key.
104
+ def enclosing_specification(node)
105
+ node.each_ancestor(:block).find { |block| gem_specification?(block) }
106
+ end
107
+
98
108
  def register_offense(node, assignment, line_of_first_occurrence)
99
109
  line_range = node.loc.column...node.loc.last_column
100
110
  offense_location = source_range(processed_source.buffer, node.first_line, line_range)
@@ -143,7 +143,10 @@ module RuboCop
143
143
  #{block_var}.metadata['rubygems_mfa_required'] = 'true'
144
144
  RUBY
145
145
 
146
- if (last_assignment = metadata_assignment(processed_source.ast).to_a.last)
146
+ # Scope the search to the current spec block. Searching the whole file
147
+ # would, for a second `Gem::Specification.new` block, insert the directive
148
+ # into the first block, leaving this block uncorrected and looping forever.
149
+ if (last_assignment = metadata_assignment(node).to_a.last)
147
150
  corrector.insert_after(last_assignment, "\n#{require_mfa_directive}")
148
151
  else
149
152
  corrector.insert_before(node.loc.end, "#{require_mfa_directive}\n")
@@ -46,8 +46,7 @@ module RuboCop
46
46
 
47
47
  def on_block(node)
48
48
  return unless let_rubocop_config_new?(node)
49
-
50
- describe = find_describe_method_node(node)
49
+ return unless (describe = find_describe_method_node(node))
51
50
 
52
51
  unless (exist_config = describe.last_argument.source == ':config')
53
52
  additional_message = ' and specify `:config` in `describe`'
@@ -65,7 +64,10 @@ module RuboCop
65
64
  private
66
65
 
67
66
  def find_describe_method_node(block_node)
68
- block_node.ancestors.find { |node| node.block_type? && node.method?(:describe) }.send_node
67
+ describe = block_node.ancestors.find do |ancestor|
68
+ ancestor.block_type? && ancestor.method?(:describe)
69
+ end
70
+ describe&.send_node
69
71
  end
70
72
  end
71
73
  end
@@ -18,6 +18,10 @@ module RuboCop
18
18
  # `either` (which is the default) : the `end` is allowed to be in either
19
19
  # location. The autocorrect will default to `start_of_line`.
20
20
  #
21
+ # When the `do` or `{` appears on a continuation line of multiline
22
+ # method arguments, the start of the line where the method is called
23
+ # is used as the alignment target instead of that continuation line.
24
+ #
21
25
  # @example EnforcedStyleAlignWith: either (default)
22
26
  # # bad
23
27
  #
@@ -116,6 +120,7 @@ module RuboCop
116
120
  end_loc = block_node.loc.end
117
121
  return unless begins_its_line?(end_loc)
118
122
 
123
+ start_node = start_for_line_node(block_node) if style == :start_of_line
119
124
  start_loc = start_node.source_range
120
125
  return unless start_loc.column != end_loc.column || style == :start_of_block
121
126
 
@@ -196,23 +201,34 @@ module RuboCop
196
201
 
197
202
  def compute_do_source_line_column(node, end_loc)
198
203
  do_loc = node.loc.begin # Actually it's either do or {.
204
+ anchor_loc = do_line_anchor_loc(node, do_loc)
199
205
 
200
206
  # We've found that "end" is not aligned with the start node (which
201
207
  # can be a block, a variable assignment, etc). But we also allow
202
208
  # the "end" to be aligned with the start of the line where the "do"
203
209
  # is, which is a style some people use in multi-line chains of
204
210
  # blocks.
205
- match = /\S.*/.match(do_loc.source_line)
211
+ match = /\S.*/.match(anchor_loc.source_line)
206
212
  indentation_of_do_line = match.begin(0)
207
- return unless end_loc.column != indentation_of_do_line || style == :start_of_line
213
+ permitted_columns = permitted_do_line_columns(do_loc, indentation_of_do_line)
214
+ return if permitted_columns.include?(end_loc.column) && style != :start_of_line
208
215
 
209
216
  {
210
217
  source: match[0],
211
- line: do_loc.line,
218
+ line: anchor_loc.line,
212
219
  column: indentation_of_do_line
213
220
  }
214
221
  end
215
222
 
223
+ # `end` aligned with an argument continuation line that holds the `do`
224
+ # was accepted before the anchor moved to the method dispatch line;
225
+ # keep accepting it so that such code does not become an offense.
226
+ def permitted_do_line_columns(do_loc, indentation_of_do_line)
227
+ columns = [indentation_of_do_line]
228
+ columns << (do_loc.source_line =~ /\S/) if style == :either
229
+ columns
230
+ end
231
+
216
232
  def loc_to_source_line_column(loc)
217
233
  {
218
234
  source: loc.source.lines.to_a.first.chomp,
@@ -239,7 +255,7 @@ module RuboCop
239
255
  def compute_start_col(ancestor_node, node)
240
256
  if style == :start_of_block
241
257
  do_loc = node.loc.begin
242
- return do_loc.source_line =~ /\S/
258
+ return do_line_anchor_loc(node, do_loc).source_line =~ /\S/
243
259
  end
244
260
  (ancestor_node || node).source_range.column
245
261
  end
@@ -253,6 +269,44 @@ module RuboCop
253
269
 
254
270
  corrector.remove(range)
255
271
  end
272
+
273
+ # When the `do` or `{` is on a continuation line of multiline method
274
+ # arguments, the indentation of that line is not a meaningful
275
+ # alignment target; anchor on the method dispatch position instead.
276
+ def do_line_anchor_loc(node, do_loc)
277
+ if do_line_begins_inside_argument?(node, do_loc)
278
+ node.send_node.selector || node.send_node.source_range
279
+ else
280
+ do_loc
281
+ end
282
+ end
283
+
284
+ # rubocop:disable Metrics/AbcSize
285
+ def do_line_begins_inside_argument?(node, do_loc)
286
+ line_begin_pos = do_loc.begin_pos - do_loc.column
287
+ first_char_pos = line_begin_pos + (do_loc.source_line =~ /\S/)
288
+ return false unless inside_parentheses?(node, first_char_pos)
289
+
290
+ (node.send_node.arguments + node.arguments).any? do |argument|
291
+ argument.source_range.begin_pos <= first_char_pos &&
292
+ first_char_pos < argument.source_range.end_pos
293
+ end
294
+ end
295
+ # rubocop:enable Metrics/AbcSize
296
+
297
+ # The continuation line indentation is only an unmeaningful alignment target when
298
+ # it is dictated by an opening delimiter, i.e. the line begins inside `(` or `[`.
299
+ # For a bare argument list without parentheses the indentation is the author's
300
+ # deliberate alignment, so the anchor must not move to the method dispatch line.
301
+ def inside_parentheses?(node, pos)
302
+ preceding_tokens = processed_source.tokens.select do |token|
303
+ token.begin_pos >= node.source_range.begin_pos && token.begin_pos < pos
304
+ end
305
+ opens = preceding_tokens.count { |token| token.left_parens? || token.left_bracket? }
306
+ closes = preceding_tokens.count { |token| token.right_parens? || token.right_bracket? }
307
+
308
+ opens > closes
309
+ end
256
310
  end
257
311
  end
258
312
  end
@@ -278,10 +278,14 @@ module RuboCop
278
278
 
279
279
  return [] unless class_def
280
280
 
281
- if class_def.type?(:def, :send)
282
- [class_def]
283
- else
281
+ # Only a multi-statement body (`begin`/`kwbegin`) wraps several elements; any
282
+ # single statement (`def`, `send`, `csend`, `if`, ...) is itself the sole element.
283
+ # Exploding such a node into its children would yield non-node values (e.g. a
284
+ # method-name `Symbol` from a `csend`) and crash later checks.
285
+ if class_def.type?(:begin, :kwbegin)
284
286
  class_def.children.compact
287
+ else
288
+ [class_def]
285
289
  end
286
290
  end
287
291
 
@@ -155,8 +155,15 @@ module RuboCop
155
155
 
156
156
  def less_indented?(line)
157
157
  rule = config.for_cop('Layout/AccessModifierIndentation')['EnforcedStyle'] == 'outdent'
158
- access_modifier = 'private|protected|public'
159
- /\A\s*(end\b|[)}\]])/.match?(line) || (rule && /\A\s*(#{access_modifier})\b/.match?(line))
158
+ /\A\s*(end\b|[)}\]])/.match?(line) || (rule && bare_access_modifier?(line))
159
+ end
160
+
161
+ # Only a bare access modifier (the keyword alone on its line) is outdented by
162
+ # `Layout/AccessModifierIndentation`. An inline modifier such as `private def foo`
163
+ # keeps the regular method indentation, so a comment above it must not be pushed
164
+ # one level deeper.
165
+ def bare_access_modifier?(line)
166
+ /\A\s*(private|protected|public)\s*(#.*)?\z/.match?(line)
160
167
  end
161
168
 
162
169
  def two_alternatives?(line)
@@ -44,16 +44,26 @@ module RuboCop
44
44
  message = message(condition)
45
45
 
46
46
  add_offense(condition, message: message) do |corrector|
47
- range = range_by_whole_lines(condition.source_range, include_final_newline: true)
48
-
49
47
  corrector.insert_after(condition.parent.loc.keyword, " #{condition.source}")
50
- corrector.remove(range)
48
+ corrector.remove(removal_range(node, condition))
51
49
  end
52
50
  end
53
51
 
54
52
  def message(condition)
55
53
  format(MSG, keyword: condition.parent.keyword)
56
54
  end
55
+
56
+ # When a body statement shares the condition's line (e.g. `while\n cond; body\nend`),
57
+ # removing the whole line would delete the body too. In that case only remove the
58
+ # condition and its trailing separator, preserving the body statement.
59
+ def removal_range(node, condition)
60
+ body = node.body
61
+ if body && body.source_range.line == condition.source_range.last_line
62
+ range_between(condition.source_range.begin_pos, body.source_range.begin_pos)
63
+ else
64
+ range_by_whole_lines(condition.source_range, include_final_newline: true)
65
+ end
66
+ end
57
67
  end
58
68
  end
59
69
  end
@@ -92,13 +92,7 @@ module RuboCop
92
92
  case parent.type
93
93
  when :def, :defs then base_for_method_definition(parent)
94
94
  when :kwbegin then parent.loc.begin
95
- when :block, :numblock, :itblock
96
- assignment_node = assignment_node(parent)
97
- if same_line?(parent, assignment_node)
98
- assignment_node.source_range
99
- else
100
- parent.send_node.source_range
101
- end
95
+ when :block, :numblock, :itblock then start_line_range(parent)
102
96
  else node.loc.keyword
103
97
  end
104
98
  end
@@ -143,13 +137,6 @@ module RuboCop
143
137
  autocorrect(corrector, else_range)
144
138
  end
145
139
  end
146
-
147
- def assignment_node(node)
148
- assignment_node = node.ancestors.first
149
- return unless assignment_node&.assignment?
150
-
151
- assignment_node
152
- end
153
140
  end
154
141
  end
155
142
  end
@@ -95,8 +95,7 @@ module RuboCop
95
95
  end
96
96
 
97
97
  def autocorrect(corrector, node)
98
- previous_token = previous_token(node)
99
- range = if previous_token && same_line?(node, previous_token)
98
+ range = if inline_comment?(node)
100
99
  range_with_surrounding_space(node.source_range, newlines: false)
101
100
  else
102
101
  range_by_whole_lines(node.source_range, include_final_newline: true)
@@ -138,14 +137,13 @@ module RuboCop
138
137
  cop_config['AllowMarginComment']
139
138
  end
140
139
 
141
- def current_token(comment)
142
- processed_source.tokens.find { |token| token.pos == comment.source_range }
143
- end
144
-
145
- def previous_token(node)
146
- current_token = current_token(node)
147
- index = processed_source.tokens.index(current_token)
148
- index.zero? ? nil : processed_source.tokens[index - 1]
140
+ # A comment is inline when code precedes it on the same line. Detecting this
141
+ # from the source (rather than the token stream) is required because, for a
142
+ # comment trailing a heredoc opener, the preceding token is the heredoc end on
143
+ # a later line, which would wrongly trigger whole-line removal of the opener.
144
+ def inline_comment?(comment)
145
+ preceding_source = processed_source.lines[comment.loc.line - 1][0...comment.loc.column]
146
+ !preceding_source.strip.empty?
149
147
  end
150
148
  end
151
149
  end
@@ -274,7 +274,20 @@ module RuboCop
274
274
  end
275
275
 
276
276
  def end_loc(node)
277
- node.source_range.end
277
+ end_location = node.source_range.end
278
+ trailing_heredoc_end(node, end_location) || end_location
279
+ end
280
+
281
+ # For an endless method whose body is a heredoc (e.g. `def a = <<~TEXT`), the
282
+ # node's source range ends at the heredoc opening line, before the heredoc body.
283
+ # Use the heredoc's closing delimiter so the def's real end is located after the
284
+ # heredoc and blank-line insertion does not land inside the heredoc body.
285
+ def trailing_heredoc_end(node, end_location)
286
+ heredocs = node.each_descendant(:any_str).select(&:heredoc?)
287
+ return if heredocs.empty?
288
+
289
+ heredoc_end = heredocs.map { |heredoc| heredoc.loc.heredoc_end }.max_by(&:end_pos)
290
+ heredoc_end if heredoc_end.end_pos > end_location.end_pos
278
291
  end
279
292
 
280
293
  def autocorrect_remove_lines(corrector, newline_pos, count)
@@ -28,10 +28,10 @@ module RuboCop
28
28
  # # bad
29
29
  # hash = {
30
30
  # key: :value
31
- # }
32
- # and_in_a_method_call({
33
- # no: :difference
34
- # })
31
+ # }
32
+ # in_a_method_call({
33
+ # foo: :bar
34
+ # })
35
35
  # takes_multi_pairs_hash(x: {
36
36
  # a: 1,
37
37
  # b: 2
@@ -42,13 +42,12 @@ module RuboCop
42
42
  # })
43
43
  #
44
44
  # # good
45
- # special_inside_parentheses
46
45
  # hash = {
47
46
  # key: :value
48
47
  # }
49
- # but_in_a_method_call({
50
- # its_like: :this
51
- # })
48
+ # in_a_method_call({
49
+ # foo: :bar
50
+ # })
52
51
  # takes_multi_pairs_hash(x: {
53
52
  # a: 1,
54
53
  # b: 2
@@ -67,17 +66,17 @@ module RuboCop
67
66
  # # bad
68
67
  # hash = {
69
68
  # key: :value
70
- # }
71
- # but_in_a_method_call({
72
- # its_like: :this
73
- # })
69
+ # }
70
+ # in_a_method_call({
71
+ # foo: :bar
72
+ # })
74
73
  #
75
74
  # # good
76
75
  # hash = {
77
76
  # key: :value
78
77
  # }
79
- # and_in_a_method_call({
80
- # no: :difference
78
+ # in_a_method_call({
79
+ # foo: :bar
81
80
  # })
82
81
  #
83
82
  #
@@ -25,6 +25,17 @@ module RuboCop
25
25
  # end
26
26
  # end
27
27
  #
28
+ # @example
29
+ # # bad
30
+ # value = (
31
+ # foo - bar
32
+ # )
33
+ #
34
+ # # good
35
+ # value = (
36
+ # foo - bar
37
+ # )
38
+ #
28
39
  # @example AllowedPatterns: ['^\s*module']
29
40
  # # bad
30
41
  # module A
@@ -95,6 +106,16 @@ module RuboCop
95
106
  check_indentation(node.loc.end, node.children.first)
96
107
  end
97
108
 
109
+ def on_begin(node)
110
+ # Only a parenthesized grouping expression (e.g. `(\n foo\n)`) has
111
+ # explicit delimiters. Indent the body one step from the line
112
+ # the opening parenthesis is on, but only when the closing parenthesis is
113
+ # first on its line (a body on the opening line is skipped downstream).
114
+ return unless parentheses?(node) && begins_its_line?(node.loc.end)
115
+
116
+ check_indentation(opening_line_start(node.loc.begin), node.children.first)
117
+ end
118
+
98
119
  def on_block(node)
99
120
  end_loc = node.loc.end
100
121
 
@@ -188,6 +209,13 @@ module RuboCop
188
209
  AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
189
210
  end
190
211
 
212
+ # Returns a range at the first non-space column of the line the opening parenthesis is on,
213
+ # used as the base to indent the body from.
214
+ def opening_line_start(begin_loc)
215
+ column = begin_loc.source_line =~ /\S/
216
+ source_range(processed_source.buffer, begin_loc.line, column)
217
+ end
218
+
191
219
  def check_members(base, members)
192
220
  check_indentation(base, select_check_member(members.first))
193
221
 
@@ -420,11 +420,16 @@ module RuboCop
420
420
  # The maximum allowed length of a string value is:
421
421
  # `Max` - end delimiter (quote) - continuation characters (space and slash)
422
422
  max_length = max - 3
423
- # If the string is on the same line as its parent, offset by the column difference
424
- # (Only apply when on same line to avoid negative offsets for multi-line dstr)
425
- if same_line?(node, node.parent)
426
- max_length -= column_offset_between(node.loc, node.parent.loc)
427
- end
423
+ # Offset by the string's starting column so the broken line actually fits
424
+ # within `Max`. When on the same line as its parent, use the column difference;
425
+ # otherwise the string is indented on its own line, so subtract that indentation.
426
+ # (Without this, an indented string under a multi-line parent never shortens
427
+ # below `Max` and the autocorrect loops, inserting empty `"" \` fragments.)
428
+ max_length -= if same_line?(node, node.parent)
429
+ column_offset_between(node.loc, node.parent.loc)
430
+ else
431
+ node.loc.column
432
+ end
428
433
  node.source[0...(max_length)]
429
434
  end
430
435
  end