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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +5 -1
- data/lib/rubocop/config.rb +6 -0
- data/lib/rubocop/config_store.rb +1 -0
- data/lib/rubocop/config_validator.rb +45 -1
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +25 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
- data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
- data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +16 -4
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +25 -7
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +14 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +47 -9
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +11 -3
- data/lib/rubocop/cop/lint/number_conversion.rb +10 -0
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +45 -1
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
- data/lib/rubocop/cop/lint/useless_times.rb +40 -12
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +16 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/naming/block_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
- data/lib/rubocop/cop/style/array_coercion.rb +1 -1
- data/lib/rubocop/cop/style/array_intersect.rb +15 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +0 -1
- data/lib/rubocop/cop/style/data_inheritance.rb +78 -9
- data/lib/rubocop/cop/style/dig_chain.rb +13 -2
- data/lib/rubocop/cop/style/each_with_object.rb +21 -5
- data/lib/rubocop/cop/style/empty_class_definition.rb +19 -1
- data/lib/rubocop/cop/style/empty_method.rb +11 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
- data/lib/rubocop/cop/style/file_read.rb +5 -1
- data/lib/rubocop/cop/style/file_write.rb +89 -20
- data/lib/rubocop/cop/style/hash_lookup_method.rb +2 -4
- data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
- data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +4 -11
- data/lib/rubocop/cop/style/non_nil_check.rb +13 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +14 -0
- data/lib/rubocop/cop/style/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +14 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +26 -6
- data/lib/rubocop/cop/style/redundant_each.rb +18 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
- data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -1
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
- data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
- data/lib/rubocop/cop/style/select_by_range.rb +1 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
- data/lib/rubocop/cop/style/self_assignment.rb +0 -4
- data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
- data/lib/rubocop/cop/style/struct_inheritance.rb +68 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +16 -7
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -6
|
@@ -82,6 +82,13 @@ module RuboCop
|
|
|
82
82
|
# 1
|
|
83
83
|
# end
|
|
84
84
|
#
|
|
85
|
+
# # good - `undef`, `undef_method`, and `remove_method` remove the earlier definition
|
|
86
|
+
# attr_reader :foo
|
|
87
|
+
# undef foo
|
|
88
|
+
# def foo
|
|
89
|
+
# 1
|
|
90
|
+
# end
|
|
91
|
+
#
|
|
85
92
|
# # bad
|
|
86
93
|
# class MyClass
|
|
87
94
|
# extend Forwardable
|
|
@@ -241,11 +248,23 @@ module RuboCop
|
|
|
241
248
|
found_instance_method(node, name)
|
|
242
249
|
end
|
|
243
250
|
|
|
251
|
+
def on_undef(node)
|
|
252
|
+
node.children.each do |name_node|
|
|
253
|
+
name = sym_name(name_node)
|
|
254
|
+
removed_instance_method(node, name) if name
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
244
258
|
# @!method alias_method?(node)
|
|
245
259
|
def_node_matcher :alias_method?, <<~PATTERN
|
|
246
260
|
(send nil? :alias_method (sym $_name) (sym $_original_name))
|
|
247
261
|
PATTERN
|
|
248
262
|
|
|
263
|
+
# @!method removed_method_names(node)
|
|
264
|
+
def_node_matcher :removed_method_names, <<~PATTERN
|
|
265
|
+
(send nil? {:remove_method :undef_method} ({sym str} $_)+)
|
|
266
|
+
PATTERN
|
|
267
|
+
|
|
249
268
|
# Matches the argument shape of an Active Support `delegate` call
|
|
250
269
|
# (`delegate :a, :b, to: :target`), regardless of the method name; the
|
|
251
270
|
# name is checked separately against `DelegatingMethods`.
|
|
@@ -302,6 +321,9 @@ module RuboCop
|
|
|
302
321
|
PATTERN
|
|
303
322
|
|
|
304
323
|
def on_send(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
324
|
+
# Everything that is looked for here is called without a receiver.
|
|
325
|
+
return if node.receiver
|
|
326
|
+
|
|
305
327
|
name, original_name = alias_method?(node)
|
|
306
328
|
|
|
307
329
|
if name && original_name
|
|
@@ -326,6 +348,8 @@ module RuboCop
|
|
|
326
348
|
return if inside_condition?(node)
|
|
327
349
|
|
|
328
350
|
names.each { |name| found_instance_method(node, name) }
|
|
351
|
+
elsif (names = removed_method_names(node))
|
|
352
|
+
names.each { |name| removed_instance_method(node, name) }
|
|
329
353
|
elsif (name = active_support_redefinition_marker(node))
|
|
330
354
|
# `redefine_method` takes a block; scope resolution must start from the
|
|
331
355
|
# block node, since `parent_module_name` cannot see through a block
|
|
@@ -403,16 +427,26 @@ module RuboCop
|
|
|
403
427
|
end
|
|
404
428
|
|
|
405
429
|
def found_instance_method(node, name)
|
|
430
|
+
method_name, scope_id = instance_method_name(node, name)
|
|
431
|
+
found_method(node, method_name, scope_id: scope_id) if method_name
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# `undef`, `undef_method`, and `remove_method` remove the method defined so far,
|
|
435
|
+
# so a method defined after them with the same name is not a duplicate.
|
|
436
|
+
def removed_instance_method(node, name)
|
|
437
|
+
method_name, scope_id = instance_method_name(node, name)
|
|
438
|
+
@definitions.delete(definition_key(node, method_name, scope_id)) if method_name
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
def instance_method_name(node, name)
|
|
406
442
|
if (scope = node.parent_module_name)
|
|
407
|
-
|
|
443
|
+
["#{humanize_scope(scope)}#{name}"]
|
|
408
444
|
elsif (anon_block = anonymous_class_block(node))
|
|
409
445
|
base = qualified_name(anon_block.parent_module_name, nil, 'Object')
|
|
410
446
|
scope = node.each_ancestor(:sclass).any? ? "#<Class:#{base}>" : base
|
|
411
|
-
|
|
412
|
-
node, "#{humanize_scope(scope)}#{name}", scope_id: anon_block_scope_id(anon_block)
|
|
413
|
-
)
|
|
447
|
+
["#{humanize_scope(scope)}#{name}", anon_block_scope_id(anon_block)]
|
|
414
448
|
else
|
|
415
|
-
|
|
449
|
+
sclass_method_name(node, name)
|
|
416
450
|
end
|
|
417
451
|
end
|
|
418
452
|
|
|
@@ -463,19 +497,18 @@ module RuboCop
|
|
|
463
497
|
receiver unless class_or_module_new_block?(receiver)
|
|
464
498
|
end
|
|
465
499
|
|
|
466
|
-
def
|
|
500
|
+
def sclass_method_name(node, name)
|
|
467
501
|
singleton_ancestor = node.each_ancestor.find(&:sclass_type?)
|
|
468
502
|
return unless singleton_ancestor
|
|
469
503
|
|
|
470
504
|
singleton_receiver_node = singleton_ancestor.children[0]
|
|
471
505
|
return unless singleton_receiver_node.send_type?
|
|
472
506
|
|
|
473
|
-
|
|
507
|
+
["#{singleton_receiver_node.method_name}.#{name}"]
|
|
474
508
|
end
|
|
475
509
|
|
|
476
510
|
def found_method(node, method_name, scope_id: nil)
|
|
477
|
-
key =
|
|
478
|
-
key = "#{key}@#{scope_id}" if scope_id
|
|
511
|
+
key = definition_key(node, method_name, scope_id)
|
|
479
512
|
scope = node.each_ancestor(:rescue, :ensure).first&.type
|
|
480
513
|
|
|
481
514
|
if @definitions.key?(key)
|
|
@@ -525,6 +558,11 @@ module RuboCop
|
|
|
525
558
|
@intentionally_redefined << "#{humanize_scope(scope)}#{name}"
|
|
526
559
|
end
|
|
527
560
|
|
|
561
|
+
def definition_key(node, method_name, scope_id)
|
|
562
|
+
key = method_key(node, method_name)
|
|
563
|
+
scope_id ? "#{key}@#{scope_id}" : key
|
|
564
|
+
end
|
|
565
|
+
|
|
528
566
|
def method_key(node, method_name)
|
|
529
567
|
if (ancestor_def = node.each_ancestor(:any_def).first)
|
|
530
568
|
"#{ancestor_def.method_name}.#{method_name}"
|
|
@@ -37,12 +37,25 @@ module RuboCop
|
|
|
37
37
|
PATTERN
|
|
38
38
|
|
|
39
39
|
def on_send(node)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
end
|
|
40
|
+
argument = node.first_argument
|
|
41
|
+
return if node.parent&.block_type? || !argument || lambda_with_symbol_proc?(node)
|
|
43
42
|
|
|
44
43
|
add_offense(node) do |corrector|
|
|
45
|
-
|
|
44
|
+
next if unsafe_to_unwrap?(node, argument)
|
|
45
|
+
|
|
46
|
+
corrector.replace(node, argument.source.delete_prefix('&'))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def unsafe_to_unwrap?(node, argument)
|
|
53
|
+
return false if node.single_line?
|
|
54
|
+
|
|
55
|
+
processed_source.tokens_within(node).any? do |token|
|
|
56
|
+
next if argument.source_range.contains?(token.pos)
|
|
57
|
+
|
|
58
|
+
token.comment? || token.type == :tSTRING_END
|
|
46
59
|
end
|
|
47
60
|
end
|
|
48
61
|
end
|
|
@@ -154,16 +154,24 @@ module RuboCop
|
|
|
154
154
|
end
|
|
155
155
|
|
|
156
156
|
def autocorrect(corrector, node, range)
|
|
157
|
-
|
|
157
|
+
if_node = node.parent
|
|
158
|
+
|
|
159
|
+
corrector.remove(condition_range(if_node, range))
|
|
158
160
|
autocorrect_replace_method(corrector, node)
|
|
159
161
|
|
|
160
|
-
if
|
|
161
|
-
corrector.remove(node.source_range.end.join(
|
|
162
|
+
if if_node.modifier_form?
|
|
163
|
+
corrector.remove(node.source_range.end.join(if_node.loc.keyword.begin))
|
|
162
164
|
else
|
|
163
165
|
corrector.remove(end_keyword_range(node))
|
|
164
166
|
end
|
|
165
167
|
end
|
|
166
168
|
|
|
169
|
+
def condition_range(if_node, range)
|
|
170
|
+
return range unless (separator = if_node.loc.begin)
|
|
171
|
+
|
|
172
|
+
range.join(separator)
|
|
173
|
+
end
|
|
174
|
+
|
|
167
175
|
def end_keyword_range(node)
|
|
168
176
|
range = body_to_end_range(node)
|
|
169
177
|
return node.parent.loc.end if processed_source.contains_comment?(range)
|
|
@@ -126,6 +126,7 @@ module RuboCop
|
|
|
126
126
|
)
|
|
127
127
|
add_offense(node, message: message) do |corrector|
|
|
128
128
|
next if safe_navigation?(node) || part_of_ignored_node?(node)
|
|
129
|
+
next if conversion_removes_content?(node, receiver)
|
|
129
130
|
|
|
130
131
|
corrector.replace(node, correct_method(node, node.receiver))
|
|
131
132
|
|
|
@@ -134,6 +135,15 @@ module RuboCop
|
|
|
134
135
|
end
|
|
135
136
|
end
|
|
136
137
|
|
|
138
|
+
def conversion_removes_content?(node, receiver)
|
|
139
|
+
return false if receiver.last_line == node.last_line
|
|
140
|
+
|
|
141
|
+
range = receiver.source_range.end.join(node.source_range.end)
|
|
142
|
+
processed_source.tokens_within(range).any? do |token|
|
|
143
|
+
token.comment? || token.type == :tSTRING_END
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
137
147
|
def handle_as_symbol(node)
|
|
138
148
|
to_method_symbol(node) do |receiver, sym_node, to_method|
|
|
139
149
|
next if receiver.nil? || !node.arguments.one? || node.operator_method?
|
|
@@ -57,9 +57,23 @@ module RuboCop
|
|
|
57
57
|
message = format(template, context: context)
|
|
58
58
|
|
|
59
59
|
add_offense(node.loc.selector, message: message) do |corrector|
|
|
60
|
-
|
|
60
|
+
if receiver
|
|
61
|
+
corrector.remove(removal_range(node, receiver))
|
|
62
|
+
else
|
|
63
|
+
corrector.replace(node, 'self')
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def removal_range(node, receiver)
|
|
69
|
+
start = heredoc_body_after?(receiver) ? node.loc.dot : receiver.source_range.end
|
|
70
|
+
|
|
71
|
+
start.join(node.source_range.end)
|
|
72
|
+
end
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
def heredoc_body_after?(receiver)
|
|
75
|
+
receiver.each_node(:any_str).any? do |str_node|
|
|
76
|
+
str_node.heredoc? && str_node.loc.heredoc_end.end_pos > receiver.source_range.end_pos
|
|
63
77
|
end
|
|
64
78
|
end
|
|
65
79
|
end
|
|
@@ -24,6 +24,9 @@ module RuboCop
|
|
|
24
24
|
|
|
25
25
|
MSG = 'Remove the `require_relative` that requires itself.'
|
|
26
26
|
RESTRICT_ON_SEND = %i[require_relative].freeze
|
|
27
|
+
BODY_PARENTS = %i[
|
|
28
|
+
any_block any_def class module sclass when in_pattern resbody while until for
|
|
29
|
+
].freeze
|
|
27
30
|
|
|
28
31
|
def on_send(node)
|
|
29
32
|
return unless (required_feature = node.first_argument)
|
|
@@ -31,12 +34,44 @@ module RuboCop
|
|
|
31
34
|
return unless same_file?(processed_source.file_path, required_feature.value)
|
|
32
35
|
|
|
33
36
|
add_offense(node) do |corrector|
|
|
34
|
-
|
|
37
|
+
range = removal_range(node)
|
|
38
|
+
corrector.remove(range) if range
|
|
35
39
|
end
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
private
|
|
39
43
|
|
|
44
|
+
def removal_range(node)
|
|
45
|
+
source = node.source_range
|
|
46
|
+
range = range_for_statement_removal(source)
|
|
47
|
+
return range if !node.parent || (node.parent.type?(:begin, :kwbegin) && !node.value_used?)
|
|
48
|
+
return unless range == range_by_whole_lines(source, include_final_newline: true)
|
|
49
|
+
|
|
50
|
+
range if statement?(node)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Whether the node is a statement in the body of its parent, which stays valid without
|
|
54
|
+
# it. An operand can't go: the parent would take the next line in its place.
|
|
55
|
+
def statement?(node)
|
|
56
|
+
parent = node.parent
|
|
57
|
+
|
|
58
|
+
case parent.type
|
|
59
|
+
when :begin then !parent.loc?(:begin)
|
|
60
|
+
when :kwbegin, :rescue, :ensure then true
|
|
61
|
+
when :if then !parent.ternary? && !parent.condition.equal?(node)
|
|
62
|
+
when :case, :case_match then parent.else_branch.equal?(node)
|
|
63
|
+
else body?(parent, node)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def body?(parent, node)
|
|
68
|
+
return false unless parent.type?(*BODY_PARENTS) && parent.body.equal?(node)
|
|
69
|
+
# An endless method and a modifier `rescue` have nothing after the body to end them.
|
|
70
|
+
return !parent.endless? if parent.any_def_type?
|
|
71
|
+
|
|
72
|
+
!parent.resbody_type? || begins_its_line?(parent.source_range)
|
|
73
|
+
end
|
|
74
|
+
|
|
40
75
|
def same_file?(file_path, required_feature)
|
|
41
76
|
return false unless File.extname(file_path) == '.rb'
|
|
42
77
|
|
|
@@ -37,15 +37,26 @@ module RuboCop
|
|
|
37
37
|
# some_var = 1
|
|
38
38
|
# do_something(some_var)
|
|
39
39
|
# end
|
|
40
|
-
class UselessAssignment < Base
|
|
40
|
+
class UselessAssignment < Base # rubocop:disable Metrics/ClassLength
|
|
41
41
|
extend AutoCorrector
|
|
42
42
|
|
|
43
43
|
MSG = 'Useless assignment to variable - `%<variable>s`.'
|
|
44
44
|
|
|
45
|
+
MODIFIER_KEYWORD = /\A(?:if|unless|while|until)\b/.freeze
|
|
46
|
+
AMBIGUOUS_COMMAND_ARGUMENT = %r{#{MODIFIER_KEYWORD}|\A(?:\{|[/+-]\s|/=|\.\.)}.freeze
|
|
47
|
+
CONDITIONAL_TYPES = %i[if while until case case_match].freeze
|
|
48
|
+
STATEMENTS_PARENT_TYPES = %i[
|
|
49
|
+
begin kwbegin any_block any_def class module sclass when in_pattern for resbody ensure
|
|
50
|
+
].freeze
|
|
51
|
+
|
|
45
52
|
def self.joining_forces
|
|
46
53
|
VariableForce
|
|
47
54
|
end
|
|
48
55
|
|
|
56
|
+
def self.autocorrect_incompatible_with
|
|
57
|
+
[Style::MethodCallWithArgsParentheses, Style::RescueModifier]
|
|
58
|
+
end
|
|
59
|
+
|
|
49
60
|
def after_leaving_scope(scope, _variable_table)
|
|
50
61
|
scope.variables.each_value { |variable| check_for_unused_assignments(variable) }
|
|
51
62
|
end
|
|
@@ -245,11 +256,44 @@ module RuboCop
|
|
|
245
256
|
if expression.array_type? && !expression.bracketed?
|
|
246
257
|
corrector.replace(assignment_part, '[')
|
|
247
258
|
corrector.insert_after(expression, ']')
|
|
259
|
+
elsif parentheses_required?(node, expression)
|
|
260
|
+
corrector.replace(assignment_part, '(')
|
|
261
|
+
corrector.insert_after(expression, ')')
|
|
248
262
|
else
|
|
249
263
|
corrector.remove(assignment_part)
|
|
250
264
|
end
|
|
251
265
|
end
|
|
252
266
|
|
|
267
|
+
def parentheses_required?(node, expression)
|
|
268
|
+
return !rescue_modifier_allowed?(node) if expression.rescue_type?
|
|
269
|
+
|
|
270
|
+
ambiguous_without_assignment?(node, expression)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def rescue_modifier_allowed?(node)
|
|
274
|
+
parent = node.parent
|
|
275
|
+
return true if parent.nil? || parent.assignment?
|
|
276
|
+
return !parent.condition.equal?(node) if parent.type?(*CONDITIONAL_TYPES)
|
|
277
|
+
|
|
278
|
+
parent.type?(*STATEMENTS_PARENT_TYPES)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def ambiguous_without_assignment?(node, expression)
|
|
282
|
+
return false unless first_command_argument?(node)
|
|
283
|
+
|
|
284
|
+
keyword = node.parent.type?(:return, :break, :next)
|
|
285
|
+
expression.source.match?(keyword ? MODIFIER_KEYWORD : AMBIGUOUS_COMMAND_ARGUMENT)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def first_command_argument?(node)
|
|
289
|
+
parent = node.parent
|
|
290
|
+
return false unless parent&.type?(:call, :super, :yield, :return, :break, :next)
|
|
291
|
+
return false if parent.parenthesized?
|
|
292
|
+
return false if parent.call_type? && (parent.operator_method? || parent.setter_method?)
|
|
293
|
+
|
|
294
|
+
parent.first_argument.equal?(node)
|
|
295
|
+
end
|
|
296
|
+
|
|
253
297
|
def variable_in_loop_condition?(assignment_node, variable)
|
|
254
298
|
# A method defined inside the loop has its own scope, so the loop condition
|
|
255
299
|
# can only read the variable if the loop is nearer than any method definition.
|
|
@@ -55,27 +55,12 @@ module RuboCop
|
|
|
55
55
|
(send nil? :private_constant $...)
|
|
56
56
|
PATTERN
|
|
57
57
|
|
|
58
|
-
# Matches class/module-like constant assignments. Nested `class` / `module`
|
|
59
|
-
# keyword definitions are not visited by this cop; these assignment forms are
|
|
60
|
-
# the equivalent syntax and should be treated the same way.
|
|
61
|
-
# @!method class_or_module_definition_assignment?(node)
|
|
62
|
-
def_node_matcher :class_or_module_definition_assignment?, <<~PATTERN
|
|
63
|
-
{
|
|
64
|
-
(send (const {nil? cbase} {:Class :Module :Struct}) :new ...)
|
|
65
|
-
(send (const {nil? cbase} :Data) :define ...)
|
|
66
|
-
(any_block
|
|
67
|
-
{
|
|
68
|
-
(send (const {nil? cbase} {:Class :Module :Struct}) :new ...)
|
|
69
|
-
(send (const {nil? cbase} :Data) :define ...)
|
|
70
|
-
}
|
|
71
|
-
...)
|
|
72
|
-
}
|
|
73
|
-
PATTERN
|
|
74
|
-
|
|
75
58
|
def on_casgn(node)
|
|
76
59
|
return unless after_private_modifier?(node.left_siblings)
|
|
77
60
|
return if private_constantize?(node.right_siblings, node.name)
|
|
78
|
-
|
|
61
|
+
# Nested `class` and `module` definitions are not visited by this cop, and
|
|
62
|
+
# these assignments are the same thing in another syntax.
|
|
63
|
+
return if node.expression&.class_constructor?
|
|
79
64
|
|
|
80
65
|
add_offense(node)
|
|
81
66
|
end
|
|
@@ -49,9 +49,7 @@ module RuboCop
|
|
|
49
49
|
return unless (receiver, operation, number = useless_operation?(node))
|
|
50
50
|
return unless useless?(operation, number)
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
corrector.replace(node, receiver.source)
|
|
54
|
-
end
|
|
52
|
+
register_offense(node, receiver.source)
|
|
55
53
|
end
|
|
56
54
|
alias on_csend on_send
|
|
57
55
|
|
|
@@ -59,9 +57,7 @@ module RuboCop
|
|
|
59
57
|
return unless (variable, operation, number = useless_abbreviated_assignment?(node))
|
|
60
58
|
return unless useless?(operation, number)
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
corrector.replace(node, "#{variable.source} = #{variable.source}")
|
|
64
|
-
end
|
|
60
|
+
register_offense(node, "#{variable.source} = #{variable.source}")
|
|
65
61
|
end
|
|
66
62
|
|
|
67
63
|
private
|
|
@@ -73,6 +69,22 @@ module RuboCop
|
|
|
73
69
|
true if %i[* / **].include?(operation)
|
|
74
70
|
end
|
|
75
71
|
end
|
|
72
|
+
|
|
73
|
+
def register_offense(node, replacement)
|
|
74
|
+
add_offense(node) do |corrector|
|
|
75
|
+
next if unsafe_replacement?(node)
|
|
76
|
+
|
|
77
|
+
corrector.replace(node, replacement)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def unsafe_replacement?(node)
|
|
82
|
+
return false if node.single_line?
|
|
83
|
+
|
|
84
|
+
processed_source.tokens_within(node).any? do |token|
|
|
85
|
+
token.comment? || token.type == :tSTRING_END
|
|
86
|
+
end
|
|
87
|
+
end
|
|
76
88
|
end
|
|
77
89
|
end
|
|
78
90
|
end
|
|
@@ -38,9 +38,12 @@ module RuboCop
|
|
|
38
38
|
(block _ (args (arg $_)) ...)
|
|
39
39
|
PATTERN
|
|
40
40
|
|
|
41
|
-
# @!method
|
|
42
|
-
def_node_search :
|
|
43
|
-
|
|
41
|
+
# @!method argument_references(node, name)
|
|
42
|
+
def_node_search :argument_references, '$(lvar %)'
|
|
43
|
+
|
|
44
|
+
# @!method writes_argument?(node, name)
|
|
45
|
+
def_node_search :writes_argument?, <<~PATTERN
|
|
46
|
+
{(argument % ...) (lvasgn % ...) (match_var %) (match_with_lvasgn #named_capture?(%) _)}
|
|
44
47
|
PATTERN
|
|
45
48
|
|
|
46
49
|
def on_send(node)
|
|
@@ -93,10 +96,28 @@ module RuboCop
|
|
|
93
96
|
block_arg = block_arg(node)
|
|
94
97
|
return unless reducible_to_body?(node, block_arg)
|
|
95
98
|
|
|
96
|
-
source = node.body.source
|
|
97
|
-
|
|
99
|
+
source = block_arg ? substituted_body(node.body, block_arg) : node.body.source
|
|
100
|
+
return unless source
|
|
101
|
+
|
|
102
|
+
corrector.replace(node, fix_indentation(source, node))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def substituted_body(body, name)
|
|
106
|
+
references = argument_references(body, name).sort_by(&:source_range)
|
|
107
|
+
return unless references.all? { |variable| replaceable_reference?(variable) }
|
|
98
108
|
|
|
99
|
-
|
|
109
|
+
source = body.source
|
|
110
|
+
references.reverse_each do |variable|
|
|
111
|
+
offset = variable.source_range.begin_pos - body.source_range.begin_pos
|
|
112
|
+
source[offset, variable.source_length] = '0'
|
|
113
|
+
end
|
|
114
|
+
source
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def replaceable_reference?(variable)
|
|
118
|
+
return false if variable.parent.pin_type? || variable.each_ancestor(:defined?).any?
|
|
119
|
+
|
|
120
|
+
!variable.parent.pair_type? || !variable.parent.value_omission?
|
|
100
121
|
end
|
|
101
122
|
|
|
102
123
|
def reducible_to_body?(node, block_arg)
|
|
@@ -109,8 +130,15 @@ module RuboCop
|
|
|
109
130
|
# substituted either, so reducing to the body would leave it referencing an
|
|
110
131
|
# undefined variable.
|
|
111
132
|
return false if node.arguments.one? && block_arg.nil?
|
|
133
|
+
return false if needs_enclosing_block?(node.body)
|
|
112
134
|
|
|
113
|
-
!
|
|
135
|
+
!block_arg || !writes_argument?(node.body, block_arg)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# A `rescue` or `ensure` clause is only valid inside the block, and the source of
|
|
139
|
+
# the body doesn't cover the body of a heredoc that ends it.
|
|
140
|
+
def needs_enclosing_block?(body)
|
|
141
|
+
body.type?(:rescue, :ensure) || body.each_node(:any_str, :xstr).any?(&:heredoc?)
|
|
114
142
|
end
|
|
115
143
|
|
|
116
144
|
def orphans_loop_control_keyword?(node)
|
|
@@ -124,14 +152,14 @@ module RuboCop
|
|
|
124
152
|
regexp.to_regexp.names.include?(name.to_s)
|
|
125
153
|
end
|
|
126
154
|
|
|
127
|
-
def fix_indentation(source,
|
|
128
|
-
|
|
155
|
+
def fix_indentation(source, node)
|
|
156
|
+
shift = node.body.loc.column - node.loc.column
|
|
157
|
+
shift_range = begins_its_line?(node.body.source_range) ? "0,#{shift}" : shift
|
|
158
|
+
excess_indentation = /\A([ \t]{#{node.loc.column}})[ \t]{#{shift_range}}/
|
|
129
159
|
source_lines = source.split("\n")
|
|
130
160
|
|
|
131
161
|
source_lines[1..].each do |line|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
line[range] = ''
|
|
162
|
+
line.sub!(excess_indentation, '\1')
|
|
135
163
|
end
|
|
136
164
|
source_lines.join("\n")
|
|
137
165
|
end
|
|
@@ -67,7 +67,7 @@ module RuboCop
|
|
|
67
67
|
if first_child_requires_empty_line?(body)
|
|
68
68
|
check_beginning(:empty_lines, body, first_line)
|
|
69
69
|
else
|
|
70
|
-
|
|
70
|
+
check_no_empty_line_at_beginning(body, first_line)
|
|
71
71
|
check_deferred_empty_line(body)
|
|
72
72
|
end
|
|
73
73
|
check_ending(:empty_lines, body, last_line)
|
|
@@ -89,6 +89,7 @@ module RuboCop
|
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def check_beginning(style, body, first_line)
|
|
92
|
+
return if style == :no_empty_lines && body && body.last_line <= first_line
|
|
92
93
|
return if style == :empty_lines && body&.first_line == first_line
|
|
93
94
|
|
|
94
95
|
check_source(style, first_line, 'beginning')
|
|
@@ -127,6 +128,20 @@ module RuboCop
|
|
|
127
128
|
LEADING_DOT_TYPES.include?(first_token&.type)
|
|
128
129
|
end
|
|
129
130
|
|
|
131
|
+
def check_no_empty_line_at_beginning(body, first_line)
|
|
132
|
+
return if deferred_empty_line_at_beginning?(body, first_line)
|
|
133
|
+
|
|
134
|
+
check_beginning(:no_empty_lines, body, first_line)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def deferred_empty_line_at_beginning?(body, first_line)
|
|
138
|
+
node = first_empty_line_required_child(body)
|
|
139
|
+
return false unless node
|
|
140
|
+
return false if node.first_line <= first_line
|
|
141
|
+
|
|
142
|
+
previous_line_ignoring_comments(node.first_line) <= first_line
|
|
143
|
+
end
|
|
144
|
+
|
|
130
145
|
def check_deferred_empty_line(body)
|
|
131
146
|
node = first_empty_line_required_child(body)
|
|
132
147
|
return unless node
|
|
@@ -33,9 +33,9 @@ module RuboCop
|
|
|
33
33
|
def_node_matcher :hash_receiver?, <<~PATTERN
|
|
34
34
|
{(hash ...)
|
|
35
35
|
(send _ {:to_h :to_hash :merge :merge! :update :invert :except :tally} ...)
|
|
36
|
-
(
|
|
37
|
-
|
|
38
|
-
(
|
|
36
|
+
(any_block (send _ {:group_by :to_h :tally :transform_keys :transform_keys!
|
|
37
|
+
:transform_values :transform_values!}) ...)
|
|
38
|
+
(any_block (send _ :each_with_object (hash)) ...)}
|
|
39
39
|
PATTERN
|
|
40
40
|
|
|
41
41
|
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler -- the patterns matched here all name their block parameters
|
|
@@ -63,8 +63,8 @@ module RuboCop
|
|
|
63
63
|
|
|
64
64
|
def self.autocorrect_incompatible_with
|
|
65
65
|
[
|
|
66
|
-
|
|
67
|
-
Style::
|
|
66
|
+
Style::ArgumentsForwarding, Style::EndlessMethod, Style::ExplicitBlockArgument,
|
|
67
|
+
Style::MethodDefParentheses
|
|
68
68
|
]
|
|
69
69
|
end
|
|
70
70
|
|
|
@@ -153,14 +153,16 @@ module RuboCop
|
|
|
153
153
|
BLOCK_MSG = 'Use anonymous block arguments forwarding (`&`).'
|
|
154
154
|
|
|
155
155
|
def self.autocorrect_incompatible_with
|
|
156
|
-
[
|
|
156
|
+
[Style::EndlessMethod, Style::MethodDefParentheses]
|
|
157
157
|
end
|
|
158
158
|
|
|
159
159
|
def on_def(node)
|
|
160
160
|
return unless node.body
|
|
161
161
|
|
|
162
|
-
|
|
163
|
-
|
|
162
|
+
forwardable_args =
|
|
163
|
+
redundant_forwardable_named_args(*extract_forwardable_args(node.arguments))
|
|
164
|
+
return if forwardable_args.none?
|
|
165
|
+
|
|
164
166
|
send_nodes = node.each_descendant(:call, :super, :yield).to_a
|
|
165
167
|
|
|
166
168
|
send_classifications = classify_send_nodes(
|
|
@@ -423,17 +425,16 @@ module RuboCop
|
|
|
423
425
|
|
|
424
426
|
# @!method def_all_anonymous_args?(node)
|
|
425
427
|
def_node_matcher :def_all_anonymous_args?, <<~PATTERN
|
|
426
|
-
|
|
427
|
-
def _
|
|
428
|
-
(args ... (restarg) (kwrestarg) (blockarg nil?))
|
|
429
|
-
|
|
430
|
-
)
|
|
428
|
+
{
|
|
429
|
+
(def _ (args ... (restarg) (kwrestarg) (blockarg nil?)) _)
|
|
430
|
+
(defs _ _ (args ... (restarg) (kwrestarg) (blockarg nil?)) _)
|
|
431
|
+
}
|
|
431
432
|
PATTERN
|
|
432
433
|
|
|
433
434
|
# @!method send_all_anonymous_args?(node)
|
|
434
435
|
def_node_matcher :send_all_anonymous_args?, <<~PATTERN
|
|
435
436
|
(
|
|
436
|
-
|
|
437
|
+
call _ _
|
|
437
438
|
... (forwarded_restarg) (hash (forwarded_kwrestarg)) (block_pass nil?)
|
|
438
439
|
)
|
|
439
440
|
PATTERN
|