rubocop 1.45.1 → 1.48.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +28 -9
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +7 -0
  5. data/lib/rubocop/comment_config.rb +19 -0
  6. data/lib/rubocop/config.rb +2 -2
  7. data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
  8. data/lib/rubocop/cop/base.rb +1 -1
  9. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  10. data/lib/rubocop/cop/corrector.rb +1 -1
  11. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  12. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +3 -3
  13. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +3 -3
  14. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  15. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
  16. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -1
  17. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  18. data/lib/rubocop/cop/gemspec/dependency_version.rb +1 -1
  19. data/lib/rubocop/cop/internal_affairs/cop_description.rb +4 -4
  20. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +1 -1
  21. data/lib/rubocop/cop/internal_affairs/location_expression.rb +37 -0
  22. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  23. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  24. data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +42 -0
  25. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  26. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  27. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +39 -0
  28. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  29. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -21
  30. data/lib/rubocop/cop/layout/class_structure.rb +5 -3
  31. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -1
  32. data/lib/rubocop/cop/layout/empty_comment.rb +3 -3
  33. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  34. data/lib/rubocop/cop/layout/end_alignment.rb +4 -0
  35. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  36. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +8 -2
  37. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  38. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  39. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -3
  40. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +11 -7
  41. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
  42. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
  43. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  44. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  45. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  46. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  47. data/lib/rubocop/cop/lint/debugger.rb +3 -0
  48. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  49. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  50. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  51. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -3
  52. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  53. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  54. data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
  55. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  56. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  57. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +46 -4
  58. data/lib/rubocop/cop/lint/missing_super.rb +31 -2
  59. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -9
  60. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -0
  61. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +6 -10
  62. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  63. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  64. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +10 -4
  65. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
  66. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  67. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  68. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  69. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  70. data/lib/rubocop/cop/lint/refinement_import_methods.rb +2 -1
  71. data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
  72. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  73. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  74. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  75. data/lib/rubocop/cop/lint/syntax.rb +4 -0
  76. data/lib/rubocop/cop/lint/to_enum_arguments.rb +6 -2
  77. data/lib/rubocop/cop/lint/useless_access_modifier.rb +10 -10
  78. data/lib/rubocop/cop/lint/useless_rescue.rb +6 -2
  79. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  80. data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
  81. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  82. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  83. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  84. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  85. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  86. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  87. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
  88. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +10 -5
  89. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  90. data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
  91. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +0 -3
  92. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  93. data/lib/rubocop/cop/mixin/range_help.rb +1 -6
  94. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  95. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  96. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  97. data/lib/rubocop/cop/naming/method_name.rb +3 -3
  98. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  99. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  100. data/lib/rubocop/cop/registry.rb +3 -1
  101. data/lib/rubocop/cop/style/accessor_grouping.rb +39 -17
  102. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  103. data/lib/rubocop/cop/style/array_intersect.rb +1 -1
  104. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  105. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  106. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  107. data/lib/rubocop/cop/style/block_delimiters.rb +11 -2
  108. data/lib/rubocop/cop/style/case_like_if.rb +20 -3
  109. data/lib/rubocop/cop/style/collection_compact.rb +1 -1
  110. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  111. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  112. data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
  113. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -6
  114. data/lib/rubocop/cop/style/dir_empty.rb +60 -0
  115. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  116. data/lib/rubocop/cop/style/documentation.rb +10 -4
  117. data/lib/rubocop/cop/style/documentation_method.rb +4 -4
  118. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  119. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  120. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  121. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  122. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  123. data/lib/rubocop/cop/style/file_empty.rb +71 -0
  124. data/lib/rubocop/cop/style/file_read.rb +1 -1
  125. data/lib/rubocop/cop/style/file_write.rb +1 -1
  126. data/lib/rubocop/cop/style/guard_clause.rb +1 -1
  127. data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
  128. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  129. data/lib/rubocop/cop/style/if_unless_modifier.rb +76 -9
  130. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
  131. data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
  132. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
  133. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
  134. data/lib/rubocop/cop/style/min_max.rb +3 -3
  135. data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
  136. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  137. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  138. data/lib/rubocop/cop/style/negated_if_else_condition.rb +12 -7
  139. data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
  140. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  141. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +2 -2
  142. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  143. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  144. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +5 -6
  145. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +10 -3
  146. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  147. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  148. data/lib/rubocop/cop/style/require_order.rb +1 -3
  149. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  150. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  151. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
  152. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  153. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  154. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  155. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  156. data/lib/rubocop/cop/style/unpack_first.rb +3 -3
  157. data/lib/rubocop/cop/style/word_array.rb +17 -5
  158. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  159. data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
  160. data/lib/rubocop/cop/team.rb +11 -8
  161. data/lib/rubocop/cop/util.rb +13 -4
  162. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  163. data/lib/rubocop/directive_comment.rb +3 -3
  164. data/lib/rubocop/ext/comment.rb +18 -0
  165. data/lib/rubocop/formatter/junit_formatter.rb +4 -1
  166. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  167. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  168. data/lib/rubocop/rspec/support.rb +1 -0
  169. data/lib/rubocop/server/core.rb +1 -1
  170. data/lib/rubocop/target_ruby.rb +1 -1
  171. data/lib/rubocop/version.rb +1 -1
  172. data/lib/rubocop.rb +5 -0
  173. metadata +17 -9
@@ -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)
@@ -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
 
@@ -7,19 +7,32 @@ module RuboCop
7
7
  # By default it enforces accessors to be placed in grouped declarations,
8
8
  # but it can be configured to enforce separating them in multiple declarations.
9
9
  #
10
- # NOTE: `Sorbet` is not compatible with "grouped" style. Consider "separated" style
11
- # or disabling this cop.
10
+ # NOTE: If there is a method call before the accessor method it is always allowed
11
+ # as it might be intended like Sorbet.
12
12
  #
13
13
  # @example EnforcedStyle: grouped (default)
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
@@ -43,11 +56,9 @@ module RuboCop
43
56
  GROUPED_MSG = 'Group together all `%<accessor>s` attributes.'
44
57
  SEPARATED_MSG = 'Use one attribute per `%<accessor>s`.'
45
58
 
46
- ACCESSOR_METHODS = %i[attr_reader attr_writer attr_accessor attr].freeze
47
-
48
59
  def on_class(node)
49
60
  class_send_elements(node).each do |macro|
50
- next unless accessor?(macro)
61
+ next unless macro.attribute_accessor?
51
62
 
52
63
  check(macro)
53
64
  end
@@ -58,8 +69,8 @@ module RuboCop
58
69
  private
59
70
 
60
71
  def check(send_node)
61
- return if previous_line_comment?(send_node)
62
- return unless (grouped_style? && sibling_accessors(send_node).size > 1) ||
72
+ return if previous_line_comment?(send_node) || !groupable_accessor?(send_node)
73
+ return unless (grouped_style? && groupable_sibling_accessors(send_node).size > 1) ||
63
74
  (separated_style? && send_node.arguments.size > 1)
64
75
 
65
76
  message = message(send_node)
@@ -72,7 +83,7 @@ module RuboCop
72
83
  if (preferred_accessors = preferred_accessors(node))
73
84
  corrector.replace(node, preferred_accessors)
74
85
  else
75
- range = range_with_surrounding_space(node.loc.expression, side: :left)
86
+ range = range_with_surrounding_space(node.source_range, side: :left)
76
87
  corrector.remove(range)
77
88
  end
78
89
  end
@@ -81,6 +92,21 @@ module RuboCop
81
92
  comment_line?(processed_source[node.first_line - 2])
82
93
  end
83
94
 
95
+ def groupable_accessor?(node)
96
+ return true unless (previous_expression = node.left_siblings.last)
97
+
98
+ # Accessors with Sorbet `sig { ... }` blocks shouldn't be groupable.
99
+ if previous_expression.block_type?
100
+ previous_expression.child_nodes.each do |child_node|
101
+ break previous_expression = child_node if child_node.send_type?
102
+ end
103
+ end
104
+
105
+ return true unless previous_expression.send_type?
106
+
107
+ previous_expression.attribute_accessor? || previous_expression.access_modifier?
108
+ end
109
+
84
110
  def class_send_elements(class_node)
85
111
  class_def = class_node.body
86
112
 
@@ -93,10 +119,6 @@ module RuboCop
93
119
  end
94
120
  end
95
121
 
96
- def accessor?(send_node)
97
- send_node.macro? && ACCESSOR_METHODS.include?(send_node.method_name)
98
- end
99
-
100
122
  def grouped_style?
101
123
  style == :grouped
102
124
  end
@@ -105,12 +127,12 @@ module RuboCop
105
127
  style == :separated
106
128
  end
107
129
 
108
- def sibling_accessors(send_node)
130
+ def groupable_sibling_accessors(send_node)
109
131
  send_node.parent.each_child_node(:send).select do |sibling|
110
- accessor?(sibling) &&
132
+ sibling.attribute_accessor? &&
111
133
  sibling.method?(send_node.method_name) &&
112
134
  node_visibility(sibling) == node_visibility(send_node) &&
113
- !previous_line_comment?(sibling)
135
+ groupable_accessor?(sibling) && !previous_line_comment?(sibling)
114
136
  end
115
137
  end
116
138
 
@@ -121,7 +143,7 @@ module RuboCop
121
143
 
122
144
  def preferred_accessors(node)
123
145
  if grouped_style?
124
- accessors = sibling_accessors(node)
146
+ accessors = groupable_sibling_accessors(node)
125
147
  group_accessors(node, accessors) if node.loc == accessors.first.loc
126
148
  else
127
149
  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
@@ -12,7 +12,7 @@ module RuboCop
12
12
  # `(array1 & array2).any?` and is more readable.
13
13
  #
14
14
  # @safety
15
- # This cop cannot guarantee that array1 and array2 are
15
+ # This cop cannot guarantee that `array1` and `array2` are
16
16
  # actually arrays while method `intersect?` is for arrays only.
17
17
  #
18
18
  # @example
@@ -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)
@@ -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)
@@ -112,7 +112,7 @@ module RuboCop
112
112
  end
113
113
 
114
114
  def range(begin_pos_node, end_pos_node)
115
- range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.loc.expression.end_pos)
115
+ range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.source_range.end_pos)
116
116
  end
117
117
  end
118
118
  end
@@ -104,7 +104,7 @@ module RuboCop
104
104
  end
105
105
 
106
106
  def inline_comment?(comment)
107
- !comment_line?(comment.loc.expression.source_line)
107
+ !comment_line?(comment.source_range.source_line)
108
108
  end
109
109
 
110
110
  def annotation_range(annotation)
@@ -66,7 +66,7 @@ module RuboCop
66
66
 
67
67
  def register_offense(comment, matched_keyword)
68
68
  add_offense(comment, message: format(MSG, keyword: matched_keyword)) do |corrector|
69
- range = range_with_surrounding_space(comment.loc.expression, newlines: false)
69
+ range = range_with_surrounding_space(comment.source_range, newlines: false)
70
70
  corrector.remove(range)
71
71
 
72
72
  unless matched_keyword == 'end'
@@ -84,7 +84,7 @@ module RuboCop
84
84
  end
85
85
 
86
86
  def source_line(comment)
87
- comment.location.expression.source_line
87
+ comment.source_range.source_line
88
88
  end
89
89
  end
90
90
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  offense = offense_range(node)
39
39
  current = offense.source
40
40
 
41
- if node.arguments.any?(&:percent_literal?)
41
+ if (use_percent_literal = node.arguments.any?(&:percent_literal?))
42
42
  if percent_literals_includes_only_basic_literals?(node)
43
43
  prefer = preferred_method(node)
44
44
  message = format(MSG, prefer: prefer, current: current)
@@ -51,7 +51,15 @@ module RuboCop
51
51
  end
52
52
 
53
53
  add_offense(offense, message: message) do |corrector|
54
- corrector.replace(offense, prefer)
54
+ if use_percent_literal
55
+ corrector.replace(offense, prefer)
56
+ else
57
+ corrector.replace(node.loc.selector, 'push')
58
+ node.arguments.each do |argument|
59
+ corrector.remove(argument.loc.begin)
60
+ corrector.remove(argument.loc.end)
61
+ end
62
+ end
55
63
  end
56
64
  end
57
65
  # rubocop:enable Metrics
@@ -450,7 +450,7 @@ module RuboCop
450
450
  end
451
451
 
452
452
  def white_space_range(node, column)
453
- expression = node.loc.expression
453
+ expression = node.source_range
454
454
  begin_pos = expression.begin_pos - (expression.column - column - 2)
455
455
 
456
456
  Parser::Source::Range.new(expression.source_buffer, begin_pos, expression.begin_pos)
@@ -458,9 +458,9 @@ module RuboCop
458
458
 
459
459
  def assignment(node)
460
460
  *_, condition = *node
461
- Parser::Source::Range.new(node.loc.expression.source_buffer,
462
- node.loc.expression.begin_pos,
463
- condition.loc.expression.begin_pos)
461
+ Parser::Source::Range.new(node.source_range.source_buffer,
462
+ node.source_range.begin_pos,
463
+ condition.source_range.begin_pos)
464
464
  end
465
465
 
466
466
  def correct_if_branches(corrector, cop, node)
@@ -565,7 +565,7 @@ module RuboCop
565
565
  end
566
566
 
567
567
  def move_assignment_inside_condition(corrector, node)
568
- column = node.loc.expression.column
568
+ column = node.source_range.column
569
569
  *_var, condition = *node
570
570
  assignment = assignment(node)
571
571
 
@@ -616,7 +616,7 @@ module RuboCop
616
616
  end
617
617
 
618
618
  def move_assignment_inside_condition(corrector, node)
619
- column = node.loc.expression.column
619
+ column = node.source_range.column
620
620
  *_var, condition = *node
621
621
  assignment = assignment(node)
622
622
 
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Prefer to use `Dir.empty?('path/to/dir')` when checking if a directory is empty.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # Dir.entries('path/to/dir').size == 2
11
+ # Dir.children('path/to/dir').empty?
12
+ # Dir.children('path/to/dir').size == 0
13
+ # Dir.each_child('path/to/dir').none?
14
+ #
15
+ # # good
16
+ # Dir.empty?('path/to/dir')
17
+ #
18
+ class DirEmpty < Base
19
+ extend AutoCorrector
20
+ extend TargetRubyVersion
21
+
22
+ MSG = 'Use `Dir.empty?(%<arg>s)` instead.'
23
+ RESTRICT_ON_SEND = %i[== > empty? none?].freeze
24
+
25
+ minimum_target_ruby_version 2.4
26
+
27
+ # @!method offensive?(node)
28
+ def_node_matcher :offensive?, <<~PATTERN
29
+ {
30
+ (send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) {:== :>} (int 2))
31
+ (send (send (send $(const {nil? cbase} :Dir) :children $_) :size) {:== :>} (int 0))
32
+ (send (send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) :!) {:== :>} (int 2))
33
+ (send (send (send (send $(const {nil? cbase} :Dir) :children $_) :size) :!) {:== :>} (int 0))
34
+ (send (send $(const {nil? cbase} :Dir) :children $_) :empty?)
35
+ (send (send $(const {nil? cbase} :Dir) :each_child $_) :none?)
36
+ }
37
+ PATTERN
38
+
39
+ def on_send(node)
40
+ offensive?(node) do |const_node, arg_node|
41
+ add_offense(node, message: format(MSG, arg: arg_node.source)) do |corrector|
42
+ bang(node)
43
+ corrector.replace(node,
44
+ "#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})")
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def bang(node)
52
+ if (node.method?(:==) && node.child_nodes.first.method?(:!)) ||
53
+ (node.method?(:>) && !node.child_nodes.first.method?(:!))
54
+ '!'
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -116,7 +116,7 @@ module RuboCop
116
116
 
117
117
  def preceding_comment_blocks(node)
118
118
  # Collect comments in the method call, but outside the heredoc
119
- comments = processed_source.each_comment_in_lines(node.loc.expression.line_span)
119
+ comments = processed_source.each_comment_in_lines(node.source_range.line_span)
120
120
 
121
121
  comments.each_with_object({}) do |comment, hash|
122
122
  merge_adjacent_comments(comment.text, comment.loc.line, hash)
@@ -110,7 +110,7 @@ module RuboCop
110
110
  return if nodoc_self_or_outer_module?(node)
111
111
  return if include_statement_only?(body)
112
112
 
113
- range = range_between(node.loc.expression.begin_pos, node.loc.name.end_pos)
113
+ range = range_between(node.source_range.begin_pos, node.loc.name.end_pos)
114
114
  message = format(MSG, type: node.type, identifier: identifier(node))
115
115
  add_offense(range, message: message)
116
116
  end
@@ -178,13 +178,19 @@ module RuboCop
178
178
  def identifier(node)
179
179
  # Get the fully qualified identifier for a class/module
180
180
  nodes = [node, *node.each_ancestor(:class, :module)]
181
- nodes.reverse_each.flat_map { |n| qualify_const(n.identifier) }.join('::')
181
+ identifier = nodes.reverse_each.flat_map { |n| qualify_const(n.identifier) }.join('::')
182
+
183
+ identifier.sub('::::', '::')
182
184
  end
183
185
 
184
186
  def qualify_const(node)
185
- return if node.nil? || node.cbase_type? || node.self_type? || node.send_type?
187
+ return if node.nil?
186
188
 
187
- [qualify_const(node.namespace), node.short_name].compact
189
+ if node.cbase_type? || node.self_type? || node.call_type? || node.variable?
190
+ node.source
191
+ else
192
+ [qualify_const(node.namespace), node.short_name].compact
193
+ end
188
194
  end
189
195
  end
190
196
  end
@@ -101,16 +101,16 @@ module RuboCop
101
101
 
102
102
  MSG = 'Missing method documentation comment.'
103
103
 
104
- # @!method module_function_node?(node)
105
- def_node_matcher :module_function_node?, <<~PATTERN
106
- (send nil? :module_function ...)
104
+ # @!method modifier_node?(node)
105
+ def_node_matcher :modifier_node?, <<~PATTERN
106
+ (send nil? {:module_function :ruby2_keywords} ...)
107
107
  PATTERN
108
108
 
109
109
  def on_def(node)
110
110
  return if node.method?(:initialize)
111
111
 
112
112
  parent = node.parent
113
- module_function_node?(parent) ? check(parent) : check(node)
113
+ modifier_node?(parent) ? check(parent) : check(node)
114
114
  end
115
115
  alias on_defs on_def
116
116
 
@@ -131,7 +131,7 @@ module RuboCop
131
131
  end
132
132
 
133
133
  def whole_line_expression(node)
134
- range_by_whole_lines(node.loc.expression, include_final_newline: true)
134
+ range_by_whole_lines(node.source_range, include_final_newline: true)
135
135
  end
136
136
  end
137
137
  end
@@ -37,7 +37,7 @@ module RuboCop
37
37
 
38
38
  def autocorrect(corrector, node)
39
39
  block = node.parent
40
- range = range_between(block.loc.begin.end_pos, node.loc.expression.end_pos)
40
+ range = range_between(block.loc.begin.end_pos, node.source_range.end_pos)
41
41
 
42
42
  corrector.remove(range)
43
43
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
 
35
35
  def autocorrect(corrector, node)
36
36
  send_node = node.parent.send_node
37
- range = range_between(send_node.loc.expression.end_pos, node.loc.expression.end_pos)
37
+ range = range_between(send_node.source_range.end_pos, node.source_range.end_pos)
38
38
 
39
39
  corrector.remove(range)
40
40
  end
@@ -146,7 +146,7 @@ module RuboCop
146
146
  actual: line_node.source,
147
147
  expected: expected)
148
148
 
149
- add_offense(line_node.loc.expression, message: message) do |corrector|
149
+ add_offense(line_node.source_range, message: message) do |corrector|
150
150
  corrector.replace(line_node, expected)
151
151
  end
152
152
  end
@@ -175,14 +175,14 @@ module RuboCop
175
175
  end
176
176
 
177
177
  def line_difference(line_node, code)
178
- string_first_line(code) - line_node.loc.expression.first_line
178
+ string_first_line(code) - line_node.source_range.first_line
179
179
  end
180
180
 
181
181
  def string_first_line(str_node)
182
182
  if str_node.heredoc?
183
183
  str_node.loc.heredoc_body.first_line
184
184
  else
185
- str_node.loc.expression.first_line
185
+ str_node.source_range.first_line
186
186
  end
187
187
  end
188
188
 
@@ -210,7 +210,7 @@ module RuboCop
210
210
  def add_offense_for_missing_line(node, code)
211
211
  register_offense(node) do |corrector|
212
212
  line_str = missing_line(node, code)
213
- corrector.insert_after(node.loc.expression.end, ", #{line_str}")
213
+ corrector.insert_after(node.source_range.end, ", #{line_str}")
214
214
  end
215
215
  end
216
216
 
@@ -145,7 +145,7 @@ module RuboCop
145
145
  end
146
146
 
147
147
  def block_body_range(block_node, send_node)
148
- range_between(send_node.loc.expression.end_pos, block_node.loc.end.end_pos)
148
+ range_between(send_node.source_range.end_pos, block_node.loc.end.end_pos)
149
149
  end
150
150
  end
151
151
  end