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
|
@@ -62,15 +62,24 @@ module RuboCop
|
|
|
62
62
|
# baz
|
|
63
63
|
# end
|
|
64
64
|
# end
|
|
65
|
-
class RedundantBegin < Base
|
|
65
|
+
class RedundantBegin < Base # rubocop:disable Metrics/ClassLength
|
|
66
66
|
include RangeHelp
|
|
67
67
|
extend AutoCorrector
|
|
68
68
|
|
|
69
69
|
MSG = 'Redundant `begin` block detected.'
|
|
70
70
|
SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
|
|
71
|
+
VALUE_PARENT_TYPES = %i[return next break optarg kwoptarg].freeze
|
|
72
|
+
STATEMENT_CONTAINER_TYPES = %i[
|
|
73
|
+
begin kwbegin any_block any_def class module sclass rescue resbody ensure
|
|
74
|
+
].freeze
|
|
71
75
|
|
|
72
76
|
def self.autocorrect_incompatible_with
|
|
73
|
-
[
|
|
77
|
+
[
|
|
78
|
+
Style::BlockDelimiters,
|
|
79
|
+
Style::TrailingCommaInArguments,
|
|
80
|
+
Style::TrailingCommaInArrayLiteral,
|
|
81
|
+
Style::TrailingCommaInHashLiteral
|
|
82
|
+
]
|
|
74
83
|
end
|
|
75
84
|
|
|
76
85
|
# @!method offensive_kwbegins(node)
|
|
@@ -103,7 +112,7 @@ module RuboCop
|
|
|
103
112
|
body = node.body
|
|
104
113
|
|
|
105
114
|
return unless body&.kwbegin_type?
|
|
106
|
-
return if body.
|
|
115
|
+
return if body.ensure_node || body.rescue_node
|
|
107
116
|
|
|
108
117
|
register_offense(body)
|
|
109
118
|
end
|
|
@@ -138,27 +147,59 @@ module RuboCop
|
|
|
138
147
|
end
|
|
139
148
|
|
|
140
149
|
def register_offense(node)
|
|
150
|
+
return if end_cannot_join_statement_line?(node)
|
|
151
|
+
|
|
141
152
|
offense_range = node.loc.begin
|
|
142
153
|
|
|
143
154
|
add_offense(offense_range) do |corrector|
|
|
144
|
-
if node.parent&.assignment? || node.parent&.type?(
|
|
155
|
+
if node.parent&.assignment? || node.parent&.type?(*VALUE_PARENT_TYPES)
|
|
145
156
|
replace_begin_with_statement(corrector, offense_range, node)
|
|
146
157
|
else
|
|
147
158
|
remove_begin(corrector, offense_range, node)
|
|
148
159
|
end
|
|
149
160
|
|
|
150
|
-
|
|
151
|
-
correct_modifier_form_after_multiline_begin_block(corrector, node)
|
|
152
|
-
end
|
|
153
|
-
corrector.remove(node.loc.end)
|
|
161
|
+
correct_end(corrector, node)
|
|
154
162
|
end
|
|
155
163
|
end
|
|
156
164
|
|
|
165
|
+
def end_cannot_join_statement_line?(node)
|
|
166
|
+
return false if use_modifier_form_after_multiline_begin_block?(node)
|
|
167
|
+
return false unless code_after_end_on_its_own_line?(node)
|
|
168
|
+
|
|
169
|
+
range_with_end_line_break(node).begin_pos != statement_end_pos(node)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def use_modifier_form_after_multiline_begin_block?(node)
|
|
173
|
+
return false unless (parent = node.parent)
|
|
174
|
+
return false unless node.multiline? && parent.if_type? && parent.modifier_form?
|
|
175
|
+
|
|
176
|
+
!parent.condition.equal?(node)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def code_after_end_on_its_own_line?(node)
|
|
180
|
+
end_range = node.loc.end
|
|
181
|
+
return false unless begins_its_line?(end_range)
|
|
182
|
+
|
|
183
|
+
rest_of_line = end_range.source_line[end_range.last_column..].lstrip
|
|
184
|
+
|
|
185
|
+
!rest_of_line.empty? && !rest_of_line.start_with?('#')
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def range_with_end_line_break(node)
|
|
189
|
+
range_with_surrounding_space(node.loc.end, side: :left, newlines: true)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def statement_end_pos(node)
|
|
193
|
+
return node.loc.begin.end_pos if node.children.empty?
|
|
194
|
+
|
|
195
|
+
node.children.last.source_range.end_pos
|
|
196
|
+
end
|
|
197
|
+
|
|
157
198
|
def replace_begin_with_statement(corrector, offense_range, node)
|
|
158
199
|
first_child = node.children.first
|
|
159
200
|
|
|
160
201
|
source = first_child.source
|
|
161
|
-
source = "(#{source})" if
|
|
202
|
+
source = "(#{source})" if parentheses_required?(first_child, node.parent)
|
|
162
203
|
|
|
163
204
|
corrector.replace(offense_range, source)
|
|
164
205
|
corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
|
|
@@ -166,6 +207,15 @@ module RuboCop
|
|
|
166
207
|
restore_removed_comments(corrector, offense_range, node, first_child)
|
|
167
208
|
end
|
|
168
209
|
|
|
210
|
+
def parentheses_required?(statement, parent)
|
|
211
|
+
return true if low_precedence_statement?(statement)
|
|
212
|
+
return false unless command_call?(statement)
|
|
213
|
+
return parent.type?(:optarg, :kwoptarg) unless parent.assignment?
|
|
214
|
+
|
|
215
|
+
parent = parent.parent while parent.parent&.assignment?
|
|
216
|
+
!statement_position?(parent)
|
|
217
|
+
end
|
|
218
|
+
|
|
169
219
|
def low_precedence_statement?(node)
|
|
170
220
|
return node.modifier_form? if node.basic_conditional?
|
|
171
221
|
return node.prefix_not? if node.send_type?
|
|
@@ -173,6 +223,22 @@ module RuboCop
|
|
|
173
223
|
node.operator_keyword? && node.semantic_operator?
|
|
174
224
|
end
|
|
175
225
|
|
|
226
|
+
def command_call?(node)
|
|
227
|
+
node = node.send_node if node.any_block_type?
|
|
228
|
+
return false unless node.type?(:call, :super, :yield)
|
|
229
|
+
|
|
230
|
+
node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def statement_position?(node)
|
|
234
|
+
parent = node.parent
|
|
235
|
+
return true if parent.nil? || parent.type?(*STATEMENT_CONTAINER_TYPES)
|
|
236
|
+
return parent.body.equal?(node) if parent.type?(:when, :in_pattern, :for)
|
|
237
|
+
return false if parent.if_type? && parent.ternary?
|
|
238
|
+
|
|
239
|
+
parent.type?(:if, :while, :until, :case, :case_match) && !parent.condition.equal?(node)
|
|
240
|
+
end
|
|
241
|
+
|
|
176
242
|
def remove_begin(corrector, offense_range, node)
|
|
177
243
|
if node.parent.respond_to?(:endless?) && node.parent.endless?
|
|
178
244
|
offense_range = range_with_surrounding_space(offense_range, newlines: true)
|
|
@@ -190,11 +256,14 @@ module RuboCop
|
|
|
190
256
|
corrector.insert_before(node.parent, comments) unless comments.blank?
|
|
191
257
|
end
|
|
192
258
|
|
|
193
|
-
def
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
259
|
+
def correct_end(corrector, node)
|
|
260
|
+
if use_modifier_form_after_multiline_begin_block?(node)
|
|
261
|
+
correct_modifier_form_after_multiline_begin_block(corrector, node)
|
|
262
|
+
elsif code_after_end_on_its_own_line?(node)
|
|
263
|
+
corrector.remove(range_with_end_line_break(node))
|
|
264
|
+
else
|
|
265
|
+
corrector.remove(node.loc.end)
|
|
266
|
+
end
|
|
198
267
|
end
|
|
199
268
|
|
|
200
269
|
def correct_modifier_form_after_multiline_begin_block(corrector, node)
|
|
@@ -238,7 +307,9 @@ module RuboCop
|
|
|
238
307
|
end
|
|
239
308
|
|
|
240
309
|
def valid_begin_assignment?(node)
|
|
241
|
-
node.parent&.assignment?
|
|
310
|
+
return false unless node.parent&.assignment?
|
|
311
|
+
|
|
312
|
+
!node.children.one? || too_long_when_joined?(node) || multiline_memoization?(node)
|
|
242
313
|
end
|
|
243
314
|
|
|
244
315
|
def too_long_when_joined?(node)
|
|
@@ -264,10 +335,19 @@ module RuboCop
|
|
|
264
335
|
SPACE_INSIDE_PARENS_STYLES.include?(style)
|
|
265
336
|
end
|
|
266
337
|
|
|
338
|
+
def multiline_memoization?(node)
|
|
339
|
+
if !node.parent.or_asgn_type? || !config.cop_enabled?('Style/MultilineMemoization')
|
|
340
|
+
return false
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
statement = node.children.first
|
|
344
|
+
statement.multiline? && low_precedence_statement?(statement)
|
|
345
|
+
end
|
|
346
|
+
|
|
267
347
|
def inspect_branches(node)
|
|
268
348
|
node.branches.each do |branch|
|
|
269
349
|
next unless branch&.kwbegin_type?
|
|
270
|
-
next if branch.
|
|
350
|
+
next if branch.ensure_node || branch.rescue_node
|
|
271
351
|
next if ternary_operand_requiring_begin?(branch)
|
|
272
352
|
|
|
273
353
|
register_offense(branch)
|
|
@@ -85,7 +85,7 @@ module RuboCop
|
|
|
85
85
|
def range(node)
|
|
86
86
|
return node.selector unless node.method?(:each)
|
|
87
87
|
|
|
88
|
-
if node
|
|
88
|
+
if receiver_of_dot_call?(node)
|
|
89
89
|
node.selector.join(node.parent.loc.dot)
|
|
90
90
|
else
|
|
91
91
|
node.loc.dot.join(node.selector)
|
|
@@ -112,6 +112,13 @@ module RuboCop
|
|
|
112
112
|
corrector.replace(redundant_node.loc.selector, 'each.with_object')
|
|
113
113
|
end
|
|
114
114
|
end
|
|
115
|
+
|
|
116
|
+
def receiver_of_dot_call?(node)
|
|
117
|
+
parent = node.parent
|
|
118
|
+
return false unless parent&.call_type?
|
|
119
|
+
|
|
120
|
+
parent.receiver.equal?(node) && parent.loc?(:dot)
|
|
121
|
+
end
|
|
115
122
|
end
|
|
116
123
|
end
|
|
117
124
|
end
|
|
@@ -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
|
|
@@ -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
|
|
@@ -100,7 +102,7 @@ module RuboCop
|
|
|
100
102
|
end
|
|
101
103
|
|
|
102
104
|
# The body gets parenthesized when it becomes endless, so it is no longer a command.
|
|
103
|
-
def
|
|
105
|
+
def command_body_outside_statement?(body)
|
|
104
106
|
!require_parentheses?(body) && super
|
|
105
107
|
end
|
|
106
108
|
|
|
@@ -119,12 +121,13 @@ module RuboCop
|
|
|
119
121
|
end
|
|
120
122
|
|
|
121
123
|
def correct_to_endless(corrector, node)
|
|
122
|
-
receiver =
|
|
124
|
+
receiver = node.loc.keyword.end.join(node.loc.name.begin).source.lstrip if node.receiver
|
|
123
125
|
body_source = method_body_source(node.body)
|
|
124
126
|
arguments = endless_arguments(node)
|
|
125
127
|
replacement = "def #{receiver}#{node.method_name}#{arguments} = #{body_source}"
|
|
126
128
|
|
|
127
129
|
corrector.replace(node, replacement)
|
|
130
|
+
ignore_node(node)
|
|
128
131
|
end
|
|
129
132
|
|
|
130
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)
|
|
@@ -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)
|