rubocop 1.92.2 → 1.92.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rubocop/#directive_comment.rb# +224 -0
- data/lib/rubocop/cache_config.rb +3 -0
- data/lib/rubocop/cli.rb +5 -4
- data/lib/rubocop/config.rb +16 -0
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
- data/lib/rubocop/cop/layout/class_structure.rb +91 -13
- data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
- data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +14 -6
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
- data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
- data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
- data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
- data/lib/rubocop/cop/lint/useless_times.rb +13 -1
- data/lib/rubocop/cop/lint/void.rb +20 -9
- data/lib/rubocop/cop/message_annotator.rb +6 -8
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
- data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
- data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
- data/lib/rubocop/cop/mixin/range_help.rb +13 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
- data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/and_or.rb +8 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
- data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
- data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
- data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +16 -4
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
- data/lib/rubocop/cop/style/endless_method.rb +30 -10
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/for.rb +59 -4
- data/lib/rubocop/cop/style/guard_clause.rb +56 -11
- data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
- data/lib/rubocop/cop/style/hash_lookup_method.rb +11 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
- data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
- data/lib/rubocop/cop/style/lambda.rb +13 -5
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_into_array.rb +4 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
- data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +96 -16
- data/lib/rubocop/cop/style/redundant_each.rb +8 -1
- data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
- data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
- data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
- data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- data/lib/rubocop/cop/style/word_array.rb +1 -3
- data/lib/rubocop/cop/util.rb +2 -0
- data/lib/rubocop/cop/variable_force.rb +14 -1
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +13 -6
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/runner.rb +5 -0
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +1 -1
- metadata +4 -3
|
@@ -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
|
|
@@ -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
|
|
|
@@ -150,6 +150,7 @@ module RuboCop
|
|
|
150
150
|
# TODO: More precise handling (rescue, ensure, nested begin, etc.)
|
|
151
151
|
def return_value_node_of_scope(scope)
|
|
152
152
|
body_node = scope.body_node
|
|
153
|
+
return if body_node.nil?
|
|
153
154
|
|
|
154
155
|
if body_node.begin_type?
|
|
155
156
|
body_node.children.last
|
|
@@ -202,6 +203,31 @@ module RuboCop
|
|
|
202
203
|
|
|
203
204
|
def remove_trailing_character_from_operator(corrector, node)
|
|
204
205
|
corrector.remove(node.parent.location.operator.end.adjust(begin_pos: -1))
|
|
206
|
+
|
|
207
|
+
value = node.parent.expression
|
|
208
|
+
corrector.wrap(value, '(', ')') if operand_requires_parentheses?(value)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def operand_requires_parentheses?(node)
|
|
212
|
+
if node.any_block_type?
|
|
213
|
+
return true unless node.braces?
|
|
214
|
+
|
|
215
|
+
node = node.send_node
|
|
216
|
+
end
|
|
217
|
+
return call_requires_parentheses?(node) if node.type?(:call, :yield, :super)
|
|
218
|
+
|
|
219
|
+
return node.op_asgn_type? if node.assignment?
|
|
220
|
+
|
|
221
|
+
!node.basic_literal? && !node.variable? &&
|
|
222
|
+
!node.type?(:const, :self, :begin, :kwbegin, :zsuper, :array, :hash, :dstr, :dsym,
|
|
223
|
+
:xstr, :regexp)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def call_requires_parentheses?(node)
|
|
227
|
+
return true if node.prefix_not?
|
|
228
|
+
return false if node.call_type? && node.assignment_method?
|
|
229
|
+
|
|
230
|
+
node.arguments? && !node.parenthesized? && !node.method?(:[])
|
|
205
231
|
end
|
|
206
232
|
|
|
207
233
|
def replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name)
|
|
@@ -40,7 +40,7 @@ module RuboCop
|
|
|
40
40
|
|
|
41
41
|
# @!method block_reassigns_arg?(node)
|
|
42
42
|
def_node_search :block_reassigns_arg?, <<~PATTERN
|
|
43
|
-
(lvasgn %)
|
|
43
|
+
{(lvasgn % ...) (match_var %) (match_with_lvasgn #named_capture?(%) _)}
|
|
44
44
|
PATTERN
|
|
45
45
|
|
|
46
46
|
def on_send(node)
|
|
@@ -74,9 +74,17 @@ module RuboCop
|
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
def remove_node(corrector, node)
|
|
77
|
+
return if code_after?(node)
|
|
78
|
+
|
|
77
79
|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
|
|
78
80
|
end
|
|
79
81
|
|
|
82
|
+
def code_after?(node)
|
|
83
|
+
last_line = processed_source.buffer.source_line(node.last_line)
|
|
84
|
+
|
|
85
|
+
last_line[node.loc.last_column..].match?(/\A\s*[^\s#]/)
|
|
86
|
+
end
|
|
87
|
+
|
|
80
88
|
def autocorrect_block_pass(corrector, node, proc_name)
|
|
81
89
|
corrector.replace(node, proc_name)
|
|
82
90
|
end
|
|
@@ -112,6 +120,10 @@ module RuboCop
|
|
|
112
120
|
end
|
|
113
121
|
end
|
|
114
122
|
|
|
123
|
+
def named_capture?(regexp, name)
|
|
124
|
+
regexp.to_regexp.names.include?(name.to_s)
|
|
125
|
+
end
|
|
126
|
+
|
|
115
127
|
def fix_indentation(source, range)
|
|
116
128
|
# Cleanup indentation in a multiline block
|
|
117
129
|
source_lines = source.split("\n")
|
|
@@ -75,6 +75,7 @@ module RuboCop
|
|
|
75
75
|
BINARY_OPERATORS = %i[* / % + - == === != < > <= >= <=>].freeze
|
|
76
76
|
UNARY_OPERATORS = %i[+@ -@ ~ !].freeze
|
|
77
77
|
OPERATORS = (BINARY_OPERATORS + UNARY_OPERATORS).freeze
|
|
78
|
+
NON_VALUE_ARGUMENT_TYPES = %i[splat block_pass forwarded_args forwarded_restarg].freeze
|
|
78
79
|
NONMUTATING_METHODS_WITH_BANG_VERSION = %i[capitalize chomp chop compact
|
|
79
80
|
delete_prefix delete_suffix downcase
|
|
80
81
|
encode flatten gsub lstrip merge next
|
|
@@ -153,13 +154,11 @@ module RuboCop
|
|
|
153
154
|
check_expression(case_node.else_branch) if case_node.else_branch
|
|
154
155
|
end
|
|
155
156
|
|
|
156
|
-
# rubocop:disable-next Metrics/
|
|
157
|
+
# rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
157
158
|
def check_void_op(node, &block)
|
|
158
159
|
node = node.children.first while node&.begin_type?
|
|
159
160
|
return unless node&.call_type? && OPERATORS.include?(node.method_name)
|
|
160
|
-
|
|
161
|
-
return
|
|
162
|
-
end
|
|
161
|
+
return unless operation?(node)
|
|
163
162
|
|
|
164
163
|
return if block && yield(node)
|
|
165
164
|
|
|
@@ -169,6 +168,19 @@ module RuboCop
|
|
|
169
168
|
end
|
|
170
169
|
end
|
|
171
170
|
|
|
171
|
+
def operation?(node)
|
|
172
|
+
return UNARY_OPERATORS.include?(node.method_name) || !node.loc.dot if node.arguments.none?
|
|
173
|
+
return false if !node.arguments.one? || trailing_comma?(node)
|
|
174
|
+
|
|
175
|
+
argument = node.first_argument
|
|
176
|
+
|
|
177
|
+
argument.hash_type? ? argument.braces? : !argument.type?(*NON_VALUE_ARGUMENT_TYPES)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def trailing_comma?(node)
|
|
181
|
+
node.last_argument.source_range.end.join(node.source_range.end).source.include?(',')
|
|
182
|
+
end
|
|
183
|
+
|
|
172
184
|
def check_var(node)
|
|
173
185
|
return unless node.variable? || node.const_type?
|
|
174
186
|
|
|
@@ -193,6 +205,8 @@ module RuboCop
|
|
|
193
205
|
end
|
|
194
206
|
|
|
195
207
|
add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
|
|
208
|
+
next if node.each_descendant(:begin).any?
|
|
209
|
+
|
|
196
210
|
autocorrect_void_expression(corrector, node)
|
|
197
211
|
end
|
|
198
212
|
end
|
|
@@ -282,11 +296,8 @@ module RuboCop
|
|
|
282
296
|
end
|
|
283
297
|
|
|
284
298
|
def autocorrect_nonmutating_send(corrector, node, suggestion)
|
|
285
|
-
send_node =
|
|
286
|
-
|
|
287
|
-
else
|
|
288
|
-
node.send_node
|
|
289
|
-
end
|
|
299
|
+
send_node = node.call_type? ? node : node.send_node
|
|
300
|
+
|
|
290
301
|
corrector.replace(send_node.loc.selector, suggestion)
|
|
291
302
|
end
|
|
292
303
|
|
|
@@ -75,14 +75,12 @@ module RuboCop
|
|
|
75
75
|
url = cop_config['StyleGuide']
|
|
76
76
|
return nil if url.nil? || url.empty?
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
end
|
|
85
|
-
end
|
|
78
|
+
base_url = style_guide_base_url
|
|
79
|
+
return url if base_url.nil? || base_url.empty?
|
|
80
|
+
|
|
81
|
+
# Departments can have style guides of their own, so the same guideline
|
|
82
|
+
# can resolve against different bases.
|
|
83
|
+
self.class.style_guide_urls[[base_url, url]] ||= URI.join(base_url, url).to_s
|
|
86
84
|
end
|
|
87
85
|
|
|
88
86
|
# Returns the base style guide URL from AllCops or the specific department
|