rubocop 1.92.2 → 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/#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 +22 -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/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- 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/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 +26 -6
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
- 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 +5 -2
- 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/line_length.rb +14 -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 +53 -15
- data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
- 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/lambda_without_literal_block.rb +17 -4
- 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 +53 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +14 -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/redundant_string_coercion.rb +16 -2
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -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/shadowing_outer_local_variable.rb +1 -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 +71 -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 +52 -12
- 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 +49 -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/hash_transform_method.rb +3 -3
- 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 +11 -3
- 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/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 +5 -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 +82 -9
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- 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 +21 -4
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +27 -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/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/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 +12 -5
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- 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/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/map_into_array.rb +4 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- 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/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/mutable_constant.rb +1 -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 +22 -17
- data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +37 -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/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
- data/lib/rubocop/cop/style/redundant_each.rb +26 -1
- 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 +37 -16
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
- 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 +6 -2
- 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/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
- data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
- 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/sole_nested_conditional.rb +12 -6
- 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 +82 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- 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/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/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +21 -7
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -5
|
@@ -168,8 +168,10 @@ module RuboCop
|
|
|
168
168
|
range = autocorrect_range(last_item)
|
|
169
169
|
msg = format(MSG, command: 'Put a', unit: format(kind, article: 'a multiline'))
|
|
170
170
|
|
|
171
|
+
# The comma is anchored to the whole item rather than to the offense range on its
|
|
172
|
+
# last line, so that parentheses other cops wrap around a part of the item precede it.
|
|
171
173
|
add_offense(range, message: msg) do |corrector|
|
|
172
|
-
|
|
174
|
+
corrector.insert_after(last_item, ',')
|
|
173
175
|
end
|
|
174
176
|
end
|
|
175
177
|
|
|
@@ -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
|
|
|
@@ -133,7 +133,7 @@ module RuboCop
|
|
|
133
133
|
|
|
134
134
|
arguments = block_argument.parent
|
|
135
135
|
|
|
136
|
-
add_parentheses(arguments, corrector)
|
|
136
|
+
add_parentheses(arguments, corrector) if require_parentheses?(arguments)
|
|
137
137
|
else
|
|
138
138
|
unless use_block_argument_as_local_variable?(node, block_forwarding_name)
|
|
139
139
|
corrector.replace(block_argument, "&#{block_forwarding_name}")
|
|
@@ -142,6 +142,14 @@ module RuboCop
|
|
|
142
142
|
end
|
|
143
143
|
end
|
|
144
144
|
|
|
145
|
+
def require_parentheses?(node)
|
|
146
|
+
!node.parenthesized_call? && !index_call?(node)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def index_call?(node)
|
|
150
|
+
node.send_type? && (node.method?(:[]) || node.method?(:[]=)) && !node.loc.dot
|
|
151
|
+
end
|
|
152
|
+
|
|
145
153
|
def use_block_argument_as_local_variable?(node, last_argument)
|
|
146
154
|
return false if node.body.nil?
|
|
147
155
|
|
|
@@ -41,7 +41,7 @@ module RuboCop
|
|
|
41
41
|
expr = node.source_range
|
|
42
42
|
|
|
43
43
|
corrector.replace(expr, correct_delimiters(expr.source))
|
|
44
|
-
corrector.replace(node.loc.heredoc_end, correct_delimiters(
|
|
44
|
+
corrector.replace(node.loc.heredoc_end, correct_delimiters(node.loc.heredoc_end.source))
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
|
|
@@ -52,16 +52,20 @@ module RuboCop
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
def correct_case_delimiters?(node)
|
|
55
|
-
delimiters =
|
|
55
|
+
delimiters = delimiter(node)
|
|
56
56
|
delimiters == correct_delimiters(delimiters)
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def corrected_delimiter_in_body?(node)
|
|
60
|
-
corrected = correct_delimiters(
|
|
60
|
+
corrected = correct_delimiters(delimiter(node))
|
|
61
61
|
|
|
62
62
|
node.loc.heredoc_body.source.each_line.any? { |line| line.strip == corrected }
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
def delimiter(node)
|
|
66
|
+
node.loc.heredoc_end.source.strip
|
|
67
|
+
end
|
|
68
|
+
|
|
65
69
|
def correct_delimiters(source)
|
|
66
70
|
if style == :uppercase
|
|
67
71
|
source.upcase
|
|
@@ -93,15 +93,19 @@ module RuboCop
|
|
|
93
93
|
variable.source_range
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
+
# rubocop:disable-next Metrics/CyclomaticComplexity
|
|
96
97
|
def autocorrect(corrector, node, range, offending_name, preferred_name)
|
|
98
|
+
kwbegin_node = node.parent.each_ancestor(:kwbegin).first
|
|
99
|
+
return if kwbegin_node&.parent&.call_type?
|
|
100
|
+
|
|
97
101
|
corrector.replace(range, preferred_name)
|
|
98
102
|
# Once the exception variable is reassigned, later references point to a
|
|
99
103
|
# different value, so stop correcting after the reassignment - both in the
|
|
100
104
|
# body and in the code following the `begin`/`rescue`.
|
|
101
105
|
return if correct_node(corrector, node.body, offending_name, preferred_name)
|
|
102
|
-
return unless
|
|
106
|
+
return unless kwbegin_node
|
|
103
107
|
|
|
104
|
-
kwbegin_node.right_siblings.each do |child_node|
|
|
108
|
+
kwbegin_node.right_siblings.grep(AST::Node).each do |child_node|
|
|
105
109
|
break if correct_node(corrector, child_node, offending_name, preferred_name)
|
|
106
110
|
end
|
|
107
111
|
end
|
|
@@ -362,7 +362,7 @@ module RuboCop
|
|
|
362
362
|
if argument_less_modifier_node
|
|
363
363
|
corrector.insert_after(argument_less_modifier_node, "\n\n#{source}")
|
|
364
364
|
elsif (ancestor = node.each_ancestor(:class, :module, :sclass).first)
|
|
365
|
-
corrector
|
|
365
|
+
insert_before_end(corrector, ancestor, "#{node.method_name}\n\n#{source}\n")
|
|
366
366
|
else
|
|
367
367
|
corrector.replace(node, "#{node.method_name}\n\n#{source}")
|
|
368
368
|
return
|
|
@@ -371,16 +371,29 @@ module RuboCop
|
|
|
371
371
|
remove_nodes(corrector, *def_nodes, node)
|
|
372
372
|
end
|
|
373
373
|
|
|
374
|
+
def insert_before_end(corrector, ancestor, source)
|
|
375
|
+
end_keyword = ancestor.loc.end
|
|
376
|
+
source = "\n#{source}" unless begins_its_line?(end_keyword)
|
|
377
|
+
|
|
378
|
+
corrector.insert_before(end_keyword, source)
|
|
379
|
+
end
|
|
380
|
+
|
|
374
381
|
def insert_inline_modifier(corrector, node, modifier_name)
|
|
375
382
|
corrector.insert_before(node, "#{modifier_name} ")
|
|
376
383
|
end
|
|
377
384
|
|
|
378
385
|
def remove_nodes(corrector, *nodes)
|
|
379
386
|
nodes.each do |node|
|
|
380
|
-
|
|
387
|
+
range = on_own_lines?(node) ? range_with_comments_and_lines(node) : node.source_range
|
|
388
|
+
|
|
389
|
+
corrector.remove(range)
|
|
381
390
|
end
|
|
382
391
|
end
|
|
383
392
|
|
|
393
|
+
def on_own_lines?(node)
|
|
394
|
+
range_by_whole_lines(node.source_range).source.strip == node.source
|
|
395
|
+
end
|
|
396
|
+
|
|
384
397
|
def remove_modifier_node_within_begin(corrector, modifier_node, begin_node)
|
|
385
398
|
modifier_index = begin_node.children.find_index { |child| child.equal?(modifier_node) }
|
|
386
399
|
def_node = begin_node.children[modifier_index + 1]
|
|
@@ -52,6 +52,7 @@ module RuboCop
|
|
|
52
52
|
return unless operation
|
|
53
53
|
|
|
54
54
|
return unless modifier_form?(operation)
|
|
55
|
+
return if heredoc_in_body?(node)
|
|
55
56
|
|
|
56
57
|
add_offense(operation, message: format(MSG, keyword: keyword(operation))) do |corrector|
|
|
57
58
|
correct_to_multiline(corrector, node)
|
|
@@ -67,6 +68,10 @@ module RuboCop
|
|
|
67
68
|
operation.modifier_form?
|
|
68
69
|
end
|
|
69
70
|
|
|
71
|
+
def heredoc_in_body?(node)
|
|
72
|
+
node.body.each_node(:any_str).any?(&:heredoc?)
|
|
73
|
+
end
|
|
74
|
+
|
|
70
75
|
def keyword(operation)
|
|
71
76
|
if operation.respond_to?(:keyword)
|
|
72
77
|
operation.keyword
|
|
@@ -109,7 +109,7 @@ module RuboCop
|
|
|
109
109
|
def correct_send(node, corrector)
|
|
110
110
|
return correct_not(node, node.receiver, corrector) if node.method?(:!)
|
|
111
111
|
return correct_setter(node, corrector) if node.setter_method?
|
|
112
|
-
return correct_other(node, corrector) if node
|
|
112
|
+
return correct_other(node.block_node || node, corrector) if wrap_whole_call?(node)
|
|
113
113
|
|
|
114
114
|
correct_block_receiver(node, corrector)
|
|
115
115
|
return unless correctable_send?(node)
|
|
@@ -149,6 +149,13 @@ module RuboCop
|
|
|
149
149
|
corrector.wrap(node, '(', ')')
|
|
150
150
|
end
|
|
151
151
|
|
|
152
|
+
def wrap_whole_call?(node)
|
|
153
|
+
return true if node.comparison_method?
|
|
154
|
+
return false if node.method?(:[])
|
|
155
|
+
|
|
156
|
+
node.dot? && node.operator_method?
|
|
157
|
+
end
|
|
158
|
+
|
|
152
159
|
def keep_operator_precedence(corrector, node)
|
|
153
160
|
if node.or_type? && node.parent&.and_type?
|
|
154
161
|
corrector.wrap(node, '(', ')')
|
|
@@ -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
|
|
@@ -154,9 +154,7 @@ module RuboCop
|
|
|
154
154
|
# rewrite `!a&.intersect?(b)` returns `true` there, flipping the result.
|
|
155
155
|
return if bang == '!' && dot == '&.'
|
|
156
156
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
register_offense(node, replacement)
|
|
157
|
+
register_offense(node, receiver, argument, dot, bang)
|
|
160
158
|
end
|
|
161
159
|
alias on_csend on_send
|
|
162
160
|
|
|
@@ -168,9 +166,7 @@ module RuboCop
|
|
|
168
166
|
|
|
169
167
|
dot = node.send_node.loc.dot.source
|
|
170
168
|
bang = method_name == :any? ? '' : '!'
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
register_offense(node, replacement)
|
|
169
|
+
register_offense(node, receiver, argument, dot, bang)
|
|
174
170
|
end
|
|
175
171
|
alias on_numblock on_block
|
|
176
172
|
alias on_itblock on_block
|
|
@@ -205,13 +201,25 @@ module RuboCop
|
|
|
205
201
|
block_method == :include? && !argument.array_type?
|
|
206
202
|
end
|
|
207
203
|
|
|
208
|
-
def register_offense(node,
|
|
204
|
+
def register_offense(node, receiver, argument, dot, bang)
|
|
205
|
+
replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})"
|
|
209
206
|
message = format(MSG, replacement: replacement, existing: node.source)
|
|
210
207
|
|
|
211
208
|
add_offense(node, message: message) do |corrector|
|
|
209
|
+
next if drops_heredoc?(receiver, node) || drops_heredoc?(argument, node)
|
|
210
|
+
|
|
212
211
|
corrector.replace(node, replacement)
|
|
213
212
|
end
|
|
214
213
|
end
|
|
214
|
+
|
|
215
|
+
def drops_heredoc?(operand, node)
|
|
216
|
+
return false if operand.last_line == node.last_line
|
|
217
|
+
|
|
218
|
+
removed_lines = (operand.last_line + 1)..node.last_line
|
|
219
|
+
operand.each_node(:any_str).any? do |str|
|
|
220
|
+
str.heredoc? && removed_lines.cover?(str.loc.heredoc_end.line)
|
|
221
|
+
end
|
|
222
|
+
end
|
|
215
223
|
end
|
|
216
224
|
end
|
|
217
225
|
end
|
|
@@ -177,7 +177,11 @@ module RuboCop
|
|
|
177
177
|
BRACES_REQUIRED_MESSAGE = "Brace delimiters `{...}` required for '%<method_name>s' method."
|
|
178
178
|
|
|
179
179
|
def self.autocorrect_incompatible_with
|
|
180
|
-
[
|
|
180
|
+
[
|
|
181
|
+
Style::MethodCallWithArgsParentheses,
|
|
182
|
+
Style::RedundantBegin,
|
|
183
|
+
Style::RescueStandardError
|
|
184
|
+
]
|
|
181
185
|
end
|
|
182
186
|
|
|
183
187
|
def on_send(node)
|
|
@@ -207,16 +207,18 @@ module RuboCop
|
|
|
207
207
|
end
|
|
208
208
|
|
|
209
209
|
def remove_end(corrector, body)
|
|
210
|
-
range =
|
|
211
|
-
single_line_end_range(body)
|
|
212
|
-
else
|
|
213
|
-
multiline_end_range(body)
|
|
214
|
-
end
|
|
210
|
+
range = begins_its_line?(body.loc.end) ? end_line_range(body) : inline_end_range(body)
|
|
215
211
|
|
|
216
212
|
corrector.remove(range)
|
|
217
213
|
end
|
|
218
214
|
|
|
219
|
-
def
|
|
215
|
+
def end_line_range(body)
|
|
216
|
+
end_keyword = body.loc.end
|
|
217
|
+
|
|
218
|
+
range_between(end_keyword.begin_pos - end_keyword.column, end_keyword.end_pos + 1)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def inline_end_range(body)
|
|
220
222
|
raw = processed_source.raw_source
|
|
221
223
|
pos = body.loc.end.begin_pos
|
|
222
224
|
pos -= 1 while raw[pos - 1] == ' '
|
|
@@ -224,12 +226,6 @@ module RuboCop
|
|
|
224
226
|
range_between(pos, body.loc.end.end_pos)
|
|
225
227
|
end
|
|
226
228
|
|
|
227
|
-
def multiline_end_range(body)
|
|
228
|
-
remove_begin_pos = body.loc.end.begin_pos - leading_spaces(body).size
|
|
229
|
-
adjustment = processed_source.raw_source[remove_begin_pos] == ';' ? 0 : 1
|
|
230
|
-
range_between(remove_begin_pos, body.loc.end.end_pos + adjustment)
|
|
231
|
-
end
|
|
232
|
-
|
|
233
229
|
def unindent(corrector, node)
|
|
234
230
|
column_delta = unindent_column_delta(node)
|
|
235
231
|
return unless column_delta&.negative?
|
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
|
72
72
|
return unless def_self_style?
|
|
73
73
|
return unless node.identifier.self_type?
|
|
74
74
|
return unless all_methods_public?(node)
|
|
75
|
-
return unless def_nodes(node).all? { |def_node|
|
|
75
|
+
return unless def_nodes(node).all? { |def_node| on_own_lines?(def_node.source_range) }
|
|
76
76
|
|
|
77
77
|
add_offense(node, message: MSG_SCLASS) do |corrector|
|
|
78
78
|
next if inside_corrected_sclass?(node)
|
|
@@ -116,6 +116,14 @@ module RuboCop
|
|
|
116
116
|
end
|
|
117
117
|
end
|
|
118
118
|
|
|
119
|
+
def on_own_lines?(range)
|
|
120
|
+
no_code_before?(range) && no_code_after?(range)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def no_code_before?(range)
|
|
124
|
+
/\A[\s;]*\z/.match?(processed_source.lines[range.line - 1][0...range.column])
|
|
125
|
+
end
|
|
126
|
+
|
|
119
127
|
def no_code_after?(range)
|
|
120
128
|
trailing = processed_source.lines[range.last_line - 1][range.last_column..].to_s.strip
|
|
121
129
|
trailing = trailing.sub(/\A;+\s*/, '')
|
|
@@ -66,7 +66,7 @@ module RuboCop
|
|
|
66
66
|
MSG = 'Combine this loop with the previous loop.'
|
|
67
67
|
|
|
68
68
|
def self.autocorrect_incompatible_with
|
|
69
|
-
[Layout::BlockEndNewline, Layout::RedundantLineBreak]
|
|
69
|
+
[Layout::BlockEndNewline, Layout::RedundantLineBreak, Style::ItBlockParameter, Style::SymbolProc]
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
def on_block(node)
|
|
@@ -126,15 +126,24 @@ module RuboCop
|
|
|
126
126
|
end
|
|
127
127
|
end
|
|
128
128
|
|
|
129
|
-
# Whatever sits between the last element and the `]` of an array
|
|
130
|
-
#
|
|
131
|
-
#
|
|
129
|
+
# Whatever sits between the last element and the `]` of an array followed by
|
|
130
|
+
# a comma would end up before that comma (a newline before it is a syntax error),
|
|
131
|
+
# so it's removed along with the `]`.
|
|
132
132
|
def closing_bracket_range(node, argument)
|
|
133
|
-
return argument.loc.end if argument.equal?(node.last_argument)
|
|
133
|
+
return argument.loc.end if argument.equal?(node.last_argument) && !trailing_comma?(node)
|
|
134
134
|
|
|
135
135
|
range_between(argument.children.last.source_range.end_pos, argument.loc.end.end_pos)
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
+
def trailing_comma?(node)
|
|
139
|
+
return false unless node.parenthesized?
|
|
140
|
+
|
|
141
|
+
tokens = processed_source.tokens_within(node)
|
|
142
|
+
closing_bracket_index = tokens.index(processed_source.last_token_of(node.last_argument))
|
|
143
|
+
|
|
144
|
+
tokens[closing_bracket_index + 1].comma?
|
|
145
|
+
end
|
|
146
|
+
|
|
138
147
|
def replace_selector(corrector, node)
|
|
139
148
|
if node.parenthesized? || node.single_line?
|
|
140
149
|
corrector.replace(node.loc.selector, 'push')
|
|
@@ -41,18 +41,30 @@ module RuboCop
|
|
|
41
41
|
|
|
42
42
|
MSG = "Don't extend an instance initialized by `Data.define`. " \
|
|
43
43
|
'Use a block to customize the class.'
|
|
44
|
+
LEFT_OPERAND_TYPES = %i[and or irange erange match_pattern match_pattern_p].freeze
|
|
45
|
+
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn op_asgn or_asgn and_asgn].freeze
|
|
46
|
+
BODY_TYPES = %i[module class sclass any_def any_block when in_pattern resbody for].freeze
|
|
44
47
|
|
|
45
48
|
minimum_target_ruby_version 3.2
|
|
46
49
|
|
|
50
|
+
def self.autocorrect_incompatible_with
|
|
51
|
+
[Style::MethodCallWithArgsParentheses]
|
|
52
|
+
end
|
|
53
|
+
|
|
47
54
|
def on_class(node)
|
|
48
55
|
return unless data_define?(node.parent_class)
|
|
49
56
|
return if defines_constants?(node.body)
|
|
50
57
|
|
|
51
58
|
add_offense(node.parent_class) do |corrector|
|
|
52
|
-
|
|
59
|
+
keyword_range = range_with_surrounding_space(
|
|
60
|
+
node.loc.keyword, side: :right, newlines: false
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
corrector.remove(keyword_range)
|
|
53
64
|
corrector.replace(node.loc.operator, '=')
|
|
54
65
|
|
|
55
66
|
correct_parent(node.parent_class, corrector)
|
|
67
|
+
parenthesize_left_operand(corrector, node, keyword_range)
|
|
56
68
|
end
|
|
57
69
|
end
|
|
58
70
|
|
|
@@ -80,19 +92,80 @@ module RuboCop
|
|
|
80
92
|
# left behind, producing invalid Ruby.
|
|
81
93
|
corrector.replace(parent.loc.begin, 'do') if parent.braces?
|
|
82
94
|
corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
|
|
83
|
-
elsif (
|
|
84
|
-
corrector.remove(
|
|
95
|
+
elsif (range = range_for_empty_class_body(parent))
|
|
96
|
+
corrector.remove(range)
|
|
97
|
+
parenthesize_arguments(corrector, parent) unless statement?(parent.parent)
|
|
85
98
|
else
|
|
99
|
+
parenthesize_arguments(corrector, parent)
|
|
86
100
|
corrector.insert_after(parent, ' do')
|
|
87
101
|
end
|
|
88
102
|
end
|
|
89
103
|
|
|
90
|
-
def range_for_empty_class_body(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
end
|
|
104
|
+
def range_for_empty_class_body(data_define)
|
|
105
|
+
class_node = data_define.parent
|
|
106
|
+
return if class_node.body
|
|
107
|
+
|
|
108
|
+
range = range_between(data_define.source_range.end_pos, class_node.source_range.end_pos)
|
|
109
|
+
return range if /\A[\s;]*end\z/.match?(range.source)
|
|
110
|
+
return if end_line_kept?(class_node)
|
|
111
|
+
|
|
112
|
+
range_by_whole_lines(class_node.loc.end, include_final_newline: true)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def end_line_kept?(class_node)
|
|
116
|
+
return true if left_operand?(class_node)
|
|
117
|
+
|
|
118
|
+
class_node.loc.end.source_line.strip != 'end'
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def left_operand?(class_node)
|
|
122
|
+
parent = class_node.parent
|
|
123
|
+
return false if parent.nil?
|
|
124
|
+
return parent.receiver.equal?(class_node) if parent.call_type?
|
|
125
|
+
return parent.condition.equal?(class_node) if parent.if_type? && parent.ternary?
|
|
126
|
+
|
|
127
|
+
LEFT_OPERAND_TYPES.include?(parent.type) && parent.children.first.equal?(class_node)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def statement?(class_node)
|
|
131
|
+
parent = class_node.parent
|
|
132
|
+
return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
133
|
+
return !parent.condition.equal?(class_node) if conditional_statement?(parent)
|
|
134
|
+
|
|
135
|
+
statement_child(parent).equal?(class_node)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def conditional_statement?(node)
|
|
139
|
+
return true if node.type?(:while, :until)
|
|
140
|
+
|
|
141
|
+
node.if_type? && !node.ternary?
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def statement_child(node)
|
|
145
|
+
return node.expression if node.type?(*ASSIGNMENT_TYPES)
|
|
146
|
+
return node.else_branch if node.type?(:case, :case_match)
|
|
147
|
+
return if node.any_def_type? && node.endless?
|
|
148
|
+
|
|
149
|
+
node.body if node.type?(*BODY_TYPES)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def parenthesize_arguments(corrector, data_define)
|
|
153
|
+
return unless unparenthesized_data_define?(data_define)
|
|
154
|
+
|
|
155
|
+
arguments_begin = data_define.first_argument.source_range.begin
|
|
156
|
+
corrector.replace(data_define.loc.selector.end.join(arguments_begin), '(')
|
|
157
|
+
corrector.insert_after(data_define.last_argument, ')')
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def unparenthesized_data_define?(data_define)
|
|
161
|
+
data_define.arguments.any? && !data_define.parenthesized?
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def parenthesize_left_operand(corrector, class_node, keyword_range)
|
|
165
|
+
return unless left_operand?(class_node)
|
|
166
|
+
|
|
167
|
+
corrector.insert_before(keyword_range, '(')
|
|
168
|
+
corrector.insert_after(class_node.source_range.end, ')')
|
|
96
169
|
end
|
|
97
170
|
end
|
|
98
171
|
end
|
|
@@ -43,6 +43,7 @@ module RuboCop
|
|
|
43
43
|
extend AutoCorrector
|
|
44
44
|
|
|
45
45
|
MSG = "Omit the parentheses in defs when the method doesn't accept any arguments."
|
|
46
|
+
SPACES_BEFORE_ASSIGNMENT = /\A(?:[ \t]|\\\n)+\z/.freeze
|
|
46
47
|
|
|
47
48
|
def on_def(node)
|
|
48
49
|
return unless !node.arguments? && (arguments_range = node.arguments.source_range)
|
|
@@ -64,9 +65,15 @@ module RuboCop
|
|
|
64
65
|
# signature from the body, so the parentheses can be removed (`def foo; end`).
|
|
65
66
|
return false if token_after_argument == ';'
|
|
66
67
|
|
|
67
|
-
return
|
|
68
|
+
return node.single_line? unless node.endless?
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
!spaces_before_assignment?(node, end_pos)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def spaces_before_assignment?(node, end_pos)
|
|
74
|
+
source = range_between(end_pos, node.loc.assignment.begin_pos).source
|
|
75
|
+
|
|
76
|
+
SPACES_BEFORE_ASSIGNMENT.match?(source)
|
|
70
77
|
end
|
|
71
78
|
end
|
|
72
79
|
end
|
|
@@ -72,10 +72,11 @@ module RuboCop
|
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def register_offense(node, range, arguments)
|
|
75
|
-
|
|
76
|
-
replacement = "dig(#{arguments})"
|
|
75
|
+
replacement = "dig(#{arguments.map(&:source).join(', ')})"
|
|
77
76
|
|
|
78
77
|
add_offense(range, message: format(MSG, replacement: replacement)) do |corrector|
|
|
78
|
+
next if loses_heredoc?(range, arguments)
|
|
79
|
+
|
|
79
80
|
corrector.replace(range, replacement)
|
|
80
81
|
|
|
81
82
|
comments_in_range(node).reverse_each do |comment|
|
|
@@ -88,6 +89,16 @@ module RuboCop
|
|
|
88
89
|
end
|
|
89
90
|
end
|
|
90
91
|
end
|
|
92
|
+
|
|
93
|
+
def loses_heredoc?(range, arguments)
|
|
94
|
+
return false if range.single_line?
|
|
95
|
+
|
|
96
|
+
processed_source.tokens_within(range).any? do |token|
|
|
97
|
+
next unless token.type == :tSTRING_END
|
|
98
|
+
|
|
99
|
+
arguments.none? { |arg| arg.source_range.contains?(token.pos) }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
91
102
|
end
|
|
92
103
|
end
|
|
93
104
|
end
|
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
|
28
28
|
_, method_name, method_arg = *method
|
|
29
29
|
return if simple_method_arg?(method_arg)
|
|
30
30
|
|
|
31
|
-
return_value =
|
|
31
|
+
return_value = returned_accumulator(body)
|
|
32
32
|
return unless return_value
|
|
33
33
|
return unless first_argument_returned?(args, return_value)
|
|
34
34
|
return if accumulator_param_assigned_to?(body, args)
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
|
47
47
|
_, method_name, method_arg = *method
|
|
48
48
|
return if simple_method_arg?(method_arg)
|
|
49
49
|
|
|
50
|
-
return unless
|
|
50
|
+
return unless returned_accumulator(body)&.source == '_1'
|
|
51
51
|
|
|
52
52
|
message = format(MSG, method: method_name)
|
|
53
53
|
add_offense(method.loc.selector, message: message) do |corrector|
|
|
@@ -112,11 +112,27 @@ module RuboCop
|
|
|
112
112
|
end
|
|
113
113
|
end
|
|
114
114
|
|
|
115
|
-
def
|
|
115
|
+
def returned_accumulator(body)
|
|
116
116
|
return unless body
|
|
117
117
|
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
value = body.begin_type? ? body.children.last : body
|
|
119
|
+
value if value&.lvar_type? && !next_changes_accumulator?(body, value)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def next_changes_accumulator?(body, value)
|
|
123
|
+
body.each_descendant(:next).any? do |jump|
|
|
124
|
+
jump.child_nodes != [value] && next_target(jump).equal?(body.parent)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def next_target(node)
|
|
129
|
+
while (parent = node.parent)
|
|
130
|
+
if (parent.any_block_type? || parent.loop_keyword?) && parent.body.equal?(node)
|
|
131
|
+
return parent
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
node = parent
|
|
135
|
+
end
|
|
120
136
|
end
|
|
121
137
|
|
|
122
138
|
def first_argument_returned?(args, return_value)
|