rubocop 1.46.0 → 1.52.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/default.yml +83 -11
  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 +6 -6
  7. data/lib/rubocop/comment_config.rb +2 -0
  8. data/lib/rubocop/config.rb +7 -3
  9. data/lib/rubocop/config_loader.rb +8 -8
  10. data/lib/rubocop/config_obsoletion.rb +2 -2
  11. data/lib/rubocop/cop/autocorrect_logic.rb +29 -13
  12. data/lib/rubocop/cop/base.rb +6 -2
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  14. data/lib/rubocop/cop/cop.rb +2 -2
  15. data/lib/rubocop/cop/corrector.rb +1 -1
  16. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -3
  17. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +3 -3
  18. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +3 -3
  19. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  20. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
  21. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -1
  22. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  23. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  24. data/lib/rubocop/cop/gemspec/dependency_version.rb +1 -1
  25. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
  26. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  27. data/lib/rubocop/cop/internal_affairs/cop_description.rb +37 -13
  28. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +3 -3
  29. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
  30. data/lib/rubocop/cop/internal_affairs/location_expression.rb +37 -0
  31. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +3 -3
  32. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  33. data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +1 -1
  34. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  35. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  36. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +66 -0
  37. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  38. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -21
  39. data/lib/rubocop/cop/layout/class_structure.rb +6 -3
  40. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
  41. data/lib/rubocop/cop/layout/empty_comment.rb +3 -3
  42. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  43. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  44. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  45. data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
  46. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
  47. data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
  48. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
  49. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
  50. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
  51. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
  52. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -4
  53. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  54. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  55. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  56. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -3
  57. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
  58. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
  59. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
  60. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
  61. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
  62. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
  63. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  64. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
  65. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -1
  66. data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
  67. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  68. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  69. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
  70. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  71. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
  72. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  73. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
  74. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  75. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -3
  76. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  77. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  78. data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
  79. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  80. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
  81. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  82. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
  83. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  84. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  85. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  86. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +46 -4
  87. data/lib/rubocop/cop/lint/missing_super.rb +34 -2
  88. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  89. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
  90. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -0
  91. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  92. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
  93. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  94. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  95. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -4
  96. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
  97. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  98. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  99. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
  100. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  101. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  102. data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
  103. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  104. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  105. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  106. data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -12
  107. data/lib/rubocop/cop/lint/syntax.rb +4 -0
  108. data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
  109. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
  110. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  111. data/lib/rubocop/cop/lint/useless_access_modifier.rb +9 -1
  112. data/lib/rubocop/cop/lint/useless_assignment.rb +59 -1
  113. data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
  114. data/lib/rubocop/cop/lint/useless_rescue.rb +4 -1
  115. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  116. data/lib/rubocop/cop/lint/void.rb +69 -9
  117. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  118. data/lib/rubocop/cop/metrics/class_length.rb +1 -0
  119. data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
  120. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  121. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +3 -3
  122. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  123. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  124. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  125. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  126. data/lib/rubocop/cop/mixin/comments_help.rb +9 -5
  127. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  128. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
  129. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +4 -2
  130. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  131. data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
  132. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  133. data/lib/rubocop/cop/mixin/range_help.rb +1 -6
  134. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  135. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  136. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  137. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  138. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  139. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  140. data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
  141. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +22 -7
  142. data/lib/rubocop/cop/naming/method_name.rb +3 -3
  143. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  144. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +12 -4
  145. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  146. data/lib/rubocop/cop/registry.rb +3 -1
  147. data/lib/rubocop/cop/style/accessor_grouping.rb +32 -7
  148. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  149. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  150. data/lib/rubocop/cop/style/attr.rb +11 -1
  151. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  152. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  153. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  154. data/lib/rubocop/cop/style/block_delimiters.rb +11 -2
  155. data/lib/rubocop/cop/style/case_like_if.rb +20 -3
  156. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  157. data/lib/rubocop/cop/style/class_equality_comparison.rb +51 -40
  158. data/lib/rubocop/cop/style/collection_compact.rb +20 -7
  159. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  160. data/lib/rubocop/cop/style/combinable_loops.rb +26 -6
  161. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  162. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  163. data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
  164. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  165. data/lib/rubocop/cop/style/copyright.rb +6 -3
  166. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  167. data/lib/rubocop/cop/style/dir.rb +1 -1
  168. data/lib/rubocop/cop/style/dir_empty.rb +54 -0
  169. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  170. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +2 -2
  171. data/lib/rubocop/cop/style/documentation.rb +11 -5
  172. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  173. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  174. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  175. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  176. data/lib/rubocop/cop/style/eval_with_location.rb +8 -8
  177. data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
  178. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  179. data/lib/rubocop/cop/style/file_empty.rb +71 -0
  180. data/lib/rubocop/cop/style/file_read.rb +3 -3
  181. data/lib/rubocop/cop/style/file_write.rb +1 -1
  182. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  183. data/lib/rubocop/cop/style/guard_clause.rb +3 -1
  184. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
  185. data/lib/rubocop/cop/style/hash_except.rb +23 -12
  186. data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
  187. data/lib/rubocop/cop/style/hash_syntax.rb +5 -2
  188. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  189. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  190. data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
  191. data/lib/rubocop/cop/style/if_unless_modifier.rb +111 -15
  192. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
  193. data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
  194. data/lib/rubocop/cop/style/invertible_unless_condition.rb +9 -5
  195. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
  196. data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
  197. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  198. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -9
  199. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +44 -37
  200. data/lib/rubocop/cop/style/min_max.rb +3 -3
  201. data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
  202. data/lib/rubocop/cop/style/multiline_method_signature.rb +7 -4
  203. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  204. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  205. data/lib/rubocop/cop/style/negated_if_else_condition.rb +12 -7
  206. data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
  207. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  208. data/lib/rubocop/cop/style/parallel_assignment.rb +26 -18
  209. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  210. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  211. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  212. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  213. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +2 -2
  214. data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
  215. data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
  216. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  217. data/lib/rubocop/cop/style/redundant_line_continuation.rb +183 -0
  218. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  219. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  220. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  221. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  222. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +11 -4
  223. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  224. data/lib/rubocop/cop/style/redundant_string_escape.rb +3 -4
  225. data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
  226. data/lib/rubocop/cop/style/require_order.rb +12 -8
  227. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  228. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  229. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  230. data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
  231. data/lib/rubocop/cop/style/semicolon.rb +12 -1
  232. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  233. data/lib/rubocop/cop/style/sole_nested_conditional.rb +6 -4
  234. data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
  235. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  236. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  237. data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
  238. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  239. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  240. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  241. data/lib/rubocop/cop/style/unpack_first.rb +3 -3
  242. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  243. data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
  244. data/lib/rubocop/cop/team.rb +2 -2
  245. data/lib/rubocop/cop/util.rb +1 -1
  246. data/lib/rubocop/cop/variable_force/assignment.rb +33 -1
  247. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  248. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  249. data/lib/rubocop/cop/variable_force.rb +1 -0
  250. data/lib/rubocop/cops_documentation_generator.rb +10 -3
  251. data/lib/rubocop/directive_comment.rb +3 -3
  252. data/lib/rubocop/ext/comment.rb +18 -0
  253. data/lib/rubocop/ext/regexp_node.rb +1 -1
  254. data/lib/rubocop/ext/regexp_parser.rb +1 -1
  255. data/lib/rubocop/formatter/junit_formatter.rb +4 -1
  256. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  257. data/lib/rubocop/options.rb +4 -1
  258. data/lib/rubocop/result_cache.rb +2 -2
  259. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  260. data/lib/rubocop/rspec/support.rb +1 -0
  261. data/lib/rubocop/server/cache.rb +1 -1
  262. data/lib/rubocop/server/client_command/exec.rb +2 -1
  263. data/lib/rubocop/server/core.rb +1 -1
  264. data/lib/rubocop/server/helper.rb +1 -1
  265. data/lib/rubocop/server/server_command/exec.rb +1 -1
  266. data/lib/rubocop/target_ruby.rb +3 -2
  267. data/lib/rubocop/version.rb +10 -6
  268. data/lib/rubocop.rb +13 -0
  269. metadata +23 -8
@@ -36,7 +36,6 @@ module RuboCop
36
36
  # STR
37
37
  class RedundantStringEscape < Base
38
38
  include MatchRange
39
- include RangeHelp
40
39
  extend AutoCorrector
41
40
 
42
41
  MSG = 'Redundant escape of %<char>s inside string literal.'
@@ -64,10 +63,10 @@ module RuboCop
64
63
  def str_contents_range(node)
65
64
  if heredoc?(node)
66
65
  node.loc.heredoc_body
66
+ elsif node.str_type?
67
+ node.source_range
67
68
  elsif begin_loc_present?(node)
68
69
  contents_range(node)
69
- else
70
- node.loc.expression
71
70
  end
72
71
  end
73
72
 
@@ -139,7 +138,7 @@ module RuboCop
139
138
 
140
139
  def heredoc_with_disabled_interpolation?(node)
141
140
  if heredoc?(node)
142
- node.loc.expression.source.end_with?("'")
141
+ node.source.end_with?("'")
143
142
  elsif node.parent&.dstr_type?
144
143
  heredoc_with_disabled_interpolation?(node.parent)
145
144
  else
@@ -3,7 +3,16 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Enforces using // or %r around regular expressions.
6
+ # Enforces using `//` or `%r` around regular expressions.
7
+ #
8
+ # NOTE: The following `%r` cases using a regexp starts with a blank or `=`
9
+ # as a method argument allowed to prevent syntax errors.
10
+ #
11
+ # [source,ruby]
12
+ # ----
13
+ # do_something %r{ regexp} # `do_something / regexp/` is an invalid syntax.
14
+ # do_something %r{=regexp} # `do_something /=regexp/` is an invalid syntax.
15
+ # ----
7
16
  #
8
17
  # @example EnforcedStyle: slashes (default)
9
18
  # # bad
@@ -151,7 +160,7 @@ module RuboCop
151
160
 
152
161
  def allowed_omit_parentheses_with_percent_r_literal?(node)
153
162
  return false unless node.parent&.call_type?
154
- return true if node.content.start_with?(' ')
163
+ return true if node.content.start_with?(' ', '=')
155
164
 
156
165
  enforced_style = config.for_cop('Style/MethodCallWithArgsParentheses')['EnforcedStyle']
157
166
 
@@ -88,10 +88,7 @@ module RuboCop
88
88
  return unless previous_older_sibling
89
89
 
90
90
  add_offense(node, message: format(MSG, name: node.method_name)) do |corrector|
91
- corrector.swap(
92
- range_with_comments_and_lines(previous_older_sibling),
93
- range_with_comments_and_lines(node.parent.if_type? ? node.parent : node)
94
- )
91
+ autocorrect(corrector, node, previous_older_sibling)
95
92
  end
96
93
  end
97
94
 
@@ -109,11 +106,20 @@ module RuboCop
109
106
  break unless sibling&.send_type? && sibling&.method?(node.method_name)
110
107
  break unless sibling.arguments? && !sibling.receiver
111
108
  break unless in_same_section?(sibling, node)
109
+ break unless node.first_argument.str_type? && sibling.first_argument.str_type?
112
110
 
113
- node.first_argument.source < sibling.first_argument.source
111
+ node.first_argument.value < sibling.first_argument.value
114
112
  end
115
113
  end
116
114
 
115
+ def autocorrect(corrector, node, previous_older_sibling)
116
+ range1 = range_with_comments_and_lines(previous_older_sibling)
117
+ range2 = range_with_comments_and_lines(node.parent.if_type? ? node.parent : node)
118
+
119
+ corrector.remove(range2)
120
+ corrector.insert_before(range1, range2.source)
121
+ end
122
+
117
123
  def search_node(node)
118
124
  node.parent.if_type? ? node.parent : node
119
125
  end
@@ -125,9 +131,7 @@ module RuboCop
125
131
  end
126
132
 
127
133
  def in_same_section?(node1, node2)
128
- !node1.location.expression.with(
129
- end_pos: node2.location.expression.end_pos
130
- ).source.include?("\n\n")
134
+ !node1.source_range.with(end_pos: node2.source_range.end_pos).source.include?("\n\n")
131
135
  end
132
136
  end
133
137
  end
@@ -3,9 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for uses of rescue in its modifier form.
7
- #
8
- # The cop to check `rescue` in its modifier form is added for following
6
+ # Checks for uses of `rescue` in its modifier form is added for following
9
7
  # reasons:
10
8
  #
11
9
  # * The syntax of modifier form `rescue` can be misleading because it
@@ -105,11 +105,11 @@ module RuboCop
105
105
  private
106
106
 
107
107
  def offense_for_implicit_enforced_style(node, error)
108
- range = node.loc.keyword.join(error.loc.expression)
108
+ range = node.loc.keyword.join(error.source_range)
109
109
 
110
110
  add_offense(range, message: MSG_IMPLICIT) do |corrector|
111
111
  error = rescue_standard_error?(node)
112
- range = range_between(node.loc.keyword.end_pos, error.loc.expression.end_pos)
112
+ range = range_between(node.loc.keyword.end_pos, error.source_range.end_pos)
113
113
 
114
114
  corrector.remove(range)
115
115
  end
@@ -297,11 +297,11 @@ module RuboCop
297
297
  end
298
298
 
299
299
  def begin_range(node, method_call)
300
- range_between(node.loc.expression.begin_pos, method_call.loc.expression.begin_pos)
300
+ range_between(node.source_range.begin_pos, method_call.source_range.begin_pos)
301
301
  end
302
302
 
303
303
  def end_range(node, method_call)
304
- range_between(method_call.loc.expression.end_pos, node.loc.expression.end_pos)
304
+ range_between(method_call.source_range.end_pos, node.source_range.end_pos)
305
305
  end
306
306
 
307
307
  def add_safe_nav_to_all_methods_in_chain(corrector,
@@ -84,6 +84,7 @@ module RuboCop
84
84
  }
85
85
  PATTERN
86
86
 
87
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
87
88
  def on_send(node)
88
89
  return unless (block_node = node.block_node)
89
90
  return if block_node.body&.begin_type?
@@ -91,11 +92,14 @@ module RuboCop
91
92
  return unless (regexp_method_send_node = extract_send_node(block_node))
92
93
  return if match_predicate_without_receiver?(regexp_method_send_node)
93
94
 
94
- opposite = opposite?(regexp_method_send_node)
95
+ replacement = replacement(regexp_method_send_node, node)
96
+ return if target_ruby_version <= 2.2 && replacement == 'grep_v'
97
+
95
98
  regexp = find_regexp(regexp_method_send_node, block_node)
96
99
 
97
- register_offense(node, block_node, regexp, opposite)
100
+ register_offense(node, block_node, regexp, replacement)
98
101
  end
102
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
99
103
 
100
104
  private
101
105
 
@@ -105,9 +109,15 @@ module RuboCop
105
109
  node.hash_type? || creates_hash?(node) || env_const?(node)
106
110
  end
107
111
 
108
- def register_offense(node, block_node, regexp, opposite)
109
- method_name = node.method_name.to_sym
110
- replacement = opposite ? OPPOSITE_REPLACEMENTS[method_name] : REPLACEMENTS[method_name]
112
+ def replacement(regexp_method_send_node, node)
113
+ opposite = opposite?(regexp_method_send_node)
114
+
115
+ method_name = node.method_name
116
+
117
+ opposite ? OPPOSITE_REPLACEMENTS[method_name] : REPLACEMENTS[method_name]
118
+ end
119
+
120
+ def register_offense(node, block_node, regexp, replacement)
111
121
  message = format(MSG, replacement: replacement, original_method: node.method_name)
112
122
 
113
123
  add_offense(block_node, message: message) do |corrector|
@@ -90,8 +90,11 @@ module RuboCop
90
90
  0
91
91
  elsif exist_semicolon_before_right_curly_brace?(tokens)
92
92
  -3
93
- elsif exist_semicolon_after_left_curly_brace?(tokens)
93
+ elsif exist_semicolon_after_left_curly_brace?(tokens) ||
94
+ exist_semicolon_after_left_string_interpolation_brace?(tokens)
94
95
  2
96
+ elsif exist_semicolon_before_right_string_interpolation_brace?(tokens)
97
+ -4
95
98
  end
96
99
  end
97
100
 
@@ -103,6 +106,14 @@ module RuboCop
103
106
  tokens[1]&.left_curly_brace? && tokens[2]&.semicolon?
104
107
  end
105
108
 
109
+ def exist_semicolon_before_right_string_interpolation_brace?(tokens)
110
+ tokens[-3]&.type == :tSTRING_DEND && tokens[-4]&.semicolon?
111
+ end
112
+
113
+ def exist_semicolon_after_left_string_interpolation_brace?(tokens)
114
+ tokens[1]&.type == :tSTRING_DBEG && tokens[2]&.semicolon?
115
+ end
116
+
106
117
  def register_semicolon(line, column, after_expression, token_before_semicolon = nil)
107
118
  range = source_range(processed_source.buffer, line, column)
108
119
 
@@ -135,7 +135,7 @@ module RuboCop
135
135
 
136
136
  def disallow_endless_method_style?
137
137
  endless_method_config = config.for_cop('Style/EndlessMethod')
138
- return false unless endless_method_config['Enabled']
138
+ return true unless endless_method_config['Enabled']
139
139
 
140
140
  endless_method_config['EnforcedStyle'] == 'disallow'
141
141
  end
@@ -167,7 +167,7 @@ module RuboCop
167
167
  corrector.insert_before(condition,
168
168
  "#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
169
169
 
170
- corrector.remove(node.condition.loc.expression)
170
+ corrector.remove(node.condition)
171
171
  corrector.remove(range_with_surrounding_space(node.loc.keyword, newlines: false))
172
172
  corrector.replace(if_branch.loc.keyword, 'if')
173
173
  end
@@ -186,8 +186,10 @@ module RuboCop
186
186
  begin_pos = condition.first_argument.source_range.begin_pos
187
187
  return if end_pos > begin_pos
188
188
 
189
- corrector.replace(range_between(end_pos, begin_pos), '(')
190
- corrector.insert_after(condition.last_argument.source_range, ')')
189
+ range = range_between(end_pos, begin_pos)
190
+ corrector.remove(range)
191
+ corrector.insert_after(range, '(')
192
+ corrector.insert_after(condition.last_argument, ')')
191
193
  end
192
194
 
193
195
  def insert_bang(corrector, node, is_modify_form)
@@ -240,7 +242,7 @@ module RuboCop
240
242
  end
241
243
 
242
244
  def outer_condition_modify_form?(node, if_branch)
243
- node.condition.loc.expression.begin_pos > if_branch.condition.loc.expression.begin_pos
245
+ node.condition.source_range.begin_pos > if_branch.condition.source_range.begin_pos
244
246
  end
245
247
  end
246
248
  end
@@ -3,8 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- #
7
- # This cop looks for uses of Perl-style global variables.
6
+ # Looks for uses of Perl-style global variables.
8
7
  # Correcting to global variables in the 'English' library
9
8
  # will add a require statement to the top of the file if
10
9
  # enabled by RequireEnglish config.
@@ -234,9 +233,9 @@ module RuboCop
234
233
  end
235
234
 
236
235
  def matching_styles(global)
237
- STYLE_VARS_MAP.map do |style, vars|
236
+ STYLE_VARS_MAP.filter_map do |style, vars|
238
237
  style if vars.values.flatten(1).include? global
239
- end.compact
238
+ end
240
239
  end
241
240
 
242
241
  def english_name_replacement(preferred_name, node)
@@ -49,7 +49,7 @@ module RuboCop
49
49
  end
50
50
 
51
51
  def stderr_puts_range(send)
52
- range_between(send.loc.expression.begin_pos, send.loc.selector.end_pos)
52
+ range_between(send.source_range.begin_pos, send.loc.selector.end_pos)
53
53
  end
54
54
  end
55
55
  end
@@ -55,7 +55,7 @@ module RuboCop
55
55
  elsif (class_node = parent.parent).body.nil?
56
56
  corrector.remove(range_for_empty_class_body(class_node, parent))
57
57
  else
58
- corrector.insert_after(parent.loc.expression, ' do')
58
+ corrector.insert_after(parent, ' do')
59
59
  end
60
60
  end
61
61
 
@@ -34,6 +34,7 @@ module RuboCop
34
34
  )
35
35
  end
36
36
  end
37
+ alias on_sclass on_class
37
38
  end
38
39
  end
39
40
  end
@@ -144,7 +144,7 @@ module RuboCop
144
144
  end
145
145
 
146
146
  def range_for_parentheses(offense, left)
147
- range_between(offense.source_range.begin_pos - 1, left.loc.expression.end_pos - 1)
147
+ range_between(offense.source_range.begin_pos - 1, left.source_range.end_pos - 1)
148
148
  end
149
149
  end
150
150
  end
@@ -238,7 +238,7 @@ module RuboCop
238
238
 
239
239
  indent = ' ' * node.loc.column
240
240
  corrector.replace(
241
- node.source_range,
241
+ node,
242
242
  ['class << self',
243
243
  "#{indent} #{accessor(kind, node.method_name)}",
244
244
  "#{indent}end"].join("\n")
@@ -8,6 +8,7 @@ module RuboCop
8
8
  # to read and understand.
9
9
  #
10
10
  # This cop supports two styles:
11
+ #
11
12
  # - `forbid_mixed_logical_operators` (default)
12
13
  # - `forbid_logical_operators`
13
14
  #
@@ -52,9 +52,9 @@ module RuboCop
52
52
  private
53
53
 
54
54
  def first_element_range(node, unpack_call)
55
- Parser::Source::Range.new(node.loc.expression.source_buffer,
56
- unpack_call.loc.expression.end_pos,
57
- node.loc.expression.end_pos)
55
+ Parser::Source::Range.new(node.source_range.source_buffer,
56
+ unpack_call.source_range.end_pos,
57
+ node.source_range.end_pos)
58
58
  end
59
59
  end
60
60
  end
@@ -145,7 +145,7 @@ module RuboCop
145
145
  end
146
146
 
147
147
  def actual_code_range(node)
148
- range_between(node.loc.expression.begin_pos, node.loc.expression.end_pos)
148
+ range_between(node.source_range.begin_pos, node.source_range.end_pos)
149
149
  end
150
150
 
151
151
  def reverse_comparison(operator)
@@ -4,10 +4,13 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Checks for numeric comparisons that can be replaced
7
- # by a predicate method, such as receiver.length == 0,
8
- # receiver.length > 0, receiver.length != 0,
9
- # receiver.length < 1 and receiver.size == 0 that can be
10
- # replaced by receiver.empty? and !receiver.empty?.
7
+ # by a predicate method, such as `receiver.length == 0`,
8
+ # `receiver.length > 0`, and `receiver.length != 0`,
9
+ # `receiver.length < 1` and `receiver.size == 0` that can be
10
+ # replaced by `receiver.empty?` and `!receiver.empty?`.
11
+ #
12
+ # NOTE: `File`, `Tempfile`, and `StringIO` do not have `empty?`
13
+ # so allow `size == 0` and `size.zero?`.
11
14
  #
12
15
  # @safety
13
16
  # This cop is unsafe because it cannot be guaranteed that the receiver
@@ -49,6 +52,7 @@ module RuboCop
49
52
 
50
53
  def check_zero_length_predicate(node)
51
54
  return unless (length_method = zero_length_predicate(node.parent))
55
+ return if non_polymorphic_collection?(node.parent)
52
56
 
53
57
  offense = node.loc.selector.join(node.parent.source_range.end)
54
58
  message = format(ZERO_MSG, current: "#{length_method}.zero?")
@@ -134,7 +138,7 @@ module RuboCop
134
138
  # @!method non_polymorphic_collection?(node)
135
139
  def_node_matcher :non_polymorphic_collection?, <<~PATTERN
136
140
  {(send (send (send (const {nil? cbase} :File) :stat _) ...) ...)
137
- (send (send (send (const {nil? cbase} {:Tempfile :StringIO}) {:new :open} ...) ...) ...)}
141
+ (send (send (send (const {nil? cbase} {:File :Tempfile :StringIO}) {:new :open} ...) ...) ...)}
138
142
  PATTERN
139
143
  end
140
144
  end
@@ -112,7 +112,7 @@ module RuboCop
112
112
  end
113
113
 
114
114
  def external_dependency_checksum
115
- keys = cops.map(&:external_dependency_checksum).compact
115
+ keys = cops.filter_map(&:external_dependency_checksum)
116
116
  Digest::SHA1.hexdigest(keys.join)
117
117
  end
118
118
 
@@ -160,8 +160,8 @@ module RuboCop
160
160
  def roundup_relevant_cops(processed_source)
161
161
  cops.select do |cop|
162
162
  next true if processed_source.comment_config.cop_opted_in?(cop)
163
- next false unless @registry.enabled?(cop, @config)
164
163
  next false if cop.excluded_file?(processed_source.file_path)
164
+ next false unless @registry.enabled?(cop, @config)
165
165
 
166
166
  support_target_ruby_version?(cop) && support_target_rails_version?(cop)
167
167
  end
@@ -81,7 +81,7 @@ module RuboCop
81
81
  end
82
82
 
83
83
  def args_end(node)
84
- node.loc.expression.end
84
+ node.source_range.end
85
85
  end
86
86
 
87
87
  def on_node(syms, sexp, excludes = [], &block)
@@ -47,6 +47,10 @@ module RuboCop
47
47
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
48
48
  end
49
49
 
50
+ def exception_assignment?
51
+ node.parent&.resbody_type? && node.parent.exception_variable == node
52
+ end
53
+
50
54
  def operator_assignment?
51
55
  return false unless meta_assignment_node
52
56
 
@@ -59,6 +63,18 @@ module RuboCop
59
63
  meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE
60
64
  end
61
65
 
66
+ def rest_assignment?
67
+ return false unless meta_assignment_node
68
+
69
+ meta_assignment_node.type == REST_ASSIGNMENT_TYPE
70
+ end
71
+
72
+ def for_assignment?
73
+ return false unless meta_assignment_node
74
+
75
+ meta_assignment_node.for_type?
76
+ end
77
+
62
78
  def operator
63
79
  assignment_node = meta_assignment_node || @node
64
80
  assignment_node.loc.operator.source
@@ -66,7 +82,10 @@ module RuboCop
66
82
 
67
83
  def meta_assignment_node
68
84
  unless instance_variable_defined?(:@meta_assignment_node)
69
- @meta_assignment_node = operator_assignment_node || multiple_assignment_node
85
+ @meta_assignment_node = operator_assignment_node ||
86
+ multiple_assignment_node ||
87
+ rest_assignment_node ||
88
+ for_assignment_node
70
89
  end
71
90
 
72
91
  @meta_assignment_node
@@ -90,6 +109,19 @@ module RuboCop
90
109
 
91
110
  grandparent_node
92
111
  end
112
+
113
+ def rest_assignment_node
114
+ return nil unless node.parent
115
+ return nil unless node.parent.type == REST_ASSIGNMENT_TYPE
116
+
117
+ node.parent
118
+ end
119
+
120
+ def for_assignment_node
121
+ return nil unless node.parent&.for_type?
122
+
123
+ node.parent
124
+ end
93
125
  end
94
126
  end
95
127
  end
@@ -52,7 +52,7 @@ module RuboCop
52
52
  # if/unless keyword. A preceding assignment is needed to put the
53
53
  # variable in scope. For this reason we skip to the next assignment
54
54
  # here.
55
- next if in_modifier_if?(assignment)
55
+ next if in_modifier_conditional?(assignment)
56
56
 
57
57
  break if !assignment.branch || assignment.branch == reference.branch
58
58
 
@@ -63,10 +63,12 @@ module RuboCop
63
63
  end
64
64
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
65
65
 
66
- def in_modifier_if?(assignment)
66
+ def in_modifier_conditional?(assignment)
67
67
  parent = assignment.node.parent
68
68
  parent = parent.parent if parent&.begin_type?
69
- parent&.if_type? && parent&.modifier_form?
69
+ return false if parent.nil?
70
+
71
+ (parent.if_type? || parent.while_type? || parent.until_type?) && parent.modifier_form?
70
72
  end
71
73
 
72
74
  def capture_with_block!
@@ -113,14 +113,14 @@ module RuboCop
113
113
  def accessible_variables
114
114
  scope_stack.reverse_each.with_object([]) do |scope, variables|
115
115
  variables.concat(scope.variables.values)
116
- break variables unless scope.node.block_type?
116
+ break variables unless scope.node.block_type? || scope.node.numblock_type?
117
117
  end
118
118
  end
119
119
 
120
120
  private
121
121
 
122
122
  def mark_variable_as_captured_by_block_if_so(variable)
123
- return unless current_scope.node.block_type?
123
+ return unless current_scope.node.block_type? || current_scope.node.numblock_type?
124
124
  return if variable.scope == current_scope
125
125
 
126
126
  variable.capture_with_block!
@@ -40,6 +40,7 @@ module RuboCop
40
40
  OPERATOR_ASSIGNMENT_TYPES = (LOGICAL_OPERATOR_ASSIGNMENT_TYPES + [:op_asgn]).freeze
41
41
 
42
42
  MULTIPLE_ASSIGNMENT_TYPE = :masgn
43
+ REST_ASSIGNMENT_TYPE = :splat
43
44
 
44
45
  VARIABLE_REFERENCE_TYPE = :lvar
45
46
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
4
+
3
5
  # Class for generating documentation of all cops departments
4
6
  # @api private
5
7
  class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
@@ -14,6 +16,8 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
14
16
  @departments = departments.map(&:to_sym).sort!
15
17
  @cops = RuboCop::Cop::Registry.global
16
18
  @config = RuboCop::ConfigLoader.default_configuration
19
+ @docs_path = "#{Dir.pwd}/docs/modules/ROOT/pages/"
20
+ FileUtils.mkdir_p(@docs_path)
17
21
  end
18
22
 
19
23
  def call
@@ -27,7 +31,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
27
31
 
28
32
  private
29
33
 
30
- attr_reader :departments, :cops, :config
34
+ attr_reader :departments, :cops, :config, :docs_path
31
35
 
32
36
  def cops_of_department(department)
33
37
  cops.with_department(department).sort!
@@ -252,7 +256,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
252
256
  content = +"= #{department}\n"
253
257
  selected_cops.each { |cop| content << print_cop_with_doc(cop) }
254
258
  content << footer_for_department(department)
255
- file_name = "#{Dir.pwd}/docs/modules/ROOT/pages/#{department_to_basename(department)}.adoc"
259
+ file_name = "#{docs_path}/#{department_to_basename(department)}.adoc"
256
260
  File.open(file_name, 'w') do |file|
257
261
  puts "* generated #{file_name}"
258
262
  file.write("#{content.strip}\n")
@@ -298,7 +302,10 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
298
302
  end
299
303
 
300
304
  def print_table_of_contents
301
- path = "#{Dir.pwd}/docs/modules/ROOT/pages/cops.adoc"
305
+ path = "#{docs_path}/cops.adoc"
306
+
307
+ File.write(path, table_contents) and return unless File.exist?(path)
308
+
302
309
  original = File.read(path)
303
310
  content = +"// START_COP_LIST\n\n"
304
311
 
@@ -45,9 +45,9 @@ module RuboCop
45
45
 
46
46
  def range
47
47
  match = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
48
- begin_pos = comment.loc.expression.begin_pos
48
+ begin_pos = comment.source_range.begin_pos
49
49
  Parser::Source::Range.new(
50
- comment.loc.expression.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0)
50
+ comment.source_range.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0)
51
51
  )
52
52
  end
53
53
 
@@ -108,7 +108,7 @@ module RuboCop
108
108
 
109
109
  # Returns line number for directive
110
110
  def line_number
111
- comment.loc.expression.line
111
+ comment.source_range.line
112
112
  end
113
113
 
114
114
  private
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Ext
5
+ # Extensions to `Parser::Source::Comment`.
6
+ module Comment
7
+ def source
8
+ loc.expression.source
9
+ end
10
+
11
+ def source_range
12
+ loc.expression
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Parser::Source::Comment.include RuboCop::Ext::Comment
@@ -29,7 +29,7 @@ module RuboCop
29
29
  @parsed_tree&.each_expression(true) { |e| e.origin = origin }
30
30
  end
31
31
  # Please remove this `else` branch when support for regexp_parser 1.8 will be dropped.
32
- # It's for compatibility with regexp_arser 1.8 and will never be maintained.
32
+ # It's for compatibility with regexp_parser 1.8 and will never be maintained.
33
33
  else
34
34
  def assign_properties(*)
35
35
  super
@@ -28,7 +28,7 @@ module RuboCop
28
28
  @expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length)
29
29
  end
30
30
  # Please remove this `else` branch when support for regexp_parser 1.8 will be dropped.
31
- # It's for compatibility with regexp_arser 1.8 and will never be maintained.
31
+ # It's for compatibility with regexp_parser 1.8 and will never be maintained.
32
32
  else
33
33
  attr_accessor :source
34
34