rubocop 1.78.0 → 1.81.6

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 (117) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -3
  3. data/config/default.yml +46 -21
  4. data/exe/rubocop +1 -8
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +2 -2
  6. data/lib/rubocop/cli.rb +6 -2
  7. data/lib/rubocop/config_loader.rb +3 -1
  8. data/lib/rubocop/config_store.rb +5 -0
  9. data/lib/rubocop/cop/autocorrect_logic.rb +4 -4
  10. data/lib/rubocop/cop/correctors/alignment_corrector.rb +7 -4
  11. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
  12. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +3 -1
  13. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +3 -2
  14. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
  16. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  17. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  18. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +30 -12
  19. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
  20. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
  21. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
  22. data/lib/rubocop/cop/layout/hash_alignment.rb +0 -5
  23. data/lib/rubocop/cop/layout/line_length.rb +9 -1
  24. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +8 -4
  25. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +8 -0
  26. data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
  27. data/lib/rubocop/cop/layout/space_around_operators.rb +8 -0
  28. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  29. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
  30. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +13 -7
  31. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -1
  32. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
  33. data/lib/rubocop/cop/lint/empty_interpolation.rb +11 -0
  34. data/lib/rubocop/cop/lint/literal_as_condition.rb +12 -0
  35. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -2
  36. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
  37. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
  38. data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
  39. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
  40. data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
  41. data/lib/rubocop/cop/lint/self_assignment.rb +6 -5
  42. data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
  43. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
  44. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
  45. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
  46. data/lib/rubocop/cop/lint/void.rb +7 -0
  47. data/lib/rubocop/cop/message_annotator.rb +1 -1
  48. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  49. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
  50. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  51. data/lib/rubocop/cop/naming/method_name.rb +40 -1
  52. data/lib/rubocop/cop/naming/predicate_method.rb +19 -3
  53. data/lib/rubocop/cop/security/json_load.rb +33 -11
  54. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -1
  55. data/lib/rubocop/cop/style/accessor_grouping.rb +13 -1
  56. data/lib/rubocop/cop/style/arguments_forwarding.rb +11 -17
  57. data/lib/rubocop/cop/style/array_intersect.rb +99 -35
  58. data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
  59. data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
  60. data/lib/rubocop/cop/style/block_delimiters.rb +1 -1
  61. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -4
  62. data/lib/rubocop/cop/style/dig_chain.rb +1 -1
  63. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  64. data/lib/rubocop/cop/style/endless_method.rb +15 -2
  65. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  66. data/lib/rubocop/cop/style/exponential_notation.rb +1 -0
  67. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  68. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  69. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  70. data/lib/rubocop/cop/style/it_assignment.rb +69 -12
  71. data/lib/rubocop/cop/style/it_block_parameter.rb +2 -0
  72. data/lib/rubocop/cop/style/map_to_hash.rb +1 -3
  73. data/lib/rubocop/cop/style/map_to_set.rb +1 -3
  74. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -4
  75. data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
  76. data/lib/rubocop/cop/style/one_line_conditional.rb +17 -9
  77. data/lib/rubocop/cop/style/parallel_assignment.rb +32 -20
  78. data/lib/rubocop/cop/style/redundant_begin.rb +34 -0
  79. data/lib/rubocop/cop/style/redundant_condition.rb +1 -1
  80. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  81. data/lib/rubocop/cop/style/redundant_format.rb +26 -5
  82. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  83. data/lib/rubocop/cop/style/redundant_interpolation.rb +11 -2
  84. data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -1
  85. data/lib/rubocop/cop/style/redundant_parentheses.rb +29 -11
  86. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
  87. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
  88. data/lib/rubocop/cop/style/safe_navigation.rb +20 -1
  89. data/lib/rubocop/cop/style/semicolon.rb +20 -5
  90. data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
  91. data/lib/rubocop/cop/style/sole_nested_conditional.rb +30 -1
  92. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
  93. data/lib/rubocop/cop/style/string_concatenation.rb +17 -13
  94. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  95. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +45 -0
  96. data/lib/rubocop/cop/style/unless_else.rb +10 -9
  97. data/lib/rubocop/cop/utils/format_string.rb +10 -0
  98. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  99. data/lib/rubocop/cop/variable_force.rb +25 -8
  100. data/lib/rubocop/cops_documentation_generator.rb +5 -4
  101. data/lib/rubocop/formatter/disabled_config_formatter.rb +18 -5
  102. data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
  103. data/lib/rubocop/formatter/pacman_formatter.rb +1 -0
  104. data/lib/rubocop/lsp/diagnostic.rb +21 -20
  105. data/lib/rubocop/lsp/routes.rb +65 -9
  106. data/lib/rubocop/lsp/runtime.rb +2 -2
  107. data/lib/rubocop/lsp/server.rb +2 -2
  108. data/lib/rubocop/lsp/stdin_runner.rb +0 -16
  109. data/lib/rubocop/result_cache.rb +14 -12
  110. data/lib/rubocop/runner.rb +6 -4
  111. data/lib/rubocop/target_finder.rb +9 -9
  112. data/lib/rubocop/target_ruby.rb +10 -1
  113. data/lib/rubocop/version.rb +1 -1
  114. data/lib/rubocop.rb +3 -0
  115. data/lib/ruby_lsp/rubocop/addon.rb +23 -8
  116. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +49 -15
  117. metadata +9 -6
@@ -41,6 +41,7 @@ module RuboCop
41
41
  ALLOWED_ALWAYS_ESCAPES = " \n[]^\\#".chars.freeze
42
42
  ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES = '-'.chars.freeze
43
43
  ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES = '.*+?{}()|$'.chars.freeze
44
+ INTERPOLATION_SIGILS = %w[@ $].freeze
44
45
 
45
46
  def on_regexp(node)
46
47
  each_escape(node) do |char, index, within_character_class|
@@ -64,6 +65,7 @@ module RuboCop
64
65
  # different versions of Ruby so that e.g. /\i/ != /i/
65
66
  return true if /[[:alnum:]]/.match?(char)
66
67
  return true if ALLOWED_ALWAYS_ESCAPES.include?(char) || delimiter?(node, char)
68
+ return true if requires_escape_to_avoid_interpolation?(node.source[index], char)
67
69
 
68
70
  if within_character_class
69
71
  ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char) &&
@@ -95,6 +97,12 @@ module RuboCop
95
97
  delimiters.include?(char)
96
98
  end
97
99
 
100
+ def requires_escape_to_avoid_interpolation?(char_before_escape, escaped_char)
101
+ # Preserve escapes after '#' that would otherwise trigger interpolation:
102
+ # '#@ivar', '#@@cvar', and '#$gvar'.
103
+ char_before_escape == '#' && INTERPOLATION_SIGILS.include?(escaped_char)
104
+ end
105
+
98
106
  def each_escape(node)
99
107
  node.parsed_tree&.traverse&.reduce(0) do |char_class_depth, (event, expr)|
100
108
  yield(expr.text[1], expr.ts, !char_class_depth.zero?) if expr.type == :escape
@@ -142,6 +142,7 @@ module RuboCop
142
142
  # @!method strip_begin(node)
143
143
  def_node_matcher :strip_begin, '{ (begin $!begin) $!(begin) }'
144
144
 
145
+ # rubocop:disable Metrics/AbcSize
145
146
  def on_if(node)
146
147
  return if allowed_if_condition?(node)
147
148
 
@@ -155,9 +156,11 @@ module RuboCop
155
156
  removal_ranges = [begin_range(node, body), end_range(node, body)]
156
157
 
157
158
  report_offense(node, method_chain, method_call, *removal_ranges) do |corrector|
159
+ corrector.replace(receiver, checked_variable.source) if checked_variable.csend_type?
158
160
  corrector.insert_before(method_call.loc.dot, '&') unless method_call.safe_navigation?
159
161
  end
160
162
  end
163
+ # rubocop:enable Metrics/AbcSize
161
164
 
162
165
  def on_and(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
163
166
  collect_and_clauses(node).each do |(lhs, lhs_operator_range), (rhs, _rhs_operator_range)|
@@ -259,6 +262,14 @@ module RuboCop
259
262
  end
260
263
 
261
264
  def dotless_operator_call?(method_call)
265
+ return true if dotless_operator_method?(method_call)
266
+
267
+ method_call = method_call.parent while method_call.parent.send_type?
268
+
269
+ dotless_operator_method?(method_call)
270
+ end
271
+
272
+ def dotless_operator_method?(method_call)
262
273
  return false if method_call.loc.dot
263
274
 
264
275
  method_call.method?(:[]) || method_call.method?(:[]=) || method_call.operator_method?
@@ -333,8 +344,16 @@ module RuboCop
333
344
 
334
345
  def matching_call_nodes?(left, right)
335
346
  return false unless left && right.respond_to?(:call_type?)
347
+ return false unless left.call_type? && right.call_type?
348
+
349
+ # Compare receiver and method name, but ignore the difference between
350
+ # safe navigation method call (`&.`) and dot method call (`.`).
351
+ left_receiver, left_method, *left_args = left.children
352
+ right_receiver, right_method, *right_args = right.children
336
353
 
337
- left.call_type? && right.call_type? && left.children == right.children
354
+ left_method == right_method &&
355
+ matching_nodes?(left_receiver, right_receiver) &&
356
+ left_args == right_args
338
357
  end
339
358
 
340
359
  def chain_length(method_chain, method)
@@ -119,6 +119,7 @@ module RuboCop
119
119
  tokens[1]&.type == :tSTRING_DBEG && tokens[2]&.semicolon?
120
120
  end
121
121
 
122
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
122
123
  def register_semicolon(line, column, after_expression, token_before_semicolon = nil)
123
124
  range = source_range(processed_source.buffer, line, column)
124
125
 
@@ -130,14 +131,19 @@ module RuboCop
130
131
  # without parentheses.
131
132
  # See: https://github.com/rubocop/rubocop/issues/10791
132
133
  if token_before_semicolon&.regexp_dots?
133
- range_node = find_range_node(token_before_semicolon)
134
- corrector.wrap(range_node, '(', ')') if range_node
134
+ node = find_node(range_nodes, token_before_semicolon)
135
+ elsif token_before_semicolon&.type == :tLABEL
136
+ node = find_node(value_omission_pair_nodes, token_before_semicolon).parent
137
+ space = node.parent.loc.selector.end.join(node.source_range.begin)
138
+ corrector.remove(space)
135
139
  end
136
140
 
141
+ corrector.wrap(node, '(', ')') if node
137
142
  corrector.remove(range)
138
143
  end
139
144
  end
140
145
  end
146
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
141
147
 
142
148
  def expressions_per_line(exprs)
143
149
  # create a map matching lines to the number of expressions on them
@@ -153,9 +159,9 @@ module RuboCop
153
159
  end
154
160
  end
155
161
 
156
- def find_range_node(token_before_semicolon)
157
- range_nodes.detect do |range_node|
158
- range_node.source_range.contains?(token_before_semicolon.pos)
162
+ def find_node(nodes, token_before_semicolon)
163
+ nodes.detect do |node|
164
+ node.source_range.overlaps?(token_before_semicolon.pos)
159
165
  end
160
166
  end
161
167
 
@@ -166,6 +172,15 @@ module RuboCop
166
172
  @range_nodes = ast.range_type? ? [ast] : []
167
173
  @range_nodes.concat(ast.each_descendant(:range).to_a)
168
174
  end
175
+
176
+ def value_omission_pair_nodes
177
+ if instance_variable_defined?(:@value_omission_pair_nodes)
178
+ return @value_omission_pair_nodes
179
+ end
180
+
181
+ ast = processed_source.ast
182
+ @value_omission_pair_nodes = ast.each_descendant(:pair).to_a.select(&:value_omission?)
183
+ end
169
184
  end
170
185
  end
171
186
  end
@@ -65,7 +65,7 @@ module RuboCop
65
65
  return false if target_ruby_version < 3.0
66
66
  return false if disallow_endless_method_style?
67
67
  return false unless body_node
68
- return false if body_node.parent.assignment_method? ||
68
+ return false if body_node.basic_conditional? || body_node.parent.assignment_method? ||
69
69
  NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type)
70
70
 
71
71
  !body_node.type?(:begin, :kwbegin)
@@ -86,10 +86,10 @@ module RuboCop
86
86
  end
87
87
 
88
88
  def correct_to_endless(corrector, node)
89
- self_receiver = node.self_receiver? ? 'self.' : ''
89
+ receiver = "#{node.receiver.source}." if node.receiver
90
90
  arguments = node.arguments.any? ? node.arguments.source : '()'
91
91
  body_source = method_body_source(node.body)
92
- replacement = "def #{self_receiver}#{node.method_name}#{arguments} = #{body_source}"
92
+ replacement = "def #{receiver}#{node.method_name}#{arguments} = #{body_source}"
93
93
 
94
94
  corrector.replace(node, replacement)
95
95
  end
@@ -175,6 +175,8 @@ module RuboCop
175
175
 
176
176
  if parenthesize_method?(condition)
177
177
  parenthesized_method_arguments(condition)
178
+ elsif condition.and_type?
179
+ parenthesized_and(condition)
178
180
  else
179
181
  "(#{condition.source})"
180
182
  end
@@ -186,12 +188,19 @@ module RuboCop
186
188
  end
187
189
 
188
190
  def add_parentheses?(node)
189
- return true if node.assignment? || (node.operator_keyword? && !node.and_type?)
191
+ return true if node.assignment? || node.or_type?
192
+ return true if assignment_in_and?(node)
190
193
  return false unless node.call_type?
191
194
 
192
195
  (node.arguments.any? && !node.parenthesized?) || node.prefix_not?
193
196
  end
194
197
 
198
+ def assignment_in_and?(node)
199
+ return false unless node.and_type?
200
+
201
+ node.each_descendant.any?(&:assignment?)
202
+ end
203
+
195
204
  def parenthesized_method_arguments(node)
196
205
  method_call = node.source_range.begin.join(node.loc.selector.end).source
197
206
  arguments = node.first_argument.source_range.begin.join(node.source_range.end).source
@@ -199,6 +208,26 @@ module RuboCop
199
208
  "#{method_call}(#{arguments})"
200
209
  end
201
210
 
211
+ def parenthesized_and(node)
212
+ # We only need to add parentheses around the last clause if it's an assignment,
213
+ # because other clauses will be unchanged by merging conditionals.
214
+ lhs = node.lhs.source
215
+ rhs = parenthesized_and_clause(node.rhs)
216
+ operator = range_with_surrounding_space(node.loc.operator, whitespace: true).source
217
+
218
+ "#{lhs}#{operator}#{rhs}"
219
+ end
220
+
221
+ def parenthesized_and_clause(node)
222
+ if node.and_type?
223
+ parenthesized_and(node)
224
+ elsif node.assignment?
225
+ "(#{node.source})"
226
+ else
227
+ node.source
228
+ end
229
+ end
230
+
202
231
  def allow_modifier?
203
232
  cop_config['AllowModifier']
204
233
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Check for parentheses around stabby lambda arguments.
6
+ # Checks for parentheses around stabby lambda arguments.
7
7
  # There are two different styles. Defaults to `require_parentheses`.
8
8
  #
9
9
  # @example EnforcedStyle: require_parentheses (default)
@@ -100,7 +100,7 @@ module RuboCop
100
100
  node.receiver.str_type? &&
101
101
  node.first_argument.str_type? &&
102
102
  node.multiline? &&
103
- node.source =~ /\+\s*\n/
103
+ node.source.match?(/\+\s*\n/)
104
104
  end
105
105
 
106
106
  def find_topmost_plus_node(node)
@@ -141,22 +141,26 @@ module RuboCop
141
141
  end
142
142
 
143
143
  def replacement(parts)
144
- interpolated_parts = parts.map do |part|
145
- case part.type
146
- when :str
147
- adjust_str(part)
148
- when :dstr
149
- part.children.all?(&:str_type?) ? adjust_str(part) : part.value
150
- else
151
- "\#{#{part.source}}"
152
- end
153
- end
144
+ interpolated_parts = parts.map { |part| adjust_str(part) }
154
145
 
155
146
  "\"#{handle_quotes(interpolated_parts).join}\""
156
147
  end
157
148
 
158
- def adjust_str(node)
159
- single_quoted?(node) ? node.value.gsub(/(\\|")/, '\\\\\&') : node.value.inspect[1..-2]
149
+ def adjust_str(part)
150
+ case part.type
151
+ when :str
152
+ if single_quoted?(part)
153
+ part.value.gsub(/(\\|"|#\{|#@|#\$)/, '\\\\\&')
154
+ else
155
+ part.value.inspect[1..-2]
156
+ end
157
+ when :dstr, :begin
158
+ part.children.map do |child|
159
+ adjust_str(child)
160
+ end.join
161
+ else
162
+ "\#{#{part.source}}"
163
+ end
160
164
  end
161
165
 
162
166
  def handle_quotes(parts)
@@ -81,7 +81,7 @@ module RuboCop
81
81
 
82
82
  content = *sym
83
83
  content = content.map { |c| c.is_a?(AST::Node) ? c.source : c }.join
84
- content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s\(\)]*\))/, '')
84
+ content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s()]*\))/, '')
85
85
 
86
86
  content.include?(' ') || DELIMITERS.any? do |delimiter|
87
87
  content_without_delimiter_pairs.include?(delimiter)
@@ -10,6 +10,9 @@ module RuboCop
10
10
  # for all parenthesized multi-line method calls with arguments.
11
11
  # * `comma`: Requires a comma after the last argument, but only for
12
12
  # parenthesized method calls where each argument is on its own line.
13
+ # * `diff_comma`: Requires a comma after the last argument, but only
14
+ # when that argument is followed by an immediate newline, even if
15
+ # there is an inline comment on the same line.
13
16
  # * `no_comma`: Requires that there is no comma after the last
14
17
  # argument.
15
18
  #
@@ -75,6 +78,48 @@ module RuboCop
75
78
  # 2,
76
79
  # )
77
80
  #
81
+ # @example EnforcedStyleForMultiline: diff_comma
82
+ # # bad
83
+ # method(1, 2,)
84
+ #
85
+ # # good
86
+ # method(1, 2)
87
+ #
88
+ # # good
89
+ # method(
90
+ # 1, 2,
91
+ # 3,
92
+ # )
93
+ #
94
+ # # good
95
+ # method(
96
+ # 1, 2, 3,
97
+ # )
98
+ #
99
+ # # good
100
+ # method(
101
+ # 1,
102
+ # 2,
103
+ # )
104
+ #
105
+ # # bad
106
+ # method(1, [
107
+ # 2,
108
+ # ],)
109
+ #
110
+ # # good
111
+ # method(1, [
112
+ # 2,
113
+ # ])
114
+ #
115
+ # # bad
116
+ # object[1, 2,
117
+ # 3, 4,]
118
+ #
119
+ # # good
120
+ # object[1, 2,
121
+ # 3, 4]
122
+ #
78
123
  # @example EnforcedStyleForMultiline: no_comma (default)
79
124
  # # bad
80
125
  # method(1, 2,)
@@ -20,7 +20,6 @@ module RuboCop
20
20
  # # do a different thing...
21
21
  # end
22
22
  class UnlessElse < Base
23
- include RangeHelp
24
23
  extend AutoCorrector
25
24
 
26
25
  MSG = 'Do not use `unless` with `else`. Rewrite these with the positive case first.'
@@ -29,25 +28,27 @@ module RuboCop
29
28
  return unless node.unless? && node.else?
30
29
 
31
30
  add_offense(node) do |corrector|
32
- body_range = range_between_condition_and_else(node, node.condition)
33
- else_range = range_between_else_and_end(node)
34
-
35
31
  next if part_of_ignored_node?(node)
36
32
 
37
33
  corrector.replace(node.loc.keyword, 'if')
38
- corrector.replace(body_range, else_range.source)
39
- corrector.replace(else_range, body_range.source)
34
+
35
+ body_range = range_between_condition_and_else(node)
36
+ else_range = range_between_else_and_end(node)
37
+
38
+ corrector.swap(body_range, else_range)
40
39
  end
41
40
 
42
41
  ignore_node(node)
43
42
  end
44
43
 
45
- def range_between_condition_and_else(node, condition)
46
- range_between(condition.source_range.end_pos, node.loc.else.begin_pos)
44
+ def range_between_condition_and_else(node)
45
+ range = node.loc.begin ? node.loc.begin.end : node.condition.source_range
46
+
47
+ range.end.join(node.loc.else.begin)
47
48
  end
48
49
 
49
50
  def range_between_else_and_end(node)
50
- range_between(node.loc.else.end_pos, node.loc.end.begin_pos)
51
+ node.loc.else.end.join(node.loc.end.begin)
51
52
  end
52
53
  end
53
54
  end
@@ -71,6 +71,16 @@ module RuboCop
71
71
  name && @source.include?('{')
72
72
  end
73
73
 
74
+ def variable_width?
75
+ !!width&.start_with?('*')
76
+ end
77
+
78
+ def variable_width_argument_number
79
+ return unless variable_width?
80
+
81
+ width == '*' ? 1 : width.match(DIGIT_DOLLAR)['arg_number'].to_i
82
+ end
83
+
74
84
  # Number of arguments required for the format sequence
75
85
  def arity
76
86
  @source.scan('*').count + 1
@@ -79,7 +79,7 @@ module RuboCop
79
79
  parent = parent.parent if parent&.begin_type?
80
80
  return false if parent.nil?
81
81
 
82
- parent.type?(:if, :while, :until) && parent.modifier_form?
82
+ parent.basic_conditional? && parent.modifier_form?
83
83
  end
84
84
 
85
85
  def capture_with_block!
@@ -71,6 +71,8 @@ module RuboCop
71
71
  end
72
72
  end
73
73
 
74
+ BRANCH_NODES = %i[if case case_match rescue].freeze
75
+
74
76
  def variable_table
75
77
  @variable_table ||= VariableTable.new(self)
76
78
  end
@@ -236,11 +238,16 @@ module RuboCop
236
238
  end
237
239
 
238
240
  def process_loop(node)
239
- if POST_CONDITION_LOOP_TYPES.include?(node.type)
241
+ if node.post_condition_loop?
240
242
  # See the comment at the end of file for this behavior.
241
243
  condition_node, body_node = *node
242
244
  process_node(body_node)
243
245
  process_node(condition_node)
246
+ elsif node.for_type?
247
+ # In `for item in items` the rightmost expression is evaluated first.
248
+ process_node(node.collection)
249
+ process_node(node.variable)
250
+ process_node(node.body) if node.body
244
251
  else
245
252
  process_children(node)
246
253
  end
@@ -296,7 +303,7 @@ module RuboCop
296
303
  variable_table.accessible_variables.each { |variable| variable.reference!(node) }
297
304
  end
298
305
 
299
- # Mark all assignments which are referenced in the same loop
306
+ # Mark last assignments which are referenced in the same loop
300
307
  # as referenced by ignoring AST order since they would be referenced
301
308
  # in next iteration.
302
309
  def mark_assignments_as_referenced_in_loop(node)
@@ -308,13 +315,12 @@ module RuboCop
308
315
  # would be skipped here.
309
316
  next unless variable
310
317
 
311
- variable.assignments.each do |assignment|
312
- next if assignment_nodes_in_loop.none? do |assignment_node|
313
- assignment_node.equal?(assignment.node)
314
- end
315
-
316
- assignment.reference!(node)
318
+ loop_assignments = variable.assignments.select do |assignment|
319
+ assignment_nodes_in_loop.include?(assignment.node)
317
320
  end
321
+ next unless loop_assignments.any?
322
+
323
+ reference_assignments(loop_assignments, node)
318
324
  end
319
325
  end
320
326
 
@@ -354,6 +360,17 @@ module RuboCop
354
360
  end
355
361
  end
356
362
 
363
+ def reference_assignments(loop_assignments, loop_node)
364
+ # If inside a branching statement, mark all as referenced.
365
+ # Otherwise, mark only the last assignment as referenced.
366
+ # Note that `rescue` must be considered as branching because of
367
+ # the `retry` keyword.
368
+ loop_assignments.each do |assignment|
369
+ assignment.reference!(loop_node) if assignment.node.each_ancestor(*BRANCH_NODES).any?
370
+ end
371
+ loop_assignments.last&.reference!(loop_node)
372
+ end
373
+
357
374
  def scanned_node?(node)
358
375
  scanned_nodes.include?(node)
359
376
  end
@@ -7,6 +7,7 @@ require 'yard'
7
7
  # @api private
8
8
  class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
9
9
  include ::RuboCop::Cop::Documentation
10
+
10
11
  CopData = Struct.new(
11
12
  :cop, :description, :example_objects, :safety_objects, :see_objects, :config, keyword_init: true
12
13
  )
@@ -193,10 +194,10 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
193
194
 
194
195
  def configurations(department, cop, cop_config)
195
196
  header = ['Name', 'Default value', 'Configurable values']
196
- configs = cop_config
197
- .each_key
198
- .reject { |key| key.start_with?('Supported') }
199
- .reject { |key| key.start_with?('AllowMultipleStyles') }
197
+ configs = cop_config.each_key.reject do |key|
198
+ key == 'AllowMultipleStyles' ||
199
+ (key != 'SupportedTypes' && key.start_with?('Supported'))
200
+ end
200
201
  return '' if configs.empty?
201
202
 
202
203
  content = configs.map do |name|
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Formatter
5
5
  # This formatter displays a YAML configuration file where all cops that
6
6
  # detected any offenses are configured to not detect the offense.
7
- class DisabledConfigFormatter < BaseFormatter
7
+ class DisabledConfigFormatter < BaseFormatter # rubocop:disable Metrics/ClassLength
8
8
  include PathUtil
9
9
 
10
10
  HEADING = <<~COMMENTS
@@ -17,6 +17,22 @@ module RuboCop
17
17
  # versions of RuboCop, may require this file to be generated again.
18
18
  COMMENTS
19
19
 
20
+ EXCLUDED_CONFIG_KEYS = %w[
21
+ AutoCorrect
22
+ Description
23
+ Enabled
24
+ Exclude
25
+ Include
26
+ Reference
27
+ References
28
+ Safe
29
+ SafeAutoCorrect
30
+ StyleGuide
31
+ VersionAdded
32
+ VersionChanged
33
+ VersionRemoved
34
+ ].freeze
35
+
20
36
  @config_to_allow_offenses = {}
21
37
  @detected_styles = {}
22
38
 
@@ -163,10 +179,7 @@ module RuboCop
163
179
  end
164
180
 
165
181
  def cop_config_params(default_cfg, cfg)
166
- default_cfg.keys -
167
- %w[Description StyleGuide Reference References Enabled Exclude Safe
168
- SafeAutoCorrect VersionAdded VersionChanged VersionRemoved] -
169
- cfg.keys
182
+ default_cfg.keys - EXCLUDED_CONFIG_KEYS - cfg.keys
170
183
  end
171
184
 
172
185
  def output_cop_param_comments(output_buffer, params, default_cfg)
@@ -6,6 +6,7 @@ module RuboCop
6
6
  class MarkdownFormatter < BaseFormatter
7
7
  include TextUtil
8
8
  include PathUtil
9
+
9
10
  attr_reader :files, :summary
10
11
 
11
12
  def initialize(output, options = {})
@@ -9,6 +9,7 @@ module RuboCop
9
9
  # https://github.com/go-labs/rspec_pacman_formatter
10
10
  class PacmanFormatter < ClangStyleFormatter
11
11
  include TextUtil
12
+
12
13
  attr_accessor :progress_line
13
14
 
14
15
  FALLBACK_TERMINAL_WIDTH = 80
@@ -16,8 +16,8 @@ module RuboCop
16
16
  # Diagnostic for Language Server Protocol of RuboCop.
17
17
  # @api private
18
18
  class Diagnostic
19
- def initialize(document_encoding, offense, uri, cop_class)
20
- @document_encoding = document_encoding
19
+ def initialize(position_encoding, offense, uri, cop_class)
20
+ @position_encoding = position_encoding
21
21
  @offense = offense
22
22
  @uri = uri
23
23
  @cop_class = cop_class
@@ -45,11 +45,11 @@ module RuboCop
45
45
  range: LanguageServer::Protocol::Interface::Range.new(
46
46
  start: LanguageServer::Protocol::Interface::Position.new(
47
47
  line: @offense.line - 1,
48
- character: highlighted.begin_pos
48
+ character: to_position_character(highlighted.begin_pos)
49
49
  ),
50
50
  end: LanguageServer::Protocol::Interface::Position.new(
51
51
  line: @offense.line - 1,
52
- character: highlighted.end_pos
52
+ character: to_position_character(highlighted.end_pos)
53
53
  )
54
54
  ),
55
55
  data: {
@@ -107,11 +107,11 @@ module RuboCop
107
107
  range: LanguageServer::Protocol::Interface::Range.new(
108
108
  start: LanguageServer::Protocol::Interface::Position.new(
109
109
  line: range.line - 1,
110
- character: range.column
110
+ character: to_position_character(range.column)
111
111
  ),
112
112
  end: LanguageServer::Protocol::Interface::Position.new(
113
113
  line: range.last_line - 1,
114
- character: range.last_column
114
+ character: to_position_character(range.last_column)
115
115
  )
116
116
  ),
117
117
  new_text: replacement
@@ -149,7 +149,7 @@ module RuboCop
149
149
 
150
150
  eol = LanguageServer::Protocol::Interface::Position.new(
151
151
  line: @offense.line - 1,
152
- character: length_of_line(@offense.source_line)
152
+ character: to_position_character
153
153
  )
154
154
 
155
155
  # TODO: fails for multiline strings - may be preferable to use block
@@ -162,19 +162,6 @@ module RuboCop
162
162
  [inline_comment]
163
163
  end
164
164
 
165
- def length_of_line(line)
166
- if @document_encoding == Encoding::UTF_16LE
167
- line_length = 0
168
- line.codepoints.each do |codepoint|
169
- line_length += 1
170
- line_length += 1 if codepoint > RubyLsp::Document::Scanner::SURROGATE_PAIR_START
171
- end
172
- line_length
173
- else
174
- line.length
175
- end
176
- end
177
-
178
165
  def correctable?
179
166
  !@offense.corrector.nil?
180
167
  end
@@ -184,6 +171,20 @@ module RuboCop
184
171
  uri.scheme = 'file' if uri.scheme.nil?
185
172
  uri
186
173
  end
174
+
175
+ def to_position_character(utf8_index = nil)
176
+ str = utf8_index ? @offense.source_line[0, utf8_index] : @offense.source_line
177
+ case @position_encoding
178
+ when 'utf-8', Encoding::UTF_8
179
+ str.bytesize
180
+ when 'utf-32', Encoding::UTF_32
181
+ str.size
182
+ else # 'utf-16'
183
+ # utf-16 is default position encoding on LSP
184
+ # https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/
185
+ str.size + str.count("\u{10000}-\u{10FFFF}")
186
+ end
187
+ end
187
188
  end
188
189
  end
189
190
  end