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,9 +21,16 @@ module RuboCop
|
|
|
21
21
|
# @!method constant_definition?(node)
|
|
22
22
|
def_node_matcher :constant_definition?, '{class module}'
|
|
23
23
|
|
|
24
|
+
# @!method access_modifier_send?(node)
|
|
25
|
+
def_node_matcher :access_modifier_send?, '(send nil? {:private :protected :public})'
|
|
26
|
+
|
|
24
27
|
# @!method empty_line_required?(node)
|
|
25
|
-
def_node_matcher :empty_line_required?,
|
|
26
|
-
|
|
28
|
+
def_node_matcher :empty_line_required?, <<~PATTERN
|
|
29
|
+
{
|
|
30
|
+
any_def class module #access_modifier_send?
|
|
31
|
+
[{(if _ #access_modifier_send? nil?) (if _ nil? #access_modifier_send?)} modifier_form?]
|
|
32
|
+
}
|
|
33
|
+
PATTERN
|
|
27
34
|
|
|
28
35
|
def check(node, body, adjusted_first_line: nil)
|
|
29
36
|
return if valid_body_style?(body)
|
|
@@ -39,15 +46,15 @@ module RuboCop
|
|
|
39
46
|
when :empty_lines_special
|
|
40
47
|
check_empty_lines_special(body, first_line, last_line)
|
|
41
48
|
else
|
|
42
|
-
check_both(style, first_line, last_line)
|
|
49
|
+
check_both(style, body, first_line, last_line)
|
|
43
50
|
end
|
|
44
51
|
end
|
|
45
52
|
|
|
46
53
|
def check_empty_lines_except_namespace(body, first_line, last_line)
|
|
47
54
|
if namespace?(body, with_one_child: true)
|
|
48
|
-
check_both(:no_empty_lines, first_line, last_line)
|
|
55
|
+
check_both(:no_empty_lines, body, first_line, last_line)
|
|
49
56
|
else
|
|
50
|
-
check_both(:empty_lines, first_line, last_line)
|
|
57
|
+
check_both(:empty_lines, body, first_line, last_line)
|
|
51
58
|
end
|
|
52
59
|
end
|
|
53
60
|
|
|
@@ -55,37 +62,41 @@ module RuboCop
|
|
|
55
62
|
return unless body
|
|
56
63
|
|
|
57
64
|
if namespace?(body, with_one_child: true)
|
|
58
|
-
check_both(:no_empty_lines, first_line, last_line)
|
|
65
|
+
check_both(:no_empty_lines, body, first_line, last_line)
|
|
59
66
|
else
|
|
60
67
|
if first_child_requires_empty_line?(body)
|
|
61
|
-
check_beginning(:empty_lines, first_line)
|
|
68
|
+
check_beginning(:empty_lines, body, first_line)
|
|
62
69
|
else
|
|
63
|
-
check_beginning(:no_empty_lines, first_line)
|
|
70
|
+
check_beginning(:no_empty_lines, body, first_line)
|
|
64
71
|
check_deferred_empty_line(body)
|
|
65
72
|
end
|
|
66
|
-
check_ending(:empty_lines, last_line)
|
|
73
|
+
check_ending(:empty_lines, body, last_line)
|
|
67
74
|
end
|
|
68
75
|
end
|
|
69
76
|
|
|
70
|
-
def check_both(style, first_line, last_line)
|
|
77
|
+
def check_both(style, body, first_line, last_line)
|
|
71
78
|
case style
|
|
72
79
|
when :beginning_only
|
|
73
|
-
check_beginning(:empty_lines, first_line)
|
|
74
|
-
check_ending(:no_empty_lines, last_line)
|
|
80
|
+
check_beginning(:empty_lines, body, first_line)
|
|
81
|
+
check_ending(:no_empty_lines, body, last_line)
|
|
75
82
|
when :ending_only
|
|
76
|
-
check_beginning(:no_empty_lines, first_line)
|
|
77
|
-
check_ending(:empty_lines, last_line)
|
|
83
|
+
check_beginning(:no_empty_lines, body, first_line)
|
|
84
|
+
check_ending(:empty_lines, body, last_line)
|
|
78
85
|
else
|
|
79
|
-
check_beginning(style, first_line)
|
|
80
|
-
check_ending(style, last_line)
|
|
86
|
+
check_beginning(style, body, first_line)
|
|
87
|
+
check_ending(style, body, last_line)
|
|
81
88
|
end
|
|
82
89
|
end
|
|
83
90
|
|
|
84
|
-
def check_beginning(style, first_line)
|
|
91
|
+
def check_beginning(style, body, first_line)
|
|
92
|
+
return if style == :empty_lines && body&.first_line == first_line
|
|
93
|
+
|
|
85
94
|
check_source(style, first_line, 'beginning')
|
|
86
95
|
end
|
|
87
96
|
|
|
88
|
-
def check_ending(style, last_line)
|
|
97
|
+
def check_ending(style, body, last_line)
|
|
98
|
+
return if style == :empty_lines && body&.last_line == last_line
|
|
99
|
+
|
|
89
100
|
check_source(style, last_line - 2, 'end')
|
|
90
101
|
end
|
|
91
102
|
|
|
@@ -119,6 +130,7 @@ module RuboCop
|
|
|
119
130
|
def check_deferred_empty_line(body)
|
|
120
131
|
node = first_empty_line_required_child(body)
|
|
121
132
|
return unless node
|
|
133
|
+
return if same_line_as_previous_statement?(node)
|
|
122
134
|
|
|
123
135
|
line = previous_line_ignoring_comments(node.first_line)
|
|
124
136
|
return if processed_source[line].empty?
|
|
@@ -156,6 +168,10 @@ module RuboCop
|
|
|
156
168
|
end
|
|
157
169
|
end
|
|
158
170
|
|
|
171
|
+
def same_line_as_previous_statement?(node)
|
|
172
|
+
node.left_sibling.last_line == node.first_line
|
|
173
|
+
end
|
|
174
|
+
|
|
159
175
|
def previous_line_ignoring_comments(send_line)
|
|
160
176
|
(send_line - 2).downto(0) do |line|
|
|
161
177
|
return line unless comment_line?(processed_source[line])
|
|
@@ -5,6 +5,10 @@ module RuboCop
|
|
|
5
5
|
# Common functionality for checking whether a method body can become the
|
|
6
6
|
# body of an endless method definition without changing its meaning.
|
|
7
7
|
module EndlessMethodBodyHelp
|
|
8
|
+
STATEMENT_BODY_PARENT_TYPES = %i[
|
|
9
|
+
resbody when in_pattern while until for class module sclass any_block
|
|
10
|
+
].freeze
|
|
11
|
+
|
|
8
12
|
private
|
|
9
13
|
|
|
10
14
|
def endless_method_body?(body)
|
|
@@ -45,7 +49,7 @@ module RuboCop
|
|
|
45
49
|
|
|
46
50
|
def command_disallowed_in_endless_body?(body)
|
|
47
51
|
return true if body.assignment? && command_call?(assigned_value(body))
|
|
48
|
-
return true if
|
|
52
|
+
return true if body_disallowed_outside_statement?(body)
|
|
49
53
|
return true if negated_command_call?(body)
|
|
50
54
|
|
|
51
55
|
body.each_node(:any_block).any? do |block|
|
|
@@ -53,20 +57,53 @@ module RuboCop
|
|
|
53
57
|
end
|
|
54
58
|
end
|
|
55
59
|
|
|
56
|
-
def
|
|
57
|
-
|
|
60
|
+
def body_disallowed_outside_statement?(body)
|
|
61
|
+
return true if endless_method_with_command_body?(body)
|
|
62
|
+
|
|
63
|
+
command_body_outside_statement?(body) || do_end_block_body_in_command_argument?(body)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def endless_method_with_command_body?(node)
|
|
67
|
+
node.any_def_type? && node.endless? && command_call?(node.body)
|
|
58
68
|
end
|
|
59
69
|
|
|
60
|
-
|
|
61
|
-
def command_body_in_argument?(body)
|
|
70
|
+
def command_body_outside_statement?(body)
|
|
62
71
|
return false unless command_call?(body)
|
|
72
|
+
return false if command_position?(body.parent)
|
|
63
73
|
|
|
64
74
|
call = argument_receiving_call(body.parent)
|
|
65
|
-
return
|
|
75
|
+
return true if call.nil?
|
|
66
76
|
|
|
67
77
|
target_ruby_version < 4.0 || !call.arguments.one?
|
|
68
78
|
end
|
|
69
79
|
|
|
80
|
+
def command_position?(def_node)
|
|
81
|
+
parent = def_node.parent
|
|
82
|
+
return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
83
|
+
return false if parent.masgn_type?
|
|
84
|
+
return true if parent.assignment?
|
|
85
|
+
return parent.last_argument.equal?(def_node) if setter_call?(parent)
|
|
86
|
+
|
|
87
|
+
statement_body?(def_node, parent)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def setter_call?(node)
|
|
91
|
+
node.call_type? && node.assignment_method?
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def statement_body?(def_node, parent)
|
|
95
|
+
return def_node.equal?(parent.body) if parent.type?(*STATEMENT_BODY_PARENT_TYPES)
|
|
96
|
+
|
|
97
|
+
case parent.type
|
|
98
|
+
when :if
|
|
99
|
+
!parent.ternary? && !def_node.equal?(parent.condition)
|
|
100
|
+
when :case, :case_match
|
|
101
|
+
def_node.equal?(parent.else_branch)
|
|
102
|
+
else
|
|
103
|
+
false
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
70
107
|
def argument_receiving_call(def_node)
|
|
71
108
|
call = def_node.parent
|
|
72
109
|
return unless call&.call_type?
|
|
@@ -90,8 +127,10 @@ module RuboCop
|
|
|
90
127
|
node
|
|
91
128
|
end
|
|
92
129
|
|
|
130
|
+
# `return foo` is a command too, even as `return(foo)`.
|
|
93
131
|
def command_call?(node)
|
|
94
132
|
node = node.send_node if node.any_block_type?
|
|
133
|
+
return node.arguments? if node.return_type?
|
|
95
134
|
return false unless node.type?(:call, :super, :yield)
|
|
96
135
|
|
|
97
136
|
node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
|
|
@@ -77,6 +77,7 @@ module RuboCop
|
|
|
77
77
|
add_offense(node.value, message: message) do |corrector|
|
|
78
78
|
corrector.replace(node, replacement)
|
|
79
79
|
|
|
80
|
+
next if node.value_omission?
|
|
80
81
|
next unless (def_node = def_node_that_require_parentheses(node))
|
|
81
82
|
|
|
82
83
|
last_argument = def_node.last_argument
|
|
@@ -16,6 +16,7 @@ module RuboCop
|
|
|
16
16
|
# to adjust the brace layout because this will result in invalid code.
|
|
17
17
|
return if last_line_heredoc?(node.children.last)
|
|
18
18
|
return if heredoc_opened_on_closing_brace_line?(node)
|
|
19
|
+
return if heredoc_method_call_to_be_repositioned?(node.children.last)
|
|
19
20
|
|
|
20
21
|
check(node)
|
|
21
22
|
end
|
|
@@ -145,6 +146,17 @@ module RuboCop
|
|
|
145
146
|
str.heredoc? && str.first_line == closing_brace_line
|
|
146
147
|
end
|
|
147
148
|
end
|
|
149
|
+
|
|
150
|
+
def heredoc_method_call_to_be_repositioned?(node)
|
|
151
|
+
return false unless config.cop_enabled?('Lint/HeredocMethodCallPosition')
|
|
152
|
+
|
|
153
|
+
node.each_node(:call).any? do |call_node|
|
|
154
|
+
receiver = call_node.receiver
|
|
155
|
+
next false unless receiver&.any_str_type? && receiver.heredoc?
|
|
156
|
+
|
|
157
|
+
receiver.loc.heredoc_end.end_pos < call_node.source_range.end_pos
|
|
158
|
+
end
|
|
159
|
+
end
|
|
148
160
|
end
|
|
149
161
|
end
|
|
150
162
|
end
|
|
@@ -77,6 +77,27 @@ module RuboCop
|
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
+
# Builds a double-quoted string literal for an interpolated element of a `%W` or `%I`
|
|
81
|
+
# array. Only the text around the interpolations is escaped, the code inside them is
|
|
82
|
+
# kept as written.
|
|
83
|
+
# @param [RuboCop::AST::Node] node a `dstr` or `dsym` node
|
|
84
|
+
# @return [String]
|
|
85
|
+
def build_interpolated_string_literal(node)
|
|
86
|
+
contents = node.children.map do |child|
|
|
87
|
+
if child.str_type?
|
|
88
|
+
child.value.inspect[1..-2]
|
|
89
|
+
elsif child.begin_type?
|
|
90
|
+
child.source
|
|
91
|
+
else
|
|
92
|
+
# A variable interpolated without braces, such as `#@foo`. The braces keep
|
|
93
|
+
# the text that follows from becoming part of the variable name.
|
|
94
|
+
"\#{#{child.source}}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
"\"#{contents.join}\""
|
|
99
|
+
end
|
|
100
|
+
|
|
80
101
|
# @param [RuboCop::AST::ArrayNode] node
|
|
81
102
|
# @param [Array<String>] elements
|
|
82
103
|
# @return [String]
|
|
@@ -97,7 +118,7 @@ module RuboCop
|
|
|
97
118
|
# @return [String]
|
|
98
119
|
def whitespace_between(node)
|
|
99
120
|
if node.children.length >= 2
|
|
100
|
-
node.children[0].source_range.end.join(node.children[1]
|
|
121
|
+
node.children[0].source_range.end.join(element_begin(node.children[1])).source
|
|
101
122
|
else
|
|
102
123
|
' '
|
|
103
124
|
end
|
|
@@ -109,7 +130,19 @@ module RuboCop
|
|
|
109
130
|
# @param [RuboCop::AST::ArrayNode] node
|
|
110
131
|
# @return [String]
|
|
111
132
|
def whitespace_leading(node)
|
|
112
|
-
node.loc.begin.end.join(node.children[0]
|
|
133
|
+
node.loc.begin.end.join(element_begin(node.children[0])).source
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The location of an element starting with a variable interpolated without
|
|
137
|
+
# braces, such as `#@foo`, does not include the `#`.
|
|
138
|
+
# @param [RuboCop::AST::Node] element
|
|
139
|
+
# @return [Parser::Source::Range]
|
|
140
|
+
def element_begin(element)
|
|
141
|
+
range = element.source_range.begin
|
|
142
|
+
return range unless element.type?(:dstr, :dsym)
|
|
143
|
+
return range if element.children.first.type?(:str, :begin)
|
|
144
|
+
|
|
145
|
+
range.adjust(begin_pos: -1, end_pos: -1)
|
|
113
146
|
end
|
|
114
147
|
|
|
115
148
|
# Provides trailing whitespace for building a bracketed array.
|
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
|
19
19
|
|
|
20
20
|
class << self
|
|
21
21
|
# The signature is a property of the index, not of the cop, and computing
|
|
22
|
-
# it
|
|
22
|
+
# it reads every indexed file, so all cops sharing an index share one
|
|
23
23
|
# computation. A single-entry cache (instead of a hash keyed by index)
|
|
24
24
|
# avoids retaining stale graphs in long-lived processes.
|
|
25
25
|
attr_accessor :cached_index_signature
|
|
@@ -188,14 +188,13 @@ module RuboCop
|
|
|
188
188
|
next if uri == BUILTIN_DOCUMENT_URI
|
|
189
189
|
|
|
190
190
|
path = uri.delete_prefix(FILE_URI_PREFIX).sub(WINDOWS_DRIVE_PREFIX, '')
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
[stat.mtime.to_f, stat.size]
|
|
191
|
+
digest = begin
|
|
192
|
+
Digest::SHA1.file(path).hexdigest
|
|
194
193
|
rescue StandardError
|
|
195
|
-
|
|
194
|
+
''
|
|
196
195
|
end
|
|
197
196
|
|
|
198
|
-
"#{path}:#{
|
|
197
|
+
"#{path}:#{digest}"
|
|
199
198
|
end.sort
|
|
200
199
|
end
|
|
201
200
|
end
|
|
@@ -90,6 +90,19 @@ module RuboCop
|
|
|
90
90
|
range.adjust(begin_pos: -range.column, end_pos: end_offset).intersect(buffer.source_range)
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
+
# Returns the range to remove a statement: its whole lines, including a trailing comment
|
|
94
|
+
# and the final newline, when no other code shares them, and otherwise the range itself
|
|
95
|
+
# with the spaces after it.
|
|
96
|
+
def range_for_statement_removal(range)
|
|
97
|
+
rest_of_line = range.source_buffer.source_line(range.last_line)[range.last_column..]
|
|
98
|
+
|
|
99
|
+
if begins_its_line?(range) && rest_of_line.match?(/\A\s*(#|\z)/)
|
|
100
|
+
range_by_whole_lines(range, include_final_newline: true)
|
|
101
|
+
else
|
|
102
|
+
range_with_surrounding_space(range, side: :right, newlines: false)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
93
106
|
def column_offset_between(base_range, range)
|
|
94
107
|
effective_column(base_range) - effective_column(range)
|
|
95
108
|
end
|
|
@@ -8,6 +8,7 @@ module RuboCop
|
|
|
8
8
|
include RangeHelp
|
|
9
9
|
|
|
10
10
|
MSG = 'Space found before %<token>s.'
|
|
11
|
+
SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
|
|
11
12
|
|
|
12
13
|
def on_new_investigation
|
|
13
14
|
each_missing_space(processed_source.sorted_tokens) do |token, pos_before|
|
|
@@ -36,7 +37,10 @@ module RuboCop
|
|
|
36
37
|
end
|
|
37
38
|
|
|
38
39
|
def space_required_after?(token)
|
|
39
|
-
token.left_curly_brace?
|
|
40
|
+
return space_required_after_lcurly? if token.left_curly_brace?
|
|
41
|
+
return space_required_after_lparen? if token.left_parens?
|
|
42
|
+
|
|
43
|
+
false
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
def space_required_after_lcurly?
|
|
@@ -44,6 +48,12 @@ module RuboCop
|
|
|
44
48
|
style = cfg['EnforcedStyle'] || 'space'
|
|
45
49
|
style == 'space'
|
|
46
50
|
end
|
|
51
|
+
|
|
52
|
+
def space_required_after_lparen?
|
|
53
|
+
style = config.for_enabled_cop('Layout/SpaceInsideParens')['EnforcedStyle']
|
|
54
|
+
|
|
55
|
+
SPACE_INSIDE_PARENS_STYLES.include?(style)
|
|
56
|
+
end
|
|
47
57
|
end
|
|
48
58
|
end
|
|
49
59
|
end
|
|
@@ -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
|
|
|
@@ -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, '(', ')')
|
|
@@ -177,7 +177,12 @@ 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
|
+
Layout::SpaceBeforeBlockBraces,
|
|
182
|
+
Style::MethodCallWithArgsParentheses,
|
|
183
|
+
Style::RedundantBegin,
|
|
184
|
+
Style::RescueStandardError
|
|
185
|
+
]
|
|
181
186
|
end
|
|
182
187
|
|
|
183
188
|
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)
|