rubocop 1.92.2 → 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.
- checksums.yaml +4 -4
- data/lib/rubocop/#directive_comment.rb# +224 -0
- data/lib/rubocop/cache_config.rb +3 -0
- data/lib/rubocop/cli.rb +5 -4
- data/lib/rubocop/config.rb +16 -0
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
- data/lib/rubocop/cop/layout/class_structure.rb +91 -13
- data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
- data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +14 -6
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
- data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
- data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
- data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
- data/lib/rubocop/cop/lint/useless_times.rb +13 -1
- data/lib/rubocop/cop/lint/void.rb +20 -9
- data/lib/rubocop/cop/message_annotator.rb +6 -8
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
- data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
- data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
- data/lib/rubocop/cop/mixin/range_help.rb +13 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
- data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/and_or.rb +8 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
- data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
- data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
- data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +16 -4
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
- data/lib/rubocop/cop/style/endless_method.rb +30 -10
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/for.rb +59 -4
- data/lib/rubocop/cop/style/guard_clause.rb +56 -11
- data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
- data/lib/rubocop/cop/style/hash_lookup_method.rb +11 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
- data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
- data/lib/rubocop/cop/style/lambda.rb +13 -5
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_into_array.rb +4 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
- data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +96 -16
- data/lib/rubocop/cop/style/redundant_each.rb +8 -1
- data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
- data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
- data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
- data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- data/lib/rubocop/cop/style/word_array.rb +1 -3
- data/lib/rubocop/cop/util.rb +2 -0
- data/lib/rubocop/cop/variable_force.rb +14 -1
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +13 -6
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/runner.rb +5 -0
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +1 -1
- metadata +4 -3
|
@@ -109,10 +109,14 @@ module RuboCop
|
|
|
109
109
|
# end
|
|
110
110
|
class IdenticalConditionalBranches < Base
|
|
111
111
|
include RangeHelp
|
|
112
|
+
include RescueNode
|
|
112
113
|
extend AutoCorrector
|
|
113
114
|
|
|
114
115
|
MSG = 'Move `%<source>s` out of the conditional.'
|
|
115
116
|
|
|
117
|
+
STATEMENT_BODY_TYPES = %i[kwbegin block numblock itblock ensure].freeze
|
|
118
|
+
VOID_VALUE_TYPES = %i[return break next redo retry].freeze
|
|
119
|
+
|
|
116
120
|
def on_if(node)
|
|
117
121
|
return if node.elsif?
|
|
118
122
|
|
|
@@ -193,10 +197,12 @@ module RuboCop
|
|
|
193
197
|
return if expressions.any?(&:nil?)
|
|
194
198
|
|
|
195
199
|
inserted_expression = false
|
|
200
|
+
hoistable = hoistable?(node)
|
|
196
201
|
|
|
197
202
|
expressions.each do |expression|
|
|
198
203
|
add_offense(expression) do |corrector|
|
|
199
|
-
next
|
|
204
|
+
next unless hoistable
|
|
205
|
+
next if void_value_position?(node, expression, insert_position)
|
|
200
206
|
|
|
201
207
|
corrector.remove(removal_range(expression))
|
|
202
208
|
next if inserted_expression
|
|
@@ -212,6 +218,77 @@ module RuboCop
|
|
|
212
218
|
end
|
|
213
219
|
end
|
|
214
220
|
|
|
221
|
+
def hoistable?(node)
|
|
222
|
+
return false if node.if_type? && (node.ternary? || node.then?)
|
|
223
|
+
|
|
224
|
+
statement?(node.parent&.assignment? ? node.parent : node)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def statement?(node)
|
|
228
|
+
parent = node.parent
|
|
229
|
+
return true if parent.nil? || parent.type?(*STATEMENT_BODY_TYPES)
|
|
230
|
+
return false if inline_form?(parent)
|
|
231
|
+
|
|
232
|
+
statement_branch?(node, parent)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def inline_form?(node)
|
|
236
|
+
case node.type
|
|
237
|
+
when :def, :defs
|
|
238
|
+
node.endless?
|
|
239
|
+
when :if, :while, :until
|
|
240
|
+
node.modifier_form?
|
|
241
|
+
when :rescue
|
|
242
|
+
rescue_modifier?(node.resbody_branches.first)
|
|
243
|
+
when :resbody
|
|
244
|
+
rescue_modifier?(node)
|
|
245
|
+
else
|
|
246
|
+
false
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def statement_branch?(node, parent)
|
|
251
|
+
case parent.type
|
|
252
|
+
when :begin, :def, :defs, :rescue, :resbody
|
|
253
|
+
true
|
|
254
|
+
when :if
|
|
255
|
+
!parent.ternary? && !node.equal?(parent.condition)
|
|
256
|
+
when :class, :module, :sclass, :when, :in_pattern, :for, :while, :until
|
|
257
|
+
node.equal?(parent.body)
|
|
258
|
+
when :case, :case_match
|
|
259
|
+
node.equal?(parent.else_branch)
|
|
260
|
+
else
|
|
261
|
+
false
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def void_value_position?(node, expression, insert_position)
|
|
266
|
+
return false if insert_position != :after_condition
|
|
267
|
+
return false unless value_position?(node)
|
|
268
|
+
|
|
269
|
+
jump?(expression)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def value_position?(node)
|
|
273
|
+
parent = node.parent
|
|
274
|
+
return false unless parent
|
|
275
|
+
return true if parent.assignment?
|
|
276
|
+
return false unless explicit_begin?(parent)
|
|
277
|
+
|
|
278
|
+
node.value_used?
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def explicit_begin?(node)
|
|
282
|
+
node.kwbegin_type? || (node.begin_type? && !node.loc.begin.nil?)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def jump?(node)
|
|
286
|
+
return true if node.type?(*VOID_VALUE_TYPES)
|
|
287
|
+
return false if node.type?(:any_block, :any_def)
|
|
288
|
+
|
|
289
|
+
node.each_child_node.any? { |child| jump?(child) }
|
|
290
|
+
end
|
|
291
|
+
|
|
215
292
|
def removal_range(expression)
|
|
216
293
|
range = expression.source_range
|
|
217
294
|
whole_lines = range_by_whole_lines(range, include_final_newline: true)
|
|
@@ -88,7 +88,7 @@ module RuboCop
|
|
|
88
88
|
MSG_USE_NORMAL = 'Modifier form of `%<keyword>s` makes the line too long.'
|
|
89
89
|
|
|
90
90
|
def self.autocorrect_incompatible_with
|
|
91
|
-
[Style::Next, Style::SoleNestedConditional]
|
|
91
|
+
[Style::MethodCallWithArgsParentheses, Style::Next, Style::SoleNestedConditional]
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
@@ -29,7 +29,7 @@ module RuboCop
|
|
|
29
29
|
MSG = 'Avoid modifier `%<keyword>s` after another conditional.'
|
|
30
30
|
|
|
31
31
|
def self.autocorrect_incompatible_with
|
|
32
|
-
[Style::SoleNestedConditional]
|
|
32
|
+
[Style::MethodCallWithArgsParentheses, Style::SoleNestedConditional]
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
# rubocop:disable-next Metrics/AbcSize
|
|
@@ -126,8 +126,9 @@ module RuboCop
|
|
|
126
126
|
def lower_precedence_than_ternary?(node)
|
|
127
127
|
return true if node.type?(*LOWER_PRECEDENCE_THAN_TERNARY_TYPES)
|
|
128
128
|
return true if node.send_type? && (node.prefix_not? || prefix_bang_on_command?(node))
|
|
129
|
+
return true if command_assignment?(node)
|
|
129
130
|
|
|
130
|
-
|
|
131
|
+
keyword_command?(node)
|
|
131
132
|
end
|
|
132
133
|
|
|
133
134
|
def operand_of_parent?(node)
|
|
@@ -174,8 +175,37 @@ module RuboCop
|
|
|
174
175
|
require_argument_parentheses?(receiver) || lower_precedence_than_ternary?(receiver)
|
|
175
176
|
end
|
|
176
177
|
|
|
178
|
+
def command_assignment?(node)
|
|
179
|
+
return false unless node.assignment?
|
|
180
|
+
|
|
181
|
+
value = node.call_type? ? node.last_argument : node.expression
|
|
182
|
+
return true if value.array_type? && !value.bracketed?
|
|
183
|
+
|
|
184
|
+
value = value.body if value.rescue_type?
|
|
185
|
+
|
|
186
|
+
command_value?(value)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def command_value?(node)
|
|
190
|
+
node = node.send_node if node.any_block_type?
|
|
191
|
+
return true if command_assignment?(node)
|
|
192
|
+
return false if node.defined_type?
|
|
193
|
+
return true if keyword_command?(node)
|
|
194
|
+
return false if binary_operator_call?(node)
|
|
195
|
+
|
|
196
|
+
require_argument_parentheses?(node)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def keyword_command?(node)
|
|
200
|
+
node.type?(:yield, :super, :defined?) && node.arguments.any? && !node.parenthesized?
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def binary_operator_call?(node)
|
|
204
|
+
node.call_type? && node.binary_operation? && !node.loc.dot
|
|
205
|
+
end
|
|
206
|
+
|
|
177
207
|
def require_argument_parentheses?(node)
|
|
178
|
-
return false if !node.call_type? || node.arithmetic_operation?
|
|
208
|
+
return false if !node.call_type? || node.arithmetic_operation? || node.setter_method?
|
|
179
209
|
|
|
180
210
|
!node.parenthesized? && node.arguments.any? && !node.method?(:[]) && !node.method?(:[]=)
|
|
181
211
|
end
|
|
@@ -86,7 +86,7 @@ module RuboCop
|
|
|
86
86
|
find_block_variables(node, block_argument_name).each do |variable|
|
|
87
87
|
add_offense(variable, message: MSG_USE_IT_PARAMETER) do |corrector|
|
|
88
88
|
corrector.remove(node.arguments)
|
|
89
|
-
corrector
|
|
89
|
+
replace_with_it(corrector, variable)
|
|
90
90
|
end
|
|
91
91
|
end
|
|
92
92
|
end
|
|
@@ -131,6 +131,16 @@ module RuboCop
|
|
|
131
131
|
end
|
|
132
132
|
end
|
|
133
133
|
|
|
134
|
+
def replace_with_it(corrector, variable)
|
|
135
|
+
parent = variable.parent
|
|
136
|
+
|
|
137
|
+
if parent.pair_type? && parent.value_omission?
|
|
138
|
+
corrector.replace(parent.loc.operator, ': it')
|
|
139
|
+
else
|
|
140
|
+
corrector.replace(variable, 'it')
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
134
144
|
def it_variable_in_scope?(node)
|
|
135
145
|
scope = nearest_scope(node)
|
|
136
146
|
return true if it_argument_in_scope?(node, scope)
|
|
@@ -76,11 +76,9 @@ module RuboCop
|
|
|
76
76
|
return if uncorrectable_lambda_literal?(node)
|
|
77
77
|
|
|
78
78
|
add_offense(node.send_node, message: message(node, selector)) do |corrector|
|
|
79
|
-
if node.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
autocorrect_method_to_literal(corrector, node)
|
|
83
|
-
end
|
|
79
|
+
next if node.each_ancestor.any? { |ancestor| ignored_node?(ancestor) }
|
|
80
|
+
|
|
81
|
+
autocorrect(corrector, node)
|
|
84
82
|
end
|
|
85
83
|
end
|
|
86
84
|
alias on_numblock on_block
|
|
@@ -124,6 +122,16 @@ module RuboCop
|
|
|
124
122
|
end
|
|
125
123
|
end
|
|
126
124
|
|
|
125
|
+
def autocorrect(corrector, node)
|
|
126
|
+
if node.send_node.lambda_literal?
|
|
127
|
+
LambdaLiteralToMethodCorrector.new(node).call(corrector)
|
|
128
|
+
else
|
|
129
|
+
autocorrect_method_to_literal(corrector, node)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
ignore_node(node.arguments) if node.arguments?
|
|
133
|
+
end
|
|
134
|
+
|
|
127
135
|
def autocorrect_method_to_literal(corrector, node)
|
|
128
136
|
corrector.replace(node.send_node, '->')
|
|
129
137
|
|
|
@@ -63,6 +63,10 @@ module RuboCop
|
|
|
63
63
|
|
|
64
64
|
MSG = 'Use `%<new_method_name>s` instead of `each` to map elements into an array.'
|
|
65
65
|
|
|
66
|
+
def self.autocorrect_incompatible_with
|
|
67
|
+
[Style::TrailingCommaInArguments]
|
|
68
|
+
end
|
|
69
|
+
|
|
66
70
|
# @!method suitable_argument_node?(node)
|
|
67
71
|
def_node_matcher :suitable_argument_node?, <<-PATTERN
|
|
68
72
|
!{splat forwarded-restarg forwarded-args (hash (forwarded-kwrestarg)) block-pass}
|
|
@@ -298,9 +298,8 @@ module RuboCop
|
|
|
298
298
|
parent.assignment? && (grandparent.conditional? || grandparent.when_type?)
|
|
299
299
|
end
|
|
300
300
|
|
|
301
|
-
# rubocop:disable-next Metrics/PerceivedComplexity
|
|
302
301
|
def command_assignment_in_expression?(node)
|
|
303
|
-
value = node
|
|
302
|
+
value = assigned_value(node)
|
|
304
303
|
assignment = outermost_assignment(value)
|
|
305
304
|
return false if assignment.equal?(value)
|
|
306
305
|
|
|
@@ -308,9 +307,18 @@ module RuboCop
|
|
|
308
307
|
call_in_logical_operators?(assignment) ||
|
|
309
308
|
call_in_optional_arguments?(assignment) ||
|
|
310
309
|
call_as_argument_or_chain?(assignment) ||
|
|
310
|
+
call_in_match_pattern?(assignment) ||
|
|
311
311
|
assignment.parent&.type?(:masgn, :return, :next, :break, :defined?)
|
|
312
312
|
end
|
|
313
313
|
|
|
314
|
+
def assigned_value(node)
|
|
315
|
+
parent = node.parent
|
|
316
|
+
return parent if parent&.rescue_type?
|
|
317
|
+
return parent if parent&.any_block_type? && parent.send_node.equal?(node)
|
|
318
|
+
|
|
319
|
+
node
|
|
320
|
+
end
|
|
321
|
+
|
|
314
322
|
def outermost_assignment(value)
|
|
315
323
|
parent = value.parent
|
|
316
324
|
return value if !parent&.assignment? || parent.masgn_type?
|
|
@@ -29,9 +29,47 @@ module RuboCop
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def eligible_for_parentheses_omission?(node)
|
|
32
|
+
return true if do_end_block_in_command_arguments?(node)
|
|
33
|
+
|
|
32
34
|
node.operator_method? || node.setter_method? || ignored_macro?(node)
|
|
33
35
|
end
|
|
34
36
|
|
|
37
|
+
def do_end_block_in_command_arguments?(node)
|
|
38
|
+
return false unless node.block_node&.keywords?
|
|
39
|
+
|
|
40
|
+
child = node.block_node
|
|
41
|
+
child.each_ancestor do |ancestor|
|
|
42
|
+
return !ancestor.parenthesized? if command_argument?(ancestor, child)
|
|
43
|
+
return false if command_arguments_boundary?(ancestor, child)
|
|
44
|
+
|
|
45
|
+
child = ancestor
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
false
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def command_argument?(node, child)
|
|
52
|
+
return false unless node.type?(:call, :super, :yield)
|
|
53
|
+
return false if node.operator_method? || node.setter_method?
|
|
54
|
+
|
|
55
|
+
node.arguments.any? { |argument| argument.equal?(child) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def command_arguments_boundary?(node, child)
|
|
59
|
+
return true if node.type?(:any_block, :any_def, :class, :module, :sclass, :kwbegin)
|
|
60
|
+
return true if node.type?(:break, :next, :return) || index_argument?(node, child)
|
|
61
|
+
|
|
62
|
+
node.type?(:begin, :array, :hash) && node.loc?(:begin)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def index_argument?(node, child)
|
|
66
|
+
return false unless node.call_type?
|
|
67
|
+
return node.arguments.any? { |argument| argument.equal?(child) } if node.method?(:[])
|
|
68
|
+
return false unless node.method?(:[]=)
|
|
69
|
+
|
|
70
|
+
node.arguments[0...-1].any? { |argument| argument.equal?(child) }
|
|
71
|
+
end
|
|
72
|
+
|
|
35
73
|
def included_macros_list
|
|
36
74
|
@included_macros_list ||= cop_config.fetch('IncludedMacros', []).map(&:to_sym).freeze
|
|
37
75
|
end
|
|
@@ -245,6 +245,7 @@ module RuboCop
|
|
|
245
245
|
[
|
|
246
246
|
Layout::HeredocArgumentClosingParenthesis,
|
|
247
247
|
Style::NestedParenthesizedCalls,
|
|
248
|
+
Style::ParenthesesAroundCondition,
|
|
248
249
|
Style::RedundantParentheses,
|
|
249
250
|
Style::RescueModifier,
|
|
250
251
|
Style::TernaryParentheses,
|
|
@@ -278,7 +279,8 @@ module RuboCop
|
|
|
278
279
|
first_node = node.first_argument
|
|
279
280
|
return false unless first_node.begin_type? && first_node.parenthesized_call?
|
|
280
281
|
|
|
281
|
-
first_node.children.one? && mergeable_parentheses?(first_node.children.first)
|
|
282
|
+
first_node.children.one? && mergeable_parentheses?(first_node.children.first) &&
|
|
283
|
+
!semicolon_around_grouped_expression?(first_node)
|
|
282
284
|
end
|
|
283
285
|
|
|
284
286
|
def mergeable_parentheses?(node)
|
|
@@ -295,6 +297,14 @@ module RuboCop
|
|
|
295
297
|
true
|
|
296
298
|
end
|
|
297
299
|
end
|
|
300
|
+
|
|
301
|
+
def semicolon_around_grouped_expression?(node)
|
|
302
|
+
expression = node.children.first.source_range
|
|
303
|
+
|
|
304
|
+
processed_source.tokens_within(node).any? do |token|
|
|
305
|
+
token.semicolon? && !expression.contains?(token.pos)
|
|
306
|
+
end
|
|
307
|
+
end
|
|
298
308
|
end
|
|
299
309
|
end
|
|
300
310
|
end
|
|
@@ -376,7 +376,7 @@ module RuboCop
|
|
|
376
376
|
(send (const {nil? cbase} :ENV) :[] _)
|
|
377
377
|
(or (send (const {nil? cbase} :ENV) :[] _) _)
|
|
378
378
|
(send _ {:count :length :size} ...)
|
|
379
|
-
(
|
|
379
|
+
(any_block (send _ {:count :length :size} ...) ...)
|
|
380
380
|
}
|
|
381
381
|
PATTERN
|
|
382
382
|
|
|
@@ -64,13 +64,25 @@ module RuboCop
|
|
|
64
64
|
private
|
|
65
65
|
|
|
66
66
|
def autocorrect(corrector, node)
|
|
67
|
-
|
|
68
|
-
range_with_surrounding_space(node.body.source_range)
|
|
69
|
-
else
|
|
70
|
-
range_by_whole_lines(node.body.source_range, include_final_newline: true)
|
|
71
|
-
end
|
|
67
|
+
body_range = node.body.source_range
|
|
72
68
|
|
|
73
|
-
|
|
69
|
+
if node.single_line?
|
|
70
|
+
corrector.replace(range_with_surrounding_space(body_range), node.braces? ? '' : ' ')
|
|
71
|
+
elsif own_lines?(body_range)
|
|
72
|
+
corrector.remove(range_by_whole_lines(body_range, include_final_newline: true))
|
|
73
|
+
else
|
|
74
|
+
corrector.remove(body_range_on_shared_line(body_range))
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def own_lines?(range)
|
|
79
|
+
range_by_whole_lines(range).source.strip == range.source
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def body_range_on_shared_line(body_range)
|
|
83
|
+
side = begins_its_line?(body_range) ? :right : :left
|
|
84
|
+
|
|
85
|
+
range_with_surrounding_space(body_range, side: side, newlines: false)
|
|
74
86
|
end
|
|
75
87
|
end
|
|
76
88
|
end
|
|
@@ -154,7 +154,7 @@ module RuboCop
|
|
|
154
154
|
|
|
155
155
|
def autocorrect_unless_nil(corrector, node, receiver)
|
|
156
156
|
corrector.replace(node.parent.loc.keyword, 'if')
|
|
157
|
-
corrector.replace(node, receiver.source)
|
|
157
|
+
corrector.replace(node, receiver ? receiver.source : 'self')
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
def nil_comparison_style
|
|
@@ -5,7 +5,10 @@ module RuboCop
|
|
|
5
5
|
module Style
|
|
6
6
|
# Checks for redundant dot before operator method call.
|
|
7
7
|
# The target operator methods are `|`, `^`, `&`, ``<=>``, `==`, `===`, `=~`, `>`, `>=`, `<`,
|
|
8
|
-
# ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`,
|
|
8
|
+
# ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `!=`, and `!~`.
|
|
9
|
+
#
|
|
10
|
+
# `!` and `~` are not targeted because they have no binary operator form,
|
|
11
|
+
# so `foo.! bar` cannot be written as `foo ! bar`, which means `foo(!bar)`.
|
|
9
12
|
#
|
|
10
13
|
# @example
|
|
11
14
|
#
|
|
@@ -21,7 +24,7 @@ module RuboCop
|
|
|
21
24
|
extend AutoCorrector
|
|
22
25
|
|
|
23
26
|
MSG = 'Redundant dot detected.'
|
|
24
|
-
RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % **
|
|
27
|
+
RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** != !~].freeze
|
|
25
28
|
INVALID_SYNTAX_ARG_TYPES = %i[
|
|
26
29
|
splat kwsplat forwarded_args forwarded_restarg forwarded_kwrestarg block_pass
|
|
27
30
|
].freeze
|
|
@@ -29,8 +32,7 @@ module RuboCop
|
|
|
29
32
|
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
30
33
|
def on_send(node)
|
|
31
34
|
return unless (dot = node.loc.dot)
|
|
32
|
-
return if
|
|
33
|
-
return if node.receiver.const_type? || !node.arguments.one?
|
|
35
|
+
return if node.receiver.const_type? || !node.arguments.one? || node.block_literal?
|
|
34
36
|
|
|
35
37
|
return unless (rhs = node.first_argument)
|
|
36
38
|
return if method_call_with_parenthesized_arg?(rhs)
|
|
@@ -47,14 +49,6 @@ module RuboCop
|
|
|
47
49
|
|
|
48
50
|
private
|
|
49
51
|
|
|
50
|
-
# `foo.~@` and `foo.!@` call the method `~` and `!` respectively. While those
|
|
51
|
-
# are operator methods, we don't want to actually consider them as such.
|
|
52
|
-
def unary_method_no_operator?(node)
|
|
53
|
-
return false unless node.nonmutating_unary_operator_method?
|
|
54
|
-
|
|
55
|
-
node.method_name.to_s != node.selector.source
|
|
56
|
-
end
|
|
57
|
-
|
|
58
52
|
# Checks for an acceptable case of `foo.+(bar).baz`.
|
|
59
53
|
def method_call_with_parenthesized_arg?(argument)
|
|
60
54
|
return false unless argument.parent.parent&.send_type?
|
|
@@ -69,13 +63,12 @@ module RuboCop
|
|
|
69
63
|
end
|
|
70
64
|
|
|
71
65
|
def wrap_in_parentheses_if_chained(corrector, node)
|
|
72
|
-
return unless node
|
|
73
|
-
return if node.parent.first_argument == node
|
|
74
|
-
|
|
75
|
-
operator = node.loc.selector
|
|
66
|
+
return unless chained?(node)
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
if node.parenthesized?
|
|
69
|
+
ParenthesesCorrector.correct(corrector, node)
|
|
70
|
+
corrector.insert_after(node.loc.selector, ' ')
|
|
71
|
+
end
|
|
79
72
|
corrector.wrap(node, '(', ')')
|
|
80
73
|
end
|
|
81
74
|
|
|
@@ -84,14 +77,20 @@ module RuboCop
|
|
|
84
77
|
selector = node.loc.selector
|
|
85
78
|
|
|
86
79
|
return true if selector.end_pos == rhs.source_range.begin_pos
|
|
87
|
-
return false if node
|
|
80
|
+
return false if chained?(node) # if chained, a space is already added
|
|
81
|
+
|
|
82
|
+
# Without a space, `foo +(bar)` is parsed as `foo(+(bar))` and `foo /(bar)` starts
|
|
83
|
+
# a regexp literal.
|
|
84
|
+
node.parenthesized?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def chained?(node)
|
|
88
|
+
parent = node.parent
|
|
89
|
+
return false unless parent&.call_type?
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
# add one to avoid a syntax error.
|
|
91
|
-
range = selector.end.join(rhs.source_range.begin)
|
|
92
|
-
return true if node.method?(:/) && range.source == '('
|
|
91
|
+
return true unless parent.first_argument.equal?(node)
|
|
93
92
|
|
|
94
|
-
|
|
93
|
+
parent.binary_operation? && !parent.method?(:[])
|
|
95
94
|
end
|
|
96
95
|
end
|
|
97
96
|
end
|
|
@@ -56,7 +56,6 @@ module RuboCop
|
|
|
56
56
|
class ParenthesesAroundCondition < Base
|
|
57
57
|
include SafeAssignment
|
|
58
58
|
include Parentheses
|
|
59
|
-
include RangeHelp
|
|
60
59
|
extend AutoCorrector
|
|
61
60
|
|
|
62
61
|
def on_if(node)
|
|
@@ -82,7 +81,8 @@ module RuboCop
|
|
|
82
81
|
|
|
83
82
|
control_op_condition(cond) do |first_child, rest_children|
|
|
84
83
|
return if require_parentheses?(node, first_child)
|
|
85
|
-
|
|
84
|
+
# `(foo; bar)` and `(foo\n bar)` would leave only `foo` as the condition.
|
|
85
|
+
return if rest_children.any?
|
|
86
86
|
return if modifier_op?(first_child) || first_child.masgn_type?
|
|
87
87
|
return if command_call_assignment?(first_child)
|
|
88
88
|
return if parens_allowed?(cond)
|
|
@@ -101,14 +101,6 @@ module RuboCop
|
|
|
101
101
|
condition_body.send_node.block_literal? && condition_body.keywords?
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
-
def semicolon_separated_expressions?(first_exp, rest_exps)
|
|
105
|
-
return false unless (second_exp = rest_exps.first)
|
|
106
|
-
|
|
107
|
-
range = range_between(first_exp.source_range.end_pos, second_exp.source_range.begin_pos)
|
|
108
|
-
|
|
109
|
-
range.source.include?(';')
|
|
110
|
-
end
|
|
111
|
-
|
|
112
104
|
def modifier_op?(node)
|
|
113
105
|
return false if node.if_type? && node.ternary?
|
|
114
106
|
return true if node.rescue_type?
|
|
@@ -220,8 +220,7 @@ module RuboCop
|
|
|
220
220
|
replacement = "#{select_var}, #{reject_var} = #{partition_call}"
|
|
221
221
|
|
|
222
222
|
corrector.replace(sibling_container, replacement)
|
|
223
|
-
|
|
224
|
-
corrector.remove(range)
|
|
223
|
+
corrector.remove(range_with_surrounding_space(container.source_range, side: :left))
|
|
225
224
|
end
|
|
226
225
|
|
|
227
226
|
def complementary_variable_order(sibling, container, sibling_container)
|
|
@@ -109,6 +109,9 @@ module RuboCop
|
|
|
109
109
|
# @private
|
|
110
110
|
# @param [RuboCop::AST::Node] node
|
|
111
111
|
def on_back_ref_or_gvar_or_nth_ref(node)
|
|
112
|
+
# `alias $MATCH $&` names the variables, it does not read them.
|
|
113
|
+
return if node.parent&.alias_type?
|
|
114
|
+
|
|
112
115
|
preferred_expression = preferred_expression_to_node_with_constant_prefix(node)
|
|
113
116
|
return unless preferred_expression
|
|
114
117
|
|
|
@@ -67,8 +67,11 @@ module RuboCop
|
|
|
67
67
|
private
|
|
68
68
|
|
|
69
69
|
def register_offense(range, node, replacement)
|
|
70
|
+
replacement = replacement.source_range if replacement.is_a?(AST::Node)
|
|
71
|
+
|
|
70
72
|
add_offense(range) do |corrector|
|
|
71
|
-
corrector.
|
|
73
|
+
corrector.remove(node.source_range.begin.join(replacement.begin))
|
|
74
|
+
corrector.remove(replacement.end.join(node.source_range.end))
|
|
72
75
|
end
|
|
73
76
|
end
|
|
74
77
|
end
|