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
@@ -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
@@ -17,7 +17,8 @@ module RuboCop
17
17
  # @safety
18
18
  # This cop relies on the pattern `@instance_var ||= ...`,
19
19
  # but this is sometimes used for other purposes than memoization
20
- # so this cop is considered unsafe.
20
+ # so this cop is considered unsafe. Also, its autocorrection is unsafe
21
+ # because it may conflict with instance variable names already in use.
21
22
  #
22
23
  # @example EnforcedStyleForLeadingUnderscores: disallowed (default)
23
24
  # # bad
@@ -145,6 +146,8 @@ module RuboCop
145
146
  # @_foo ||= calculate_expensive_thing
146
147
  # end
147
148
  class MemoizedInstanceVariableName < Base
149
+ extend AutoCorrector
150
+
148
151
  include ConfigurableEnforcedStyle
149
152
 
150
153
  MSG = 'Memoized variable `%<var>s` does not match ' \
@@ -163,6 +166,7 @@ module RuboCop
163
166
  PATTERN
164
167
 
165
168
  # rubocop:disable Metrics/AbcSize
169
+ # rubocop:disable Metrics/MethodLength
166
170
  def on_or_asgn(node)
167
171
  lhs, _value = *node
168
172
  return unless lhs.ivasgn_type?
@@ -175,14 +179,18 @@ module RuboCop
175
179
 
176
180
  return if matches?(method_name, lhs)
177
181
 
182
+ suggested_var = suggested_var(method_name)
178
183
  msg = format(
179
184
  message(lhs.children.first.to_s),
180
185
  var: lhs.children.first.to_s,
181
- suggested_var: suggested_var(method_name),
186
+ suggested_var: suggested_var,
182
187
  method: method_name
183
188
  )
184
- add_offense(lhs, message: msg)
189
+ add_offense(lhs, message: msg) do |corrector|
190
+ corrector.replace(lhs.loc.name, "@#{suggested_var}")
191
+ end
185
192
  end
193
+ # rubocop:enable Metrics/MethodLength
186
194
  # rubocop:enable Metrics/AbcSize
187
195
 
188
196
  # @!method defined_memoized?(node, ivar)
@@ -205,15 +213,22 @@ module RuboCop
205
213
  defined_memoized?(method_node.body, var_name) do |defined_ivar, return_ivar, ivar_assign|
206
214
  return if matches?(method_name, ivar_assign)
207
215
 
216
+ suggested_var = suggested_var(method_name)
208
217
  msg = format(
209
218
  message(var_name.to_s),
210
219
  var: var_name.to_s,
211
- suggested_var: suggested_var(method_name),
220
+ suggested_var: suggested_var,
212
221
  method: method_name
213
222
  )
214
- add_offense(defined_ivar, message: msg)
215
- add_offense(return_ivar, message: msg)
216
- add_offense(ivar_assign.loc.name, message: msg)
223
+ add_offense(defined_ivar, message: msg) do |corrector|
224
+ corrector.replace(defined_ivar, "@#{suggested_var}")
225
+ end
226
+ add_offense(return_ivar, message: msg) do |corrector|
227
+ corrector.replace(return_ivar, "@#{suggested_var}")
228
+ end
229
+ add_offense(ivar_assign.loc.name, message: msg) do |corrector|
230
+ corrector.replace(ivar_assign.loc.name, "@#{suggested_var}")
231
+ end
217
232
  end
218
233
  end
219
234
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
@@ -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
@@ -82,9 +82,7 @@ module RuboCop
82
82
  message = message(node)
83
83
 
84
84
  add_offense(range, message: message) do |corrector|
85
- corrector.replace(range, preferred_name)
86
-
87
- correct_node(corrector, node.body, offending_name, preferred_name)
85
+ autocorrect(corrector, node, range, offending_name, preferred_name)
88
86
  end
89
87
  end
90
88
 
@@ -92,7 +90,17 @@ module RuboCop
92
90
 
93
91
  def offense_range(resbody)
94
92
  variable = resbody.exception_variable
95
- variable.loc.expression
93
+ variable.source_range
94
+ end
95
+
96
+ def autocorrect(corrector, node, range, offending_name, preferred_name)
97
+ corrector.replace(range, preferred_name)
98
+ correct_node(corrector, node.body, offending_name, preferred_name)
99
+ return unless (kwbegin_node = node.parent.each_ancestor(:kwbegin).first)
100
+
101
+ kwbegin_node.right_siblings.each do |child_node|
102
+ correct_node(corrector, child_node, offending_name, preferred_name)
103
+ end
96
104
  end
97
105
 
98
106
  def variable_name_matches?(node, name)
@@ -20,9 +20,14 @@ module RuboCop
20
20
  # # good
21
21
  # fooBar = 1
22
22
  #
23
+ # @example AllowedIdentifiers: ['fooBar']
24
+ # # good (with EnforcedStyle: snake_case)
25
+ # fooBar = 1
26
+ #
23
27
  # @example AllowedPatterns: ['_v\d+\z']
24
- # # good
28
+ # # good (with EnforcedStyle: camelCase)
25
29
  # :release_v1
30
+ #
26
31
  class VariableName < Base
27
32
  include AllowedIdentifiers
28
33
  include ConfigurableNaming
@@ -197,7 +197,9 @@ module RuboCop
197
197
  def enabled?(cop, config)
198
198
  return true if options[:only]&.include?(cop.cop_name)
199
199
 
200
- cfg = config.for_cop(cop)
200
+ # We need to use `cop_name` in this case, because `for_cop` uses caching
201
+ # which expects cop names or cop classes as keys.
202
+ cfg = config.for_cop(cop.cop_name)
201
203
 
202
204
  cop_enabled = cfg.fetch('Enabled') == true || enabled_pending_cop?(cfg, config)
203
205
 
@@ -14,12 +14,25 @@ module RuboCop
14
14
  # # bad
15
15
  # class Foo
16
16
  # attr_reader :bar
17
+ # attr_reader :bax
17
18
  # attr_reader :baz
18
19
  # end
19
20
  #
20
21
  # # good
21
22
  # class Foo
22
- # attr_reader :bar, :baz
23
+ # attr_reader :bar, :bax, :baz
24
+ # end
25
+ #
26
+ # # good
27
+ # class Foo
28
+ # # may be intended comment for bar.
29
+ # attr_reader :bar
30
+ #
31
+ # sig { returns(String) }
32
+ # attr_reader :bax
33
+ #
34
+ # may_be_intended_annotation :baz
35
+ # attr_reader :baz
23
36
  # end
24
37
  #
25
38
  # @example EnforcedStyle: separated
@@ -57,7 +70,7 @@ module RuboCop
57
70
 
58
71
  def check(send_node)
59
72
  return if previous_line_comment?(send_node) || !groupable_accessor?(send_node)
60
- return unless (grouped_style? && sibling_accessors(send_node).size > 1) ||
73
+ return unless (grouped_style? && groupable_sibling_accessors(send_node).size > 1) ||
61
74
  (separated_style? && send_node.arguments.size > 1)
62
75
 
63
76
  message = message(send_node)
@@ -70,7 +83,7 @@ module RuboCop
70
83
  if (preferred_accessors = preferred_accessors(node))
71
84
  corrector.replace(node, preferred_accessors)
72
85
  else
73
- range = range_with_surrounding_space(node.loc.expression, side: :left)
86
+ range = range_with_surrounding_space(node.source_range, side: :left)
74
87
  corrector.remove(range)
75
88
  end
76
89
  end
@@ -79,12 +92,24 @@ module RuboCop
79
92
  comment_line?(processed_source[node.first_line - 2])
80
93
  end
81
94
 
95
+ # rubocop:disable Metrics/CyclomaticComplexity
82
96
  def groupable_accessor?(node)
83
97
  return true unless (previous_expression = node.left_siblings.last)
98
+
99
+ # Accessors with Sorbet `sig { ... }` blocks shouldn't be groupable.
100
+ if previous_expression.block_type?
101
+ previous_expression.child_nodes.each do |child_node|
102
+ break previous_expression = child_node if child_node.send_type?
103
+ end
104
+ end
105
+
84
106
  return true unless previous_expression.send_type?
85
107
 
86
- previous_expression.attribute_accessor? || previous_expression.access_modifier?
108
+ previous_expression.attribute_accessor? ||
109
+ previous_expression.access_modifier? ||
110
+ node.first_line - previous_expression.last_line > 1 # there is a space between nodes
87
111
  end
112
+ # rubocop:enable Metrics/CyclomaticComplexity
88
113
 
89
114
  def class_send_elements(class_node)
90
115
  class_def = class_node.body
@@ -106,12 +131,12 @@ module RuboCop
106
131
  style == :separated
107
132
  end
108
133
 
109
- def sibling_accessors(send_node)
134
+ def groupable_sibling_accessors(send_node)
110
135
  send_node.parent.each_child_node(:send).select do |sibling|
111
136
  sibling.attribute_accessor? &&
112
137
  sibling.method?(send_node.method_name) &&
113
138
  node_visibility(sibling) == node_visibility(send_node) &&
114
- !previous_line_comment?(sibling)
139
+ groupable_accessor?(sibling) && !previous_line_comment?(sibling)
115
140
  end
116
141
  end
117
142
 
@@ -122,7 +147,7 @@ module RuboCop
122
147
 
123
148
  def preferred_accessors(node)
124
149
  if grouped_style?
125
- accessors = sibling_accessors(node)
150
+ accessors = groupable_sibling_accessors(node)
126
151
  group_accessors(node, accessors) if node.loc == accessors.first.loc
127
152
  else
128
153
  separate_accessors(node)
@@ -124,9 +124,9 @@ module RuboCop
124
124
 
125
125
  def register_offense_to_forwarding_method_arguments(forwarding_method)
126
126
  add_offense(arguments_range(forwarding_method)) do |corrector|
127
- range = range_between(
128
- forwarding_method.loc.selector.end_pos, forwarding_method.source_range.end_pos
129
- )
127
+ begin_pos = forwarding_method.loc.selector&.end_pos || forwarding_method.loc.dot.end_pos
128
+ range = range_between(begin_pos, forwarding_method.source_range.end_pos)
129
+
130
130
  corrector.replace(range, '(...)')
131
131
  end
132
132
  end
@@ -30,7 +30,7 @@ module RuboCop
30
30
  private
31
31
 
32
32
  def first_offense_range(comment)
33
- expression = comment.loc.expression
33
+ expression = comment.source_range
34
34
  first_offense = first_non_ascii_chars(comment.text)
35
35
 
36
36
  start_position = expression.begin_pos + comment.text.index(first_offense)
@@ -24,7 +24,7 @@ module RuboCop
24
24
  def on_send(node)
25
25
  return unless node.command?(:attr) && node.arguments?
26
26
  # check only for method definitions in class/module body
27
- return if node.parent && !node.parent.class_type? && !class_eval?(node.parent)
27
+ return if allowed_context?(node)
28
28
 
29
29
  message = message(node)
30
30
  add_offense(node.loc.selector, message: message) do |corrector|
@@ -34,6 +34,16 @@ module RuboCop
34
34
 
35
35
  private
36
36
 
37
+ def allowed_context?(node)
38
+ return false unless (class_node = node.each_ancestor(:class, :block).first)
39
+
40
+ (!class_node.class_type? && !class_eval?(class_node)) || define_attr_method?(class_node)
41
+ end
42
+
43
+ def define_attr_method?(node)
44
+ node.each_descendant(:def).any? { |def_node| def_node.method?(:attr) }
45
+ end
46
+
37
47
  def autocorrect(corrector, node)
38
48
  attr_name, setter = *node.arguments
39
49
 
@@ -3,8 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- #
7
- # This cop checks for BEGIN blocks.
6
+ # Checks for BEGIN blocks.
8
7
  #
9
8
  # @example
10
9
  # # bad
@@ -55,7 +55,7 @@ module RuboCop
55
55
  def after_class(class_node)
56
56
  @macros_to_rewrite[class_node].each do |macro|
57
57
  node = macro.node
58
- range = range_by_whole_lines(node.loc.expression, include_final_newline: true)
58
+ range = range_by_whole_lines(node.source_range, include_final_newline: true)
59
59
 
60
60
  correct(range) do |corrector|
61
61
  if macro.writer?
@@ -46,7 +46,7 @@ module RuboCop
46
46
  private
47
47
 
48
48
  def parts(comment)
49
- expr = comment.loc.expression
49
+ expr = comment.source_range
50
50
  eq_begin = expr.resize(BEGIN_LENGTH)
51
51
  eq_end = eq_end_part(comment, expr)
52
52
  contents = range_between(eq_begin.end_pos, eq_end.begin_pos)
@@ -299,7 +299,7 @@ module RuboCop
299
299
 
300
300
  def move_comment_before_block(corrector, comment, block_node, closing_brace)
301
301
  range = block_node.chained? ? end_of_chain(block_node.parent).source_range : closing_brace
302
- corrector.remove(range_with_surrounding_space(comment.loc.expression, side: :right))
302
+ corrector.remove(range_with_surrounding_space(comment.source_range, side: :right))
303
303
  remove_trailing_whitespace(corrector, range, comment)
304
304
  corrector.insert_after(range, "\n")
305
305
 
@@ -314,7 +314,7 @@ module RuboCop
314
314
  end
315
315
 
316
316
  def remove_trailing_whitespace(corrector, range, comment)
317
- range_of_trailing = range.end.join(comment.loc.expression.begin)
317
+ range_of_trailing = range.end.join(comment.source_range.begin)
318
318
 
319
319
  corrector.remove(range_of_trailing) if range_of_trailing.source.match?(/\A\s+\z/)
320
320
  end
@@ -342,6 +342,7 @@ module RuboCop
342
342
  end
343
343
 
344
344
  def proper_block_style?(node)
345
+ return true if require_braces?(node)
345
346
  return special_method_proper_block_style?(node) if special_method?(node.method_name)
346
347
 
347
348
  case style
@@ -352,6 +353,14 @@ module RuboCop
352
353
  end
353
354
  end
354
355
 
356
+ def require_braces?(node)
357
+ return false unless node.braces?
358
+
359
+ node.each_ancestor(:send).any? do |send|
360
+ send.arithmetic_operation? && node.source_range.end_pos < send.loc.selector.begin_pos
361
+ end
362
+ end
363
+
355
364
  def special_method?(method_name)
356
365
  allowed_method?(method_name) ||
357
366
  matches_allowed_pattern?(method_name) ||
@@ -11,12 +11,14 @@ module RuboCop
11
11
  # so if the original conditional used a different equality operator, the
12
12
  # behavior may be different.
13
13
  #
14
- # @example
14
+ # @example MinBranchesCount: 3 (default)
15
15
  # # bad
16
16
  # if status == :active
17
17
  # perform_action
18
18
  # elsif status == :inactive || status == :hibernating
19
19
  # check_timeout
20
+ # elsif status == :invalid
21
+ # report_invalid
20
22
  # else
21
23
  # final_action
22
24
  # end
@@ -27,12 +29,27 @@ module RuboCop
27
29
  # perform_action
28
30
  # when :inactive, :hibernating
29
31
  # check_timeout
32
+ # when :invalid
33
+ # report_invalid
34
+ # else
35
+ # final_action
36
+ # end
37
+ #
38
+ # @example MinBranchesCount: 4
39
+ # # good
40
+ # if status == :active
41
+ # perform_action
42
+ # elsif status == :inactive || status == :hibernating
43
+ # check_timeout
44
+ # elsif status == :invalid
45
+ # report_invalid
30
46
  # else
31
47
  # final_action
32
48
  # end
33
49
  #
34
50
  class CaseLikeIf < Base
35
51
  include RangeHelp
52
+ include MinBranchesCount
36
53
  extend AutoCorrector
37
54
 
38
55
  MSG = 'Convert `if-elsif` to `case-when`.'
@@ -78,7 +95,7 @@ module RuboCop
78
95
 
79
96
  def should_check?(node)
80
97
  !node.unless? && !node.elsif? && !node.modifier_form? && !node.ternary? &&
81
- node.elsif_conditional?
98
+ node.elsif_conditional? && min_branches_count?(node)
82
99
  end
83
100
 
84
101
  # rubocop:disable Metrics/MethodLength
@@ -239,7 +256,7 @@ module RuboCop
239
256
  end
240
257
 
241
258
  def correction_range(node)
242
- range_between(node.parent.loc.keyword.begin_pos, node.loc.expression.end_pos)
259
+ range_between(node.parent.loc.keyword.begin_pos, node.source_range.end_pos)
243
260
  end
244
261
 
245
262
  # Named captures work with `=~` (if regexp is on lhs) and with `match` (both sides)
@@ -11,7 +11,7 @@ module RuboCop
11
11
  #
12
12
  # Moving from compact to nested children requires knowledge of whether the
13
13
  # outer parent is a module or a class. Moving from nested to compact requires
14
- # verification that the outer parent is defined elsewhere. Rubocop does not
14
+ # verification that the outer parent is defined elsewhere. RuboCop does not
15
15
  # have the knowledge to perform either operation safely and thus requires
16
16
  # manual oversight.
17
17
  #
@@ -139,7 +139,7 @@ module RuboCop
139
139
  end
140
140
 
141
141
  def check_style(node, body)
142
- return if node.identifier.children[0]&.cbase_type?
142
+ return if node.identifier.namespace&.cbase_type?
143
143
 
144
144
  if style == :nested
145
145
  check_nested_style(node)