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
|
@@ -21,6 +21,10 @@ module RuboCop
|
|
|
21
21
|
|
|
22
22
|
MSG = '`%<argument>s` interpreted as grouped expression.'
|
|
23
23
|
|
|
24
|
+
def self.autocorrect_incompatible_with
|
|
25
|
+
[Style::RedundantParentheses]
|
|
26
|
+
end
|
|
27
|
+
|
|
24
28
|
def on_send(node)
|
|
25
29
|
return if valid_context?(node)
|
|
26
30
|
|
|
@@ -54,15 +58,46 @@ module RuboCop
|
|
|
54
58
|
end
|
|
55
59
|
|
|
56
60
|
def invalid_bare_argument?(node)
|
|
57
|
-
|
|
61
|
+
return true if multiple_statements?(node) || not_a_single_expression?(node)
|
|
62
|
+
|
|
63
|
+
node = innermost_expression(node)
|
|
58
64
|
return false unless node
|
|
65
|
+
return true if multiple_values_assignment?(node)
|
|
59
66
|
|
|
60
67
|
keyword_operator?(node) || modifier_expression?(node) || command_call_operand?(node)
|
|
61
68
|
end
|
|
62
69
|
|
|
70
|
+
def multiple_statements?(node)
|
|
71
|
+
node.begin_type? && node.children.size > 1
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def not_a_single_expression?(node)
|
|
75
|
+
return false unless node.begin_type?
|
|
76
|
+
|
|
77
|
+
expression = node.children.first
|
|
78
|
+
return true unless expression
|
|
79
|
+
|
|
80
|
+
processed_source.tokens_within(node).any? do |token|
|
|
81
|
+
token.semicolon? && !expression.source_range.contains?(token.pos)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def innermost_expression(node)
|
|
86
|
+
node = node.children.first while node&.begin_type? && node.children.one?
|
|
87
|
+
node
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def multiple_values_assignment?(node)
|
|
91
|
+
return true if node.masgn_type?
|
|
92
|
+
return false unless node.assignment? || (node.call_type? && node.assignment_method?)
|
|
93
|
+
|
|
94
|
+
value = node.children.last
|
|
95
|
+
value.array_type? && !value.bracketed?
|
|
96
|
+
end
|
|
97
|
+
|
|
63
98
|
def keyword_operator?(node)
|
|
64
99
|
(node.operator_keyword? && node.semantic_operator?) ||
|
|
65
|
-
node.rescue_type? || (node.send_type? && node.prefix_not?)
|
|
100
|
+
node.rescue_type? || node.match_pattern_p_type? || (node.send_type? && node.prefix_not?)
|
|
66
101
|
end
|
|
67
102
|
|
|
68
103
|
def modifier_expression?(node)
|
|
@@ -75,7 +110,7 @@ module RuboCop
|
|
|
75
110
|
elsif node.send_type? && node.prefix_bang?
|
|
76
111
|
command_call_value?(node.receiver)
|
|
77
112
|
else
|
|
78
|
-
|
|
113
|
+
command_call_with_do_block?(node)
|
|
79
114
|
end
|
|
80
115
|
end
|
|
81
116
|
|
|
@@ -90,6 +125,13 @@ module RuboCop
|
|
|
90
125
|
node.arguments? && !node.parenthesized? && !node.operator_method?
|
|
91
126
|
end
|
|
92
127
|
|
|
128
|
+
def command_call_with_do_block?(node)
|
|
129
|
+
node = node.receiver while node.call_type? && node.receiver
|
|
130
|
+
return false unless node.any_block_type?
|
|
131
|
+
|
|
132
|
+
node.keywords? && command_call?(node)
|
|
133
|
+
end
|
|
134
|
+
|
|
93
135
|
def chained_calls?(node)
|
|
94
136
|
first_argument = node.first_argument
|
|
95
137
|
first_argument.call_type? && (node.children.last&.children&.count || 0) > 1
|
|
@@ -111,12 +111,25 @@ module RuboCop
|
|
|
111
111
|
# offenses, so its directives cannot be judged - they typically
|
|
112
112
|
# prepare the code for the moment the cop gets enabled.
|
|
113
113
|
next if pending_cop_not_run?(cop) || disabled_by_preview?(cop)
|
|
114
|
+
next if excluded_from_run?(cop)
|
|
114
115
|
|
|
115
116
|
each_already_disabled(cop, line_ranges, &block)
|
|
116
117
|
each_line_range(cop, line_ranges, &block)
|
|
117
118
|
end
|
|
118
119
|
end
|
|
119
120
|
|
|
121
|
+
# A cop left out with `--except` produces no offenses in this run, but
|
|
122
|
+
# its directives are still needed in a full run.
|
|
123
|
+
def excluded_from_run?(cop)
|
|
124
|
+
Badge.parse(cop).match_name?(@options[:except])
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def department_partly_excluded_from_run?(department)
|
|
128
|
+
Array(@options[:except]).any? do |name|
|
|
129
|
+
name == department || name.start_with?("#{department}/")
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
120
133
|
def pending_cop_not_run?(cop)
|
|
121
134
|
# `Config#for_cop` corrects wrongly-namespaced names, but a name that
|
|
122
135
|
# is not registered as written never suppresses anything, so it is
|
|
@@ -255,6 +268,9 @@ module RuboCop
|
|
|
255
268
|
end
|
|
256
269
|
|
|
257
270
|
def find_redundant_all(range, next_range)
|
|
271
|
+
# The cops left out with `--except` may have offenses in the range.
|
|
272
|
+
return if @options[:except]
|
|
273
|
+
|
|
258
274
|
# If there's a disable all comment followed by a comment
|
|
259
275
|
# specifically disabling `cop`, we don't report the `all`
|
|
260
276
|
# comment. If the disable all comment is truly redundant, we will
|
|
@@ -266,6 +282,8 @@ module RuboCop
|
|
|
266
282
|
|
|
267
283
|
def find_redundant_department(cop, range)
|
|
268
284
|
department = cop.split('/').first
|
|
285
|
+
return if department_partly_excluded_from_run?(department)
|
|
286
|
+
|
|
269
287
|
offenses = offenses_to_check.select { |offense| offense.cop_name.start_with?(department) }
|
|
270
288
|
add_department_marker(department) if range_with_offense?(range, offenses)
|
|
271
289
|
end
|
|
@@ -76,7 +76,15 @@ module RuboCop
|
|
|
76
76
|
|
|
77
77
|
def register_orphan_pop(directive)
|
|
78
78
|
add_offense(directive.range, message: MSG_ORPHAN_POP) do |corrector|
|
|
79
|
-
corrector
|
|
79
|
+
remove_directive(corrector, directive.range_with_reason, directive.comment)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def remove_directive(corrector, range, comment)
|
|
84
|
+
if range.end.join(comment.source_range.end).source.match?(/\A\s*(?:#|\z)/)
|
|
85
|
+
corrector.remove(range_with_surrounding_space(range, side: :right))
|
|
86
|
+
else
|
|
87
|
+
corrector.replace(range, '#')
|
|
80
88
|
end
|
|
81
89
|
end
|
|
82
90
|
|
|
@@ -89,18 +97,18 @@ module RuboCop
|
|
|
89
97
|
range_of_offense(comment, name),
|
|
90
98
|
message: format(MSG, cop: all_or_name(name))
|
|
91
99
|
) do |corrector|
|
|
92
|
-
corrector
|
|
100
|
+
correct_directive(corrector, directive, cop_names, name)
|
|
93
101
|
end
|
|
94
102
|
end
|
|
95
103
|
end
|
|
96
104
|
|
|
97
|
-
# When every cop on the directive is redundant the whole
|
|
105
|
+
# When every cop on the directive is redundant the whole directive goes, `--` reason
|
|
98
106
|
# included; otherwise just the one cop is spliced out of the list.
|
|
99
|
-
def
|
|
107
|
+
def correct_directive(corrector, directive, cop_names, name)
|
|
100
108
|
if directive.match?(cop_names)
|
|
101
|
-
|
|
109
|
+
remove_directive(corrector, directive.range_with_reason, directive.comment)
|
|
102
110
|
else
|
|
103
|
-
range_with_comma(comment, name)
|
|
111
|
+
corrector.remove(range_with_comma(directive.comment, name))
|
|
104
112
|
end
|
|
105
113
|
end
|
|
106
114
|
|
|
@@ -38,6 +38,10 @@ module RuboCop
|
|
|
38
38
|
RESTRICT_ON_SEND = %i[sort].freeze
|
|
39
39
|
GLOB_METHODS = %i[glob []].freeze
|
|
40
40
|
|
|
41
|
+
def self.autocorrect_incompatible_with
|
|
42
|
+
[Layout::DotPosition]
|
|
43
|
+
end
|
|
44
|
+
|
|
41
45
|
def on_send(node)
|
|
42
46
|
return unless dir_glob?(node.receiver)
|
|
43
47
|
# `sort` with a comparator block or block-pass changes the order, so it is
|
|
@@ -75,9 +79,23 @@ module RuboCop
|
|
|
75
79
|
corrector.remove(dot)
|
|
76
80
|
corrector.remove(selector)
|
|
77
81
|
else
|
|
78
|
-
corrector.remove(
|
|
82
|
+
corrector.remove(removal_range(range))
|
|
79
83
|
end
|
|
80
84
|
end
|
|
85
|
+
|
|
86
|
+
def removal_range(range)
|
|
87
|
+
range_with_space = range_with_surrounding_space(range, side: :left)
|
|
88
|
+
return range if comment_line_joined_with_code?(range_with_space, range)
|
|
89
|
+
|
|
90
|
+
range_with_space
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def comment_line_joined_with_code?(range_with_space, range)
|
|
94
|
+
comment = processed_source.comment_at_line(range_with_space.line)
|
|
95
|
+
return false if comment&.source_range&.end_pos != range_with_space.begin_pos
|
|
96
|
+
|
|
97
|
+
!processed_source.lines[range.last_line - 1][range.last_column..].strip.empty?
|
|
98
|
+
end
|
|
81
99
|
end
|
|
82
100
|
end
|
|
83
101
|
end
|
|
@@ -33,11 +33,15 @@ module RuboCop
|
|
|
33
33
|
# require 'unloaded_feature'
|
|
34
34
|
class RedundantRequireStatement < Base
|
|
35
35
|
include RangeHelp
|
|
36
|
+
include RescueNode
|
|
36
37
|
extend AutoCorrector
|
|
37
38
|
|
|
38
39
|
MSG = 'Remove unnecessary `require` statement.'
|
|
39
40
|
RESTRICT_ON_SEND = %i[require].freeze
|
|
40
41
|
RUBY_22_LOADED_FEATURES = %w[rational complex].freeze
|
|
42
|
+
BODY_PARENT_TYPES = %i[
|
|
43
|
+
class module sclass any_def any_block when in_pattern for resbody
|
|
44
|
+
].freeze
|
|
41
45
|
|
|
42
46
|
# @!method redundant_require_statement?(node)
|
|
43
47
|
def_node_matcher :redundant_require_statement?, <<~PATTERN
|
|
@@ -47,6 +51,7 @@ module RuboCop
|
|
|
47
51
|
|
|
48
52
|
def on_send(node)
|
|
49
53
|
return unless redundant_require_statement?(node)
|
|
54
|
+
return unless statement?(node)
|
|
50
55
|
|
|
51
56
|
add_offense(node) do |corrector|
|
|
52
57
|
if node.parent.respond_to?(:modifier_form?) && node.parent.modifier_form?
|
|
@@ -54,7 +59,7 @@ module RuboCop
|
|
|
54
59
|
|
|
55
60
|
range = range_with_surrounding_space(node.source_range, side: :right)
|
|
56
61
|
else
|
|
57
|
-
range =
|
|
62
|
+
range = range_for_statement_removal(node.source_range)
|
|
58
63
|
end
|
|
59
64
|
|
|
60
65
|
corrector.remove(range)
|
|
@@ -73,6 +78,27 @@ module RuboCop
|
|
|
73
78
|
(target_ruby_version >= 3.2 && feature_name == 'set') ||
|
|
74
79
|
(target_ruby_version >= 4.0 && feature_name == 'pathname')
|
|
75
80
|
end
|
|
81
|
+
|
|
82
|
+
def statement?(node)
|
|
83
|
+
parent = node.parent
|
|
84
|
+
return true unless parent
|
|
85
|
+
return false if value_only_form?(parent)
|
|
86
|
+
return true if parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
87
|
+
return !parent.condition.equal?(node) if parent.type?(:if, :while, :until)
|
|
88
|
+
return parent.else_branch.equal?(node) if parent.type?(:case, :case_match)
|
|
89
|
+
|
|
90
|
+
parent.type?(*BODY_PARENT_TYPES) && parent.body.equal?(node)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def value_only_form?(node)
|
|
94
|
+
case node.type
|
|
95
|
+
when :if then node.ternary?
|
|
96
|
+
when :rescue then rescue_modifier?(node.resbody_branches.first)
|
|
97
|
+
when :resbody then rescue_modifier?(node)
|
|
98
|
+
when :def, :defs then node.endless?
|
|
99
|
+
else false
|
|
100
|
+
end
|
|
101
|
+
end
|
|
76
102
|
end
|
|
77
103
|
end
|
|
78
104
|
end
|
|
@@ -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
|
|
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
|
28
28
|
# x&.foo || bar
|
|
29
29
|
class SafeNavigationChain < Base
|
|
30
30
|
include NilMethods
|
|
31
|
+
include RescueNode
|
|
31
32
|
extend AutoCorrector
|
|
32
33
|
extend TargetRubyVersion
|
|
33
34
|
|
|
@@ -35,6 +36,9 @@ module RuboCop
|
|
|
35
36
|
|
|
36
37
|
MSG = 'Do not chain ordinary method call after safe navigation operator.'
|
|
37
38
|
PLUS_MINUS_METHODS = %i[+@ -@].freeze
|
|
39
|
+
BODY_PARENT_TYPES = %i[
|
|
40
|
+
block numblock itblock when in_pattern while until class module sclass
|
|
41
|
+
].to_set.freeze
|
|
38
42
|
|
|
39
43
|
# @!method bad_method?(node)
|
|
40
44
|
def_node_matcher :bad_method?, <<~PATTERN
|
|
@@ -121,18 +125,67 @@ module RuboCop
|
|
|
121
125
|
end
|
|
122
126
|
|
|
123
127
|
def require_parentheses?(send_node)
|
|
124
|
-
return
|
|
125
|
-
return false unless send_node.comparison_method?
|
|
126
|
-
return false unless (node = send_node.parent)
|
|
128
|
+
return false if send_node.loc.dot || brackets?(send_node)
|
|
127
129
|
|
|
128
|
-
|
|
129
|
-
(node.respond_to?(:comparison_method?) && node.comparison_method?)
|
|
130
|
+
!command_call_allowed?(send_node)
|
|
130
131
|
end
|
|
131
132
|
|
|
132
|
-
def
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
def command_call_allowed?(node)
|
|
134
|
+
return true unless (parent = node.parent)
|
|
135
|
+
return sole_argument?(node, parent) if argument_taking?(parent)
|
|
136
|
+
return true if condition?(node, parent)
|
|
137
|
+
|
|
138
|
+
statement?(node, parent)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def argument_taking?(node)
|
|
142
|
+
return node.type?(:yield, :super, :return, :break, :next) unless node.call_type?
|
|
143
|
+
|
|
144
|
+
!node.assignment? && !node.operator_method?
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def sole_argument?(node, parent)
|
|
148
|
+
return false unless parent.arguments.one?
|
|
149
|
+
return false unless node.equal?(parent.first_argument)
|
|
150
|
+
return false if parent.parenthesized? && parent.multiline?
|
|
151
|
+
|
|
152
|
+
!processed_source.buffer.source.match?(/\G\s*,/, node.source_range.end_pos)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def condition?(node, parent)
|
|
156
|
+
case parent.type
|
|
157
|
+
when :and, :or then parent.semantic_operator?
|
|
158
|
+
when :if then !parent.ternary? && node.equal?(parent.condition)
|
|
159
|
+
when :while, :until, :while_post, :until_post, :case, :case_match
|
|
160
|
+
node.equal?(parent.condition)
|
|
161
|
+
else false
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def statement?(node, parent)
|
|
166
|
+
return true if parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
167
|
+
return node.equal?(parent.body) if BODY_PARENT_TYPES.include?(parent.type)
|
|
168
|
+
return branch?(node, parent) if parent.type?(:if, :case, :case_match)
|
|
169
|
+
|
|
170
|
+
case parent.type
|
|
171
|
+
when :def, :defs then !parent.endless?
|
|
172
|
+
when :resbody then node.equal?(parent.body) && !rescue_modifier?(parent)
|
|
173
|
+
else assigned_value_in_statement?(node, parent)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def branch?(node, parent)
|
|
178
|
+
return false if parent.if_type? && parent.ternary?
|
|
179
|
+
|
|
180
|
+
!node.equal?(parent.condition)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def assigned_value_in_statement?(node, parent)
|
|
184
|
+
return false unless parent.assignment?
|
|
185
|
+
return false unless node.equal?(parent.children.last)
|
|
186
|
+
return true unless (grandparent = parent.parent)
|
|
187
|
+
|
|
188
|
+
statement?(parent, grandparent)
|
|
136
189
|
end
|
|
137
190
|
end
|
|
138
191
|
end
|
|
@@ -57,7 +57,7 @@ module RuboCop
|
|
|
57
57
|
|
|
58
58
|
bad_location = bad_location(node)
|
|
59
59
|
add_offense(bad_location, message: message) do |corrector|
|
|
60
|
-
corrector.replace(bad_location,
|
|
60
|
+
corrector.replace(bad_location, replacement(node, method, module_names_source))
|
|
61
61
|
end
|
|
62
62
|
end
|
|
63
63
|
end
|
|
@@ -74,6 +74,26 @@ module RuboCop
|
|
|
74
74
|
format(MSG, method: method, module_name: module_name, bad_method: bad_method)
|
|
75
75
|
end
|
|
76
76
|
|
|
77
|
+
def replacement(node, method, module_names_source)
|
|
78
|
+
if statement?(node)
|
|
79
|
+
"#{method} #{module_names_source}"
|
|
80
|
+
else
|
|
81
|
+
"#{method}(#{module_names_source})"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def statement?(node)
|
|
86
|
+
parent = node.parent
|
|
87
|
+
return true if parent.nil? || parent.type?(:begin, :kwbegin)
|
|
88
|
+
return body?(parent, node) if parent.type?(:class, :module, :sclass, :any_def, :any_block)
|
|
89
|
+
|
|
90
|
+
parent.if_type? && !parent.ternary? && !parent.condition.equal?(node)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def body?(parent, node)
|
|
94
|
+
parent.body.equal?(node) && !(parent.any_def_type? && parent.endless?)
|
|
95
|
+
end
|
|
96
|
+
|
|
77
97
|
def mixin_method?(node)
|
|
78
98
|
MIXIN_METHODS.include?(node.to_sym)
|
|
79
99
|
end
|
|
@@ -34,6 +34,10 @@ module RuboCop
|
|
|
34
34
|
MSG = 'Avoid leaving a trailing comma in attribute declarations.'
|
|
35
35
|
RESTRICT_ON_SEND = %i[attr_reader attr_writer attr_accessor attr].freeze
|
|
36
36
|
|
|
37
|
+
def self.autocorrect_incompatible_with
|
|
38
|
+
[Style::MethodCallWithArgsParentheses]
|
|
39
|
+
end
|
|
40
|
+
|
|
37
41
|
def on_send(node)
|
|
38
42
|
return unless node.attribute_accessor? && node.last_argument.any_def_type?
|
|
39
43
|
# A lone `def` argument (e.g. `attr_reader def foo; end`) has no preceding
|
|
@@ -44,6 +44,10 @@ module RuboCop
|
|
|
44
44
|
|
|
45
45
|
MSG = 'Delimiting a string with multiple quotes has no effect, use a single quote instead.'
|
|
46
46
|
|
|
47
|
+
def self.autocorrect_incompatible_with
|
|
48
|
+
[Lint::ImplicitStringConcatenation]
|
|
49
|
+
end
|
|
50
|
+
|
|
47
51
|
def on_dstr(node)
|
|
48
52
|
return if (empty_str_nodes = empty_str_nodes(node)).none?
|
|
49
53
|
|
|
@@ -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
|
|
@@ -150,6 +161,7 @@ module RuboCop
|
|
|
150
161
|
# TODO: More precise handling (rescue, ensure, nested begin, etc.)
|
|
151
162
|
def return_value_node_of_scope(scope)
|
|
152
163
|
body_node = scope.body_node
|
|
164
|
+
return if body_node.nil?
|
|
153
165
|
|
|
154
166
|
if body_node.begin_type?
|
|
155
167
|
body_node.children.last
|
|
@@ -202,6 +214,31 @@ module RuboCop
|
|
|
202
214
|
|
|
203
215
|
def remove_trailing_character_from_operator(corrector, node)
|
|
204
216
|
corrector.remove(node.parent.location.operator.end.adjust(begin_pos: -1))
|
|
217
|
+
|
|
218
|
+
value = node.parent.expression
|
|
219
|
+
corrector.wrap(value, '(', ')') if operand_requires_parentheses?(value)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def operand_requires_parentheses?(node)
|
|
223
|
+
if node.any_block_type?
|
|
224
|
+
return true unless node.braces?
|
|
225
|
+
|
|
226
|
+
node = node.send_node
|
|
227
|
+
end
|
|
228
|
+
return call_requires_parentheses?(node) if node.type?(:call, :yield, :super)
|
|
229
|
+
|
|
230
|
+
return node.op_asgn_type? if node.assignment?
|
|
231
|
+
|
|
232
|
+
!node.basic_literal? && !node.variable? &&
|
|
233
|
+
!node.type?(:const, :self, :begin, :kwbegin, :zsuper, :array, :hash, :dstr, :dsym,
|
|
234
|
+
:xstr, :regexp)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def call_requires_parentheses?(node)
|
|
238
|
+
return true if node.prefix_not?
|
|
239
|
+
return false if node.call_type? && node.assignment_method?
|
|
240
|
+
|
|
241
|
+
node.arguments? && !node.parenthesized? && !node.method?(:[])
|
|
205
242
|
end
|
|
206
243
|
|
|
207
244
|
def replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name)
|
|
@@ -219,11 +256,44 @@ module RuboCop
|
|
|
219
256
|
if expression.array_type? && !expression.bracketed?
|
|
220
257
|
corrector.replace(assignment_part, '[')
|
|
221
258
|
corrector.insert_after(expression, ']')
|
|
259
|
+
elsif parentheses_required?(node, expression)
|
|
260
|
+
corrector.replace(assignment_part, '(')
|
|
261
|
+
corrector.insert_after(expression, ')')
|
|
222
262
|
else
|
|
223
263
|
corrector.remove(assignment_part)
|
|
224
264
|
end
|
|
225
265
|
end
|
|
226
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
|
+
|
|
227
297
|
def variable_in_loop_condition?(assignment_node, variable)
|
|
228
298
|
# A method defined inside the loop has its own scope, so the loop condition
|
|
229
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
|