rubocop 1.71.2 → 1.75.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 (205) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/default.yml +118 -21
  4. data/config/internal_affairs.yml +20 -0
  5. data/config/obsoletion.yml +3 -1
  6. data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
  7. data/lib/rubocop/cli.rb +1 -1
  8. data/lib/rubocop/comment_config.rb +1 -1
  9. data/lib/rubocop/config.rb +39 -6
  10. data/lib/rubocop/config_loader.rb +48 -9
  11. data/lib/rubocop/config_loader_resolver.rb +24 -9
  12. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
  13. data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
  14. data/lib/rubocop/config_obsoletion.rb +46 -2
  15. data/lib/rubocop/config_validator.rb +2 -1
  16. data/lib/rubocop/cop/internal_affairs/example_description.rb +7 -3
  17. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  18. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +1 -1
  19. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +2 -1
  20. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +91 -0
  21. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  22. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
  23. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +7 -1
  24. data/lib/rubocop/cop/internal_affairs.rb +2 -16
  25. data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
  26. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
  27. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +4 -4
  28. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  29. data/lib/rubocop/cop/layout/else_alignment.rb +2 -2
  30. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  31. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +3 -3
  32. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +27 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
  34. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  35. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  36. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  37. data/lib/rubocop/cop/layout/line_length.rb +8 -4
  38. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
  39. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  40. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
  41. data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
  42. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -5
  43. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  44. data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
  45. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
  46. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
  47. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  48. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  49. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  50. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -17
  51. data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
  52. data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
  53. data/lib/rubocop/cop/lint/float_comparison.rb +1 -6
  54. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  55. data/lib/rubocop/cop/lint/literal_as_condition.rb +103 -9
  56. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
  57. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  58. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
  59. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
  60. data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
  61. data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
  62. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +261 -0
  63. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
  64. data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
  65. data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
  66. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +8 -1
  67. data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
  68. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  69. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  70. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  71. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  72. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
  73. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  74. data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
  75. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -0
  76. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +71 -0
  77. data/lib/rubocop/cop/lint/void.rb +7 -0
  78. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  79. data/lib/rubocop/cop/metrics/method_length.rb +1 -0
  80. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  81. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  82. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  83. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  84. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  85. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  86. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  87. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  88. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  89. data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
  90. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
  91. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +0 -1
  92. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +18 -18
  93. data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
  94. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  95. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
  96. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  97. data/lib/rubocop/cop/mixin/range_help.rb +15 -3
  98. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  99. data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
  100. data/lib/rubocop/cop/mixin/trailing_comma.rb +12 -0
  101. data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
  102. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  103. data/lib/rubocop/cop/naming/method_name.rb +64 -8
  104. data/lib/rubocop/cop/naming/predicate_name.rb +44 -0
  105. data/lib/rubocop/cop/naming/variable_name.rb +51 -6
  106. data/lib/rubocop/cop/registry.rb +9 -6
  107. data/lib/rubocop/cop/style/accessor_grouping.rb +19 -5
  108. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  109. data/lib/rubocop/cop/style/array_intersect.rb +39 -28
  110. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  111. data/lib/rubocop/cop/style/class_and_module_children.rb +29 -7
  112. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
  113. data/lib/rubocop/cop/style/collection_methods.rb +1 -0
  114. data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
  115. data/lib/rubocop/cop/style/commented_keyword.rb +10 -3
  116. data/lib/rubocop/cop/style/comparable_between.rb +75 -0
  117. data/lib/rubocop/cop/style/conditional_assignment.rb +3 -0
  118. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  119. data/lib/rubocop/cop/style/empty_literal.rb +4 -0
  120. data/lib/rubocop/cop/style/endless_method.rb +163 -18
  121. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
  122. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  123. data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
  124. data/lib/rubocop/cop/style/for.rb +1 -0
  125. data/lib/rubocop/cop/style/format_string_token.rb +38 -11
  126. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
  127. data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
  128. data/lib/rubocop/cop/style/guard_clause.rb +2 -1
  129. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
  130. data/lib/rubocop/cop/style/hash_fetch_chain.rb +105 -0
  131. data/lib/rubocop/cop/style/hash_syntax.rb +3 -0
  132. data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
  133. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  134. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  135. data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
  136. data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
  137. data/lib/rubocop/cop/style/it_block_parameter.rb +100 -0
  138. data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
  139. data/lib/rubocop/cop/style/lambda.rb +1 -0
  140. data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
  141. data/lib/rubocop/cop/style/map_into_array.rb +1 -0
  142. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -4
  143. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -1
  144. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
  145. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
  146. data/lib/rubocop/cop/style/next.rb +44 -0
  147. data/lib/rubocop/cop/style/object_then.rb +1 -0
  148. data/lib/rubocop/cop/style/proc.rb +1 -0
  149. data/lib/rubocop/cop/style/raise_args.rb +8 -8
  150. data/lib/rubocop/cop/style/redundant_begin.rb +1 -0
  151. data/lib/rubocop/cop/style/redundant_condition.rb +57 -0
  152. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
  153. data/lib/rubocop/cop/style/redundant_format.rb +257 -0
  154. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  155. data/lib/rubocop/cop/style/redundant_parentheses.rb +20 -5
  156. data/lib/rubocop/cop/style/redundant_self.rb +1 -0
  157. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  158. data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
  159. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
  160. data/lib/rubocop/cop/style/return_nil.rb +2 -2
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
  162. data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
  163. data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
  164. data/lib/rubocop/cop/style/sole_nested_conditional.rb +41 -106
  165. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  166. data/lib/rubocop/cop/style/super_arguments.rb +1 -2
  167. data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
  168. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
  169. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
  170. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
  171. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  172. data/lib/rubocop/cop/util.rb +2 -2
  173. data/lib/rubocop/cop/utils/format_string.rb +10 -5
  174. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  175. data/lib/rubocop/cop/variable_force/variable.rb +2 -7
  176. data/lib/rubocop/cop/variable_force.rb +1 -1
  177. data/lib/rubocop/cops_documentation_generator.rb +12 -1
  178. data/lib/rubocop/directive_comment.rb +36 -3
  179. data/lib/rubocop/ext/regexp_node.rb +0 -1
  180. data/lib/rubocop/lsp/runtime.rb +6 -4
  181. data/lib/rubocop/lsp/server.rb +0 -2
  182. data/lib/rubocop/lsp/stdin_runner.rb +3 -1
  183. data/lib/rubocop/magic_comment.rb +8 -0
  184. data/lib/rubocop/options.rb +26 -11
  185. data/lib/rubocop/path_util.rb +4 -0
  186. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  187. data/lib/rubocop/plugin/load_error.rb +26 -0
  188. data/lib/rubocop/plugin/loader.rb +100 -0
  189. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  190. data/lib/rubocop/plugin.rb +46 -0
  191. data/lib/rubocop/rake_task.rb +4 -1
  192. data/lib/rubocop/rspec/cop_helper.rb +13 -1
  193. data/lib/rubocop/rspec/shared_contexts.rb +35 -0
  194. data/lib/rubocop/rspec/support.rb +3 -0
  195. data/lib/rubocop/runner.rb +5 -1
  196. data/lib/rubocop/server/cache.rb +47 -11
  197. data/lib/rubocop/server/cli.rb +2 -2
  198. data/lib/rubocop/target_finder.rb +1 -1
  199. data/lib/rubocop/target_ruby.rb +1 -1
  200. data/lib/rubocop/version.rb +30 -8
  201. data/lib/rubocop.rb +10 -1
  202. data/lib/ruby_lsp/rubocop/addon.rb +7 -10
  203. data/lib/ruby_lsp/rubocop/{wraps_built_in_lsp_runtime.rb → runtime_adapter.rb} +25 -10
  204. metadata +43 -12
  205. data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
@@ -49,7 +49,7 @@ module RuboCop
49
49
  end
50
50
 
51
51
  def on_rescue(node)
52
- return unless node.loc.respond_to?(:else) && node.loc.else
52
+ return unless node.loc?(:else)
53
53
 
54
54
  check_alignment(base_range_of_rescue(node), node.loc.else)
55
55
  end
@@ -92,7 +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
95
+ when :block, :numblock, :itblock
96
96
  assignment_node = assignment_node(parent)
97
97
  if same_line?(parent, assignment_node)
98
98
  assignment_node.source_range
@@ -189,7 +189,7 @@ module RuboCop
189
189
  end
190
190
 
191
191
  def offense_location(node)
192
- if node.loc.respond_to?(:end) && node.loc.end
192
+ if node.loc?(:end)
193
193
  node.loc.end
194
194
  else
195
195
  node
@@ -180,7 +180,7 @@ module RuboCop
180
180
  end
181
181
 
182
182
  def method_candidate?(node)
183
- cop_config['EmptyLineBetweenMethodDefs'] && node.type?(:def, :defs)
183
+ cop_config['EmptyLineBetweenMethodDefs'] && node.any_def_type?
184
184
  end
185
185
 
186
186
  def class_candidate?(node)
@@ -252,7 +252,7 @@ module RuboCop
252
252
  end
253
253
 
254
254
  def end_loc(node)
255
- if node.type?(:def, :defs) && node.endless?
255
+ if node.any_def_type? && node.endless?
256
256
  node.source_range.end
257
257
  else
258
258
  node.loc.end
@@ -277,7 +277,7 @@ module RuboCop
277
277
  case node.type
278
278
  when :def, :defs
279
279
  :method
280
- when :numblock
280
+ when :numblock, :itblock
281
281
  :block
282
282
  else
283
283
  node.type
@@ -83,6 +83,7 @@ module RuboCop
83
83
  end
84
84
 
85
85
  alias on_numblock on_block
86
+ alias on_itblock on_block
86
87
 
87
88
  def on_send(node)
88
89
  return unless node.bare_access_modifier? && !node.block_literal?
@@ -93,7 +94,7 @@ module RuboCop
93
94
  add_offense(node, message: message) do |corrector|
94
95
  line = range_by_whole_lines(node.source_range)
95
96
 
96
- corrector.insert_before(line, "\n") unless previous_line_empty?(node.first_line)
97
+ corrector.insert_before(line, "\n") if should_insert_line_before?(node)
97
98
 
98
99
  correct_next_line_if_denied_style(corrector, node, line)
99
100
  end
@@ -122,6 +123,8 @@ module RuboCop
122
123
  end
123
124
 
124
125
  def correct_next_line_if_denied_style(corrector, node, line)
126
+ return unless should_insert_line_after?(node)
127
+
125
128
  case style
126
129
  when :around
127
130
  corrector.insert_after(line, "\n") unless next_line_empty?(node.last_line)
@@ -205,6 +208,29 @@ module RuboCop
205
208
  format(MSG_BEFORE_FOR_ONLY_BEFORE, modifier: modifier)
206
209
  end
207
210
  end
211
+
212
+ def should_insert_line_before?(node)
213
+ return false if previous_line_empty?(node.first_line)
214
+ return true unless inside_block?(node) && no_empty_lines_around_block_body?
215
+ return true unless node.parent.begin_type?
216
+
217
+ node.parent.children.first != node
218
+ end
219
+
220
+ def should_insert_line_after?(node)
221
+ return true unless inside_block?(node) && no_empty_lines_around_block_body?
222
+
223
+ node.parent.children.last != node
224
+ end
225
+
226
+ def inside_block?(node)
227
+ node.parent.block_type? || (node.parent.begin_type? && node.parent.parent&.block_type?)
228
+ end
229
+
230
+ def no_empty_lines_around_block_body?
231
+ config.for_enabled_cop('Layout/EmptyLinesAroundBlockBody')['EnforcedStyle'] ==
232
+ 'no_empty_lines'
233
+ end
208
234
  end
209
235
  end
210
236
  end
@@ -34,6 +34,7 @@ module RuboCop
34
34
  end
35
35
 
36
36
  alias on_numblock on_block
37
+ alias on_itblock on_block
37
38
  end
38
39
  end
39
40
  end
@@ -27,9 +27,14 @@ module RuboCop
27
27
  KIND = 'method'
28
28
 
29
29
  def on_def(node)
30
- first_line = node.arguments.source_range&.last_line
30
+ if node.endless?
31
+ return unless offending_endless_method?(node)
31
32
 
32
- check(node, node.body, adjusted_first_line: first_line)
33
+ register_offense_for_endless_method(node)
34
+ else
35
+ first_line = node.arguments.source_range&.last_line
36
+ check(node, node.body, adjusted_first_line: first_line)
37
+ end
33
38
  end
34
39
  alias on_defs on_def
35
40
 
@@ -38,6 +43,21 @@ module RuboCop
38
43
  def style
39
44
  :no_empty_lines
40
45
  end
46
+
47
+ def offending_endless_method?(node)
48
+ node.body.first_line > node.loc.assignment.line + 1 &&
49
+ processed_source.lines[node.loc.assignment.line].empty?
50
+ end
51
+
52
+ def register_offense_for_endless_method(node)
53
+ range = processed_source.buffer.line_range(node.loc.assignment.line + 1).resize(1)
54
+
55
+ msg = message(MSG_EXTRA, 'beginning')
56
+
57
+ add_offense(range, message: msg) do |corrector|
58
+ corrector.remove(range)
59
+ end
60
+ end
41
61
  end
42
62
  end
43
63
  end
@@ -162,7 +162,7 @@ module RuboCop
162
162
 
163
163
  def end_keyword_before_closing_parenthesis?(parenthesized_send_node)
164
164
  parenthesized_send_node.ancestors.any? do |ancestor|
165
- ancestor.loc.respond_to?(:end) && ancestor.loc.end&.source == 'end'
165
+ ancestor.loc_is?(:end, 'end')
166
166
  end
167
167
  end
168
168
 
@@ -91,6 +91,7 @@ module RuboCop
91
91
  end
92
92
 
93
93
  alias on_numblock on_block
94
+ alias on_itblock on_block
94
95
 
95
96
  def on_class(node)
96
97
  base = node.loc.keyword
@@ -75,6 +75,7 @@ module RuboCop
75
75
  check_for_breakable_block(node)
76
76
  end
77
77
  alias on_numblock on_block
78
+ alias on_itblock on_block
78
79
 
79
80
  def on_str(node)
80
81
  check_for_breakable_str(node)
@@ -92,6 +93,7 @@ module RuboCop
92
93
  alias on_send on_potential_breakable_node
93
94
  alias on_csend on_potential_breakable_node
94
95
  alias on_def on_potential_breakable_node
96
+ alias on_defs on_potential_breakable_node
95
97
 
96
98
  def on_new_investigation
97
99
  return unless processed_source.raw_source.include?(';')
@@ -209,7 +211,7 @@ module RuboCop
209
211
  # are not bisected.
210
212
  # If the string contains spaces, use them to determine a place for a clean break;
211
213
  # otherwise, the string will be broken at the line length limit.
212
- def breakable_string_range(node) # rubocop:disable Metrics/AbcSize
214
+ def breakable_string_range(node)
213
215
  source_range = node.source_range
214
216
  relevant_substr = largest_possible_string(node)
215
217
 
@@ -221,13 +223,13 @@ module RuboCop
221
223
  adjustment = max - source_range.last_column - 3
222
224
  return if adjustment.abs > source_range.size
223
225
 
224
- source_range.adjust(end_pos: max - source_range.last_column - 3)
226
+ source_range.adjust(end_pos: adjustment)
225
227
  end
226
228
  end
227
229
 
228
230
  def breakable_dstr_begin_position(node)
229
231
  source_range = node.source_range
230
- source_range.begin_pos if source_range.begin_pos < max && source_range.end_pos >= max
232
+ source_range.begin_pos if source_range.column < max && source_range.last_column >= max
231
233
  end
232
234
 
233
235
  def breakable_range_by_line_index
@@ -370,7 +372,9 @@ module RuboCop
370
372
 
371
373
  def string_delimiter(node)
372
374
  delimiter = node.loc.begin
373
- delimiter ||= node.parent.loc.begin if node.parent&.dstr_type?
375
+ if node.parent&.dstr_type? && node.parent.loc.respond_to?(:begin)
376
+ delimiter ||= node.parent.loc.begin
377
+ end
374
378
  delimiter = delimiter&.source
375
379
 
376
380
  delimiter if %w[' "].include?(delimiter)
@@ -69,6 +69,7 @@ module RuboCop
69
69
  end
70
70
 
71
71
  alias on_numblock on_block
72
+ alias on_itblock on_block
72
73
 
73
74
  private
74
75
 
@@ -216,7 +216,7 @@ module RuboCop
216
216
 
217
217
  def get_dot_right_above(node)
218
218
  node.each_ancestor.find do |a|
219
- dot = a.loc.respond_to?(:dot) && a.loc.dot
219
+ dot = a.loc.dot if a.loc?(:dot)
220
220
  next unless dot
221
221
 
222
222
  dot.line == node.loc.dot.line - 1 && dot.column == node.loc.dot.column
@@ -239,7 +239,7 @@ module RuboCop
239
239
  node = node.receiver while node.receiver
240
240
  # ascend to first call which has a dot
241
241
  node = node.parent
242
- node = node.parent until node.loc.respond_to?(:dot) && node.loc.dot
242
+ node = node.parent until node.loc?(:dot)
243
243
 
244
244
  node
245
245
  end
@@ -65,6 +65,7 @@ module RuboCop
65
65
 
66
66
  check_line_breaks(node, node.arguments, ignore_last: ignore_last_element?)
67
67
  end
68
+ alias on_defs on_def
68
69
 
69
70
  private
70
71
 
@@ -6,25 +6,29 @@ module RuboCop
6
6
  # Checks whether certain expressions, e.g. method calls, that could fit
7
7
  # completely on a single line, are broken up into multiple lines unnecessarily.
8
8
  #
9
- # @example any configuration
9
+ # @example
10
10
  # # bad
11
11
  # foo(
12
12
  # a,
13
13
  # b
14
14
  # )
15
15
  #
16
+ # # good
17
+ # foo(a, b)
18
+ #
19
+ # # bad
16
20
  # puts 'string that fits on ' \
17
21
  # 'a single line'
18
22
  #
23
+ # # good
24
+ # puts 'string that fits on a single line'
25
+ #
26
+ # # bad
19
27
  # things
20
28
  # .select { |thing| thing.cond? }
21
29
  # .join('-')
22
30
  #
23
31
  # # good
24
- # foo(a, b)
25
- #
26
- # puts 'string that fits on a single line'
27
- #
28
32
  # things.select { |thing| thing.cond? }.join('-')
29
33
  #
30
34
  # @example InspectBlocks: false (default)
@@ -29,8 +29,7 @@ module RuboCop
29
29
  MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
30
30
  'aligned with `%<beginning>s` at ' \
31
31
  '%<begin_loc_line>d, %<begin_loc_column>d.'
32
- ANCESTOR_TYPES = %i[kwbegin def defs class module any_block].freeze
33
- ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
32
+ ANCESTOR_TYPES = %i[kwbegin any_def class module any_block].freeze
34
33
  ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method private_class_method].freeze
35
34
 
36
35
  def on_resbody(node)
@@ -96,7 +95,7 @@ module RuboCop
96
95
  def alignment_source(node, starting_loc)
97
96
  ending_loc =
98
97
  case node.type
99
- when :block, :numblock, :kwbegin
98
+ when :block, :numblock, :itblock, :kwbegin
100
99
  node.loc.begin
101
100
  when :def, :defs, :class, :module,
102
101
  :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn
@@ -162,8 +161,7 @@ module RuboCop
162
161
  end
163
162
 
164
163
  def access_modifier_node(node)
165
- return nil unless
166
- ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS.include?(node.type)
164
+ return nil unless node.any_def_type?
167
165
 
168
166
  access_modifier_node = node.ancestors.first
169
167
  return nil unless access_modifier?(access_modifier_node)
@@ -51,7 +51,7 @@ module RuboCop
51
51
  alias on_csend on_send
52
52
 
53
53
  def on_const(node)
54
- return unless node.loc.respond_to?(:double_colon) && node.loc.double_colon
54
+ return unless node.loc?(:double_colon)
55
55
 
56
56
  check_space_after_double_colon(node)
57
57
  end
@@ -260,7 +260,10 @@ module RuboCop
260
260
  end
261
261
 
262
262
  def hash_table_style?
263
- align_hash_cop_config && align_hash_cop_config['EnforcedHashRocketStyle'] == 'table'
263
+ return false unless align_hash_cop_config
264
+
265
+ enforced_styles = Array(align_hash_cop_config['EnforcedHashRocketStyle'])
266
+ enforced_styles.include?('table')
264
267
  end
265
268
 
266
269
  def space_around_exponent_operator?
@@ -77,6 +77,7 @@ module RuboCop
77
77
  end
78
78
 
79
79
  alias on_numblock on_block
80
+ alias on_itblock on_block
80
81
 
81
82
  private
82
83
 
@@ -103,6 +103,7 @@ module RuboCop
103
103
  end
104
104
 
105
105
  alias on_numblock on_block
106
+ alias on_itblock on_block
106
107
 
107
108
  private
108
109
 
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Lint/RedundantCopDisableDirective
4
+ # rubocop:disable Style/DoubleCopDisableDirective
5
+ module RuboCop
6
+ module Cop
7
+ module Lint
8
+ # Checks that `# rubocop:enable ...` and `# rubocop:disable ...` statements
9
+ # are strictly formatted.
10
+ #
11
+ # A comment can be added to the directive by prefixing it with `--`.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # # rubocop:disable Layout/LineLength Style/Encoding
16
+ # # ^ missing comma
17
+ #
18
+ # # bad
19
+ # # rubocop:disable
20
+ #
21
+ # # bad
22
+ # # rubocop:disable Layout/LineLength # rubocop:disable Style/Encoding
23
+ #
24
+ # # bad
25
+ # # rubocop:wrongmode Layout/LineLength
26
+ #
27
+ # # good
28
+ # # rubocop:disable Layout/LineLength
29
+ #
30
+ # # good
31
+ # # rubocop:disable Layout/LineLength, Style/Encoding
32
+ #
33
+ # # good
34
+ # # rubocop:disable all
35
+ #
36
+ # # good
37
+ # # rubocop:disable Layout/LineLength -- This is a good comment.
38
+ #
39
+ class CopDirectiveSyntax < Base
40
+ COMMON_MSG = 'Malformed directive comment detected.'
41
+
42
+ MISSING_MODE_NAME_MSG = 'The mode name is missing.'
43
+ INVALID_MODE_NAME_MSG = 'The mode name must be one of `enable`, `disable`, or `todo`.'
44
+ MISSING_COP_NAME_MSG = 'The cop name is missing.'
45
+ MALFORMED_COP_NAMES_MSG = 'Cop names must be separated by commas. ' \
46
+ 'Comment in the directive must start with `--`.'
47
+
48
+ def on_new_investigation
49
+ processed_source.comments.each do |comment|
50
+ directive_comment = DirectiveComment.new(comment)
51
+ next unless directive_comment.start_with_marker?
52
+ next unless directive_comment.malformed?
53
+
54
+ message = offense_message(directive_comment)
55
+ add_offense(comment, message: message)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ # rubocop:disable Metrics/MethodLength
62
+ def offense_message(directive_comment)
63
+ comment = directive_comment.comment
64
+ after_marker = comment.text.sub(DirectiveComment::DIRECTIVE_MARKER_REGEXP, '')
65
+ mode = after_marker.split(' ', 2).first
66
+ additional_msg = if mode.nil?
67
+ MISSING_MODE_NAME_MSG
68
+ elsif !DirectiveComment::AVAILABLE_MODES.include?(mode)
69
+ INVALID_MODE_NAME_MSG
70
+ elsif directive_comment.missing_cop_name?
71
+ MISSING_COP_NAME_MSG
72
+ else
73
+ MALFORMED_COP_NAMES_MSG
74
+ end
75
+
76
+ "#{COMMON_MSG} #{additional_msg}"
77
+ end
78
+ # rubocop:enable Metrics/MethodLength
79
+ end
80
+ end
81
+ end
82
+ end
83
+ # rubocop:enable Lint/RedundantCopDisableDirective
84
+ # rubocop:enable Style/DoubleCopDisableDirective
@@ -73,7 +73,7 @@ module RuboCop
73
73
  # require 'my_debugger/start'
74
74
  class Debugger < Base
75
75
  MSG = 'Remove debugger entry point `%<source>s`.'
76
- BLOCK_TYPES = %i[block numblock kwbegin].freeze
76
+ BLOCK_TYPES = %i[block numblock itblock kwbegin].freeze
77
77
 
78
78
  def on_send(node)
79
79
  return if assumed_usage_context?(node)
@@ -120,7 +120,7 @@ module RuboCop
120
120
  return true if assumed_argument?(node)
121
121
 
122
122
  node.each_ancestor.none? do |ancestor|
123
- BLOCK_TYPES.include?(ancestor.type) || ancestor.lambda_or_proc?
123
+ ancestor.type?(:any_block, :kwbegin) || ancestor.lambda_or_proc?
124
124
  end
125
125
  end
126
126
 
@@ -134,7 +134,7 @@ module RuboCop
134
134
  if NO_ARG_ALGORITHM.include?(algorithm_parts.first.upcase) && no_arguments
135
135
  "'#{algorithm_parts.first}'"
136
136
  else
137
- mode = 'cbc' unless size_and_mode == ['cbc']
137
+ mode = 'cbc' if size_and_mode.empty?
138
138
 
139
139
  "'#{(algorithm_parts + size_and_mode + [mode]).compact.take(3).join('-')}'"
140
140
  end
@@ -42,7 +42,6 @@ module RuboCop
42
42
  class DuplicateMethods < Base
43
43
  MSG = 'Method `%<method>s` is defined at both %<defined>s and %<current>s.'
44
44
  RESTRICT_ON_SEND = %i[alias_method attr_reader attr_writer attr_accessor attr].freeze
45
- DEF_TYPES = %i[def defs].freeze
46
45
 
47
46
  def initialize(config = nil, options = nil)
48
47
  super
@@ -54,14 +53,12 @@ module RuboCop
54
53
  # if a method definition is inside an if, it is very likely
55
54
  # that a different definition is used depending on platform, etc.
56
55
  return if node.each_ancestor.any?(&:if_type?)
57
- return if possible_dsl?(node)
58
56
 
59
57
  found_instance_method(node, node.method_name)
60
58
  end
61
59
 
62
60
  def on_defs(node)
63
61
  return if node.each_ancestor.any?(&:if_type?)
64
- return if possible_dsl?(node)
65
62
 
66
63
  if node.receiver.const_type?
67
64
  _, const_name = *node.receiver
@@ -79,7 +76,6 @@ module RuboCop
79
76
  def on_alias(node)
80
77
  return unless (name = method_alias?(node))
81
78
  return if node.ancestors.any?(&:if_type?)
82
- return if possible_dsl?(node)
83
79
 
84
80
  found_instance_method(node, name)
85
81
  end
@@ -94,7 +90,6 @@ module RuboCop
94
90
  def on_send(node)
95
91
  if (name = alias_method?(node))
96
92
  return if node.ancestors.any?(&:if_type?)
97
- return if possible_dsl?(node)
98
93
 
99
94
  found_instance_method(node, name)
100
95
  elsif (attr = node.attribute_accessor?)
@@ -166,7 +161,7 @@ module RuboCop
166
161
  end
167
162
 
168
163
  def method_key(node, method_name)
169
- if (ancestor_def = node.each_ancestor(*DEF_TYPES).first)
164
+ if (ancestor_def = node.each_ancestor(:any_def).first)
170
165
  "#{ancestor_def.method_name}.#{method_name}"
171
166
  else
172
167
  method_name
@@ -174,7 +169,7 @@ module RuboCop
174
169
  end
175
170
 
176
171
  def location(node)
177
- if DEF_TYPES.include?(node.type)
172
+ if node.any_def_type?
178
173
  node.loc.keyword.join(node.loc.name)
179
174
  else
180
175
  node.source_range
@@ -237,16 +232,6 @@ module RuboCop
237
232
  end
238
233
  end
239
234
 
240
- def possible_dsl?(node)
241
- # DSL methods may evaluate a block in the context of a newly created
242
- # class or module
243
- # Assume that if a method definition is inside any block call which
244
- # we can't identify, it could be a DSL
245
- node.each_ancestor(:block).any? do |ancestor|
246
- !ancestor.method?(:class_eval) && !ancestor.class_constructor?
247
- end
248
- end
249
-
250
235
  def source_location(node)
251
236
  range = node.source_range
252
237
  path = smart_path(range.source_buffer.name)