rubocop 1.92.3 → 1.93.0

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 (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -1
  4. data/lib/rubocop/config.rb +6 -0
  5. data/lib/rubocop/config_store.rb +1 -0
  6. data/lib/rubocop/config_validator.rb +45 -1
  7. data/lib/rubocop/cop/base.rb +5 -1
  8. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +25 -1
  9. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  10. data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
  11. data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
  12. data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
  13. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  14. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +16 -4
  15. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +25 -7
  16. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  17. data/lib/rubocop/cop/layout/line_length.rb +14 -1
  18. data/lib/rubocop/cop/lint/duplicate_methods.rb +47 -9
  19. data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
  20. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
  21. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +11 -3
  22. data/lib/rubocop/cop/lint/number_conversion.rb +10 -0
  23. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
  24. data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
  25. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  26. data/lib/rubocop/cop/lint/useless_assignment.rb +45 -1
  27. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
  28. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
  29. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
  30. data/lib/rubocop/cop/lint/useless_times.rb +40 -12
  31. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +16 -1
  32. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  33. data/lib/rubocop/cop/naming/block_forwarding.rb +2 -2
  34. data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
  35. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  36. data/lib/rubocop/cop/style/array_intersect.rb +15 -7
  37. data/lib/rubocop/cop/style/block_delimiters.rb +0 -1
  38. data/lib/rubocop/cop/style/data_inheritance.rb +78 -9
  39. data/lib/rubocop/cop/style/dig_chain.rb +13 -2
  40. data/lib/rubocop/cop/style/each_with_object.rb +21 -5
  41. data/lib/rubocop/cop/style/empty_class_definition.rb +19 -1
  42. data/lib/rubocop/cop/style/empty_method.rb +11 -0
  43. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  44. data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
  45. data/lib/rubocop/cop/style/file_read.rb +5 -1
  46. data/lib/rubocop/cop/style/file_write.rb +89 -20
  47. data/lib/rubocop/cop/style/hash_lookup_method.rb +2 -4
  48. data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
  49. data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
  50. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  51. data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
  52. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  53. data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
  54. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
  55. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
  56. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
  57. data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
  58. data/lib/rubocop/cop/style/nil_lambda.rb +4 -11
  59. data/lib/rubocop/cop/style/non_nil_check.rb +13 -0
  60. data/lib/rubocop/cop/style/operator_method_call.rb +14 -0
  61. data/lib/rubocop/cop/style/raise_args.rb +15 -0
  62. data/lib/rubocop/cop/style/redundant_array_constructor.rb +14 -0
  63. data/lib/rubocop/cop/style/redundant_begin.rb +26 -6
  64. data/lib/rubocop/cop/style/redundant_each.rb +18 -0
  65. data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
  66. data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
  67. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -1
  68. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
  69. data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -0
  70. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
  71. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  72. data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
  73. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  74. data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
  75. data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
  76. data/lib/rubocop/cop/style/select_by_range.rb +1 -6
  77. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
  78. data/lib/rubocop/cop/style/self_assignment.rb +0 -4
  79. data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
  80. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  81. data/lib/rubocop/cop/style/struct_inheritance.rb +68 -16
  82. data/lib/rubocop/cop/style/swap_values.rb +10 -13
  83. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  84. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
  85. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  86. data/lib/rubocop/rspec/shared_contexts.rb +11 -11
  87. data/lib/rubocop/runner.rb +16 -7
  88. data/lib/rubocop/target_ruby.rb +6 -8
  89. data/lib/rubocop/version.rb +18 -8
  90. metadata +9 -6
@@ -25,7 +25,6 @@ module RuboCop
25
25
  # foo.concat(ary)
26
26
  #
27
27
  class RedundantSelfAssignment < Base
28
- include RangeHelp
29
28
  extend AutoCorrector
30
29
 
31
30
  MSG = 'Redundant self assignment detected. ' \
@@ -55,7 +54,6 @@ module RuboCop
55
54
  ...))
56
55
  PATTERN
57
56
 
58
- # rubocop:disable-next Metrics/AbcSize
59
57
  def on_lvasgn(node)
60
58
  return unless (rhs = node.rhs)
61
59
  return unless rhs.type?(:any_block, :call) && method_returning_self?(rhs.method_name)
@@ -64,10 +62,7 @@ module RuboCop
64
62
  receiver_type = ASSIGNMENT_TYPE_TO_RECEIVER_TYPE[node.type]
65
63
  return unless receiver.type == receiver_type && receiver.children.first == node.lhs
66
64
 
67
- message = format(MSG, method_name: rhs.method_name)
68
- add_offense(node.loc.operator, message: message) do |corrector|
69
- corrector.replace(node, rhs.source)
70
- end
65
+ register_offense(node, rhs)
71
66
  end
72
67
  alias on_ivasgn on_lvasgn
73
68
  alias on_cvasgn on_lvasgn
@@ -77,10 +72,7 @@ module RuboCop
77
72
  return unless node.assignment_method?
78
73
  return unless redundant_assignment?(node)
79
74
 
80
- message = format(MSG, method_name: node.first_argument.method_name)
81
- add_offense(node.loc.operator, message: message) do |corrector|
82
- corrector.remove(correction_range(node))
83
- end
75
+ register_offense(node, node.first_argument)
84
76
  end
85
77
  alias on_csend on_send
86
78
 
@@ -96,8 +88,24 @@ module RuboCop
96
88
  redundant_self_assignment?(node, node.receiver, receiver_name)
97
89
  end
98
90
 
99
- def correction_range(node)
100
- range_between(node.source_range.begin_pos, node.first_argument.source_range.begin_pos)
91
+ def register_offense(node, rhs)
92
+ message = format(MSG, method_name: rhs.method_name)
93
+ add_offense(node.loc.operator, message: message) do |corrector|
94
+ prefix = node.source_range.begin.join(rhs.source_range.begin)
95
+ corrector.remove(prefix) unless unsafe_prefix?(prefix)
96
+ end
97
+ end
98
+
99
+ def unsafe_prefix?(range)
100
+ return false if range.single_line? && !range.source.include?('<<')
101
+
102
+ processed_source.tokens_within(range).any? do |token|
103
+ case token.type
104
+ when :tSTRING_BEG, :tXSTRING_BEG then token.text.start_with?('<<')
105
+ when :tSTRING_END then true
106
+ else token.comment?
107
+ end
108
+ end
101
109
  end
102
110
  end
103
111
  end
@@ -91,7 +91,7 @@ module RuboCop
91
91
  (call $(call _ $:sort) ${:[] :at :slice} {(int 0) (int -1)})
92
92
 
93
93
  (call $(call _ $:sort_by _) ${:last :first})
94
- (send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
94
+ (call $(call _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
95
95
 
96
96
  (call (any_block $(call _ ${:sort_by :sort}) ...) ${:last :first})
97
97
  (call
@@ -45,10 +45,6 @@ module RuboCop
45
45
 
46
46
  MSG = 'Avoid using `rescue` in its modifier form.'
47
47
 
48
- def self.autocorrect_incompatible_with
49
- [Style::MethodCallWithArgsParentheses]
50
- end
51
-
52
48
  def on_resbody(node)
53
49
  return unless rescue_modifier?(node)
54
50
 
@@ -63,11 +63,6 @@ module RuboCop
63
63
  }
64
64
  PATTERN
65
65
 
66
- # @!method env_const?(node)
67
- def_node_matcher :env_const?, <<~PATTERN
68
- (const {nil? cbase} :ENV)
69
- PATTERN
70
-
71
66
  # @!method calls_lvar?(node, name)
72
67
  def_node_matcher :calls_lvar?, <<~PATTERN
73
68
  (send (lvar %1) %CLASS_CHECK_METHODS _)
@@ -96,7 +91,7 @@ module RuboCop
96
91
  def receiver_allowed?(node)
97
92
  return false unless node
98
93
 
99
- node.hash_type? || creates_hash?(node) || env_const?(node)
94
+ node.hash_type? || creates_hash?(node) || node.global_const?(:ENV)
100
95
  end
101
96
 
102
97
  def replacement(class_check_send_node, node)
@@ -84,11 +84,6 @@ module RuboCop
84
84
  }
85
85
  PATTERN
86
86
 
87
- # @!method env_const?(node)
88
- def_node_matcher :env_const?, <<~PATTERN
89
- (const {nil? cbase} :ENV)
90
- PATTERN
91
-
92
87
  # @!method between_call?(node, name)
93
88
  def_node_matcher :between_call?, <<~PATTERN
94
89
  (send (lvar %1) :between? _ _)
@@ -117,7 +112,7 @@ module RuboCop
117
112
  def receiver_allowed?(node)
118
113
  return false unless node
119
114
 
120
- node.hash_type? || creates_hash?(node) || env_const?(node)
115
+ node.hash_type? || creates_hash?(node) || node.global_const?(:ENV)
121
116
  end
122
117
 
123
118
  def replacement(range_check_send_node, node)
@@ -77,11 +77,6 @@ module RuboCop
77
77
  }
78
78
  PATTERN
79
79
 
80
- # @!method env_const?(node)
81
- def_node_matcher :env_const?, <<~PATTERN
82
- (const {nil? cbase} :ENV)
83
- PATTERN
84
-
85
80
  # @!method calls_lvar?(node, name)
86
81
  def_node_matcher :calls_lvar?, <<~PATTERN
87
82
  {
@@ -120,7 +115,7 @@ module RuboCop
120
115
  def receiver_allowed?(node)
121
116
  return false unless node
122
117
 
123
- node.hash_type? || creates_hash?(node) || env_const?(node)
118
+ node.hash_type? || creates_hash?(node) || node.global_const?(:ENV)
124
119
  end
125
120
 
126
121
  def replacement(regexp_method_send_node, node)
@@ -18,10 +18,6 @@ module RuboCop
18
18
  MSG = 'Use self-assignment shorthand `%<method>s=`.'
19
19
  OPS = %i[+ - * ** / % ^ << >> | &].freeze
20
20
 
21
- def self.autocorrect_incompatible_with
22
- [Layout::SpaceAroundOperators]
23
- end
24
-
25
21
  def on_lvasgn(node)
26
22
  check(node, :lvar)
27
23
  end
@@ -54,8 +54,7 @@ module RuboCop
54
54
  add_offense(node, message: message) do |corrector|
55
55
  next if part_of_ignored_node?(node)
56
56
 
57
- correct_access = "#{receiver}[#{argument}]"
58
- corrector.replace(node, correct_access)
57
+ correct_access(corrector, node, expression)
59
58
  end
60
59
 
61
60
  ignore_node(node)
@@ -67,6 +66,27 @@ module RuboCop
67
66
  dig_chain_enabled? &&
68
67
  (dig?(node.receiver) || dig?(node.parent))
69
68
  end
69
+
70
+ def correct_access(corrector, node, argument)
71
+ key_start = node.loc.begin || argument.source_range.begin
72
+ opening = node.receiver.source_range.end.join(key_start)
73
+ return if unsafe_opening?(opening)
74
+
75
+ corrector.replace(opening, '[')
76
+ if node.parenthesized?
77
+ corrector.replace(node.loc.end, ']')
78
+ else
79
+ corrector.insert_after(argument, ']')
80
+ end
81
+ end
82
+
83
+ def unsafe_opening?(range)
84
+ return false if range.single_line?
85
+
86
+ processed_source.tokens_within(range).any? do |token|
87
+ token.comment? || token.type == :tSTRING_END
88
+ end
89
+ end
70
90
  end
71
91
  end
72
92
  end
@@ -157,12 +157,18 @@ module RuboCop
157
157
 
158
158
  corrector.replace(if_branch.condition, chainable_condition(if_branch))
159
159
 
160
- end_range = if same_line?(node.loc.end, node.if_branch.loc.end)
161
- node.loc.end
162
- else
163
- range_by_whole_lines(node.loc.end, include_final_newline: true)
164
- end
165
- corrector.remove(end_range)
160
+ corrector.remove(redundant_end_range(node.loc.end, if_branch.loc.end))
161
+ end
162
+
163
+ def redundant_end_range(outer_end, inner_end)
164
+ return inner_end.begin.join(outer_end.begin) if same_line?(outer_end, inner_end)
165
+
166
+ [outer_end, inner_end].each do |keyword|
167
+ line = range_by_whole_lines(keyword, include_final_newline: true)
168
+ return line if line.source.strip == 'end'
169
+ end
170
+
171
+ range_with_surrounding_space(inner_end, side: :right, newlines: false)
166
172
  end
167
173
 
168
174
  def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
@@ -40,6 +40,9 @@ module RuboCop
40
40
 
41
41
  MSG = "Don't extend an instance initialized by `Struct.new`. " \
42
42
  'Use a block to customize the struct.'
43
+ LEFT_OPERAND_TYPES = %i[and or irange erange match_pattern match_pattern_p].freeze
44
+ ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn op_asgn or_asgn and_asgn].freeze
45
+ BODY_TYPES = %i[module class sclass any_def any_block when in_pattern resbody for].freeze
43
46
 
44
47
  # When the closing parenthesis of `Struct.new` is on its own line, omitting the
45
48
  # parentheses in the same pass leaves a line break before the appended `do`.
@@ -52,12 +55,13 @@ module RuboCop
52
55
  return if defines_constants?(node.body)
53
56
 
54
57
  add_offense(node.parent_class) do |corrector|
55
- corrector.remove(
58
+ keyword_range =
56
59
  range_with_surrounding_space(node.loc.keyword, side: :right, newlines: false)
57
- )
60
+ corrector.remove(keyword_range)
58
61
  corrector.replace(node.loc.operator, '=')
59
62
 
60
63
  correct_parent(node.parent_class, corrector)
64
+ parenthesize_left_operand(corrector, node, keyword_range)
61
65
  end
62
66
  end
63
67
 
@@ -82,11 +86,11 @@ module RuboCop
82
86
  if parent.block_type?
83
87
  replace_opening_brace_with_do(corrector, parent) if parent.braces?
84
88
  corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
85
- elsif (class_node = parent.parent).body.nil?
86
- corrector.remove(range_for_empty_class_body(class_node, parent))
87
- elsif unparenthesized_struct_new?(parent)
88
- wrap_unparenthesized_call_with_do(corrector, parent)
89
+ elsif (range = range_for_empty_class_body(parent))
90
+ corrector.remove(range)
91
+ parenthesize_arguments(corrector, parent) unless statement?(parent.parent)
89
92
  else
93
+ parenthesize_arguments(corrector, parent)
90
94
  corrector.insert_after(parent, ' do')
91
95
  end
92
96
  end
@@ -104,23 +108,71 @@ module RuboCop
104
108
  opening_range.last_column < opening_range.source_line.length
105
109
  end
106
110
 
107
- def range_for_empty_class_body(class_node, struct_new)
108
- if class_node.single_line?
109
- range_between(struct_new.source_range.end_pos, class_node.source_range.end_pos)
110
- else
111
- range_by_whole_lines(class_node.loc.end, include_final_newline: true)
112
- end
111
+ def range_for_empty_class_body(struct_new)
112
+ class_node = struct_new.parent
113
+ return if class_node.body
114
+
115
+ range = range_between(struct_new.source_range.end_pos, class_node.source_range.end_pos)
116
+ return range if /\A[\s;]*end\z/.match?(range.source)
117
+ return if end_line_kept?(class_node)
118
+
119
+ range_by_whole_lines(class_node.loc.end, include_final_newline: true)
120
+ end
121
+
122
+ def end_line_kept?(class_node)
123
+ return true if left_operand?(class_node)
124
+
125
+ class_node.loc.end.source_line.strip != 'end'
126
+ end
127
+
128
+ def left_operand?(class_node)
129
+ parent = class_node.parent
130
+ return false if parent.nil?
131
+ return parent.receiver.equal?(class_node) if parent.call_type?
132
+ return parent.condition.equal?(class_node) if parent.if_type? && parent.ternary?
133
+
134
+ LEFT_OPERAND_TYPES.include?(parent.type) && parent.children.first.equal?(class_node)
135
+ end
136
+
137
+ def statement?(class_node)
138
+ parent = class_node.parent
139
+ return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
140
+ return !parent.condition.equal?(class_node) if conditional_statement?(parent)
141
+
142
+ statement_child(parent).equal?(class_node)
143
+ end
144
+
145
+ def conditional_statement?(node)
146
+ return true if node.type?(:while, :until)
147
+
148
+ node.if_type? && !node.ternary?
149
+ end
150
+
151
+ def statement_child(node)
152
+ return node.expression if node.type?(*ASSIGNMENT_TYPES)
153
+ return node.else_branch if node.type?(:case, :case_match)
154
+ return if node.any_def_type? && node.endless?
155
+
156
+ node.body if node.type?(*BODY_TYPES)
157
+ end
158
+
159
+ def parenthesize_arguments(corrector, struct_new)
160
+ return unless unparenthesized_struct_new?(struct_new)
161
+
162
+ arguments_begin = struct_new.first_argument.source_range.begin
163
+ corrector.replace(struct_new.loc.selector.end.join(arguments_begin), '(')
164
+ corrector.insert_after(struct_new.last_argument, ')')
113
165
  end
114
166
 
115
167
  def unparenthesized_struct_new?(parent)
116
168
  parent.send_type? && parent.arguments.any? && !parent.parenthesized?
117
169
  end
118
170
 
119
- def wrap_unparenthesized_call_with_do(corrector, parent)
120
- args_source = parent.arguments.map(&:source).join(', ')
121
- range = parent.loc.selector.end.join(parent.source_range.end)
171
+ def parenthesize_left_operand(corrector, class_node, keyword_range)
172
+ return unless left_operand?(class_node)
122
173
 
123
- corrector.replace(range, "(#{args_source}) do")
174
+ corrector.insert_before(keyword_range, '(')
175
+ corrector.insert_after(class_node.source_range.end, ')')
124
176
  end
125
177
  end
126
178
  end
@@ -19,7 +19,6 @@ module RuboCop
19
19
  # x, y = y, x
20
20
  #
21
21
  class SwapValues < Base
22
- include RangeHelp
23
22
  extend AutoCorrector
24
23
 
25
24
  MSG = 'Replace this and assignments at lines %<x_line>d ' \
@@ -28,14 +27,16 @@ module RuboCop
28
27
  SIMPLE_ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn].to_set.freeze
29
28
 
30
29
  def on_asgn(node)
31
- return if allowed_assignment?(node)
30
+ return unless node.parent&.type?(:begin, :kwbegin)
32
31
 
33
32
  tmp_assign = node
34
33
  x_assign, y_assign = *node.right_siblings.take(2)
35
34
  return unless x_assign && y_assign && swapping_values?(tmp_assign, x_assign, y_assign)
36
35
 
37
36
  add_offense(node, message: message(x_assign, y_assign)) do |corrector|
38
- range = correction_range(tmp_assign, y_assign)
37
+ range = tmp_assign.source_range.join(y_assign.source_range)
38
+ next if contains_comment_or_heredoc?(range)
39
+
39
40
  corrector.replace(range, replacement(x_assign))
40
41
  end
41
42
  end
@@ -44,10 +45,6 @@ module RuboCop
44
45
 
45
46
  private
46
47
 
47
- def allowed_assignment?(node)
48
- node.parent&.mlhs_type? || node.parent&.shorthand_asgn?
49
- end
50
-
51
48
  def swapping_values?(tmp_assign, x_assign, y_assign)
52
49
  simple_assignment?(tmp_assign) &&
53
50
  simple_assignment?(x_assign) &&
@@ -57,6 +54,12 @@ module RuboCop
57
54
  rhs(y_assign) == lhs(tmp_assign)
58
55
  end
59
56
 
57
+ def contains_comment_or_heredoc?(range)
58
+ processed_source.tokens_within(range).any? do |token|
59
+ token.comment? || token.type == :tSTRING_END
60
+ end
61
+ end
62
+
60
63
  def simple_assignment?(node)
61
64
  return false unless node.respond_to?(:type)
62
65
 
@@ -89,12 +92,6 @@ module RuboCop
89
92
  def rhs(node)
90
93
  node.expression.source
91
94
  end
92
-
93
- def correction_range(tmp_assign, y_assign)
94
- range_by_whole_lines(
95
- range_between(tmp_assign.source_range.begin_pos, y_assign.source_range.end_pos)
96
- )
97
- end
98
95
  end
99
96
  end
100
97
  end
@@ -166,7 +166,7 @@ module RuboCop
166
166
  PATTERN
167
167
 
168
168
  def self.autocorrect_incompatible_with
169
- [Layout::SpaceBeforeBlockBraces, Style::TrailingCommaInArguments]
169
+ [Style::TrailingCommaInArguments]
170
170
  end
171
171
 
172
172
  # rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -142,10 +142,6 @@ module RuboCop
142
142
  include TrailingComma
143
143
  extend AutoCorrector
144
144
 
145
- def self.autocorrect_incompatible_with
146
- [Layout::HeredocArgumentClosingParenthesis]
147
- end
148
-
149
145
  def on_send(node)
150
146
  return unless node.arguments? && (node.parenthesized? || node.method?(:[]))
151
147
 
@@ -8,17 +8,23 @@ module CopHelper
8
8
 
9
9
  @integrated_plugins = false
10
10
 
11
+ # Whether the specs run on one of the Prism parser engines.
12
+ def self.prism_engine?
13
+ %w[parser_prism prism].include?(ENV.fetch('PARSER_ENGINE', nil))
14
+ end
15
+
11
16
  class << self
12
17
  attr_accessor :integrated_plugins
13
18
  end
14
19
 
15
20
  let(:ruby_version) do
16
21
  # The minimum version Prism can parse is 3.3.
17
- ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
22
+ CopHelper.prism_engine? ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
18
23
  end
19
24
  let(:parser_engine) do
25
+ engine = ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym
20
26
  # The maximum version Parser can correctly parse is 3.3.
21
- ruby_version >= 3.4 ? :parser_prism : ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym
27
+ engine == :parser_whitequark && ruby_version >= 3.4 ? :parser_prism : engine
22
28
  end
23
29
  let(:rails_version) { false }
24
30
 
@@ -249,57 +249,57 @@ end
249
249
 
250
250
  RSpec.shared_context 'ruby 2.0' do
251
251
  # Prism supports parsing Ruby 3.3+.
252
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.0 }
252
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.0 }
253
253
  end
254
254
 
255
255
  RSpec.shared_context 'ruby 2.1' do
256
256
  # Prism supports parsing Ruby 3.3+.
257
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.1 }
257
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.1 }
258
258
  end
259
259
 
260
260
  RSpec.shared_context 'ruby 2.2' do
261
261
  # Prism supports parsing Ruby 3.3+.
262
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.2 }
262
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.2 }
263
263
  end
264
264
 
265
265
  RSpec.shared_context 'ruby 2.3' do
266
266
  # Prism supports parsing Ruby 3.3+.
267
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.3 }
267
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.3 }
268
268
  end
269
269
 
270
270
  RSpec.shared_context 'ruby 2.4' do
271
271
  # Prism supports parsing Ruby 3.3+.
272
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.4 }
272
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.4 }
273
273
  end
274
274
 
275
275
  RSpec.shared_context 'ruby 2.5' do
276
276
  # Prism supports parsing Ruby 3.3+.
277
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.5 }
277
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.5 }
278
278
  end
279
279
 
280
280
  RSpec.shared_context 'ruby 2.6' do
281
281
  # Prism supports parsing Ruby 3.3+.
282
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.6 }
282
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.6 }
283
283
  end
284
284
 
285
285
  RSpec.shared_context 'ruby 2.7' do
286
286
  # Prism supports parsing Ruby 3.3+.
287
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.7 }
287
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.7 }
288
288
  end
289
289
 
290
290
  RSpec.shared_context 'ruby 3.0' do
291
291
  # Prism supports parsing Ruby 3.3+.
292
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 3.0 }
292
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 3.0 }
293
293
  end
294
294
 
295
295
  RSpec.shared_context 'ruby 3.1' do
296
296
  # Prism supports parsing Ruby 3.3+.
297
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 3.1 }
297
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 3.1 }
298
298
  end
299
299
 
300
300
  RSpec.shared_context 'ruby 3.2' do
301
301
  # Prism supports parsing Ruby 3.3+.
302
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 3.2 }
302
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 3.2 }
303
303
  end
304
304
 
305
305
  RSpec.shared_context 'ruby 3.3' do
@@ -347,20 +347,29 @@ module RuboCop
347
347
  end
348
348
 
349
349
  def add_redundant_disables(file, offenses, source)
350
- team_for_redundant_disables(file, offenses, source) do |team|
351
- new_offenses, redundant_updated = inspect_file(source, team)
352
- offenses += new_offenses
353
- if redundant_updated
350
+ seen_sources = Set[source.raw_source]
351
+
352
+ loop do
353
+ redundant_updated = false
354
+
355
+ team_for_redundant_disables(file, offenses, source) do |team|
356
+ new_offenses, redundant_updated = inspect_file(source, team)
357
+ offenses += new_offenses
358
+ next unless redundant_updated
359
+
354
360
  # Do one extra inspection loop if any redundant disables were
355
361
  # removed. This is done in order to find rubocop:enable directives that
356
362
  # have now become useless. It finds the offenses that are left again,
357
363
  # located in the corrected source, so only the corrected ones are kept
358
364
  # from before, as `inspect_and_correct` does.
359
- _source, new_offenses = do_inspection_loop(file, team.updated_source)
365
+ source, new_offenses = do_inspection_loop(file, team.updated_source)
360
366
  offenses = offenses.select(&:corrected?) | new_offenses
367
+
368
+ redundant_updated = seen_sources.add?(source.raw_source)
361
369
  end
370
+
371
+ return offenses unless redundant_updated
362
372
  end
363
- offenses
364
373
  end
365
374
 
366
375
  def team_for_redundant_disables(file, offenses, source)
@@ -729,7 +738,7 @@ module RuboCop
729
738
 
730
739
  # rubocop:disable-next Metrics/MethodLength
731
740
  def get_processed_source(file, prism_result, source: nil)
732
- config = @config_store.for_file(file)
741
+ config = @config_store.for_file(file).validate_parser_engine
733
742
  ruby_version = config.target_ruby_version
734
743
  parser_engine = config.parser_engine
735
744
 
@@ -120,14 +120,12 @@ module RuboCop
120
120
  end
121
121
 
122
122
  def version_from_gemspec_file(file)
123
- # When using parser_prism, we need to use a Ruby version that Prism supports (3.3+)
124
- # for parsing the gemspec file. This doesn't affect the detected Ruby version,
125
- # it's just for the parsing step.
126
- ruby_version_for_parsing = if @config.parser_engine == :parser_prism
127
- 3.3
128
- else
129
- DEFAULT_VERSION
130
- end
123
+ # The version is only for parsing the gemspec, it doesn't affect the
124
+ # detected Ruby version. Prism parses Ruby 3.3 and later, and it is
125
+ # what the default engine picks for such a version, so `parser` is
126
+ # only needed here when it was asked for.
127
+ whitequark = @config.parser_engine == :parser_whitequark
128
+ ruby_version_for_parsing = whitequark ? DEFAULT_VERSION : 3.3
131
129
 
132
130
  processed_source = ProcessedSource.from_file(
133
131
  file, ruby_version_for_parsing, parser_engine: @config.parser_engine
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.92.3'
6
+ STRING = '1.93.0'
7
7
 
8
8
  MSG = '%<version>s (using %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
@@ -60,14 +60,24 @@ module RuboCop
60
60
  yaml = Util.silence_warnings do
61
61
  ConfigLoader.load_yaml_configuration(config_path)
62
62
  end
63
- parser_engine = yaml.dig('AllCops', 'ParserEngine')
64
- parser_engine_text = ", #{parser_engine}" if parser_engine
63
+ # Without the `parser` gem its version is that of the copy of its source
64
+ # layer in RuboCop AST, which is not what parses the code.
65
+ parser = "Parser #{Parser::VERSION}" if ProcessedSource.parser_gem_available?
66
+ prism = "Prism #{Prism::VERSION}"
67
+ return parser || prism if target_ruby_version < MINIMUM_PARSABLE_PRISM_VERSION
65
68
 
66
- if target_ruby_version >= MINIMUM_PARSABLE_PRISM_VERSION
67
- "Parser #{Parser::VERSION}, Prism #{Prism::VERSION}#{parser_engine_text}"
68
- else
69
- "Parser #{Parser::VERSION}"
70
- end
69
+ parser_engine = parser_engine(yaml.dig('AllCops', 'ParserEngine'), target_ruby_version)
70
+
71
+ [parser, prism, parser_engine].compact.join(', ')
72
+ end
73
+
74
+ # The default parser engine depends on the target Ruby version and on the
75
+ # version of Prism, so the one that is used is asked from RuboCop AST.
76
+ # @api private
77
+ def self.parser_engine(configured, target_ruby_version)
78
+ return configured unless configured.nil? || configured.to_s == 'default'
79
+
80
+ ProcessedSource.default_parser_engine(target_ruby_version)
71
81
  end
72
82
 
73
83
  # @api private