rubocop 1.41.1 → 1.45.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +97 -31
  5. data/lib/rubocop/cli.rb +55 -9
  6. data/lib/rubocop/config.rb +7 -7
  7. data/lib/rubocop/config_loader.rb +12 -15
  8. data/lib/rubocop/config_loader_resolver.rb +8 -5
  9. data/lib/rubocop/cop/base.rb +89 -70
  10. data/lib/rubocop/cop/commissioner.rb +8 -2
  11. data/lib/rubocop/cop/cop.rb +51 -31
  12. data/lib/rubocop/cop/corrector.rb +30 -10
  13. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
  14. data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
  15. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  16. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
  17. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  18. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
  19. data/lib/rubocop/cop/layout/class_structure.rb +31 -23
  20. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
  21. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  23. data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
  24. data/lib/rubocop/cop/layout/indentation_style.rb +4 -1
  25. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +6 -6
  26. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  27. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  28. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  29. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
  30. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
  31. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
  32. data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -2
  33. data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
  34. data/lib/rubocop/cop/lint/debugger.rb +8 -27
  35. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
  36. data/lib/rubocop/cop/lint/else_layout.rb +2 -6
  37. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
  38. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
  39. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  40. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  41. data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
  42. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +19 -0
  43. data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
  44. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  45. data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
  46. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
  47. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
  48. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
  49. data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
  50. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +14 -4
  51. data/lib/rubocop/cop/lint/void.rb +19 -10
  52. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  53. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  54. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  55. data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
  56. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  57. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
  58. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  59. data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
  60. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  61. data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
  62. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +57 -23
  63. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
  64. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  65. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -0
  66. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  67. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  68. data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
  69. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  70. data/lib/rubocop/cop/registry.rb +34 -29
  71. data/lib/rubocop/cop/security/compound_hash.rb +2 -1
  72. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
  73. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
  74. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  75. data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
  76. data/lib/rubocop/cop/style/class_and_module_children.rb +3 -10
  77. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  78. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  79. data/lib/rubocop/cop/style/concat_array_literals.rb +22 -2
  80. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
  81. data/lib/rubocop/cop/style/documentation.rb +1 -1
  82. data/lib/rubocop/cop/style/documentation_method.rb +6 -0
  83. data/lib/rubocop/cop/style/guard_clause.rb +11 -7
  84. data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
  85. data/lib/rubocop/cop/style/hash_syntax.rb +11 -7
  86. data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -0
  87. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  88. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  89. data/lib/rubocop/cop/style/map_to_set.rb +61 -0
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
  91. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
  92. data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
  93. data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
  94. data/lib/rubocop/cop/style/missing_else.rb +13 -1
  95. data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
  96. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  97. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
  98. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
  99. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
  100. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
  101. data/lib/rubocop/cop/style/operator_method_call.rb +16 -2
  102. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
  103. data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
  104. data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
  105. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
  106. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
  107. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  108. data/lib/rubocop/cop/style/redundant_string_escape.rb +4 -2
  109. data/lib/rubocop/cop/style/require_order.rb +6 -11
  110. data/lib/rubocop/cop/style/select_by_regexp.rb +6 -2
  111. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  112. data/lib/rubocop/cop/style/semicolon.rb +24 -2
  113. data/lib/rubocop/cop/style/signal_exception.rb +8 -6
  114. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
  115. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  116. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
  117. data/lib/rubocop/cop/style/word_array.rb +42 -1
  118. data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
  119. data/lib/rubocop/cop/style/yoda_expression.rb +90 -0
  120. data/lib/rubocop/cop/style/zero_length_predicate.rb +31 -14
  121. data/lib/rubocop/cop/team.rb +48 -43
  122. data/lib/rubocop/cop/variable_force/scope.rb +3 -3
  123. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
  124. data/lib/rubocop/cop/variable_force.rb +1 -4
  125. data/lib/rubocop/formatter.rb +0 -1
  126. data/lib/rubocop/options.rb +22 -1
  127. data/lib/rubocop/path_util.rb +17 -7
  128. data/lib/rubocop/result_cache.rb +1 -1
  129. data/lib/rubocop/rspec/expect_offense.rb +6 -4
  130. data/lib/rubocop/runner.rb +50 -7
  131. data/lib/rubocop/server/cache.rb +10 -3
  132. data/lib/rubocop/server/cli.rb +37 -18
  133. data/lib/rubocop/server/client_command/exec.rb +1 -1
  134. data/lib/rubocop/server/client_command/start.rb +6 -1
  135. data/lib/rubocop/server/core.rb +23 -8
  136. data/lib/rubocop/target_ruby.rb +0 -1
  137. data/lib/rubocop/version.rb +1 -1
  138. data/lib/rubocop.rb +8 -0
  139. metadata +21 -33
@@ -134,22 +134,18 @@ module RuboCop
134
134
  #
135
135
  class ClassStructure < Base
136
136
  include VisibilityHelp
137
+ include CommentsHelp
137
138
  extend AutoCorrector
138
139
 
139
140
  HUMANIZED_NODE_TYPE = {
140
141
  casgn: :constants,
141
- defs: :class_methods,
142
+ defs: :public_class_methods,
142
143
  def: :public_methods,
143
144
  sclass: :class_singleton
144
145
  }.freeze
145
146
 
146
147
  MSG = '`%<category>s` is supposed to appear before `%<previous>s`.'
147
148
 
148
- # @!method dynamic_constant?(node)
149
- def_node_matcher :dynamic_constant?, <<~PATTERN
150
- (casgn nil? _ (send ...))
151
- PATTERN
152
-
153
149
  # Validates code style on class declaration.
154
150
  # Add offense when find a node out of expected order.
155
151
  def on_class(class_node)
@@ -168,7 +164,7 @@ module RuboCop
168
164
 
169
165
  # Autocorrect by swapping between two nodes autocorrecting them
170
166
  def autocorrect(corrector, node)
171
- previous = node.left_siblings.find do |sibling|
167
+ previous = node.left_siblings.reverse.find do |sibling|
172
168
  !ignore_for_autocorrect?(node, sibling)
173
169
  end
174
170
  return unless previous
@@ -221,7 +217,7 @@ module RuboCop
221
217
  def walk_over_nested_class_definition(class_node)
222
218
  class_elements(class_node).each do |node|
223
219
  classification = classify(node)
224
- next if ignore?(classification)
220
+ next if ignore?(node, classification)
225
221
 
226
222
  yield node, classification
227
223
  end
@@ -239,17 +235,20 @@ module RuboCop
239
235
  end
240
236
  end
241
237
 
242
- def ignore?(classification)
238
+ def ignore?(node, classification)
243
239
  classification.nil? ||
244
240
  classification.to_s.end_with?('=') ||
245
- expected_order.index(classification).nil?
241
+ expected_order.index(classification).nil? ||
242
+ private_constant?(node)
246
243
  end
247
244
 
248
245
  def ignore_for_autocorrect?(node, sibling)
249
246
  classification = classify(node)
250
247
  sibling_class = classify(sibling)
251
248
 
252
- ignore?(sibling_class) || classification == sibling_class || dynamic_constant?(node)
249
+ ignore?(sibling, sibling_class) ||
250
+ classification == sibling_class ||
251
+ dynamic_constant?(node)
253
252
  end
254
253
 
255
254
  def humanize_node(node)
@@ -261,19 +260,28 @@ module RuboCop
261
260
  HUMANIZED_NODE_TYPE[node.type] || node.type
262
261
  end
263
262
 
264
- def source_range_with_comment(node)
265
- begin_pos, end_pos =
266
- if (node.def_type? && !node.method?(:initialize)) ||
267
- (node.send_type? && node.def_modifier?)
268
- start_node = find_visibility_start(node) || node
269
- end_node = find_visibility_end(node) || node
270
- [begin_pos_with_comment(start_node),
271
- end_position_for(end_node) + 1]
272
- else
273
- [begin_pos_with_comment(node), end_position_for(node)]
274
- end
263
+ def dynamic_constant?(node)
264
+ return false unless node.casgn_type? && node.namespace.nil?
265
+
266
+ expression = node.expression
267
+ expression.send_type? &&
268
+ !(expression.method?(:freeze) && expression.receiver&.recursive_basic_literal?)
269
+ end
270
+
271
+ def private_constant?(node)
272
+ return false unless node.casgn_type? && node.namespace.nil?
273
+ return false unless (parent = node.parent)
274
+
275
+ parent.each_child_node(:send) do |child_node|
276
+ return true if marked_as_private_constant?(child_node, node.name)
277
+ end
278
+ false
279
+ end
280
+
281
+ def marked_as_private_constant?(node, name)
282
+ return false unless node.method?(:private_constant)
275
283
 
276
- Parser::Source::Range.new(buffer, begin_pos, end_pos)
284
+ node.arguments.any? { |arg| (arg.sym_type? || arg.str_type?) && arg.value == name }
277
285
  end
278
286
 
279
287
  def end_position_for(node)
@@ -144,7 +144,7 @@ module RuboCop
144
144
  def expected_column(left_paren, elements)
145
145
  if line_break_after_left_paren?(left_paren, elements)
146
146
  source_indent = processed_source.line_indentation(first_argument_line(elements))
147
- new_indent = source_indent - indentation_width
147
+ new_indent = source_indent - configured_indentation_width
148
148
 
149
149
  new_indent.negative? ? 0 : new_indent
150
150
  elsif all_elements_aligned?(elements)
@@ -157,7 +157,7 @@ module RuboCop
157
157
  def all_elements_aligned?(elements)
158
158
  elements.flat_map do |e|
159
159
  if e.hash_type?
160
- e.each_pair.map { |pair| pair.loc.column }
160
+ e.each_child_node.map { |child| child.loc.column }
161
161
  else
162
162
  e.loc.column
163
163
  end
@@ -184,10 +184,6 @@ module RuboCop
184
184
  end
185
185
  end
186
186
 
187
- def indentation_width
188
- @config.for_cop('Layout/IndentationWidth')['Width'] || 2
189
- end
190
-
191
187
  def line_break_after_left_paren?(left_paren, elements)
192
188
  elements.first && elements.first.loc.line > left_paren.line
193
189
  end
@@ -154,7 +154,9 @@ module RuboCop
154
154
  end
155
155
 
156
156
  def less_indented?(line)
157
- /^\s*(end\b|[)}\]])/.match?(line)
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
160
  end
159
161
 
160
162
  def two_alternatives?(line)
@@ -179,7 +179,7 @@ module RuboCop
179
179
 
180
180
  send_node = arg_node.parent
181
181
  text = base_range(send_node, arg_node).source.strip
182
- base = if !/\n/.match?(text) && special_inner_call_indentation?(send_node)
182
+ base = if !text.include?("\n") && special_inner_call_indentation?(send_node)
183
183
  "`#{text}`"
184
184
  elsif comment_line?(text.lines.reverse_each.first)
185
185
  'the start of the previous line (not counting the comment)'
@@ -22,6 +22,7 @@ module RuboCop
22
22
  # RUBY
23
23
  #
24
24
  class HeredocIndentation < Base
25
+ include Alignment
25
26
  include Heredoc
26
27
  extend AutoCorrector
27
28
 
@@ -37,7 +38,7 @@ module RuboCop
37
38
  heredoc_indent_type = heredoc_indent_type(node)
38
39
 
39
40
  if heredoc_indent_type == '~'
40
- expected_indent_level = base_indent_level(node) + indentation_width
41
+ expected_indent_level = base_indent_level(node) + configured_indentation_width
41
42
  return if expected_indent_level == body_indent_level
42
43
  else
43
44
  return unless body_indent_level.zero?
@@ -66,9 +67,9 @@ module RuboCop
66
67
  current_indent_type = "<<#{heredoc_indent_type}"
67
68
 
68
69
  if current_indent_type == '<<~'
69
- width_message(indentation_width)
70
+ width_message(configured_indentation_width)
70
71
  else
71
- type_message(indentation_width, current_indent_type)
72
+ type_message(configured_indentation_width, current_indent_type)
72
73
  end
73
74
  end
74
75
 
@@ -89,7 +90,7 @@ module RuboCop
89
90
 
90
91
  body = heredoc_body(node)
91
92
 
92
- expected_indent = base_indent_level(node) + indentation_width
93
+ expected_indent = base_indent_level(node) + configured_indentation_width
93
94
  actual_indent = indent_level(body)
94
95
  increase_indent_level = expected_indent - actual_indent
95
96
 
@@ -122,7 +123,7 @@ module RuboCop
122
123
  def indented_body(node)
123
124
  body = heredoc_body(node)
124
125
  body_indent_level = indent_level(body)
125
- correct_indent_level = base_indent_level(node) + indentation_width
126
+ correct_indent_level = base_indent_level(node) + configured_indentation_width
126
127
  body.gsub(/^[^\S\r\n]{#{body_indent_level}}/, ' ' * correct_indent_level)
127
128
  end
128
129
 
@@ -148,10 +149,6 @@ module RuboCop
148
149
  node.source[/^<<([~-])/, 1]
149
150
  end
150
151
 
151
- def indentation_width
152
- @config.for_cop('Layout/IndentationWidth')['Width'] || 2
153
- end
154
-
155
152
  def heredoc_body(node)
156
153
  node.loc.heredoc_body.source
157
154
  end
@@ -40,10 +40,13 @@ module RuboCop
40
40
  MSG = '%<type>s detected in indentation.'
41
41
 
42
42
  def on_new_investigation
43
- str_ranges = string_literal_ranges(processed_source.ast)
43
+ str_ranges = nil
44
44
 
45
45
  processed_source.lines.each.with_index(1) do |line, lineno|
46
46
  next unless (range = find_offense(line, lineno))
47
+
48
+ # Perform costly calculation only when needed.
49
+ str_ranges ||= string_literal_ranges(processed_source.ast)
47
50
  next if in_string_literal?(str_ranges, range)
48
51
 
49
52
  add_offense(range) { |corrector| autocorrect(corrector, range) }
@@ -31,15 +31,11 @@ module RuboCop
31
31
  include RangeHelp
32
32
  extend AutoCorrector
33
33
 
34
- # rubocop:disable Metrics/AbcSize
35
34
  def on_new_investigation
36
35
  return unless processed_source.raw_source.include?('\\')
37
36
 
38
37
  last_line = last_line(processed_source)
39
38
 
40
- @ignored_ranges = string_literal_ranges(processed_source.ast) +
41
- comment_ranges(processed_source.comments)
42
-
43
39
  processed_source.raw_source.lines.each_with_index do |line, index|
44
40
  break if index >= last_line
45
41
 
@@ -47,7 +43,6 @@ module RuboCop
47
43
  investigate(line, line_number)
48
44
  end
49
45
  end
50
- # rubocop:enable Metrics/AbcSize
51
46
 
52
47
  private
53
48
 
@@ -120,7 +115,12 @@ module RuboCop
120
115
  end
121
116
 
122
117
  def ignore_range?(backtick_range)
123
- @ignored_ranges.any? { |range| range.contains?(backtick_range) }
118
+ ignored_ranges.any? { |range| range.contains?(backtick_range) }
119
+ end
120
+
121
+ def ignored_ranges
122
+ @ignored_ranges ||= string_literal_ranges(processed_source.ast) +
123
+ comment_ranges(processed_source.comments)
124
124
  end
125
125
 
126
126
  def no_space_style?
@@ -91,7 +91,7 @@ module RuboCop
91
91
  if node.source.lines.first.end_with?("|\n")
92
92
  PIPE_SIZE
93
93
  else
94
- 1 + (PIPE_SIZE * 2)
94
+ (PIPE_SIZE * 2) + 1
95
95
  end
96
96
  end
97
97
 
@@ -241,7 +241,7 @@ module RuboCop
241
241
  end
242
242
 
243
243
  def accept_namespace_operator?(range)
244
- ACCEPT_NAMESPACE_OPERATOR == range.source
244
+ range.source == ACCEPT_NAMESPACE_OPERATOR
245
245
  end
246
246
 
247
247
  def safe_navigation_call?(range, pos)
@@ -256,7 +256,7 @@ module RuboCop
256
256
  # regular dotted method calls bind more tightly than operators
257
257
  # so we need to climb up the AST past them
258
258
  node.each_ancestor do |ancestor|
259
- return true if ancestor.and_type? || ancestor.or_type?
259
+ return true if ancestor.and_type? || ancestor.or_type? || ancestor.range_type?
260
260
  return false unless ancestor.send_type?
261
261
  return true if ancestor.operator_method?
262
262
  end
@@ -177,7 +177,7 @@ module RuboCop
177
177
  end
178
178
 
179
179
  def autocorrect(corrector, range)
180
- if /\*\*/.match?(range.source) && !space_around_exponent_operator?
180
+ if range.source.include?('**') && !space_around_exponent_operator?
181
181
  corrector.replace(range, '**')
182
182
  elsif range.source.end_with?("\n")
183
183
  corrector.replace(range, " #{range.source.strip}\n")
@@ -78,8 +78,11 @@ module RuboCop
78
78
  def on_array(node)
79
79
  return unless node.square_brackets?
80
80
 
81
- left, right = array_brackets(node)
82
- return empty_offenses(node, left, right, EMPTY_MSG) if empty_brackets?(left, right)
81
+ tokens, left, right = array_brackets(node)
82
+
83
+ if empty_brackets?(left, right, tokens: tokens)
84
+ return empty_offenses(node, left, right, EMPTY_MSG)
85
+ end
83
86
 
84
87
  start_ok = next_to_newline?(node, left)
85
88
  end_ok = node.single_line? ? false : end_has_own_line?(right)
@@ -90,9 +93,9 @@ module RuboCop
90
93
  private
91
94
 
92
95
  def autocorrect(corrector, node)
93
- left, right = array_brackets(node)
96
+ tokens, left, right = array_brackets(node)
94
97
 
95
- if empty_brackets?(left, right)
98
+ if empty_brackets?(left, right, tokens: tokens)
96
99
  SpaceCorrector.empty_corrections(processed_source, corrector, empty_config, left, right)
97
100
  elsif style == :no_space
98
101
  SpaceCorrector.remove_space(processed_source, corrector, left, right)
@@ -104,15 +107,12 @@ module RuboCop
104
107
  end
105
108
 
106
109
  def array_brackets(node)
107
- [left_array_bracket(node), right_array_bracket(node)]
108
- end
110
+ tokens = processed_source.tokens_within(node)
109
111
 
110
- def left_array_bracket(node)
111
- processed_source.tokens_within(node).find(&:left_array_bracket?)
112
- end
112
+ left = tokens.find(&:left_array_bracket?)
113
+ right = tokens.reverse_each.find(&:right_bracket?)
113
114
 
114
- def right_array_bracket(node)
115
- processed_source.tokens_within(node).reverse.find(&:right_bracket?)
115
+ [tokens, left, right]
116
116
  end
117
117
 
118
118
  def empty_config
@@ -178,8 +178,6 @@ module RuboCop
178
178
  def multi_dimensional_array?(node, token, side: :right)
179
179
  offset = side == :right ? -1 : +1
180
180
  i = index_for(node, token) + offset
181
- # TODO: change this type check once
182
- # https://github.com/rubocop/rubocop-ast/pull/240 is merged
183
181
  i += offset while processed_source.tokens_within(node)[i].new_line?
184
182
  if side == :right
185
183
  processed_source.tokens_within(node)[i].right_bracket?
@@ -74,7 +74,7 @@ module RuboCop
74
74
 
75
75
  right_token = closing_bracket(tokens, left_token)
76
76
 
77
- if empty_brackets?(left_token, right_token)
77
+ if empty_brackets?(left_token, right_token, tokens: tokens)
78
78
  return empty_offenses(node, left_token, right_token, EMPTY_MSG)
79
79
  end
80
80
 
@@ -90,9 +90,9 @@ module RuboCop
90
90
  private
91
91
 
92
92
  def autocorrect(corrector, node)
93
- left, right = reference_brackets(node)
93
+ tokens, left, right = reference_brackets(node)
94
94
 
95
- if empty_brackets?(left, right)
95
+ if empty_brackets?(left, right, tokens: tokens)
96
96
  SpaceCorrector.empty_corrections(processed_source, corrector, empty_config, left, right)
97
97
  elsif style == :no_space
98
98
  SpaceCorrector.remove_space(processed_source, corrector, left, right)
@@ -104,7 +104,7 @@ module RuboCop
104
104
  def reference_brackets(node)
105
105
  tokens = processed_source.tokens_within(node)
106
106
  left = left_ref_bracket(node, tokens)
107
- [left, closing_bracket(tokens, left)]
107
+ [tokens, left, closing_bracket(tokens, left)]
108
108
  end
109
109
 
110
110
  def left_ref_bracket(node, tokens)
@@ -31,13 +31,14 @@ module RuboCop
31
31
  def on_interpolation(begin_node)
32
32
  return if begin_node.multiline?
33
33
 
34
- delims = delimiters(begin_node)
35
- return if empty_brackets?(*delims)
34
+ tokens = processed_source.tokens_within(begin_node)
35
+ left, right = delimiters(begin_node)
36
+ return if empty_brackets?(left, right, tokens: tokens)
36
37
 
37
38
  if style == :no_space
38
- no_space_offenses(begin_node, *delims, NO_SPACE_MSG)
39
+ no_space_offenses(begin_node, left, right, NO_SPACE_MSG)
39
40
  else
40
- space_offenses(begin_node, *delims, SPACE_MSG)
41
+ space_offenses(begin_node, left, right, SPACE_MSG)
41
42
  end
42
43
  end
43
44
 
@@ -47,7 +47,6 @@ module RuboCop
47
47
  MSG = 'Trailing whitespace detected.'
48
48
 
49
49
  def on_new_investigation
50
- @heredocs = extract_heredocs(processed_source.ast)
51
50
  processed_source.lines.each_with_index do |line, index|
52
51
  next unless line.end_with?(' ', "\t")
53
52
 
@@ -102,10 +101,14 @@ module RuboCop
102
101
  end
103
102
 
104
103
  def find_heredoc(line_number)
105
- @heredocs.each { |node, r| return node if r.include?(line_number) }
104
+ heredocs.each { |node, r| return node if r.include?(line_number) }
106
105
  nil
107
106
  end
108
107
 
108
+ def heredocs
109
+ @heredocs ||= extract_heredocs(processed_source.ast)
110
+ end
111
+
109
112
  def extract_heredocs(ast)
110
113
  return [] unless ast
111
114
 
@@ -38,6 +38,10 @@ module RuboCop
38
38
  'a whitespace to the right of the `%<operator>s` if it ' \
39
39
  'should be %<possible>s.'
40
40
 
41
+ def self.autocorrect_incompatible_with
42
+ [Naming::BlockForwarding]
43
+ end
44
+
41
45
  def on_new_investigation
42
46
  processed_source.diagnostics.each do |diagnostic|
43
47
  next unless diagnostic.reason == :ambiguous_prefix
@@ -82,44 +82,25 @@ module RuboCop
82
82
  def debugger_methods
83
83
  @debugger_methods ||= begin
84
84
  config = cop_config.fetch('DebuggerMethods', [])
85
- values = config.is_a?(Array) ? config : config.values.flatten
86
- values.map do |v|
87
- next unless v
88
-
89
- *receiver, method_name = v.split('.')
90
- {
91
- receiver: receiver.empty? ? nil : receiver.map(&:to_sym),
92
- method_name: method_name.to_sym
93
- }
94
- end.compact
85
+ config.is_a?(Array) ? config : config.values.flatten
95
86
  end
96
87
  end
97
88
 
98
89
  def debugger_method?(send_node)
99
- method_name = send_node.method_name
100
-
101
- debugger_methods.any? do |method|
102
- next unless method[:method_name] == method_name
90
+ return if send_node.parent&.send_type? && send_node.parent.receiver == send_node
103
91
 
104
- if method[:receiver].nil?
105
- send_node.receiver.nil?
106
- else
107
- method[:receiver] == receiver_chain(send_node)
108
- end
109
- end
92
+ debugger_methods.include?(chained_method_name(send_node))
110
93
  end
111
94
 
112
- def receiver_chain(send_node)
113
- receivers = []
95
+ def chained_method_name(send_node)
96
+ chained_method_name = send_node.method_name.to_s
114
97
  receiver = send_node.receiver
115
-
116
98
  while receiver
117
- name = receiver.send_type? ? receiver.method_name : receiver.const_name&.to_sym
118
- receivers.unshift(name)
99
+ name = receiver.send_type? ? receiver.method_name : receiver.const_name
100
+ chained_method_name = "#{name}.#{chained_method_name}"
119
101
  receiver = receiver.receiver
120
102
  end
121
-
122
- receivers
103
+ chained_method_name
123
104
  end
124
105
  end
125
106
  end