rubocop 1.92.1 → 1.92.3

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 (160) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/#directive_comment.rb# +224 -0
  3. data/lib/rubocop/cache_config.rb +3 -0
  4. data/lib/rubocop/cli.rb +5 -4
  5. data/lib/rubocop/config.rb +16 -4
  6. data/lib/rubocop/config_obsoletion.rb +9 -0
  7. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  8. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  10. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  11. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  12. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  13. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  14. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  15. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  16. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  17. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +15 -7
  18. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  19. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +6 -12
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  23. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  25. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  26. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  27. data/lib/rubocop/cop/layout/line_length.rb +23 -5
  28. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  29. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  30. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  31. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  32. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  33. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  34. data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
  35. data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
  36. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  37. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
  39. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  40. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  41. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  42. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  43. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  44. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  45. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  46. data/lib/rubocop/cop/lint/interpolation_check.rb +27 -18
  47. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  48. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  49. data/lib/rubocop/cop/lint/nested_percent_literal.rb +3 -0
  50. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  51. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  52. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +66 -3
  53. data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
  54. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
  55. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  56. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  57. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  58. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +11 -4
  59. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  60. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  61. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  62. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  63. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  64. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  65. data/lib/rubocop/cop/lint/void.rb +20 -9
  66. data/lib/rubocop/cop/message_annotator.rb +6 -8
  67. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  68. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  69. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  70. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  71. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +17 -1
  72. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  73. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  74. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  75. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  76. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  77. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  78. data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -1
  79. data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
  80. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  81. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  82. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  83. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  84. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  85. data/lib/rubocop/cop/style/and_or.rb +41 -11
  86. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  87. data/lib/rubocop/cop/style/case_equality.rb +14 -3
  88. data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
  89. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  90. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  91. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  92. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  93. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  94. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  95. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  96. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  97. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  98. data/lib/rubocop/cop/style/endless_method.rb +64 -16
  99. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  100. data/lib/rubocop/cop/style/float_division.rb +16 -1
  101. data/lib/rubocop/cop/style/for.rb +59 -4
  102. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  103. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  104. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  105. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  106. data/lib/rubocop/cop/style/hash_lookup_method.rb +21 -2
  107. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  108. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  109. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  110. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  111. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  112. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  113. data/lib/rubocop/cop/style/lambda.rb +13 -5
  114. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  115. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  116. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  117. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  118. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +12 -1
  119. data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  121. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  122. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  123. data/lib/rubocop/cop/style/numeric_predicate.rb +7 -0
  124. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  125. data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -10
  126. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  127. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  128. data/lib/rubocop/cop/style/quoted_symbols.rb +5 -7
  129. data/lib/rubocop/cop/style/raise_args.rb +66 -7
  130. data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
  131. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  132. data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
  133. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  134. data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
  135. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  136. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  137. data/lib/rubocop/cop/style/redundant_self.rb +9 -1
  138. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  139. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  140. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  141. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  142. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  143. data/lib/rubocop/cop/style/single_line_methods.rb +26 -2
  144. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  145. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  146. data/lib/rubocop/cop/style/string_concatenation.rb +28 -8
  147. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  148. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  149. data/lib/rubocop/cop/style/word_array.rb +1 -3
  150. data/lib/rubocop/cop/util.rb +2 -0
  151. data/lib/rubocop/cop/variable_force.rb +14 -1
  152. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  153. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  154. data/lib/rubocop/plugin/configuration_integrator.rb +24 -3
  155. data/lib/rubocop/result_cache.rb +9 -3
  156. data/lib/rubocop/runner.rb +6 -1
  157. data/lib/rubocop/server/cache.rb +24 -5
  158. data/lib/rubocop/todo_audit.rb +61 -15
  159. data/lib/rubocop/version.rb +1 -1
  160. metadata +4 -3
@@ -55,10 +55,17 @@ module RuboCop
55
55
  # Common functionality for checking alignment of hash values.
56
56
  module ValueAlignment
57
57
  def checkable_layout?(node)
58
- !node.pairs_on_same_line? && !node.mixed_delimiters? && alignable_values?(node)
58
+ !node.pairs_on_same_line? && !node.mixed_delimiters? && alignable_values?(node) &&
59
+ !pair_after_kwsplat_on_same_line?(node)
59
60
  end
60
61
 
62
+ # With several enforced styles, deltas are computed even for a hash this style
63
+ # can't check. A pair that doesn't begin its line can't be moved without
64
+ # deleting whatever precedes it, so it's left alone.
61
65
  def deltas(first_pair, current_pair)
66
+ return {} unless current_pair.equal?(first_pair) ||
67
+ Util.begins_its_line?(current_pair.source_range)
68
+
62
69
  key_delta = key_delta(first_pair, current_pair)
63
70
  shift = separator_shift(current_pair, key_delta)
64
71
  separator_delta = separator_delta(first_pair, current_pair, shift)
@@ -79,6 +86,15 @@ module RuboCop
79
86
  pair.value && pair.key.last_line != pair.value.first_line
80
87
  end
81
88
 
89
+ def pair_after_kwsplat_on_same_line?(node)
90
+ first_pair = node.pairs.first
91
+
92
+ node.children.each_cons(2).any? do |previous, current|
93
+ current.pair_type? && !current.equal?(first_pair) &&
94
+ previous.last_line == current.first_line
95
+ end
96
+ end
97
+
82
98
  def separator_shift(current_pair, key_delta)
83
99
  current_pair.key.multiline? ? 0 : key_delta
84
100
  end
@@ -77,6 +77,7 @@ module RuboCop
77
77
  add_offense(node.value, message: message) do |corrector|
78
78
  corrector.replace(node, replacement)
79
79
 
80
+ next if node.value_omission?
80
81
  next unless (def_node = def_node_that_require_parentheses(node))
81
82
 
82
83
  last_argument = def_node.last_argument
@@ -16,6 +16,7 @@ module RuboCop
16
16
  # to adjust the brace layout because this will result in invalid code.
17
17
  return if last_line_heredoc?(node.children.last)
18
18
  return if heredoc_opened_on_closing_brace_line?(node)
19
+ return if heredoc_method_call_to_be_repositioned?(node.children.last)
19
20
 
20
21
  check(node)
21
22
  end
@@ -145,6 +146,17 @@ module RuboCop
145
146
  str.heredoc? && str.first_line == closing_brace_line
146
147
  end
147
148
  end
149
+
150
+ def heredoc_method_call_to_be_repositioned?(node)
151
+ return false unless config.cop_enabled?('Lint/HeredocMethodCallPosition')
152
+
153
+ node.each_node(:call).any? do |call_node|
154
+ receiver = call_node.receiver
155
+ next false unless receiver&.any_str_type? && receiver.heredoc?
156
+
157
+ receiver.loc.heredoc_end.end_pos < call_node.source_range.end_pos
158
+ end
159
+ end
148
160
  end
149
161
  end
150
162
  end
@@ -77,6 +77,27 @@ module RuboCop
77
77
  end
78
78
  end
79
79
 
80
+ # Builds a double-quoted string literal for an interpolated element of a `%W` or `%I`
81
+ # array. Only the text around the interpolations is escaped, the code inside them is
82
+ # kept as written.
83
+ # @param [RuboCop::AST::Node] node a `dstr` or `dsym` node
84
+ # @return [String]
85
+ def build_interpolated_string_literal(node)
86
+ contents = node.children.map do |child|
87
+ if child.str_type?
88
+ child.value.inspect[1..-2]
89
+ elsif child.begin_type?
90
+ child.source
91
+ else
92
+ # A variable interpolated without braces, such as `#@foo`. The braces keep
93
+ # the text that follows from becoming part of the variable name.
94
+ "\#{#{child.source}}"
95
+ end
96
+ end
97
+
98
+ "\"#{contents.join}\""
99
+ end
100
+
80
101
  # @param [RuboCop::AST::ArrayNode] node
81
102
  # @param [Array<String>] elements
82
103
  # @return [String]
@@ -97,7 +118,7 @@ module RuboCop
97
118
  # @return [String]
98
119
  def whitespace_between(node)
99
120
  if node.children.length >= 2
100
- node.children[0].source_range.end.join(node.children[1].source_range.begin).source
121
+ node.children[0].source_range.end.join(element_begin(node.children[1])).source
101
122
  else
102
123
  ' '
103
124
  end
@@ -109,7 +130,19 @@ module RuboCop
109
130
  # @param [RuboCop::AST::ArrayNode] node
110
131
  # @return [String]
111
132
  def whitespace_leading(node)
112
- node.loc.begin.end.join(node.children[0].source_range.begin).source
133
+ node.loc.begin.end.join(element_begin(node.children[0])).source
134
+ end
135
+
136
+ # The location of an element starting with a variable interpolated without
137
+ # braces, such as `#@foo`, does not include the `#`.
138
+ # @param [RuboCop::AST::Node] element
139
+ # @return [Parser::Source::Range]
140
+ def element_begin(element)
141
+ range = element.source_range.begin
142
+ return range unless element.type?(:dstr, :dsym)
143
+ return range if element.children.first.type?(:str, :begin)
144
+
145
+ range.adjust(begin_pos: -1, end_pos: -1)
113
146
  end
114
147
 
115
148
  # Provides trailing whitespace for building a bracketed array.
@@ -19,7 +19,7 @@ module RuboCop
19
19
 
20
20
  class << self
21
21
  # The signature is a property of the index, not of the cop, and computing
22
- # it stats every indexed file, so all cops sharing an index share one
22
+ # it reads every indexed file, so all cops sharing an index share one
23
23
  # computation. A single-entry cache (instead of a hash keyed by index)
24
24
  # avoids retaining stale graphs in long-lived processes.
25
25
  attr_accessor :cached_index_signature
@@ -188,14 +188,13 @@ module RuboCop
188
188
  next if uri == BUILTIN_DOCUMENT_URI
189
189
 
190
190
  path = uri.delete_prefix(FILE_URI_PREFIX).sub(WINDOWS_DRIVE_PREFIX, '')
191
- mtime, size = begin
192
- stat = File.stat(path)
193
- [stat.mtime.to_f, stat.size]
191
+ digest = begin
192
+ Digest::SHA1.file(path).hexdigest
194
193
  rescue StandardError
195
- [0, 0]
194
+ ''
196
195
  end
197
196
 
198
- "#{path}:#{mtime}:#{size}"
197
+ "#{path}:#{digest}"
199
198
  end.sort
200
199
  end
201
200
  end
@@ -90,6 +90,19 @@ module RuboCop
90
90
  range.adjust(begin_pos: -range.column, end_pos: end_offset).intersect(buffer.source_range)
91
91
  end
92
92
 
93
+ # Returns the range to remove a statement: its whole lines, including a trailing comment
94
+ # and the final newline, when no other code shares them, and otherwise the range itself
95
+ # with the spaces after it.
96
+ def range_for_statement_removal(range)
97
+ rest_of_line = range.source_buffer.source_line(range.last_line)[range.last_column..]
98
+
99
+ if begins_its_line?(range) && rest_of_line.match?(/\A\s*(#|\z)/)
100
+ range_by_whole_lines(range, include_final_newline: true)
101
+ else
102
+ range_with_surrounding_space(range, side: :right, newlines: false)
103
+ end
104
+ end
105
+
93
106
  def column_offset_between(base_range, range)
94
107
  effective_column(base_range) - effective_column(range)
95
108
  end
@@ -8,6 +8,7 @@ module RuboCop
8
8
  include RangeHelp
9
9
 
10
10
  MSG = 'Space found before %<token>s.'
11
+ SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
11
12
 
12
13
  def on_new_investigation
13
14
  each_missing_space(processed_source.sorted_tokens) do |token, pos_before|
@@ -36,7 +37,10 @@ module RuboCop
36
37
  end
37
38
 
38
39
  def space_required_after?(token)
39
- token.left_curly_brace? && space_required_after_lcurly?
40
+ return space_required_after_lcurly? if token.left_curly_brace?
41
+ return space_required_after_lparen? if token.left_parens?
42
+
43
+ false
40
44
  end
41
45
 
42
46
  def space_required_after_lcurly?
@@ -44,6 +48,12 @@ module RuboCop
44
48
  style = cfg['EnforcedStyle'] || 'space'
45
49
  style == 'space'
46
50
  end
51
+
52
+ def space_required_after_lparen?
53
+ style = config.for_enabled_cop('Layout/SpaceInsideParens')['EnforcedStyle']
54
+
55
+ SPACE_INSIDE_PARENS_STYLES.include?(style)
56
+ end
47
57
  end
48
58
  end
49
59
  end
@@ -7,6 +7,8 @@ module RuboCop
7
7
  include LineLengthHelp
8
8
  include AllowedPattern
9
9
 
10
+ SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
11
+
10
12
  private
11
13
 
12
14
  def single_line_as_modifier?(node)
@@ -42,7 +44,10 @@ module RuboCop
42
44
  def modifier_fits_on_single_line?(node)
43
45
  return true unless max_line_length
44
46
 
45
- line_length(line_in_modifier_form(node)) <= max_line_length
47
+ length = line_length(line_in_modifier_form(node))
48
+ length += 2 if parenthesize?(node) && space_inside_parens?
49
+
50
+ length <= max_line_length
46
51
  end
47
52
 
48
53
  def line_in_modifier_form(node)
@@ -52,6 +57,14 @@ module RuboCop
52
57
  "#{code_before}#{to_modifier_form(node)}#{code_after(node)}"
53
58
  end
54
59
 
60
+ def space_inside_parens?
61
+ return false unless config.cop_enabled?('Layout/SpaceInsideParens')
62
+
63
+ style = config.for_cop('Layout/SpaceInsideParens')['EnforcedStyle']
64
+
65
+ SPACE_INSIDE_PARENS_STYLES.include?(style)
66
+ end
67
+
55
68
  # Whether an existing line is acceptable to `Layout/LineLength`,
56
69
  # exemptions (the cop disabled at that line, an allowed pattern, an
57
70
  # allowed cop directive, an allowed URI) included. Only for judging
@@ -163,13 +163,15 @@ module RuboCop
163
163
 
164
164
  def put_comma(items, kind)
165
165
  last_item = items.last
166
- return if last_item.block_pass_type?
166
+ return if last_item.type?(:block_pass, :forwarded_args)
167
167
 
168
168
  range = autocorrect_range(last_item)
169
169
  msg = format(MSG, command: 'Put a', unit: format(kind, article: 'a multiline'))
170
170
 
171
+ # The comma is anchored to the whole item rather than to the offense range on its
172
+ # last line, so that parentheses other cops wrap around a part of the item precede it.
171
173
  add_offense(range, message: msg) do |corrector|
172
- PunctuationCorrector.swap_comma(corrector, range)
174
+ corrector.insert_after(last_item, ',')
173
175
  end
174
176
  end
175
177
 
@@ -133,7 +133,7 @@ module RuboCop
133
133
 
134
134
  arguments = block_argument.parent
135
135
 
136
- add_parentheses(arguments, corrector) unless arguments.parenthesized_call?
136
+ add_parentheses(arguments, corrector) if require_parentheses?(arguments)
137
137
  else
138
138
  unless use_block_argument_as_local_variable?(node, block_forwarding_name)
139
139
  corrector.replace(block_argument, "&#{block_forwarding_name}")
@@ -142,6 +142,14 @@ module RuboCop
142
142
  end
143
143
  end
144
144
 
145
+ def require_parentheses?(node)
146
+ !node.parenthesized_call? && !index_call?(node)
147
+ end
148
+
149
+ def index_call?(node)
150
+ node.send_type? && (node.method?(:[]) || node.method?(:[]=)) && !node.loc.dot
151
+ end
152
+
145
153
  def use_block_argument_as_local_variable?(node, last_argument)
146
154
  return false if node.body.nil?
147
155
 
@@ -41,7 +41,7 @@ module RuboCop
41
41
  expr = node.source_range
42
42
 
43
43
  corrector.replace(expr, correct_delimiters(expr.source))
44
- corrector.replace(node.loc.heredoc_end, correct_delimiters(delimiter_string(expr)))
44
+ corrector.replace(node.loc.heredoc_end, correct_delimiters(node.loc.heredoc_end.source))
45
45
  end
46
46
  end
47
47
 
@@ -52,16 +52,20 @@ module RuboCop
52
52
  end
53
53
 
54
54
  def correct_case_delimiters?(node)
55
- delimiters = delimiter_string(node)
55
+ delimiters = delimiter(node)
56
56
  delimiters == correct_delimiters(delimiters)
57
57
  end
58
58
 
59
59
  def corrected_delimiter_in_body?(node)
60
- corrected = correct_delimiters(delimiter_string(node))
60
+ corrected = correct_delimiters(delimiter(node))
61
61
 
62
62
  node.loc.heredoc_body.source.each_line.any? { |line| line.strip == corrected }
63
63
  end
64
64
 
65
+ def delimiter(node)
66
+ node.loc.heredoc_end.source.strip
67
+ end
68
+
65
69
  def correct_delimiters(source)
66
70
  if style == :uppercase
67
71
  source.upcase
@@ -93,15 +93,19 @@ module RuboCop
93
93
  variable.source_range
94
94
  end
95
95
 
96
+ # rubocop:disable-next Metrics/CyclomaticComplexity
96
97
  def autocorrect(corrector, node, range, offending_name, preferred_name)
98
+ kwbegin_node = node.parent.each_ancestor(:kwbegin).first
99
+ return if kwbegin_node&.parent&.call_type?
100
+
97
101
  corrector.replace(range, preferred_name)
98
102
  # Once the exception variable is reassigned, later references point to a
99
103
  # different value, so stop correcting after the reassignment - both in the
100
104
  # body and in the code following the `begin`/`rescue`.
101
105
  return if correct_node(corrector, node.body, offending_name, preferred_name)
102
- return unless (kwbegin_node = node.parent.each_ancestor(:kwbegin).first)
106
+ return unless kwbegin_node
103
107
 
104
- kwbegin_node.right_siblings.each do |child_node|
108
+ kwbegin_node.right_siblings.grep(AST::Node).each do |child_node|
105
109
  break if correct_node(corrector, child_node, offending_name, preferred_name)
106
110
  end
107
111
  end
@@ -362,7 +362,7 @@ module RuboCop
362
362
  if argument_less_modifier_node
363
363
  corrector.insert_after(argument_less_modifier_node, "\n\n#{source}")
364
364
  elsif (ancestor = node.each_ancestor(:class, :module, :sclass).first)
365
- corrector.insert_before(ancestor.loc.end, "#{node.method_name}\n\n#{source}\n")
365
+ insert_before_end(corrector, ancestor, "#{node.method_name}\n\n#{source}\n")
366
366
  else
367
367
  corrector.replace(node, "#{node.method_name}\n\n#{source}")
368
368
  return
@@ -371,16 +371,29 @@ module RuboCop
371
371
  remove_nodes(corrector, *def_nodes, node)
372
372
  end
373
373
 
374
+ def insert_before_end(corrector, ancestor, source)
375
+ end_keyword = ancestor.loc.end
376
+ source = "\n#{source}" unless begins_its_line?(end_keyword)
377
+
378
+ corrector.insert_before(end_keyword, source)
379
+ end
380
+
374
381
  def insert_inline_modifier(corrector, node, modifier_name)
375
382
  corrector.insert_before(node, "#{modifier_name} ")
376
383
  end
377
384
 
378
385
  def remove_nodes(corrector, *nodes)
379
386
  nodes.each do |node|
380
- corrector.remove(range_with_comments_and_lines(node))
387
+ range = on_own_lines?(node) ? range_with_comments_and_lines(node) : node.source_range
388
+
389
+ corrector.remove(range)
381
390
  end
382
391
  end
383
392
 
393
+ def on_own_lines?(node)
394
+ range_by_whole_lines(node.source_range).source.strip == node.source
395
+ end
396
+
384
397
  def remove_modifier_node_within_begin(corrector, modifier_node, begin_node)
385
398
  modifier_index = begin_node.children.find_index { |child| child.equal?(modifier_node) }
386
399
  def_node = begin_node.children[modifier_index + 1]
@@ -52,6 +52,7 @@ module RuboCop
52
52
  return unless operation
53
53
 
54
54
  return unless modifier_form?(operation)
55
+ return if heredoc_in_body?(node)
55
56
 
56
57
  add_offense(operation, message: format(MSG, keyword: keyword(operation))) do |corrector|
57
58
  correct_to_multiline(corrector, node)
@@ -67,6 +68,10 @@ module RuboCop
67
68
  operation.modifier_form?
68
69
  end
69
70
 
71
+ def heredoc_in_body?(node)
72
+ node.body.each_node(:any_str).any?(&:heredoc?)
73
+ end
74
+
70
75
  def keyword(operation)
71
76
  if operation.respond_to?(:keyword)
72
77
  operation.keyword
@@ -72,13 +72,7 @@ module RuboCop
72
72
 
73
73
  message = message(node)
74
74
  add_offense(node.loc.operator, message: message) do |corrector|
75
- node.each_child_node do |expr|
76
- if expr.call_type?
77
- correct_send(expr, corrector)
78
- elsif expr.type?(:return, :next, :break, :yield) || expr.assignment?
79
- correct_other(expr, corrector)
80
- end
81
- end
75
+ node.each_child_node { |expr| correct_operand(expr, corrector) }
82
76
 
83
77
  corrector.replace(node.loc.operator, node.alternate_operator)
84
78
 
@@ -86,6 +80,22 @@ module RuboCop
86
80
  end
87
81
  end
88
82
 
83
+ def correct_operand(expr, corrector)
84
+ expr = expr.send_node if expr.any_block_type?
85
+
86
+ if expr.type?(:call, :super)
87
+ correct_send(expr, corrector)
88
+ elsif wrap_in_parentheses?(expr)
89
+ correct_other(expr, corrector)
90
+ end
91
+ end
92
+
93
+ # These bind looser than `&&` and `||`, or can't be their operands at all.
94
+ def wrap_in_parentheses?(node)
95
+ node.type?(:return, :next, :break, :yield, :any_match_pattern, :range, :defined?) ||
96
+ node.assignment? || (node.if_type? && node.ternary?)
97
+ end
98
+
89
99
  def on_conditionals(node)
90
100
  node.condition.each_node(*AST::Node::OPERATOR_KEYWORDS) do |operator|
91
101
  process_logical_operator(operator)
@@ -99,8 +109,9 @@ module RuboCop
99
109
  def correct_send(node, corrector)
100
110
  return correct_not(node, node.receiver, corrector) if node.method?(:!)
101
111
  return correct_setter(node, corrector) if node.setter_method?
102
- return correct_other(node, corrector) if node.comparison_method?
112
+ return correct_other(node.block_node || node, corrector) if wrap_whole_call?(node)
103
113
 
114
+ correct_block_receiver(node, corrector)
104
115
  return unless correctable_send?(node)
105
116
 
106
117
  whitespace_before_arg_range = whitespace_before_arg(node)
@@ -120,7 +131,8 @@ module RuboCop
120
131
  # however, 'not x' also parses as (send x :!)
121
132
  def correct_not(node, receiver, corrector)
122
133
  if node.prefix_bang?
123
- return unless receiver.call_type?
134
+ receiver = receiver.send_node if receiver.any_block_type?
135
+ return unless receiver.type?(:call, :super)
124
136
 
125
137
  correct_send(receiver, corrector)
126
138
  elsif node.prefix_not?
@@ -137,6 +149,13 @@ module RuboCop
137
149
  corrector.wrap(node, '(', ')')
138
150
  end
139
151
 
152
+ def wrap_whole_call?(node)
153
+ return true if node.comparison_method?
154
+ return false if node.method?(:[])
155
+
156
+ node.dot? && node.operator_method?
157
+ end
158
+
140
159
  def keep_operator_precedence(corrector, node)
141
160
  if node.or_type? && node.parent&.and_type?
142
161
  corrector.wrap(node, '(', ')')
@@ -145,12 +164,23 @@ module RuboCop
145
164
  end
146
165
  end
147
166
 
167
+ # A method chained onto a `do` block, as in `bar baz do ... end.qux`,
168
+ # still needs parentheses around the arguments of the call that takes the block.
169
+ def correct_block_receiver(node, corrector)
170
+ receiver = node.receiver
171
+ receiver = receiver.receiver while receiver&.call_type?
172
+ correct_operand(receiver, corrector) if receiver&.any_block_type?
173
+ end
174
+
148
175
  def correctable_send?(node)
149
- !node.parenthesized? && node.arguments? && !node.method?(:[])
176
+ # Operator syntax such as `a + b` or `a[b]` already binds tighter than `&&` and `||`.
177
+ return false if node.operator_method? && !node.loc.dot
178
+
179
+ !node.parenthesized? && node.arguments?
150
180
  end
151
181
 
152
182
  def whitespace_before_arg(node)
153
- begin_paren = node.loc.selector.end_pos
183
+ begin_paren = node.selector.end_pos
154
184
  end_paren = begin_paren
155
185
  # Increment position of parenthesis, unless message is a predicate
156
186
  # method followed by a non-whitespace char (e.g. is_a?String).
@@ -177,7 +177,12 @@ module RuboCop
177
177
  BRACES_REQUIRED_MESSAGE = "Brace delimiters `{...}` required for '%<method_name>s' method."
178
178
 
179
179
  def self.autocorrect_incompatible_with
180
- [Layout::SpaceBeforeBlockBraces, Style::MethodCallWithArgsParentheses, Style::RedundantBegin]
180
+ [
181
+ Layout::SpaceBeforeBlockBraces,
182
+ Style::MethodCallWithArgsParentheses,
183
+ Style::RedundantBegin,
184
+ Style::RescueStandardError
185
+ ]
181
186
  end
182
187
 
183
188
  def on_send(node)
@@ -7,7 +7,7 @@ module RuboCop
7
7
  # The `===` operator has different behavior depending on the
8
8
  # receiver and its use outside of `case`/`when` is confusing.
9
9
  # Prefer more explicit alternatives like `is_a?`, `include?`,
10
- # or `match?`.
10
+ # `cover?`, or `match?`.
11
11
  #
12
12
  # If `AllowOnConstant` option is enabled, the cop will ignore violations when the receiver of
13
13
  # the case equality operator is a constant.
@@ -22,9 +22,11 @@ module RuboCop
22
22
  # @example
23
23
  # # bad
24
24
  # (1..100) === 7
25
+ # ('a'..'z') === char
25
26
  #
26
27
  # # good
27
28
  # (1..100).include?(7)
29
+ # ('a'..'z').cover?(char)
28
30
  #
29
31
  # @example AllowOnConstant: false (default)
30
32
  # # bad
@@ -90,9 +92,18 @@ module RuboCop
90
92
  end
91
93
 
92
94
  def begin_replacement(lhs, rhs)
93
- return unless lhs.children.first&.range_type?
95
+ range = lhs.children.first
96
+ return unless range&.range_type?
94
97
 
95
- "#{lhs.source}.include?(#{rhs.source})"
98
+ method = cover_semantics?(range) ? 'cover?' : 'include?'
99
+
100
+ "#{lhs.source}.#{method}(#{rhs.source})"
101
+ end
102
+
103
+ # `Range#===` behaves like `Range#cover?` since Ruby 2.7 (since Ruby 2.6 for ranges
104
+ # that aren't string ranges), and `Range#include?` only matches that for numeric ranges.
105
+ def cover_semantics?(range)
106
+ target_ruby_version >= 2.7 && !range.children.compact.all?(&:numeric_type?)
96
107
  end
97
108
 
98
109
  def const_replacement(lhs, rhs)
@@ -65,10 +65,11 @@ module RuboCop
65
65
  NESTED_MSG = 'Use nested module/class definitions instead of compact style.'
66
66
  COMPACT_MSG = 'Use compact module/class definition instead of nested style.'
67
67
 
68
- # `Style/StructInheritance` turns a class definition into a constant assignment in
69
- # the same pass, which the compacted `class` keyword would then precede.
68
+ # `Style/StructInheritance` and `Style/DataInheritance` turn a class definition into
69
+ # a constant assignment in the same pass, which the compacted `class` keyword would
70
+ # then precede.
70
71
  def self.autocorrect_incompatible_with
71
- [Style::StructInheritance]
72
+ [Style::DataInheritance, Style::StructInheritance]
72
73
  end
73
74
 
74
75
  def on_class(node)
@@ -206,16 +207,18 @@ module RuboCop
206
207
  end
207
208
 
208
209
  def remove_end(corrector, body)
209
- range = if same_line?(body.loc.name, body.loc.end)
210
- single_line_end_range(body)
211
- else
212
- multiline_end_range(body)
213
- end
210
+ range = begins_its_line?(body.loc.end) ? end_line_range(body) : inline_end_range(body)
214
211
 
215
212
  corrector.remove(range)
216
213
  end
217
214
 
218
- def single_line_end_range(body)
215
+ def end_line_range(body)
216
+ end_keyword = body.loc.end
217
+
218
+ range_between(end_keyword.begin_pos - end_keyword.column, end_keyword.end_pos + 1)
219
+ end
220
+
221
+ def inline_end_range(body)
219
222
  raw = processed_source.raw_source
220
223
  pos = body.loc.end.begin_pos
221
224
  pos -= 1 while raw[pos - 1] == ' '
@@ -223,12 +226,6 @@ module RuboCop
223
226
  range_between(pos, body.loc.end.end_pos)
224
227
  end
225
228
 
226
- def multiline_end_range(body)
227
- remove_begin_pos = body.loc.end.begin_pos - leading_spaces(body).size
228
- adjustment = processed_source.raw_source[remove_begin_pos] == ';' ? 0 : 1
229
- range_between(remove_begin_pos, body.loc.end.end_pos + adjustment)
230
- end
231
-
232
229
  def unindent(corrector, node)
233
230
  column_delta = unindent_column_delta(node)
234
231
  return unless column_delta&.negative?
@@ -72,7 +72,7 @@ module RuboCop
72
72
  return unless def_self_style?
73
73
  return unless node.identifier.self_type?
74
74
  return unless all_methods_public?(node)
75
- return unless def_nodes(node).all? { |def_node| no_code_after?(def_node.source_range) }
75
+ return unless def_nodes(node).all? { |def_node| on_own_lines?(def_node.source_range) }
76
76
 
77
77
  add_offense(node, message: MSG_SCLASS) do |corrector|
78
78
  next if inside_corrected_sclass?(node)
@@ -116,6 +116,14 @@ module RuboCop
116
116
  end
117
117
  end
118
118
 
119
+ def on_own_lines?(range)
120
+ no_code_before?(range) && no_code_after?(range)
121
+ end
122
+
123
+ def no_code_before?(range)
124
+ /\A[\s;]*\z/.match?(processed_source.lines[range.line - 1][0...range.column])
125
+ end
126
+
119
127
  def no_code_after?(range)
120
128
  trailing = processed_source.lines[range.last_line - 1][range.last_column..].to_s.strip
121
129
  trailing = trailing.sub(/\A;+\s*/, '')
@@ -66,7 +66,7 @@ module RuboCop
66
66
  MSG = 'Combine this loop with the previous loop.'
67
67
 
68
68
  def self.autocorrect_incompatible_with
69
- [Layout::BlockEndNewline, Layout::RedundantLineBreak]
69
+ [Layout::BlockEndNewline, Layout::RedundantLineBreak, Style::ItBlockParameter, Style::SymbolProc]
70
70
  end
71
71
 
72
72
  def on_block(node)