rubocop 1.9.0 → 1.12.1

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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -14
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +70 -17
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +5 -0
  7. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  8. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
  9. data/lib/rubocop/comment_config.rb +43 -94
  10. data/lib/rubocop/config.rb +4 -1
  11. data/lib/rubocop/cop/base.rb +1 -0
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  14. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  15. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  16. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  17. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  18. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  19. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  20. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  21. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  22. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  23. data/lib/rubocop/cop/generator.rb +2 -2
  24. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  25. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
  26. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  27. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  29. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  30. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  31. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  36. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  37. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
  38. data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
  39. data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
  40. data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
  41. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  42. data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
  43. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
  46. data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
  47. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +20 -3
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  50. data/lib/rubocop/cop/layout/first_argument_indentation.rb +27 -7
  51. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
  52. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
  54. data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
  55. data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
  56. data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
  57. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  59. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -5
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +10 -5
  61. data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
  62. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  63. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  64. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  65. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  66. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  67. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  68. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  69. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  70. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  71. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  72. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  73. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  74. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  75. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  76. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  77. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  78. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  79. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  80. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  81. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  82. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  83. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  84. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  85. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  86. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  87. data/lib/rubocop/cop/lint/number_conversion.rb +11 -2
  88. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  89. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  90. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
  91. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  92. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  93. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  94. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  95. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  96. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  97. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  98. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  99. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  100. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  101. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  102. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  103. data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
  104. data/lib/rubocop/cop/lint/symbol_conversion.rb +91 -3
  105. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  106. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  107. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  108. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  109. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  110. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  111. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  112. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  113. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  114. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  115. data/lib/rubocop/cop/message_annotator.rb +4 -1
  116. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  117. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  118. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  119. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  120. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  121. data/lib/rubocop/cop/mixin/alignment.rb +10 -3
  122. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  123. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  124. data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
  125. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  126. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  127. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  128. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  129. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  130. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  131. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  132. data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
  133. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  134. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
  135. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -23
  136. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  137. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  138. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  139. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  140. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
  141. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  142. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  143. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  144. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
  145. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  146. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  147. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
  148. data/lib/rubocop/cop/registry.rb +10 -1
  149. data/lib/rubocop/cop/security/eval.rb +1 -0
  150. data/lib/rubocop/cop/security/json_load.rb +1 -0
  151. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  152. data/lib/rubocop/cop/security/open.rb +1 -0
  153. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  154. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -2
  155. data/lib/rubocop/cop/style/alias.rb +1 -0
  156. data/lib/rubocop/cop/style/and_or.rb +3 -1
  157. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  158. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  159. data/lib/rubocop/cop/style/array_join.rb +1 -0
  160. data/lib/rubocop/cop/style/attr.rb +1 -0
  161. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
  162. data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
  163. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  164. data/lib/rubocop/cop/style/case_like_if.rb +15 -4
  165. data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
  166. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  167. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  168. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  169. data/lib/rubocop/cop/style/commented_keyword.rb +10 -10
  170. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  171. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  172. data/lib/rubocop/cop/style/date_time.rb +3 -0
  173. data/lib/rubocop/cop/style/dir.rb +1 -0
  174. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  175. data/lib/rubocop/cop/style/documentation.rb +30 -3
  176. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  177. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  178. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  179. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  180. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  181. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  182. data/lib/rubocop/cop/style/eval_with_location.rb +90 -28
  183. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  184. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  185. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
  186. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  187. data/lib/rubocop/cop/style/float_division.rb +4 -0
  188. data/lib/rubocop/cop/style/format_string.rb +2 -0
  189. data/lib/rubocop/cop/style/format_string_token.rb +1 -0
  190. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +0 -3
  191. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  192. data/lib/rubocop/cop/style/hash_conversion.rb +108 -0
  193. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  194. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  195. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  196. data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
  197. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  198. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  199. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +37 -11
  200. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  201. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  202. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +46 -2
  203. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -3
  204. data/lib/rubocop/cop/style/min_max.rb +1 -0
  205. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  206. data/lib/rubocop/cop/style/module_function.rb +5 -0
  207. data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -3
  208. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  209. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  210. data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
  211. data/lib/rubocop/cop/style/nil_comparison.rb +6 -1
  212. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  213. data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
  214. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  215. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  216. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  217. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  218. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  219. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  220. data/lib/rubocop/cop/style/proc.rb +1 -0
  221. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  222. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  223. data/lib/rubocop/cop/style/redundant_begin.rb +44 -4
  224. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  225. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  226. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  227. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  228. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  229. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  230. data/lib/rubocop/cop/style/redundant_return.rb +4 -0
  231. data/lib/rubocop/cop/style/redundant_self.rb +7 -3
  232. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  233. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  234. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  235. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  236. data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
  237. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  238. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  239. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  240. data/lib/rubocop/cop/style/sample.rb +1 -0
  241. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  242. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  243. data/lib/rubocop/cop/style/single_line_methods.rb +4 -1
  244. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  245. data/lib/rubocop/cop/style/sole_nested_conditional.rb +20 -4
  246. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  247. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  248. data/lib/rubocop/cop/style/string_chars.rb +38 -0
  249. data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
  250. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  251. data/lib/rubocop/cop/style/strip.rb +1 -0
  252. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  253. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  254. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
  255. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
  256. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  257. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  258. data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
  259. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  260. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  261. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  262. data/lib/rubocop/cop/util.rb +4 -1
  263. data/lib/rubocop/directive_comment.rb +69 -9
  264. data/lib/rubocop/ext/regexp_parser.rb +3 -6
  265. data/lib/rubocop/formatter/clang_style_formatter.rb +4 -2
  266. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  267. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  268. data/lib/rubocop/formatter/tap_formatter.rb +4 -2
  269. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  270. data/lib/rubocop/magic_comment.rb +1 -1
  271. data/lib/rubocop/name_similarity.rb +1 -1
  272. data/lib/rubocop/target_finder.rb +1 -0
  273. data/lib/rubocop/target_ruby.rb +21 -13
  274. data/lib/rubocop/version.rb +1 -1
  275. metadata +14 -7
@@ -19,6 +19,7 @@ module RuboCop
19
19
  ' rather than just a message.'
20
20
  RESTRICT_ON_SEND = %i[raise fail].freeze
21
21
 
22
+ # @!method implicit_runtime_error_raise_or_fail(node)
22
23
  def_node_matcher :implicit_runtime_error_raise_or_fail,
23
24
  '(send nil? ${:raise :fail} {str dstr})'
24
25
 
@@ -48,6 +48,7 @@ module RuboCop
48
48
  [Style::Not, Style::SymbolProc]
49
49
  end
50
50
 
51
+ # @!method inverse_candidate?(node)
51
52
  def_node_matcher :inverse_candidate?, <<~PATTERN
52
53
  {
53
54
  (send $(send $(...) $_ $...) :!)
@@ -56,6 +57,7 @@ module RuboCop
56
57
  }
57
58
  PATTERN
58
59
 
60
+ # @!method inverse_block?(node)
59
61
  def_node_matcher :inverse_block?, <<~PATTERN
60
62
  (block $(send (...) $_) ... { $(send ... :!)
61
63
  $(send (...) {:!= :!~} ...)
@@ -40,8 +40,12 @@ module RuboCop
40
40
  # to `true` allows the presence of parentheses in such a method call
41
41
  # even with arguments.
42
42
  #
43
- # NOTE: Parens are required around a method with arguments when inside an
44
- # endless method definition (>= Ruby 3.0).
43
+ # NOTE: Parentheses are still allowed in cases where omitting them
44
+ # results in ambiguous or syntactically incorrect code. For example,
45
+ # parentheses are required around a method with arguments when inside an
46
+ # endless method definition introduced in Ruby 3.0. Parentheses are also
47
+ # allowed when forwarding arguments with the triple-dot syntax introduced
48
+ # in Ruby 2.7 as omitting them starts an endless range.
45
49
  #
46
50
  # @example EnforcedStyle: require_parentheses (default)
47
51
  #
@@ -79,6 +83,30 @@ module RuboCop
79
83
  # # good
80
84
  # foo.enforce strict: true
81
85
  #
86
+ # # good
87
+ # # Allows parens for calls that won't produce valid Ruby or be ambiguous.
88
+ # model.validate strict(true)
89
+ #
90
+ # # good
91
+ # # Allows parens for calls that won't produce valid Ruby or be ambiguous.
92
+ # yield path, File.basename(path)
93
+ #
94
+ # # good
95
+ # # Operators methods calls with parens
96
+ # array&.[](index)
97
+ #
98
+ # # good
99
+ # # Operators methods without parens, if you prefer
100
+ # array.[] index
101
+ #
102
+ # # good
103
+ # # Operators methods calls with parens
104
+ # array&.[](index)
105
+ #
106
+ # # good
107
+ # # Operators methods without parens, if you prefer
108
+ # array.[] index
109
+ #
82
110
  # @example IgnoreMacros: true (default)
83
111
  #
84
112
  # # good
@@ -146,6 +174,22 @@ module RuboCop
146
174
  #
147
175
  # # good
148
176
  # Array 1
177
+ #
178
+ # @example AllowParenthesesInStringInterpolation: false (default)
179
+ #
180
+ # # bad
181
+ # "#{t('this.is.bad')}"
182
+ #
183
+ # # good
184
+ # "#{t 'this.is.better'}"
185
+ #
186
+ # @example AllowParenthesesInStringInterpolation: true
187
+ #
188
+ # # good
189
+ # "#{t('this.is.good')}"
190
+ #
191
+ # # good
192
+ # "#{t 'this.is.also.good'}"
149
193
  class MethodCallWithArgsParentheses < Base
150
194
  require_relative 'method_call_with_args_parentheses/omit_parentheses'
151
195
  require_relative 'method_call_with_args_parentheses/require_parentheses'
@@ -5,6 +5,7 @@ module RuboCop
5
5
  module Style
6
6
  class MethodCallWithArgsParentheses
7
7
  # Style omit_parentheses
8
+ # rubocop:disable Metrics/ModuleLength, Metrics/CyclomaticComplexity
8
9
  module OmitParentheses
9
10
  TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
10
11
  OMIT_MSG = 'Omit parentheses for method calls with arguments.'
@@ -15,10 +16,11 @@ module RuboCop
15
16
  def omit_parentheses(node)
16
17
  return unless node.parenthesized?
17
18
  return if inside_endless_method_def?(node)
18
- return if node.implicit_call?
19
+ return if syntax_like_method_call?(node)
19
20
  return if super_call_without_arguments?(node)
20
21
  return if allowed_camel_case_method_call?(node)
21
22
  return if legitimate_call_with_parentheses?(node)
23
+ return if allowed_string_interpolation_method_call?(node)
22
24
 
23
25
  add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
24
26
  auto_correct(corrector, node)
@@ -43,6 +45,10 @@ module RuboCop
43
45
  node.each_ancestor(:def).any?(&:endless?) && node.arguments.any?
44
46
  end
45
47
 
48
+ def syntax_like_method_call?(node)
49
+ node.implicit_call? || node.operator_method?
50
+ end
51
+
46
52
  def super_call_without_arguments?(node)
47
53
  node.super_type? && node.arguments.none?
48
54
  end
@@ -53,6 +59,11 @@ module RuboCop
53
59
  cop_config['AllowParenthesesInCamelCaseMethod'])
54
60
  end
55
61
 
62
+ def allowed_string_interpolation_method_call?(node)
63
+ cop_config['AllowParenthesesInStringInterpolation'] &&
64
+ inside_string_interpolation?(node)
65
+ end
66
+
56
67
  def parentheses_at_the_end_of_multiline_call?(node)
57
68
  node.multiline? &&
58
69
  node.loc.begin.source_line
@@ -101,7 +112,7 @@ module RuboCop
101
112
  call_as_argument_or_chain?(node) ||
102
113
  hash_literal_in_arguments?(node) ||
103
114
  node.descendants.any? do |n|
104
- ambigious_literal?(n) || logical_operator?(n) ||
115
+ n.forwarded_args_type? || ambigious_literal?(n) || logical_operator?(n) ||
105
116
  call_with_braced_block?(n)
106
117
  end
107
118
  end
@@ -114,7 +125,7 @@ module RuboCop
114
125
  def call_as_argument_or_chain?(node)
115
126
  node.parent &&
116
127
  (node.parent.send_type? && !assigned_before?(node.parent, node) ||
117
- node.parent.csend_type? || node.parent.super_type?)
128
+ node.parent.csend_type? || node.parent.super_type? || node.parent.yield_type?)
118
129
  end
119
130
 
120
131
  def hash_literal_in_arguments?(node)
@@ -172,7 +183,12 @@ module RuboCop
172
183
  node.assignment? &&
173
184
  node.loc.operator.begin < target.loc.begin
174
185
  end
186
+
187
+ def inside_string_interpolation?(node)
188
+ node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
189
+ end
175
190
  end
191
+ # rubocop:enable Metrics/ModuleLength, Metrics/CyclomaticComplexity
176
192
  end
177
193
  end
178
194
  end
@@ -34,6 +34,7 @@ module RuboCop
34
34
 
35
35
  private
36
36
 
37
+ # @!method min_max_candidate(node)
37
38
  def_node_matcher :min_max_candidate, <<~PATTERN
38
39
  ({array return} (send [$_receiver !nil?] :min) (send [$_receiver !nil?] :max))
39
40
  PATTERN
@@ -45,11 +45,13 @@ module RuboCop
45
45
  'or `module`.'
46
46
  RESTRICT_ON_SEND = %i[include extend prepend].freeze
47
47
 
48
+ # @!method include_statement(node)
48
49
  def_node_matcher :include_statement, <<~PATTERN
49
50
  (send nil? ${:include :extend :prepend}
50
51
  const)
51
52
  PATTERN
52
53
 
54
+ # @!method in_top_level_scope?(node)
53
55
  def_node_matcher :in_top_level_scope?, <<~PATTERN
54
56
  {
55
57
  root? # either at the top level
@@ -82,8 +82,13 @@ module RuboCop
82
82
  FORBIDDEN_MSG =
83
83
  'Do not use `module_function` or `extend self`.'
84
84
 
85
+ # @!method module_function_node?(node)
85
86
  def_node_matcher :module_function_node?, '(send nil? :module_function)'
87
+
88
+ # @!method extend_self_node?(node)
86
89
  def_node_matcher :extend_self_node?, '(send nil? :extend self)'
90
+
91
+ # @!method private_directive?(node)
87
92
  def_node_matcher :private_directive?, '(send nil? :private ...)'
88
93
 
89
94
  def on_module(node)
@@ -40,7 +40,7 @@ module RuboCop
40
40
  def autocorrect(corrector, node)
41
41
  arguments = node.arguments
42
42
  joined_arguments = arguments.map(&:source).join(', ')
43
- last_line_source_of_arguments = processed_source[arguments.last_line - 1].strip
43
+ last_line_source_of_arguments = last_line_source_of_arguments(arguments)
44
44
 
45
45
  if last_line_source_of_arguments.start_with?(')')
46
46
  joined_arguments = "#{joined_arguments}#{last_line_source_of_arguments}"
@@ -48,13 +48,20 @@ module RuboCop
48
48
  corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
49
49
  end
50
50
 
51
- corrector.replace(arguments_range(node), joined_arguments)
51
+ corrector.remove(arguments_range(node))
52
+ corrector.insert_after(arguments.loc.begin, joined_arguments)
53
+ end
54
+
55
+ def last_line_source_of_arguments(arguments)
56
+ processed_source[arguments.last_line - 1].strip
52
57
  end
53
58
 
54
59
  def arguments_range(node)
55
- range_between(
60
+ range = range_between(
56
61
  node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
57
62
  )
63
+
64
+ range_with_surrounding_space(range: range, side: :left)
58
65
  end
59
66
 
60
67
  def opening_line(node)
@@ -47,11 +47,12 @@ module RuboCop
47
47
  'in a conditional, use `Array#include?` instead.'
48
48
 
49
49
  def on_new_investigation
50
- @compared_elements = []
51
- @allowed_method_comparison = false
50
+ @last_comparison = nil
52
51
  end
53
52
 
54
53
  def on_or(node)
54
+ reset_comparison if switch_comparison?(node)
55
+
55
56
  root_of_or_node = root_of_or_node(node)
56
57
 
57
58
  return unless node == root_of_or_node
@@ -64,14 +65,21 @@ module RuboCop
64
65
 
65
66
  corrector.replace(node, prefer_method)
66
67
  end
68
+
69
+ @last_comparison = node
67
70
  end
68
71
 
69
72
  private
70
73
 
74
+ # @!method simple_double_comparison?(node)
71
75
  def_node_matcher :simple_double_comparison?, '(send $lvar :== $lvar)'
76
+
77
+ # @!method simple_comparison_lhs?(node)
72
78
  def_node_matcher :simple_comparison_lhs?, <<~PATTERN
73
79
  (send $lvar :== $_)
74
80
  PATTERN
81
+
82
+ # @!method simple_comparison_rhs?(node)
75
83
  def_node_matcher :simple_comparison_rhs?, <<~PATTERN
76
84
  (send $_ :== $lvar)
77
85
  PATTERN
@@ -131,6 +139,17 @@ module RuboCop
131
139
  end
132
140
  end
133
141
 
142
+ def switch_comparison?(node)
143
+ return true if @last_comparison.nil?
144
+
145
+ @last_comparison.descendants.none? { |descendant| descendant == node }
146
+ end
147
+
148
+ def reset_comparison
149
+ @compared_elements = []
150
+ @allowed_method_comparison = false
151
+ end
152
+
134
153
  def allow_method_comparison?
135
154
  cop_config.fetch('AllowMethodComparison', true)
136
155
  end
@@ -154,12 +154,14 @@ module RuboCop
154
154
  end
155
155
  end
156
156
 
157
+ # @!method splat_value(node)
157
158
  def_node_matcher :splat_value, <<~PATTERN
158
159
  (array (splat $_))
159
160
  PATTERN
160
161
 
161
162
  # Some of these patterns may not actually return an immutable object,
162
163
  # but we want to consider them immutable for this cop.
164
+ # @!method operation_produces_immutable_object?(node)
163
165
  def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
164
166
  {
165
167
  (const _ _)
@@ -176,6 +178,7 @@ module RuboCop
176
178
  }
177
179
  PATTERN
178
180
 
181
+ # @!method range_enclosed_in_parentheses?(node)
179
182
  def_node_matcher :range_enclosed_in_parentheses?, <<~PATTERN
180
183
  (begin ({irange erange} _ _))
181
184
  PATTERN
@@ -29,12 +29,14 @@ module RuboCop
29
29
  #
30
30
  class NegatedIfElseCondition < Base
31
31
  include RangeHelp
32
+ include CommentsHelp
32
33
  extend AutoCorrector
33
34
 
34
35
  MSG = 'Invert the negated condition and swap the %<type>s branches.'
35
36
 
36
37
  NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
37
38
 
39
+ # @!method double_negation?(node)
38
40
  def_node_matcher :double_negation?, '(send (send _ :!) :!)'
39
41
 
40
42
  def self.autocorrect_incompatible_with
@@ -96,10 +98,22 @@ module RuboCop
96
98
  if node.if_branch.nil?
97
99
  corrector.remove(range_by_whole_lines(node.loc.else, include_final_newline: true))
98
100
  else
99
- corrector.replace(node.if_branch, node.else_branch.source)
100
- corrector.replace(node.else_branch, node.if_branch.source)
101
+ if_range = node_with_comments(node.if_branch)
102
+ else_range = node_with_comments(node.else_branch)
103
+
104
+ corrector.replace(if_range, else_range.source)
105
+ corrector.replace(else_range, if_range.source)
101
106
  end
102
107
  end
108
+
109
+ def node_with_comments(node)
110
+ first_statement = node.begin_type? ? node.children[0] : node
111
+ return node if processed_source.ast_with_comments[first_statement].empty?
112
+
113
+ begin_pos = source_range_with_comment(first_statement).begin_pos
114
+ end_pos = node.source_range.end_pos
115
+ Parser::Source::Range.new(buffer, begin_pos, end_pos)
116
+ end
103
117
  end
104
118
  end
105
119
  end
@@ -37,7 +37,10 @@ module RuboCop
37
37
 
38
38
  RESTRICT_ON_SEND = %i[== === nil?].freeze
39
39
 
40
+ # @!method nil_comparison?(node)
40
41
  def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
42
+
43
+ # @!method nil_check?(node)
41
44
  def_node_matcher :nil_check?, '(send _ :nil?)'
42
45
 
43
46
  def on_send(node)
@@ -50,7 +53,9 @@ module RuboCop
50
53
  end
51
54
 
52
55
  corrector.replace(node, new_code)
53
- corrector.wrap(node, '(', ')') if node.parent&.method?(:!)
56
+
57
+ parent = node.parent
58
+ corrector.wrap(node, '(', ')') if parent.respond_to?(:method?) && parent.method?(:!)
54
59
  end
55
60
  end
56
61
  end
@@ -28,6 +28,7 @@ module RuboCop
28
28
 
29
29
  MSG = 'Use an empty lambda instead of always returning nil.'
30
30
 
31
+ # @!method nil_return?(node)
31
32
  def_node_matcher :nil_return?, <<~PATTERN
32
33
  { ({return next break} nil) (nil) }
33
34
  PATTERN
@@ -49,9 +49,16 @@ module RuboCop
49
49
 
50
50
  RESTRICT_ON_SEND = %i[!= nil? !].freeze
51
51
 
52
+ # @!method not_equal_to_nil?(node)
52
53
  def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
54
+
55
+ # @!method unless_check?(node)
53
56
  def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
57
+
58
+ # @!method nil_check?(node)
54
59
  def_node_matcher :nil_check?, '(send _ :nil?)'
60
+
61
+ # @!method not_and_nil_check?(node)
55
62
  def_node_matcher :not_and_nil_check?, '(send (send _ :nil?) :!)'
56
63
 
57
64
  def on_send(node)
@@ -28,10 +28,6 @@ module RuboCop
28
28
  # 10_000_00 # typical representation of $10,000 in cents
29
29
  #
30
30
  class NumericLiterals < Base
31
- # The parameter is called MinDigits (meaning the minimum number of
32
- # digits for which an offense can be registered), but essentially it's
33
- # a Max parameter (the maximum number of something that's allowed).
34
- include ConfigurableMax
35
31
  include IntegerNode
36
32
  extend AutoCorrector
37
33
 
@@ -39,6 +35,11 @@ module RuboCop
39
35
  'separate every 3 digits with them.'
40
36
  DELIMITER_REGEXP = /[eE.]/.freeze
41
37
 
38
+ # The parameter is called MinDigits (meaning the minimum number of
39
+ # digits for which an offense can be registered), but essentially it's
40
+ # a Max parameter (the maximum number of something that's allowed).
41
+ exclude_limit 'MinDigits'
42
+
42
43
  def on_int(node)
43
44
  check(node)
44
45
  end
@@ -49,10 +50,6 @@ module RuboCop
49
50
 
50
51
  private
51
52
 
52
- def max_parameter_name
53
- 'MinDigits'
54
- end
55
-
56
53
  def check(node)
57
54
  int = integer_part(node)
58
55
 
@@ -62,7 +59,7 @@ module RuboCop
62
59
 
63
60
  case int
64
61
  when /^\d+$/
65
- return unless (self.max = int.size + 1)
62
+ return unless (self.min_digits = int.size + 1)
66
63
 
67
64
  register_offense(node)
68
65
  when /\d{4}/, short_group_regex