rubocop 1.92.1 → 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 -4
- data/lib/rubocop/config_obsoletion.rb +9 -0
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- 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 +15 -7
- 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 +6 -12
- data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
- 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/line_length.rb +23 -5
- 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/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
- 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 +27 -18
- 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/nested_percent_literal.rb +3 -0
- 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 +66 -3
- data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
- 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_splat_expansion.rb +11 -4
- 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 +2 -2
- 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_alignment_styles.rb +17 -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/statement_modifier.rb +14 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
- 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 +41 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
- data/lib/rubocop/cop/style/case_equality.rb +14 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
- 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 +64 -16
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/float_division.rb +16 -1
- 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_as_last_array_item.rb +1 -1
- 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 +21 -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 +12 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -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/numeric_predicate.rb +7 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -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/quoted_symbols.rb +5 -7
- data/lib/rubocop/cop/style/raise_args.rb +66 -7
- data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
- data/lib/rubocop/cop/style/redundant_each.rb +8 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
- 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_self.rb +9 -1
- 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 +26 -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 +28 -8
- 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/plugin/configuration_integrator.rb +24 -3
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/runner.rb +6 -1
- 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
|
@@ -43,14 +43,14 @@ module RuboCop
|
|
|
43
43
|
MSG = 'Prefer `to_s` over string interpolation.'
|
|
44
44
|
|
|
45
45
|
def self.autocorrect_incompatible_with
|
|
46
|
-
[Style::LineEndConcatenation]
|
|
46
|
+
[Style::LineEndConcatenation, Style::MethodCallWithArgsParentheses]
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def on_dstr(node)
|
|
50
50
|
return unless single_interpolation?(node)
|
|
51
51
|
|
|
52
52
|
embedded_node = node.children.first
|
|
53
|
-
return if
|
|
53
|
+
return if void_value?(embedded_node)
|
|
54
54
|
|
|
55
55
|
add_offense(node) do |corrector|
|
|
56
56
|
if variable_interpolation?(embedded_node)
|
|
@@ -72,14 +72,31 @@ module RuboCop
|
|
|
72
72
|
!embedded_in_percent_array?(node)
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
def
|
|
76
|
-
return false
|
|
75
|
+
def void_value?(node)
|
|
76
|
+
return false unless node
|
|
77
|
+
return true if node.type?(:return, :break, :next, :redo, :retry)
|
|
78
|
+
return target_ruby_version >= 3.0 if node.match_pattern_type?
|
|
77
79
|
|
|
78
|
-
node
|
|
79
|
-
|
|
80
|
+
void_compound_statement?(node)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def void_compound_statement?(node)
|
|
84
|
+
case node.type
|
|
85
|
+
when :begin, :kwbegin then void_value?(node.children.last)
|
|
86
|
+
when :if then void_value?(node.if_branch) && void_value?(node.else_branch)
|
|
87
|
+
when :rescue then rescue_void_value?(node)
|
|
88
|
+
when :ensure then void_value?(node.children.first) || void_value?(node.branch)
|
|
89
|
+
else false
|
|
80
90
|
end
|
|
81
91
|
end
|
|
82
92
|
|
|
93
|
+
def rescue_void_value?(node)
|
|
94
|
+
return false unless void_value?(node.body)
|
|
95
|
+
return void_value?(node.else_branch) if node.else?
|
|
96
|
+
|
|
97
|
+
node.resbody_branches.all? { |resbody| void_value?(resbody.body) }
|
|
98
|
+
end
|
|
99
|
+
|
|
83
100
|
def single_variable_interpolation?(node)
|
|
84
101
|
return false unless node.children.one?
|
|
85
102
|
|
|
@@ -111,21 +128,24 @@ module RuboCop
|
|
|
111
128
|
corrector.replace(node, replacement)
|
|
112
129
|
end
|
|
113
130
|
|
|
131
|
+
# rubocop:disable-next Metrics/AbcSize
|
|
114
132
|
def autocorrect_single_variable_interpolation(corrector, embedded_node, node)
|
|
115
133
|
embedded_var = embedded_node.children.first
|
|
134
|
+
suffix = '.to_s'
|
|
135
|
+
|
|
136
|
+
if require_parentheses?(embedded_var)
|
|
137
|
+
corrector.replace(arguments_begin_range(embedded_var), '(')
|
|
138
|
+
suffix = ').to_s'
|
|
139
|
+
end
|
|
116
140
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
)
|
|
121
|
-
arguments = embedded_var.arguments.map(&:source).join(', ')
|
|
141
|
+
corrector.remove(node.source_range.begin.join(embedded_var.source_range.begin))
|
|
142
|
+
corrector.replace(embedded_var.source_range.end.join(node.source_range.end), suffix)
|
|
143
|
+
end
|
|
122
144
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
embedded_var.source
|
|
126
|
-
end
|
|
145
|
+
def arguments_begin_range(send_node)
|
|
146
|
+
first_argument_range = send_node.first_argument.source_range
|
|
127
147
|
|
|
128
|
-
|
|
148
|
+
send_node.loc.selector.end.join(first_argument_range.begin)
|
|
129
149
|
end
|
|
130
150
|
|
|
131
151
|
def autocorrect_other(corrector, embedded_node, node)
|
|
@@ -30,6 +30,8 @@ module RuboCop
|
|
|
30
30
|
PERCENT_CAPITAL_Q = '%Q'
|
|
31
31
|
STRING_INTERPOLATION_REGEXP = /#\{.+\}/.freeze
|
|
32
32
|
ESCAPED_NON_BACKSLASH = /\\[^\\]/.freeze
|
|
33
|
+
ESCAPED_QUOTE = /(?<!\\)(?:\\\\)*\\"/.freeze
|
|
34
|
+
UNESCAPED_QUOTE = /(?<!\\)(?:\\\\)*"/.freeze
|
|
33
35
|
|
|
34
36
|
def on_dstr(node)
|
|
35
37
|
return unless string_literal?(node)
|
|
@@ -53,7 +55,7 @@ module RuboCop
|
|
|
53
55
|
return if interpolated_quotes?(node) || allowed_percent_q?(node)
|
|
54
56
|
|
|
55
57
|
add_offense(node) do |corrector|
|
|
56
|
-
delimiter =
|
|
58
|
+
delimiter = double_quotes_delimiter?(node.source) ? QUOTE : SINGLE_QUOTE
|
|
57
59
|
|
|
58
60
|
corrector.replace(node.loc.begin, delimiter)
|
|
59
61
|
corrector.replace(node.loc.end, delimiter)
|
|
@@ -69,6 +71,12 @@ module RuboCop
|
|
|
69
71
|
(node.source.start_with?(PERCENT_CAPITAL_Q) && acceptable_capital_q?(node))
|
|
70
72
|
end
|
|
71
73
|
|
|
74
|
+
def double_quotes_delimiter?(src)
|
|
75
|
+
return true if /\A%Q[^"]+\z|'/.match?(src)
|
|
76
|
+
|
|
77
|
+
src.start_with?(PERCENT_CAPITAL_Q) && ESCAPED_QUOTE.match?(src)
|
|
78
|
+
end
|
|
79
|
+
|
|
72
80
|
def message(node)
|
|
73
81
|
src = node.source
|
|
74
82
|
extra = if src.start_with?(PERCENT_CAPITAL_Q)
|
|
@@ -102,7 +110,8 @@ module RuboCop
|
|
|
102
110
|
src = node.source
|
|
103
111
|
src.include?(QUOTE) &&
|
|
104
112
|
(STRING_INTERPOLATION_REGEXP.match?(src) ||
|
|
105
|
-
(node.str_type? && double_quotes_required?(src))
|
|
113
|
+
(node.str_type? && double_quotes_required?(src)) ||
|
|
114
|
+
(ESCAPED_QUOTE.match?(src) && UNESCAPED_QUOTE.match?(src)))
|
|
106
115
|
end
|
|
107
116
|
end
|
|
108
117
|
end
|
|
@@ -115,7 +115,7 @@ module RuboCop
|
|
|
115
115
|
|
|
116
116
|
def on_send(node)
|
|
117
117
|
return unless node.self_receiver? && regular_method_call?(node)
|
|
118
|
-
return if node
|
|
118
|
+
return if multiple_assignment_target?(node)
|
|
119
119
|
return if allowed_send_node?(node)
|
|
120
120
|
return if it_method_in_block?(node)
|
|
121
121
|
|
|
@@ -154,6 +154,14 @@ module RuboCop
|
|
|
154
154
|
end
|
|
155
155
|
end
|
|
156
156
|
|
|
157
|
+
def multiple_assignment_target?(node)
|
|
158
|
+
return false unless (parent = node.parent)
|
|
159
|
+
|
|
160
|
+
parent = parent.parent if parent.splat_type?
|
|
161
|
+
|
|
162
|
+
parent.mlhs_type?
|
|
163
|
+
end
|
|
164
|
+
|
|
157
165
|
def allowed_send_node?(node)
|
|
158
166
|
@allowed_send_nodes.include?(node) ||
|
|
159
167
|
@local_variables_scopes[node].include?(node.method_name) ||
|
|
@@ -101,6 +101,10 @@ module RuboCop
|
|
|
101
101
|
}
|
|
102
102
|
MATCHER
|
|
103
103
|
|
|
104
|
+
def self.autocorrect_incompatible_with
|
|
105
|
+
[Style::TrailingCommaInArguments, Style::TrailingCommaInArrayLiteral, Style::TrailingCommaInHashLiteral]
|
|
106
|
+
end
|
|
107
|
+
|
|
104
108
|
def on_send(node)
|
|
105
109
|
ancestor, sort_node, sorter, accessor =
|
|
106
110
|
find_redundant_sort(node.parent, node.parent&.parent)
|
|
@@ -139,7 +143,7 @@ module RuboCop
|
|
|
139
143
|
|
|
140
144
|
rest = code_after_accessor(node)
|
|
141
145
|
|
|
142
|
-
!rest.empty? && !rest.match?(/\A(#|&?\.|[)\]}
|
|
146
|
+
!rest.empty? && !rest.match?(/\A(#|&?\.|[)\]}])/)
|
|
143
147
|
end
|
|
144
148
|
|
|
145
149
|
def code_after_accessor(node)
|
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
|
35
35
|
MSG = 'Do not use semicolons to terminate expressions.'
|
|
36
36
|
|
|
37
37
|
def self.autocorrect_incompatible_with
|
|
38
|
-
[Style::SingleLineMethods]
|
|
38
|
+
[Style::MethodCallWithArgsParentheses, Style::SingleLineMethods, Style::SuperWithArgsParentheses]
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
def on_new_investigation
|
|
@@ -68,7 +68,7 @@ module RuboCop
|
|
|
68
68
|
[Layout::MultilineMethodArgumentLineBreaks]
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
-
# rubocop:disable Metrics/AbcSize
|
|
71
|
+
# rubocop:disable Metrics/AbcSize
|
|
72
72
|
def on_send(node)
|
|
73
73
|
return if allow_send? && !node.method?(:public_send)
|
|
74
74
|
return unless (first_argument = node.first_argument)
|
|
@@ -76,7 +76,7 @@ module RuboCop
|
|
|
76
76
|
|
|
77
77
|
offense_range = offense_range(node)
|
|
78
78
|
method_name = first_argument.value
|
|
79
|
-
return
|
|
79
|
+
return unless valid_method_name?(method_name)
|
|
80
80
|
|
|
81
81
|
add_offense(offense_range, message: format(MSG, method_name: method_name)) do |corrector|
|
|
82
82
|
if node.arguments.one?
|
|
@@ -88,7 +88,7 @@ module RuboCop
|
|
|
88
88
|
end
|
|
89
89
|
end
|
|
90
90
|
alias on_csend on_send
|
|
91
|
-
# rubocop:enable Metrics/AbcSize
|
|
91
|
+
# rubocop:enable Metrics/AbcSize
|
|
92
92
|
|
|
93
93
|
private
|
|
94
94
|
|
|
@@ -100,6 +100,11 @@ module RuboCop
|
|
|
100
100
|
node.loc.selector.join(node.source_range.end)
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
+
def valid_method_name?(method_name)
|
|
104
|
+
method_name.to_s.valid_encoding? && METHOD_NAME_PATTERN.match?(method_name) &&
|
|
105
|
+
!RESERVED_WORDS.include?(method_name)
|
|
106
|
+
end
|
|
107
|
+
|
|
103
108
|
def removal_argument_range(first_argument, second_argument)
|
|
104
109
|
first_argument.source_range.begin.join(second_argument.source_range.begin)
|
|
105
110
|
end
|
|
@@ -77,7 +77,9 @@ module RuboCop
|
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
def eligible_arguments?(node)
|
|
80
|
-
node.arguments? && node.arguments.to_a.all?(&:arg_type?)
|
|
80
|
+
return false unless node.arguments? && node.arguments.to_a.all?(&:arg_type?)
|
|
81
|
+
|
|
82
|
+
node.arguments.size <= target_args(node.method_name).size
|
|
81
83
|
end
|
|
82
84
|
|
|
83
85
|
def eligible_method?(node)
|
|
@@ -36,22 +36,18 @@ module RuboCop
|
|
|
36
36
|
|
|
37
37
|
MSG = 'Prefer multiline `do`...`end` block.'
|
|
38
38
|
|
|
39
|
-
# rubocop:disable-next Metrics/AbcSize
|
|
40
39
|
def on_block(node)
|
|
41
40
|
return if node.multiline? || node.braces?
|
|
42
41
|
return if single_line_blocks_preferred? && suitable_as_single_line?(node)
|
|
43
42
|
|
|
44
43
|
add_offense(node) do |corrector|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
# otherwise move it into the heredoc body and break the syntax.
|
|
51
|
-
corrector.remove(node.loc.end)
|
|
52
|
-
corrector.insert_after(heredoc.loc.heredoc_end, "\nend")
|
|
44
|
+
if (heredoc = heredoc_opened_before_do(node))
|
|
45
|
+
# Code after the `end`, such as a method chain, would be left behind after the `do`.
|
|
46
|
+
next if code_after_end?(node.loc.end)
|
|
47
|
+
|
|
48
|
+
move_body_after_heredoc(corrector, node, heredoc)
|
|
53
49
|
else
|
|
54
|
-
corrector
|
|
50
|
+
break_block_body(corrector, node)
|
|
55
51
|
end
|
|
56
52
|
end
|
|
57
53
|
end
|
|
@@ -60,29 +56,70 @@ module RuboCop
|
|
|
60
56
|
|
|
61
57
|
private
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
def single_line_blocks_preferred?
|
|
60
|
+
@config.for_enabled_cop('Layout/RedundantLineBreak')['InspectBlocks']
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def code_after_end?(end_keyword)
|
|
64
|
+
rest = end_keyword.source_line[end_keyword.last_column..].strip
|
|
65
|
+
|
|
66
|
+
!rest.empty? && !rest.start_with?('#')
|
|
67
|
+
end
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
def move_body_after_heredoc(corrector, node, heredoc)
|
|
70
|
+
body = node.body
|
|
71
|
+
|
|
72
|
+
corrector.remove(do_line(node).end.join(node.loc.end.end))
|
|
73
|
+
corrector.insert_after(heredoc.loc.heredoc_end, "\n#{body.source}") if body
|
|
74
|
+
|
|
75
|
+
last_heredoc = trailing_heredoc(body) || heredoc
|
|
76
|
+
corrector.insert_after(last_heredoc.loc.heredoc_end, "\nend")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def break_block_body(corrector, node)
|
|
80
|
+
corrector.insert_after(do_line(node), "\n")
|
|
81
|
+
|
|
82
|
+
if (heredoc = trailing_heredoc(node.body))
|
|
83
|
+
corrector.remove(node.loc.end)
|
|
84
|
+
corrector.insert_after(heredoc.loc.heredoc_end, "\nend")
|
|
85
|
+
else
|
|
86
|
+
corrector.insert_before(node.loc.end, "\n")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Returns the heredoc opened before the `do` on the block's line whose body extends
|
|
91
|
+
# the furthest down. It can be opened outside the block, such as in a preceding argument.
|
|
92
|
+
def heredoc_opened_before_do(node)
|
|
93
|
+
do_begin_pos = node.loc.begin.begin_pos
|
|
94
|
+
|
|
95
|
+
heredocs = processed_source.ast.each_node(:any_str).select do |str_node|
|
|
96
|
+
heredoc_opened_on_line_before?(str_node, node.first_line, do_begin_pos)
|
|
70
97
|
end
|
|
71
98
|
|
|
72
99
|
heredocs.max_by { |heredoc| heredoc.loc.heredoc_end.line }
|
|
73
100
|
end
|
|
74
101
|
|
|
102
|
+
def heredoc_opened_on_line_before?(str_node, line, pos)
|
|
103
|
+
return false unless str_node.heredoc?
|
|
104
|
+
|
|
105
|
+
str_node.first_line == line && str_node.source_range.end_pos <= pos
|
|
106
|
+
end
|
|
107
|
+
|
|
75
108
|
def do_line(node)
|
|
76
109
|
if node.type?(:numblock, :itblock) ||
|
|
77
110
|
node.arguments.children.empty? || node.send_node.lambda_literal?
|
|
78
111
|
node.loc.begin
|
|
79
112
|
else
|
|
80
|
-
node.arguments
|
|
113
|
+
node.arguments.source_range
|
|
81
114
|
end
|
|
82
115
|
end
|
|
83
116
|
|
|
84
|
-
def
|
|
85
|
-
|
|
117
|
+
def trailing_heredoc(root)
|
|
118
|
+
return unless root
|
|
119
|
+
|
|
120
|
+
heredocs = root.each_node(:any_str).select(&:heredoc?)
|
|
121
|
+
|
|
122
|
+
heredocs.max_by { |heredoc| heredoc.loc.heredoc_end.line }
|
|
86
123
|
end
|
|
87
124
|
end
|
|
88
125
|
end
|
|
@@ -53,6 +53,8 @@ module RuboCop
|
|
|
53
53
|
private
|
|
54
54
|
|
|
55
55
|
def autocorrect(corrector, node)
|
|
56
|
+
return if part_of_ignored_node?(node)
|
|
57
|
+
|
|
56
58
|
if correct_to_endless?(node.body)
|
|
57
59
|
correct_to_endless(corrector, node)
|
|
58
60
|
else
|
|
@@ -69,17 +71,38 @@ module RuboCop
|
|
|
69
71
|
return false if disallow_endless_method_style?
|
|
70
72
|
return false unless body_node
|
|
71
73
|
return false if body_node.parent.assignment_method?
|
|
74
|
+
return false if captures_following_code?(body_node.parent)
|
|
72
75
|
|
|
73
76
|
!unsupported_endless_method_body?(body_node) && endless_method_body?(body_node)
|
|
74
77
|
end
|
|
75
78
|
|
|
79
|
+
# `def foo; bar; end && baz` would become `def foo() = bar && baz`. The same goes for a def
|
|
80
|
+
# that ends an operand, as in `a && def foo; bar; end || baz`.
|
|
81
|
+
def captures_following_code?(node)
|
|
82
|
+
return false unless (parent = node.parent)
|
|
83
|
+
|
|
84
|
+
node_end = node.source_range.end_pos
|
|
85
|
+
return captures_following_code?(parent) if parent.source_range.end_pos == node_end
|
|
86
|
+
|
|
87
|
+
parent.children.first.equal?(node) && extends_endless_body?(parent, node)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def extends_endless_body?(parent, node)
|
|
91
|
+
case parent.type
|
|
92
|
+
when :and, :or then parent.logical_operator?
|
|
93
|
+
when :if then parent.ternary?
|
|
94
|
+
when :rescue then same_line?(node, parent.resbody_branches.first) # the modifier form
|
|
95
|
+
else parent.type?(:call, :range)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
76
99
|
def unsupported_endless_method_body?(body_node)
|
|
77
100
|
body_node.basic_conditional? ||
|
|
78
101
|
NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type)
|
|
79
102
|
end
|
|
80
103
|
|
|
81
104
|
# The body gets parenthesized when it becomes endless, so it is no longer a command.
|
|
82
|
-
def
|
|
105
|
+
def command_body_outside_statement?(body)
|
|
83
106
|
!require_parentheses?(body) && super
|
|
84
107
|
end
|
|
85
108
|
|
|
@@ -98,12 +121,13 @@ module RuboCop
|
|
|
98
121
|
end
|
|
99
122
|
|
|
100
123
|
def correct_to_endless(corrector, node)
|
|
101
|
-
receiver =
|
|
124
|
+
receiver = node.loc.keyword.end.join(node.loc.name.begin).source.lstrip if node.receiver
|
|
102
125
|
body_source = method_body_source(node.body)
|
|
103
126
|
arguments = endless_arguments(node)
|
|
104
127
|
replacement = "def #{receiver}#{node.method_name}#{arguments} = #{body_source}"
|
|
105
128
|
|
|
106
129
|
corrector.replace(node, replacement)
|
|
130
|
+
ignore_node(node)
|
|
107
131
|
end
|
|
108
132
|
|
|
109
133
|
def break_line_before(corrector, node, range, indent_steps: 1)
|
|
@@ -57,6 +57,8 @@ module RuboCop
|
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def arguments_require_parentheses?(arguments)
|
|
60
|
+
return true if arguments.first.mlhs_type? || arguments.any?(&:shadowarg_type?)
|
|
61
|
+
|
|
60
62
|
arguments.each_descendant(:any_block, :hash).any? do |node|
|
|
61
63
|
!node.hash_type? || node.braces?
|
|
62
64
|
end
|
|
@@ -15,6 +15,10 @@ module RuboCop
|
|
|
15
15
|
# lines, this cop does not register an offense; instead,
|
|
16
16
|
# `Style/LineEndConcatenation` will pick up the offense if enabled.
|
|
17
17
|
#
|
|
18
|
+
# This cop does not register an offense for concatenation with a heredoc
|
|
19
|
+
# either, because interpolating the heredoc into a string literal would not
|
|
20
|
+
# make the code easier to read.
|
|
21
|
+
#
|
|
18
22
|
# Two modes are supported:
|
|
19
23
|
# 1. `aggressive` style checks and corrects all occurrences of `+` where
|
|
20
24
|
# either the left or right side of `+` is a string literal.
|
|
@@ -41,6 +45,11 @@ module RuboCop
|
|
|
41
45
|
# name = 'First' +
|
|
42
46
|
# 'Last'
|
|
43
47
|
#
|
|
48
|
+
# # accepted, concatenation with a heredoc
|
|
49
|
+
# 'Hello' + <<~TEXT
|
|
50
|
+
# world
|
|
51
|
+
# TEXT
|
|
52
|
+
#
|
|
44
53
|
# @example Mode: conservative
|
|
45
54
|
# # bad
|
|
46
55
|
# 'Hello' + user.name
|
|
@@ -73,6 +82,8 @@ module RuboCop
|
|
|
73
82
|
return if line_end_concatenation?(node)
|
|
74
83
|
|
|
75
84
|
topmost_plus_node = find_topmost_plus_node(node)
|
|
85
|
+
return if heredoc?(topmost_plus_node)
|
|
86
|
+
|
|
76
87
|
parts = collect_parts(topmost_plus_node)
|
|
77
88
|
return if mode == :conservative && !parts.first.str_type?
|
|
78
89
|
|
|
@@ -111,6 +122,10 @@ module RuboCop
|
|
|
111
122
|
current
|
|
112
123
|
end
|
|
113
124
|
|
|
125
|
+
def heredoc?(node)
|
|
126
|
+
node.each_descendant(:any_str).any?(&:heredoc?)
|
|
127
|
+
end
|
|
128
|
+
|
|
114
129
|
def collect_parts(node, parts = [])
|
|
115
130
|
return unless node
|
|
116
131
|
|
|
@@ -127,13 +142,7 @@ module RuboCop
|
|
|
127
142
|
end
|
|
128
143
|
|
|
129
144
|
def uncorrectable?(part)
|
|
130
|
-
part.multiline? ||
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def heredoc?(node)
|
|
134
|
-
return false unless node.type?(:str, :dstr)
|
|
135
|
-
|
|
136
|
-
node.heredoc?
|
|
145
|
+
part.multiline? || part.each_descendant(:any_block).any?
|
|
137
146
|
end
|
|
138
147
|
|
|
139
148
|
def corrected_ancestor?(node)
|
|
@@ -143,7 +152,18 @@ module RuboCop
|
|
|
143
152
|
def replacement(parts)
|
|
144
153
|
interpolated_parts = parts.map { |part| adjust_str(part) }
|
|
145
154
|
|
|
146
|
-
"\"#{handle_quotes(interpolated_parts)
|
|
155
|
+
"\"#{join_parts(handle_quotes(interpolated_parts))}\""
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def join_parts(parts)
|
|
159
|
+
parts.each_with_object(+'') do |part, result|
|
|
160
|
+
result.insert(-2, '\\') if interpolation_formed?(result, part)
|
|
161
|
+
result << part
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def interpolation_formed?(preceding, part)
|
|
166
|
+
part.start_with?('{', '@', '$') && preceding.match?(/(?<!\\)(?:\\\\)*#\z/)
|
|
147
167
|
end
|
|
148
168
|
|
|
149
169
|
def adjust_str(part)
|
|
@@ -80,6 +80,7 @@ module RuboCop
|
|
|
80
80
|
|
|
81
81
|
def correct_parent(parent, corrector)
|
|
82
82
|
if parent.block_type?
|
|
83
|
+
replace_opening_brace_with_do(corrector, parent) if parent.braces?
|
|
83
84
|
corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
|
|
84
85
|
elsif (class_node = parent.parent).body.nil?
|
|
85
86
|
corrector.remove(range_for_empty_class_body(class_node, parent))
|
|
@@ -90,6 +91,19 @@ module RuboCop
|
|
|
90
91
|
end
|
|
91
92
|
end
|
|
92
93
|
|
|
94
|
+
def replace_opening_brace_with_do(corrector, block)
|
|
95
|
+
range = range_with_surrounding_space(block.loc.begin, side: :right, newlines: false)
|
|
96
|
+
return corrector.replace(range, 'do ') if block_content_follows?(range, block)
|
|
97
|
+
|
|
98
|
+
corrector.replace(block.loc.begin, 'do')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def block_content_follows?(opening_range, block)
|
|
102
|
+
return false if opening_range.end_pos == block.loc.end.begin_pos
|
|
103
|
+
|
|
104
|
+
opening_range.last_column < opening_range.source_line.length
|
|
105
|
+
end
|
|
106
|
+
|
|
93
107
|
def range_for_empty_class_body(class_node, struct_new)
|
|
94
108
|
if class_node.single_line?
|
|
95
109
|
range_between(struct_new.source_range.end_pos, class_node.source_range.end_pos)
|
|
@@ -98,9 +98,7 @@ module RuboCop
|
|
|
98
98
|
|
|
99
99
|
syms = node.children.map do |c|
|
|
100
100
|
if c.dsym_type?
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
":#{trim_string_interpolation_escape_character(string_literal)}"
|
|
101
|
+
":#{build_interpolated_string_literal(c)}"
|
|
104
102
|
else
|
|
105
103
|
to_symbol_literal(c.value.to_s)
|
|
106
104
|
end
|
|
@@ -141,9 +141,7 @@ module RuboCop
|
|
|
141
141
|
|
|
142
142
|
words = node.children.map do |word|
|
|
143
143
|
if word.dstr_type?
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
trim_string_interpolation_escape_character(string_literal)
|
|
144
|
+
build_interpolated_string_literal(word)
|
|
147
145
|
else
|
|
148
146
|
to_string_literal(word.children[0])
|
|
149
147
|
end
|
data/lib/rubocop/cop/util.rb
CHANGED
|
@@ -32,6 +32,15 @@ module RuboCop
|
|
|
32
32
|
VARIABLE_ASSIGNMENT_TYPE, REGEXP_NAMED_CAPTURE_TYPE, PATTERN_MATCH_VARIABLE_TYPE
|
|
33
33
|
].freeze
|
|
34
34
|
|
|
35
|
+
# Ruby assigns a named capture to a local variable only when its name is a valid
|
|
36
|
+
# local variable name, so `(?<Foo>...)` and `(?<if>...)` declare no variable.
|
|
37
|
+
LOCAL_VARIABLE_NAME = /\A(?![[:upper:]\p{Lt}])[a-z_\P{ASCII}][a-zA-Z0-9_\P{ASCII}]*\z/.freeze
|
|
38
|
+
RESERVED_WORDS = %w[
|
|
39
|
+
__ENCODING__ __FILE__ __LINE__ BEGIN END alias and begin break case class def do else
|
|
40
|
+
elsif end ensure false for if in module next nil not or redo rescue retry return self
|
|
41
|
+
super then true undef unless until when while yield
|
|
42
|
+
].to_set.freeze
|
|
43
|
+
|
|
35
44
|
ARGUMENT_DECLARATION_TYPES = [
|
|
36
45
|
:arg, :optarg, :restarg,
|
|
37
46
|
:kwarg, :kwoptarg, :kwrestarg,
|
|
@@ -192,7 +201,11 @@ module RuboCop
|
|
|
192
201
|
def regexp_captured_names(node)
|
|
193
202
|
regexp = node.to_regexp
|
|
194
203
|
|
|
195
|
-
regexp.
|
|
204
|
+
regexp.names.select { |name| local_variable_name?(name) }
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def local_variable_name?(name)
|
|
208
|
+
LOCAL_VARIABLE_NAME.match?(name) && !RESERVED_WORDS.include?(name)
|
|
196
209
|
end
|
|
197
210
|
|
|
198
211
|
def process_variable_operator_assignment(node)
|
|
@@ -297,7 +297,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
|
297
297
|
|
|
298
298
|
def references(cop, see_objects) # rubocop:disable Metrics/AbcSize
|
|
299
299
|
cop_config = config.for_cop(cop)
|
|
300
|
-
urls = RuboCop::Cop::MessageAnnotator.new(config, cop.
|
|
300
|
+
urls = RuboCop::Cop::MessageAnnotator.new(config, cop.cop_name, cop_config, {}).urls
|
|
301
301
|
return '' if urls.empty? && see_objects.empty?
|
|
302
302
|
|
|
303
303
|
content = cop_subsection('References', cop)
|
|
@@ -78,7 +78,8 @@ module RuboCop
|
|
|
78
78
|
# in which case all cops are returned for compatibility.
|
|
79
79
|
return Cop::Registry.all if @config_store.nil?
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
config = @config_store.for_file(file)
|
|
82
|
+
registry_for(config).enabled(config)
|
|
82
83
|
end
|
|
83
84
|
|
|
84
85
|
def relevant_for_output?(options, target_offenses)
|
|
@@ -100,14 +101,20 @@ module RuboCop
|
|
|
100
101
|
|
|
101
102
|
# Filtering by badge keeps lazy-loaded cops unloaded; `Registry#enabled` then loads only
|
|
102
103
|
# the enabled cops, the same set the runner mobilizes.
|
|
103
|
-
def
|
|
104
|
-
@
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
options[:only].nil? || badge.match_name?(options[:only])
|
|
104
|
+
def registry_for(config)
|
|
105
|
+
@registry_by_config ||= {}.compare_by_identity
|
|
106
|
+
@registry_by_config[config] ||= Cop::Registry.global.filter_by_badge(options) do |badge|
|
|
107
|
+
mobilized_cop_badge?(badge, config)
|
|
108
108
|
end
|
|
109
109
|
end
|
|
110
110
|
|
|
111
|
+
def mobilized_cop_badge?(badge, config)
|
|
112
|
+
return false if badge.department == :Test || badge.match_name?(options[:except])
|
|
113
|
+
return false if config.missing_default_config_for?(badge)
|
|
114
|
+
|
|
115
|
+
options[:only].nil? || badge.match_name?(options[:only])
|
|
116
|
+
end
|
|
117
|
+
|
|
111
118
|
def classname_attribute_value(file)
|
|
112
119
|
@classname_attribute_value_cache ||= Hash.new do |hash, key|
|
|
113
120
|
hash[key] = key.delete_suffix('.rb').gsub("#{PathUtil.pwd}/", '').tr('/', '.')
|