rubocop 1.43.0 → 1.50.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (286) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +108 -40
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +7 -0
  5. data/lib/rubocop/cli/command/execute_runner.rb +7 -2
  6. data/lib/rubocop/cli.rb +54 -8
  7. data/lib/rubocop/comment_config.rb +19 -0
  8. data/lib/rubocop/config.rb +3 -3
  9. data/lib/rubocop/config_loader.rb +20 -23
  10. data/lib/rubocop/config_loader_resolver.rb +3 -4
  11. data/lib/rubocop/cop/autocorrect_logic.rb +29 -13
  12. data/lib/rubocop/cop/base.rb +28 -10
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  14. data/lib/rubocop/cop/commissioner.rb +8 -2
  15. data/lib/rubocop/cop/cop.rb +25 -5
  16. data/lib/rubocop/cop/corrector.rb +11 -3
  17. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  18. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +3 -3
  19. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +3 -3
  20. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  21. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
  22. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +2 -7
  23. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  24. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  25. data/lib/rubocop/cop/gemspec/dependency_version.rb +1 -1
  26. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
  27. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  28. data/lib/rubocop/cop/internal_affairs/cop_description.rb +5 -5
  29. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +3 -3
  30. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
  31. data/lib/rubocop/cop/internal_affairs/location_expression.rb +37 -0
  32. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  33. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  34. data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +42 -0
  35. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
  36. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  37. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  38. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +66 -0
  39. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  40. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  41. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -15
  42. data/lib/rubocop/cop/layout/class_structure.rb +8 -19
  43. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -1
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
  45. data/lib/rubocop/cop/layout/empty_comment.rb +3 -3
  46. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  47. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  48. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  49. data/lib/rubocop/cop/layout/end_alignment.rb +9 -1
  50. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
  51. data/lib/rubocop/cop/layout/first_argument_indentation.rb +8 -3
  52. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
  53. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
  54. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
  55. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
  56. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +8 -2
  57. data/lib/rubocop/cop/layout/heredoc_indentation.rb +8 -11
  58. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  59. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  60. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -3
  61. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +11 -7
  62. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
  63. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
  64. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
  65. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
  66. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
  67. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
  68. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  69. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  70. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  71. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
  72. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
  73. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  74. data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
  75. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  76. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
  77. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
  78. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  79. data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
  80. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  81. data/lib/rubocop/cop/lint/debugger.rb +11 -27
  82. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
  83. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  84. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
  85. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  86. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -3
  87. data/lib/rubocop/cop/lint/else_layout.rb +3 -7
  88. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  89. data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
  90. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  91. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
  92. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
  93. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  94. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  95. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +46 -4
  96. data/lib/rubocop/cop/lint/missing_super.rb +31 -2
  97. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  98. data/lib/rubocop/cop/lint/nested_method_definition.rb +4 -9
  99. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -0
  100. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +6 -10
  101. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  102. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  103. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +11 -5
  104. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
  105. data/lib/rubocop/cop/lint/redundant_require_statement.rb +12 -2
  106. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  107. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
  108. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  109. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  110. data/lib/rubocop/cop/lint/refinement_import_methods.rb +2 -1
  111. data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
  112. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  113. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  114. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  115. data/lib/rubocop/cop/lint/syntax.rb +4 -0
  116. data/lib/rubocop/cop/lint/to_enum_arguments.rb +13 -3
  117. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  118. data/lib/rubocop/cop/lint/useless_access_modifier.rb +10 -7
  119. data/lib/rubocop/cop/lint/useless_method_definition.rb +12 -4
  120. data/lib/rubocop/cop/lint/useless_rescue.rb +20 -2
  121. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +9 -1
  122. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  123. data/lib/rubocop/cop/lint/void.rb +24 -11
  124. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  125. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  126. data/lib/rubocop/cop/metrics/class_length.rb +1 -0
  127. data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
  128. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  129. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +2 -5
  130. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +3 -3
  131. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  132. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  133. data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
  134. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  135. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  136. data/lib/rubocop/cop/mixin/comments_help.rb +8 -6
  137. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  138. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
  139. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +56 -25
  140. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  141. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
  142. data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
  143. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +0 -3
  144. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  145. data/lib/rubocop/cop/mixin/range_help.rb +1 -6
  146. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  147. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  148. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -2
  149. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  150. data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
  151. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  152. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  153. data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
  154. data/lib/rubocop/cop/naming/method_name.rb +3 -3
  155. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  156. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  157. data/lib/rubocop/cop/registry.rb +15 -8
  158. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
  159. data/lib/rubocop/cop/style/accessor_grouping.rb +39 -17
  160. data/lib/rubocop/cop/style/arguments_forwarding.rb +4 -3
  161. data/lib/rubocop/cop/style/array_intersect.rb +1 -1
  162. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  163. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  164. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  165. data/lib/rubocop/cop/style/block_delimiters.rb +17 -2
  166. data/lib/rubocop/cop/style/case_like_if.rb +20 -3
  167. data/lib/rubocop/cop/style/class_and_module_children.rb +4 -11
  168. data/lib/rubocop/cop/style/class_equality_comparison.rb +42 -9
  169. data/lib/rubocop/cop/style/collection_compact.rb +4 -1
  170. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  171. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  172. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  173. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  174. data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
  175. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -12
  176. data/lib/rubocop/cop/style/copyright.rb +1 -1
  177. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  178. data/lib/rubocop/cop/style/dir_empty.rb +60 -0
  179. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  180. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +2 -2
  181. data/lib/rubocop/cop/style/documentation.rb +11 -5
  182. data/lib/rubocop/cop/style/documentation_method.rb +10 -4
  183. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  184. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  185. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  186. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  187. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  188. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  189. data/lib/rubocop/cop/style/file_empty.rb +71 -0
  190. data/lib/rubocop/cop/style/file_read.rb +1 -1
  191. data/lib/rubocop/cop/style/file_write.rb +1 -1
  192. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  193. data/lib/rubocop/cop/style/guard_clause.rb +1 -1
  194. data/lib/rubocop/cop/style/hash_except.rb +4 -4
  195. data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
  196. data/lib/rubocop/cop/style/hash_syntax.rb +5 -2
  197. data/lib/rubocop/cop/style/if_unless_modifier.rb +108 -15
  198. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
  199. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  200. data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
  201. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  202. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
  203. data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
  204. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  205. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +14 -12
  206. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +44 -37
  207. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
  208. data/lib/rubocop/cop/style/min_max.rb +3 -3
  209. data/lib/rubocop/cop/style/min_max_comparison.rb +11 -1
  210. data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
  211. data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
  212. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  213. data/lib/rubocop/cop/style/multiline_method_signature.rb +7 -4
  214. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
  215. data/lib/rubocop/cop/style/negated_if_else_condition.rb +13 -12
  216. data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
  217. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
  218. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
  219. data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
  220. data/lib/rubocop/cop/style/parallel_assignment.rb +29 -19
  221. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  222. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  223. data/lib/rubocop/cop/style/redundant_condition.rb +18 -3
  224. data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
  225. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
  226. data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
  227. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
  228. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  229. data/lib/rubocop/cop/style/redundant_line_continuation.rb +179 -0
  230. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -2
  231. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  232. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -8
  233. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +11 -4
  234. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  235. data/lib/rubocop/cop/style/redundant_string_escape.rb +3 -4
  236. data/lib/rubocop/cop/style/require_order.rb +3 -12
  237. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  238. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  239. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  240. data/lib/rubocop/cop/style/semicolon.rb +24 -2
  241. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
  242. data/lib/rubocop/cop/style/sole_nested_conditional.rb +3 -3
  243. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  244. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  245. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  246. data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
  247. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  248. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  249. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  250. data/lib/rubocop/cop/style/unpack_first.rb +3 -3
  251. data/lib/rubocop/cop/style/word_array.rb +18 -6
  252. data/lib/rubocop/cop/style/yoda_condition.rb +13 -6
  253. data/lib/rubocop/cop/style/yoda_expression.rb +11 -2
  254. data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
  255. data/lib/rubocop/cop/team.rb +30 -22
  256. data/lib/rubocop/cop/util.rb +13 -4
  257. data/lib/rubocop/cop/variable_force/scope.rb +3 -3
  258. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  259. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
  260. data/lib/rubocop/cop/variable_force.rb +1 -1
  261. data/lib/rubocop/cops_documentation_generator.rb +10 -3
  262. data/lib/rubocop/directive_comment.rb +3 -3
  263. data/lib/rubocop/ext/comment.rb +18 -0
  264. data/lib/rubocop/ext/regexp_node.rb +1 -1
  265. data/lib/rubocop/ext/regexp_parser.rb +1 -1
  266. data/lib/rubocop/formatter/junit_formatter.rb +4 -1
  267. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  268. data/lib/rubocop/formatter.rb +0 -1
  269. data/lib/rubocop/options.rb +26 -2
  270. data/lib/rubocop/result_cache.rb +1 -1
  271. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  272. data/lib/rubocop/rspec/expect_offense.rb +6 -4
  273. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  274. data/lib/rubocop/rspec/support.rb +1 -0
  275. data/lib/rubocop/runner.rb +40 -4
  276. data/lib/rubocop/server/cache.rb +11 -4
  277. data/lib/rubocop/server/cli.rb +37 -18
  278. data/lib/rubocop/server/client_command/exec.rb +1 -1
  279. data/lib/rubocop/server/client_command/start.rb +6 -1
  280. data/lib/rubocop/server/core.rb +24 -9
  281. data/lib/rubocop/server/helper.rb +1 -1
  282. data/lib/rubocop/server/server_command/exec.rb +1 -1
  283. data/lib/rubocop/target_ruby.rb +1 -1
  284. data/lib/rubocop/version.rb +1 -1
  285. data/lib/rubocop.rb +12 -0
  286. metadata +21 -26
@@ -25,10 +25,7 @@ module RuboCop
25
25
  # > http://c2.com/cgi/wiki?AbcMetric
26
26
  CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze
27
27
 
28
- # TODO: move to rubocop-ast
29
- ARGUMENT_TYPES = %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg].freeze
30
-
31
- private_constant :BRANCH_NODES, :CONDITION_NODES, :ARGUMENT_TYPES
28
+ private_constant :BRANCH_NODES, :CONDITION_NODES
32
29
 
33
30
  def self.calculate(node, discount_repeated_attributes: false)
34
31
  new(node, discount_repeated_attributes: discount_repeated_attributes).calculate
@@ -129,7 +126,7 @@ module RuboCop
129
126
  end
130
127
 
131
128
  def argument?(node)
132
- ARGUMENT_TYPES.include?(node.type) && capturing_variable?(node.children.first)
129
+ node.argument_type? && capturing_variable?(node.children.first)
133
130
  end
134
131
 
135
132
  def condition?(node)
@@ -10,7 +10,7 @@ module RuboCop
10
10
  include Util
11
11
 
12
12
  FOLDABLE_TYPES = %i[array hash heredoc send csend].freeze
13
- CLASSLIKE_TYPES = %i[class module].freeze
13
+ CLASSLIKE_TYPES = %i[class module sclass].freeze
14
14
  private_constant :FOLDABLE_TYPES, :CLASSLIKE_TYPES
15
15
 
16
16
  def initialize(node, processed_source, count_comments: false, foldable_types: [])
@@ -163,8 +163,8 @@ module RuboCop
163
163
  return 0 unless parenthesized?(parent)
164
164
 
165
165
  [
166
- parent.loc.begin.end_pos != descendant.loc.expression.begin_pos,
167
- parent.loc.end.begin_pos != descendant.loc.expression.end_pos
166
+ parent.loc.begin.end_pos != descendant.source_range.begin_pos,
167
+ parent.loc.end.begin_pos != descendant.source_range.end_pos
168
168
  ].count(true)
169
169
  end
170
170
 
@@ -45,7 +45,7 @@ module RuboCop
45
45
  end
46
46
 
47
47
  def check_cop_name(name, comment, offset)
48
- start = comment.location.expression.begin_pos + offset
48
+ start = comment.source_range.begin_pos + offset
49
49
  range = range_between(start, start + name.length)
50
50
 
51
51
  add_offense(range) do |corrector|
@@ -12,7 +12,7 @@ module RuboCop
12
12
  attr_reader :column_delta
13
13
 
14
14
  def configured_indentation_width
15
- cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width']
15
+ cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width'] || 2
16
16
  end
17
17
 
18
18
  def indentation(node)
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # This module encapsulates the ability to allow certain methods when
6
- # parsing.
6
+ # parsing. Even if the code is in offense, if it contains methods
7
+ # that are allowed. This module is equivalent to the IgnoredMethods module,
8
+ # which will be deprecated in RuboCop 2.0.
7
9
  module AllowedMethods
8
10
  private
9
11
 
@@ -29,7 +29,7 @@ module RuboCop
29
29
 
30
30
  # Returns the range bounds for just the annotation
31
31
  def bounds
32
- start = comment.loc.expression.begin_pos + margin.length
32
+ start = comment.source_range.begin_pos + margin.length
33
33
  length = [keyword, colon, space].reduce(0) { |len, elem| len + elem.to_s.length }
34
34
  [start, start + length]
35
35
  end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  length = calculator.calculate
37
37
  return if length <= max_length
38
38
 
39
- location = node.casgn_type? ? node.loc.name : node.loc.expression
39
+ location = node.casgn_type? ? node.loc.name : node.source_range
40
40
 
41
41
  add_offense(location, message: message(length, max_length)) { self.max = length }
42
42
  end
@@ -37,7 +37,7 @@ module RuboCop
37
37
  private
38
38
 
39
39
  def end_position_for(node)
40
- end_line = buffer.line_for_position(node.loc.expression.end_pos)
40
+ end_line = buffer.line_for_position(node.source_range.end_pos)
41
41
  buffer.line_range(end_line).end_pos
42
42
  end
43
43
 
@@ -62,19 +62,21 @@ module RuboCop
62
62
  # Returns the end line of a node, which might be a comment and not part of the AST
63
63
  # End line is considered either the line at which another node starts, or
64
64
  # the line at which the parent node ends.
65
- # rubocop:disable Metrics/AbcSize
65
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
66
66
  def find_end_line(node)
67
- if node.if_type? && node.loc.else
67
+ if node.if_type? && node.else?
68
68
  node.loc.else.line
69
- elsif (next_sibling = node.right_sibling)
69
+ elsif node.if_type? && node.ternary?
70
+ node.else_branch.loc.line
71
+ elsif (next_sibling = node.right_sibling) && next_sibling.is_a?(AST::Node)
70
72
  next_sibling.loc.line
71
73
  elsif (parent = node.parent)
72
- parent.loc.end ? parent.loc.end.line : parent.loc.line
74
+ parent.loc.respond_to?(:end) && parent.loc.end ? parent.loc.end.line : parent.loc.line
73
75
  else
74
76
  node.loc.end.line
75
77
  end
76
78
  end
77
- # rubocop:enable Metrics/AbcSize
79
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
78
80
  end
79
81
  end
80
82
  end
@@ -23,7 +23,7 @@ module RuboCop
23
23
  # The args node1 & node2 may represent a RuboCop::AST::Node
24
24
  # or a Parser::Source::Comment. Both respond to #loc.
25
25
  def preceding_comment?(node1, node2)
26
- node1 && node2 && precede?(node2, node1) && comment_line?(node2.loc.expression.source)
26
+ node1 && node2 && precede?(node2, node1) && comment_line?(node2.source)
27
27
  end
28
28
 
29
29
  # The args node1 & node2 may represent a RuboCop::AST::Node
@@ -33,7 +33,7 @@ module RuboCop
33
33
 
34
34
  def separator_delta(pair)
35
35
  if pair.hash_rocket?
36
- correct_separator_column = pair.key.loc.expression.end.column + 1
36
+ correct_separator_column = pair.key.source_range.end.column + 1
37
37
  actual_separator_column = pair.loc.operator.column
38
38
 
39
39
  correct_separator_column - actual_separator_column
@@ -46,21 +46,22 @@ module RuboCop
46
46
 
47
47
  private
48
48
 
49
- # rubocop:disable Metrics/AbcSize
50
- def register_offense(node, message, replacement)
49
+ def register_offense(node, message, replacement) # rubocop:disable Metrics/AbcSize
51
50
  add_offense(node.value, message: message) do |corrector|
52
51
  if (def_node = def_node_that_require_parentheses(node))
53
- white_spaces = range_between(def_node.loc.selector.end_pos,
52
+ last_argument = def_node.last_argument
53
+ if last_argument.nil? || !last_argument.hash_type?
54
+ next corrector.replace(node, replacement)
55
+ end
56
+
57
+ white_spaces = range_between(def_node.selector.end_pos,
54
58
  def_node.first_argument.source_range.begin_pos)
55
59
  corrector.replace(white_spaces, '(')
56
-
57
- last_argument = def_node.arguments.last
58
60
  corrector.insert_after(last_argument, ')') if node == last_argument.pairs.last
59
61
  end
60
62
  corrector.replace(node, replacement)
61
63
  end
62
64
  end
63
- # rubocop:enable Metrics/AbcSize
64
65
 
65
66
  def ignore_mixed_hash_shorthand_syntax?(hash_node)
66
67
  target_ruby_version <= 3.0 || enforced_shorthand_syntax != 'consistent' ||
@@ -87,31 +88,39 @@ module RuboCop
87
88
  end
88
89
 
89
90
  def require_hash_value_for_around_hash_literal?(node)
90
- return false unless (send_node = find_ancestor_send_node(node))
91
+ return false unless (method_dispatch_node = find_ancestor_method_dispatch_node(node))
91
92
 
92
- !node.parent.braces? && !use_element_of_hash_literal_as_receiver?(send_node, node.parent) &&
93
- use_modifier_form_without_parenthesized_method_call?(send_node)
93
+ !node.parent.braces? &&
94
+ !use_element_of_hash_literal_as_receiver?(method_dispatch_node, node.parent) &&
95
+ use_modifier_form_without_parenthesized_method_call?(method_dispatch_node)
94
96
  end
95
97
 
98
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
96
99
  def def_node_that_require_parentheses(node)
97
100
  last_pair = node.parent.pairs.last
98
101
  return unless last_pair.key.source == last_pair.value.source
99
- return unless (send_node = find_ancestor_send_node(node))
100
- return unless without_parentheses_call_expr_follows?(send_node)
102
+ return unless (dispatch_node = find_ancestor_method_dispatch_node(node))
103
+ return if dispatch_node.assignment_method?
104
+ return if dispatch_node.parenthesized?
105
+ return if dispatch_node.parent && parentheses?(dispatch_node.parent)
106
+ return if last_expression?(dispatch_node) && !method_dispatch_as_argument?(dispatch_node)
101
107
 
102
- def_node = node.each_ancestor(:send, :csend).first
108
+ def_node = node.each_ancestor(:send, :csend, :super, :yield).first
103
109
 
104
- def_node unless def_node && def_node.arguments.empty?
110
+ DefNode.new(def_node) unless def_node && def_node.arguments.empty?
105
111
  end
112
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
106
113
 
107
- def find_ancestor_send_node(node)
108
- ancestor = node.parent.parent
114
+ def find_ancestor_method_dispatch_node(node)
115
+ return unless (ancestor = node.parent.parent)
116
+ return unless ancestor.call_type? || ancestor.super_type? || ancestor.yield_type?
117
+ return if brackets?(ancestor)
109
118
 
110
- ancestor if ancestor&.call_type? && !brackets?(ancestor)
119
+ ancestor
111
120
  end
112
121
 
113
- def brackets?(send_node)
114
- send_node.method?(:[]) || send_node.method?(:[]=)
122
+ def brackets?(method_dispatch_node)
123
+ method_dispatch_node.method?(:[]) || method_dispatch_node.method?(:[]=)
115
124
  end
116
125
 
117
126
  def use_element_of_hash_literal_as_receiver?(ancestor, parent)
@@ -125,15 +134,19 @@ module RuboCop
125
134
  ancestor.ancestors.any? { |node| node.respond_to?(:modifier_form?) && node.modifier_form? }
126
135
  end
127
136
 
128
- def without_parentheses_call_expr_follows?(ancestor)
129
- return false unless ancestor.respond_to?(:parenthesized?) && !ancestor.parenthesized?
137
+ def last_expression?(node)
138
+ return false if node.right_sibling
139
+ return true unless (assignment_node = node.each_ancestor.find(&:assignment?))
140
+ return last_expression?(assignment_node.parent) if assignment_node.parent&.assignment?
141
+
142
+ !assignment_node.right_sibling
143
+ end
130
144
 
131
- right_sibling = ancestor.right_sibling
132
- right_sibling ||= ancestor.each_ancestor.find do |node|
133
- node.assignment? || node.send_type?
134
- end&.right_sibling
145
+ def method_dispatch_as_argument?(method_dispatch_node)
146
+ parent = method_dispatch_node.parent
147
+ return false unless parent
135
148
 
136
- !!right_sibling
149
+ parent.call_type? || parent.super_type? || parent.yield_type?
137
150
  end
138
151
 
139
152
  def breakdown_value_types_of_hash(hash_node)
@@ -189,6 +202,24 @@ module RuboCop
189
202
  register_offense(pair_node, OMIT_HASH_VALUE_MSG, replacement)
190
203
  end
191
204
  end
205
+
206
+ DefNode = Struct.new(:node) do
207
+ def selector
208
+ if node.loc.respond_to?(:selector)
209
+ node.loc.selector
210
+ else
211
+ node.loc.keyword
212
+ end
213
+ end
214
+
215
+ def first_argument
216
+ node.first_argument
217
+ end
218
+
219
+ def last_argument
220
+ node.last_argument
221
+ end
222
+ end
192
223
  end
193
224
  end
194
225
  # rubocop:enable Metrics/ModuleLength
@@ -159,7 +159,7 @@ module RuboCop
159
159
  end
160
160
 
161
161
  def strip_prefix_and_suffix(node, corrector)
162
- expression = node.loc.expression
162
+ expression = node.source_range
163
163
  corrector.remove_leading(expression, leading)
164
164
  corrector.remove_trailing(expression, trailing)
165
165
  end
@@ -175,11 +175,11 @@ module RuboCop
175
175
  end
176
176
 
177
177
  def set_new_arg_name(transformed_argname, corrector)
178
- corrector.replace(block_node.arguments.loc.expression, "|#{transformed_argname}|")
178
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
179
179
  end
180
180
 
181
181
  def set_new_body_expression(transforming_body_expr, corrector)
182
- body_source = transforming_body_expr.loc.expression.source
182
+ body_source = transforming_body_expr.source
183
183
  if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
184
184
  body_source = "{ #{body_source} }"
185
185
  end
@@ -4,6 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  # Help methods for determining if a line is too long.
6
6
  module LineLengthHelp
7
+ include Alignment
8
+
7
9
  private
8
10
 
9
11
  def ignore_cop_directives?
@@ -85,7 +87,7 @@ module RuboCop
85
87
 
86
88
  def tab_indentation_width
87
89
  config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
88
- config.for_cop('Layout/IndentationWidth')['Width']
90
+ configured_indentation_width
89
91
  end
90
92
 
91
93
  def uri_regexp
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for checking minimum branches count.
6
+ module MinBranchesCount
7
+ private
8
+
9
+ def min_branches_count?(node)
10
+ branches =
11
+ if node.case_type?
12
+ node.when_branches
13
+ elsif node.if_type?
14
+ if_conditional_branches(node)
15
+ else
16
+ raise ArgumentError, "Unsupported #{node.type.inspect} node type"
17
+ end
18
+
19
+ branches.size >= min_branches_count
20
+ end
21
+
22
+ def min_branches_count
23
+ length = cop_config['MinBranchesCount'] || 3
24
+ return length if length.is_a?(Integer) && length.positive?
25
+
26
+ raise 'MinBranchesCount needs to be a positive integer!'
27
+ end
28
+
29
+ def if_conditional_branches(node, branches = [])
30
+ return [] if node.nil? || !node.if_type?
31
+
32
+ branches << node.if_branch
33
+
34
+ else_branch = node.else_branch
35
+ if_conditional_branches(else_branch, branches) if else_branch&.if_type?
36
+ branches
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # TEAM: backend_infra
4
- # WATCHERS: maxh
5
-
6
3
  module RuboCop
7
4
  module Cop
8
5
  # Common functionality for checking for a line break before each
@@ -10,7 +10,7 @@ module RuboCop
10
10
  def get_source_range(node, comments_as_separators)
11
11
  unless comments_as_separators
12
12
  first_comment = processed_source.ast_with_comments[node].first
13
- return first_comment.loc.expression unless first_comment.nil?
13
+ return first_comment.source_range unless first_comment.nil?
14
14
  end
15
15
  node.source_range
16
16
  end
@@ -132,12 +132,7 @@ module RuboCop
132
132
  end
133
133
 
134
134
  def range_with_comments(node)
135
- ranges = [
136
- node,
137
- *@processed_source.ast_with_comments[node]
138
- ].map do |element|
139
- element.location.expression
140
- end
135
+ ranges = [node, *@processed_source.ast_with_comments[node]].map(&:source_range)
141
136
  ranges.reduce do |result, range|
142
137
  add_range(result, range)
143
138
  end
@@ -65,14 +65,14 @@ module RuboCop
65
65
  end
66
66
 
67
67
  def method_source(if_body)
68
- range_between(if_body.loc.expression.begin_pos, if_body.loc.selector.end_pos).source
68
+ range_between(if_body.source_range.begin_pos, if_body.loc.selector.end_pos).source
69
69
  end
70
70
 
71
71
  def first_line_comment(node)
72
- comment = processed_source.find_comment { |c| same_line?(c, node) }
72
+ comment = processed_source.comments.find { |c| same_line?(c, node) }
73
73
  return unless comment
74
74
 
75
- comment_source = comment.loc.expression.source
75
+ comment_source = comment.source
76
76
  comment_source unless comment_disables_cop?(comment_source)
77
77
  end
78
78
 
@@ -107,9 +107,9 @@ module RuboCop
107
107
  end
108
108
  end
109
109
 
110
- def empty_brackets?(left_bracket_token, right_bracket_token)
111
- left_index = processed_source.tokens.index(left_bracket_token)
112
- right_index = processed_source.tokens.index(right_bracket_token)
110
+ def empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens)
111
+ left_index = tokens.index(left_bracket_token)
112
+ right_index = tokens.index(right_bracket_token)
113
113
  right_index && left_index == right_index - 1
114
114
  end
115
115
 
@@ -75,7 +75,7 @@ module RuboCop
75
75
 
76
76
  def inside_comment?(range, comma_offset)
77
77
  comment = processed_source.comment_at_line(range.line)
78
- comment && comment.loc.expression.begin_pos < range.begin_pos + comma_offset
78
+ comment && comment.source_range.begin_pos < range.begin_pos + comma_offset
79
79
  end
80
80
 
81
81
  # Returns true if the node has round/square/curly brackets.
@@ -132,7 +132,7 @@ module RuboCop
132
132
 
133
133
  def avoid_comma(kind, comma_begin_pos, extra_info)
134
134
  range = range_between(comma_begin_pos, comma_begin_pos + 1)
135
- article = /array/.match?(kind) ? 'an' : 'a'
135
+ article = kind.include?('array') ? 'an' : 'a'
136
136
  msg = format(
137
137
  MSG,
138
138
  command: 'Avoid',
@@ -57,7 +57,7 @@ module RuboCop
57
57
  CONSTANT_MSG = 'Use only ascii symbols in constants.'
58
58
 
59
59
  def on_new_investigation
60
- processed_source.each_token do |token|
60
+ processed_source.tokens.each do |token|
61
61
  next if !should_check?(token) || token.text.ascii_only?
62
62
 
63
63
  message = token.type == :tIDENTIFIER ? IDENTIFIER_MSG : CONSTANT_MSG
@@ -47,6 +47,10 @@ module RuboCop
47
47
 
48
48
  MSG = 'Use %<style>s block forwarding.'
49
49
 
50
+ def self.autocorrect_incompatible_with
51
+ [Lint::AmbiguousOperator]
52
+ end
53
+
50
54
  def on_def(node)
51
55
  return if node.arguments.empty?
52
56
 
@@ -34,7 +34,7 @@ module RuboCop
34
34
 
35
35
  allowed = /#{cop_config['AllowedNames'].join('|')}/
36
36
  name = node.loc.name.source.gsub(allowed, '')
37
- return unless /_/.match?(name)
37
+ return unless name.include?('_')
38
38
 
39
39
  add_offense(node.loc.name)
40
40
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  return if correct_case_delimiters?(node)
39
39
 
40
40
  add_offense(node.loc.heredoc_end) do |corrector|
41
- expr = node.loc.expression
41
+ expr = node.source_range
42
42
 
43
43
  corrector.replace(expr, correct_delimiters(expr.source))
44
44
  corrector.replace(node.loc.heredoc_end, correct_delimiters(delimiter_string(expr)))
@@ -5,6 +5,7 @@ module RuboCop
5
5
  module Naming
6
6
  # Recommends the use of inclusive language instead of problematic terms.
7
7
  # The cop can check the following locations for offenses:
8
+ #
8
9
  # - identifiers
9
10
  # - constants
10
11
  # - variables
@@ -12,6 +13,7 @@ module RuboCop
12
13
  # - symbols
13
14
  # - comments
14
15
  # - file paths
16
+ #
15
17
  # Each of these locations can be individually enabled/disabled via configuration,
16
18
  # for example CheckIdentifiers = true/false.
17
19
  #
@@ -22,6 +24,9 @@ module RuboCop
22
24
  # `WholeWord: true` can be set on a flagged term to indicate the cop should only match when
23
25
  # a term matches the whole word (partial matches will not be offenses).
24
26
  #
27
+ # The cop supports autocorrection when there is only one suggestion. When there are multiple
28
+ # suggestions, the best suggestion cannot be identified and will not be autocorrected.
29
+ #
25
30
  # @example FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }
26
31
  # # Suggest replacing identifier whitelist with allowlist
27
32
  #
@@ -68,6 +73,7 @@ module RuboCop
68
73
  # TeslaVehicle
69
74
  class InclusiveLanguage < Base
70
75
  include RangeHelp
76
+ extend AutoCorrector
71
77
 
72
78
  EMPTY_ARRAY = [].freeze
73
79
  MSG = "Consider replacing '%<term>s'%<suffix>s."
@@ -92,7 +98,7 @@ module RuboCop
92
98
  private
93
99
 
94
100
  def investigate_tokens
95
- processed_source.each_token do |token|
101
+ processed_source.tokens.each do |token|
96
102
  next unless check_token?(token.type)
97
103
 
98
104
  word_locations = scan_for_words(token.text)
@@ -104,9 +110,16 @@ module RuboCop
104
110
 
105
111
  def add_offenses_for_token(token, word_locations)
106
112
  word_locations.each do |word_location|
107
- start_position = token.pos.begin_pos + token.pos.source.index(word_location.word)
108
- range = range_between(start_position, start_position + word_location.word.length)
109
- add_offense(range, message: create_message(word_location.word))
113
+ word = word_location.word
114
+ range = offense_range(token, word)
115
+
116
+ add_offense(range, message: create_message(word)) do |corrector|
117
+ suggestions = find_flagged_term(word)['Suggestions']
118
+
119
+ next unless suggestions.is_a?(String)
120
+
121
+ corrector.replace(range, suggestions)
122
+ end
110
123
  end
111
124
  end
112
125
 
@@ -264,6 +277,12 @@ module RuboCop
264
277
  end
265
278
  " with #{suggestion_str}"
266
279
  end
280
+
281
+ def offense_range(token, word)
282
+ start_position = token.pos.begin_pos + token.pos.source.index(word)
283
+
284
+ range_between(start_position, start_position + word.length)
285
+ end
267
286
  end
268
287
  end
269
288
  end
@@ -10,8 +10,8 @@ module RuboCop
10
10
  #
11
11
  # Naming/MethodName:
12
12
  # AllowedPatterns:
13
- # - '\A\s*onSelectionBulkChange\s*'
14
- # - '\A\s*onSelectionCleared\s*'
13
+ # - '\AonSelectionBulkChange\z'
14
+ # - '\AonSelectionCleared\z'
15
15
  #
16
16
  # Method names matching patterns are always allowed.
17
17
  #
@@ -67,7 +67,7 @@ module RuboCop
67
67
 
68
68
  def range_position(node)
69
69
  selector_end_pos = node.loc.selector.end_pos + 1
70
- expr_end_pos = node.loc.expression.end_pos
70
+ expr_end_pos = node.source_range.end_pos
71
71
 
72
72
  range_between(selector_end_pos, expr_end_pos)
73
73
  end
@@ -72,7 +72,7 @@ module RuboCop
72
72
  next if allowed_method_name?(method_name.to_s, prefix)
73
73
 
74
74
  add_offense(
75
- node.first_argument.loc.expression,
75
+ node.first_argument.source_range,
76
76
  message: message(method_name, expected_name(method_name.to_s, prefix))
77
77
  )
78
78
  end
@@ -92,7 +92,7 @@ module RuboCop
92
92
 
93
93
  def offense_range(resbody)
94
94
  variable = resbody.exception_variable
95
- variable.loc.expression
95
+ variable.source_range
96
96
  end
97
97
 
98
98
  def variable_name_matches?(node, name)