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
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
|
35
35
|
# while true
|
|
36
36
|
# break if condition
|
|
37
37
|
# end
|
|
38
|
-
class LiteralAsCondition < Base
|
|
38
|
+
class LiteralAsCondition < Base # rubocop:disable Metrics/ClassLength
|
|
39
39
|
include RangeHelp
|
|
40
40
|
extend AutoCorrector
|
|
41
41
|
|
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
|
50
50
|
# the leftmost node can lead to a void value expression syntax error.
|
|
51
51
|
next if void_value_expression?(node.rhs)
|
|
52
52
|
|
|
53
|
-
corrector
|
|
53
|
+
replace_node(corrector, node, node.rhs.source)
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
|
|
@@ -62,7 +62,7 @@ module RuboCop
|
|
|
62
62
|
# the leftmost node can lead to a void value expression syntax error.
|
|
63
63
|
next if void_value_expression?(node.rhs)
|
|
64
64
|
|
|
65
|
-
corrector
|
|
65
|
+
replace_node(corrector, node, node.rhs.source)
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
|
|
@@ -79,11 +79,11 @@ module RuboCop
|
|
|
79
79
|
|
|
80
80
|
if node.condition.truthy_literal?
|
|
81
81
|
add_offense(node.condition) do |corrector|
|
|
82
|
-
corrector
|
|
82
|
+
replace_node(corrector, node.condition, 'true')
|
|
83
83
|
end
|
|
84
84
|
elsif node.condition.falsey_literal?
|
|
85
85
|
add_offense(node.condition) do |corrector|
|
|
86
|
-
corrector
|
|
86
|
+
replace_node(corrector, node, removable?(node) ? '' : 'nil')
|
|
87
87
|
end
|
|
88
88
|
end
|
|
89
89
|
end
|
|
@@ -94,13 +94,13 @@ module RuboCop
|
|
|
94
94
|
|
|
95
95
|
if node.condition.truthy_literal?
|
|
96
96
|
add_offense(node.condition) do |corrector|
|
|
97
|
-
corrector
|
|
97
|
+
replace_node(corrector, node, node.source.sub(node.condition.source, 'true'))
|
|
98
98
|
end
|
|
99
99
|
elsif node.condition.falsey_literal?
|
|
100
100
|
add_offense(node.condition) do |corrector|
|
|
101
101
|
next if orphans_loop_control_keyword?(node)
|
|
102
102
|
|
|
103
|
-
corrector
|
|
103
|
+
replace_node(corrector, node, node.body.child_nodes.map(&:source).join("\n"))
|
|
104
104
|
end
|
|
105
105
|
end
|
|
106
106
|
end
|
|
@@ -110,11 +110,11 @@ module RuboCop
|
|
|
110
110
|
|
|
111
111
|
if node.condition.falsey_literal?
|
|
112
112
|
add_offense(node.condition) do |corrector|
|
|
113
|
-
corrector
|
|
113
|
+
replace_node(corrector, node.condition, 'false')
|
|
114
114
|
end
|
|
115
115
|
elsif node.condition.truthy_literal?
|
|
116
116
|
add_offense(node.condition) do |corrector|
|
|
117
|
-
corrector
|
|
117
|
+
replace_node(corrector, node, removable?(node) ? '' : 'nil')
|
|
118
118
|
end
|
|
119
119
|
end
|
|
120
120
|
end
|
|
@@ -125,13 +125,13 @@ module RuboCop
|
|
|
125
125
|
|
|
126
126
|
if node.condition.falsey_literal?
|
|
127
127
|
add_offense(node.condition) do |corrector|
|
|
128
|
-
corrector
|
|
128
|
+
replace_node(corrector, node, node.source.sub(node.condition.source, 'false'))
|
|
129
129
|
end
|
|
130
130
|
elsif node.condition.truthy_literal?
|
|
131
131
|
add_offense(node.condition) do |corrector|
|
|
132
132
|
next if orphans_loop_control_keyword?(node)
|
|
133
133
|
|
|
134
|
-
corrector
|
|
134
|
+
replace_node(corrector, node, node.body.child_nodes.map(&:source).join("\n"))
|
|
135
135
|
end
|
|
136
136
|
end
|
|
137
137
|
end
|
|
@@ -286,19 +286,30 @@ module RuboCop
|
|
|
286
286
|
"#{node.else_branch.source.sub('elsif', 'if')}\nend"
|
|
287
287
|
elsif node.else? || node.ternary?
|
|
288
288
|
node.else_branch.source
|
|
289
|
-
elsif node
|
|
290
|
-
'nil'
|
|
291
|
-
else
|
|
289
|
+
elsif removable?(node)
|
|
292
290
|
'' # Equivalent to removing the node
|
|
291
|
+
else
|
|
292
|
+
'nil'
|
|
293
293
|
end
|
|
294
294
|
|
|
295
295
|
add_offense(cond) do |corrector|
|
|
296
|
-
|
|
296
|
+
replace_node(corrector, node, new_node)
|
|
297
|
+
end
|
|
298
|
+
end
|
|
297
299
|
|
|
298
|
-
|
|
300
|
+
def removable?(node)
|
|
301
|
+
return false if node.value_used?
|
|
302
|
+
return true unless (parent = node.parent)
|
|
303
|
+
return true if parent.type?(:begin, :kwbegin, :when)
|
|
299
304
|
|
|
300
|
-
|
|
301
|
-
|
|
305
|
+
parent.type?(:if, :while, :until) && parent.loc?(:end)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def replace_node(corrector, node, replacement)
|
|
309
|
+
return if node.each_ancestor.any? { |ancestor| ignored_node?(ancestor) }
|
|
310
|
+
|
|
311
|
+
corrector.replace(node, replacement)
|
|
312
|
+
ignore_node(node)
|
|
302
313
|
end
|
|
303
314
|
end
|
|
304
315
|
end
|
|
@@ -24,6 +24,10 @@ module RuboCop
|
|
|
24
24
|
MSG = 'Literal interpolation detected.'
|
|
25
25
|
COMPOSITE = %i[array hash pair irange erange].freeze
|
|
26
26
|
|
|
27
|
+
def self.autocorrect_incompatible_with
|
|
28
|
+
[Style::PercentLiteralDelimiters, Style::RegexpLiteral]
|
|
29
|
+
end
|
|
30
|
+
|
|
27
31
|
def on_interpolation(begin_node)
|
|
28
32
|
final_node = begin_node.children.last
|
|
29
33
|
return unless offending?(final_node)
|
|
@@ -71,6 +75,7 @@ module RuboCop
|
|
|
71
75
|
# interpolation should not be removed if the expanded value
|
|
72
76
|
# contains a space character.
|
|
73
77
|
return if in_array_percent_literal?(begin_node) && /\s|\A\z/.match?(value)
|
|
78
|
+
return if breaks_delimiters?(begin_node, value)
|
|
74
79
|
|
|
75
80
|
value
|
|
76
81
|
end
|
|
@@ -151,9 +156,9 @@ module RuboCop
|
|
|
151
156
|
end
|
|
152
157
|
|
|
153
158
|
def autocorrected_value_for_array(node)
|
|
154
|
-
return node.source
|
|
159
|
+
return escape_string_content(node.source) unless node.percent_literal?
|
|
155
160
|
|
|
156
|
-
|
|
161
|
+
escape_string_content(node.values.map { |value| value.value.to_s }.to_s)
|
|
157
162
|
end
|
|
158
163
|
|
|
159
164
|
def autocorrected_value_for_hash(node)
|
|
@@ -188,8 +193,15 @@ module RuboCop
|
|
|
188
193
|
|
|
189
194
|
# Does node print its own source when converted to a string?
|
|
190
195
|
def prints_as_self?(node)
|
|
191
|
-
node.basic_literal?
|
|
192
|
-
|
|
196
|
+
return true if node.basic_literal?
|
|
197
|
+
return false unless COMPOSITE.include?(node.type)
|
|
198
|
+
return numeric_open_range?(node) if node.children.any?(&:nil?)
|
|
199
|
+
|
|
200
|
+
node.children.all? { |child| prints_as_self?(child) }
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def numeric_open_range?(node)
|
|
204
|
+
node.children.compact.all? { |child| child.type?(:int, :float) }
|
|
193
205
|
end
|
|
194
206
|
|
|
195
207
|
def space_literal?(node)
|
|
@@ -215,6 +227,40 @@ module RuboCop
|
|
|
215
227
|
grandparent&.array_type? && grandparent.percent_literal?
|
|
216
228
|
end
|
|
217
229
|
|
|
230
|
+
def breaks_delimiters?(begin_node, value)
|
|
231
|
+
return false unless (literal = delimited_literal(begin_node))
|
|
232
|
+
|
|
233
|
+
opening = literal.loc.begin.source[-1]
|
|
234
|
+
closing = literal.loc.end.source[0]
|
|
235
|
+
return !balanced?(value, opening, closing) if opening != closing
|
|
236
|
+
|
|
237
|
+
!escaped_delimiter?(literal, closing) && value.include?(closing)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def delimited_literal(begin_node)
|
|
241
|
+
parent = begin_node.parent
|
|
242
|
+
literal = in_array_percent_literal?(begin_node) ? parent.parent : parent
|
|
243
|
+
|
|
244
|
+
literal if literal.loc?(:begin) && literal.loc?(:end)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def balanced?(value, opening, closing)
|
|
248
|
+
depth = 0
|
|
249
|
+
value.each_char do |char|
|
|
250
|
+
depth += 1 if char == opening
|
|
251
|
+
depth -= 1 if char == closing
|
|
252
|
+
return false if depth.negative?
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
depth.zero?
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def escaped_delimiter?(literal, delimiter)
|
|
259
|
+
return true if delimiter == '"'
|
|
260
|
+
|
|
261
|
+
delimiter == '/' && literal.regexp_type? && literal.slash_literal?
|
|
262
|
+
end
|
|
263
|
+
|
|
218
264
|
def escape_trailing_hash_sign(begin_node, value)
|
|
219
265
|
following = processed_source.buffer.source[begin_node.source_range.end_pos]
|
|
220
266
|
return value unless %w[{ @ $].include?(following) && unescaped_hash_sign?(value)
|
|
@@ -40,6 +40,7 @@ module RuboCop
|
|
|
40
40
|
# NestedPercentLiteral violation to be emitted.
|
|
41
41
|
PERCENT_LITERAL_TYPES = PreferredDelimiters::PERCENT_LITERAL_TYPES
|
|
42
42
|
REGEXES = PERCENT_LITERAL_TYPES.map { |percent_literal| /\A#{percent_literal}\W/ }.freeze
|
|
43
|
+
FORMAT_SEQUENCE = /\A#{RuboCop::Cop::Utils::FormatString::SEQUENCE}\z/.freeze
|
|
43
44
|
|
|
44
45
|
def on_array(node)
|
|
45
46
|
process(node, *PERCENT_LITERAL_TYPES)
|
|
@@ -54,6 +55,8 @@ module RuboCop
|
|
|
54
55
|
def contains_percent_literals?(node)
|
|
55
56
|
node.each_child_node.any? do |child|
|
|
56
57
|
literal = child.children.first.to_s.scrub
|
|
58
|
+
next false if literal.match?(FORMAT_SEQUENCE)
|
|
59
|
+
|
|
57
60
|
REGEXES.any? { |regex| literal.match?(regex) }
|
|
58
61
|
end
|
|
59
62
|
end
|
|
@@ -42,6 +42,7 @@ module RuboCop
|
|
|
42
42
|
# FileUtils.rm_f(path)
|
|
43
43
|
#
|
|
44
44
|
class NonAtomicFileOperation < Base
|
|
45
|
+
include RangeHelp
|
|
45
46
|
extend AutoCorrector
|
|
46
47
|
|
|
47
48
|
MSG_REMOVE_FILE_EXIST_CHECK = 'Remove unnecessary existence check ' \
|
|
@@ -77,6 +78,10 @@ module RuboCop
|
|
|
77
78
|
(pair (sym :force) (:false))
|
|
78
79
|
PATTERN
|
|
79
80
|
|
|
81
|
+
def self.autocorrect_incompatible_with
|
|
82
|
+
[Style::MethodCallWithArgsParentheses]
|
|
83
|
+
end
|
|
84
|
+
|
|
80
85
|
def on_send(node)
|
|
81
86
|
return unless node.receiver&.const_type?
|
|
82
87
|
return unless if_node_child?(node)
|
|
@@ -106,7 +111,7 @@ module RuboCop
|
|
|
106
111
|
range = parent.loc.keyword.begin.join(parent.condition.source_range.end)
|
|
107
112
|
|
|
108
113
|
add_offense(range, message: message_remove_file_exist_check(exist_node)) do |corrector|
|
|
109
|
-
autocorrect(corrector, node, range)
|
|
114
|
+
autocorrect(corrector, node, range) if autocorrectable?(node)
|
|
110
115
|
end
|
|
111
116
|
end
|
|
112
117
|
|
|
@@ -120,6 +125,34 @@ module RuboCop
|
|
|
120
125
|
format(MSG_REMOVE_FILE_EXIST_CHECK, receiver: receiver, method_name: method_name)
|
|
121
126
|
end
|
|
122
127
|
|
|
128
|
+
def autocorrectable?(node)
|
|
129
|
+
parent = node.parent
|
|
130
|
+
return false if parent.elsif?
|
|
131
|
+
return true if parent.modifier_form?
|
|
132
|
+
return true unless code_after_end?(parent)
|
|
133
|
+
return false if !node.parenthesized? || last_heredoc_end_pos(node)
|
|
134
|
+
|
|
135
|
+
!processed_source.contains_comment?(body_to_end_range(node))
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def code_after_end?(if_node)
|
|
139
|
+
end_keyword = if_node.loc.end
|
|
140
|
+
|
|
141
|
+
!end_keyword.source_line[end_keyword.last_column..].match?(/\A\s*(?:#|\z)/)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def body_to_end_range(node)
|
|
145
|
+
begin_pos = [node.source_range.end_pos, last_heredoc_end_pos(node)].compact.max
|
|
146
|
+
|
|
147
|
+
range_between(begin_pos, node.parent.loc.end.end_pos)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def last_heredoc_end_pos(node)
|
|
151
|
+
node.each_descendant(:any_str).select(&:heredoc?).map do |heredoc|
|
|
152
|
+
heredoc.loc.heredoc_end.end_pos
|
|
153
|
+
end.max
|
|
154
|
+
end
|
|
155
|
+
|
|
123
156
|
def autocorrect(corrector, node, range)
|
|
124
157
|
corrector.remove(range)
|
|
125
158
|
autocorrect_replace_method(corrector, node)
|
|
@@ -127,10 +160,17 @@ module RuboCop
|
|
|
127
160
|
if node.parent.modifier_form?
|
|
128
161
|
corrector.remove(node.source_range.end.join(node.parent.loc.keyword.begin))
|
|
129
162
|
else
|
|
130
|
-
corrector.remove(node
|
|
163
|
+
corrector.remove(end_keyword_range(node))
|
|
131
164
|
end
|
|
132
165
|
end
|
|
133
166
|
|
|
167
|
+
def end_keyword_range(node)
|
|
168
|
+
range = body_to_end_range(node)
|
|
169
|
+
return node.parent.loc.end if processed_source.contains_comment?(range)
|
|
170
|
+
|
|
171
|
+
range
|
|
172
|
+
end
|
|
173
|
+
|
|
134
174
|
def autocorrect_replace_method(corrector, node)
|
|
135
175
|
return if force_method?(node)
|
|
136
176
|
|
|
@@ -88,6 +88,10 @@ module RuboCop
|
|
|
88
88
|
CONVERSION_METHODS = %i[Integer Float Complex Rational to_i to_f to_c to_r].freeze
|
|
89
89
|
METHODS = CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')
|
|
90
90
|
|
|
91
|
+
def self.autocorrect_incompatible_with
|
|
92
|
+
[Style::MethodCallWithArgsParentheses, Style::NestedParenthesizedCalls]
|
|
93
|
+
end
|
|
94
|
+
|
|
91
95
|
# @!method to_method(node)
|
|
92
96
|
def_node_matcher :to_method, <<~PATTERN
|
|
93
97
|
(call $_ ${#{METHODS}})
|
|
@@ -21,6 +21,10 @@ module RuboCop
|
|
|
21
21
|
|
|
22
22
|
MSG = '`%<argument>s` interpreted as grouped expression.'
|
|
23
23
|
|
|
24
|
+
def self.autocorrect_incompatible_with
|
|
25
|
+
[Style::RedundantParentheses]
|
|
26
|
+
end
|
|
27
|
+
|
|
24
28
|
def on_send(node)
|
|
25
29
|
return if valid_context?(node)
|
|
26
30
|
|
|
@@ -54,21 +58,80 @@ module RuboCop
|
|
|
54
58
|
end
|
|
55
59
|
|
|
56
60
|
def invalid_bare_argument?(node)
|
|
57
|
-
|
|
61
|
+
return true if multiple_statements?(node) || not_a_single_expression?(node)
|
|
62
|
+
|
|
63
|
+
node = innermost_expression(node)
|
|
58
64
|
return false unless node
|
|
65
|
+
return true if multiple_values_assignment?(node)
|
|
66
|
+
|
|
67
|
+
keyword_operator?(node) || modifier_expression?(node) || command_call_operand?(node)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def multiple_statements?(node)
|
|
71
|
+
node.begin_type? && node.children.size > 1
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def not_a_single_expression?(node)
|
|
75
|
+
return false unless node.begin_type?
|
|
76
|
+
|
|
77
|
+
expression = node.children.first
|
|
78
|
+
return true unless expression
|
|
79
|
+
|
|
80
|
+
processed_source.tokens_within(node).any? do |token|
|
|
81
|
+
token.semicolon? && !expression.source_range.contains?(token.pos)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def innermost_expression(node)
|
|
86
|
+
node = node.children.first while node&.begin_type? && node.children.one?
|
|
87
|
+
node
|
|
88
|
+
end
|
|
59
89
|
|
|
60
|
-
|
|
90
|
+
def multiple_values_assignment?(node)
|
|
91
|
+
return true if node.masgn_type?
|
|
92
|
+
return false unless node.assignment? || (node.call_type? && node.assignment_method?)
|
|
93
|
+
|
|
94
|
+
value = node.children.last
|
|
95
|
+
value.array_type? && !value.bracketed?
|
|
61
96
|
end
|
|
62
97
|
|
|
63
98
|
def keyword_operator?(node)
|
|
64
99
|
(node.operator_keyword? && node.semantic_operator?) ||
|
|
65
|
-
node.rescue_type? || (node.send_type? && node.prefix_not?)
|
|
100
|
+
node.rescue_type? || node.match_pattern_p_type? || (node.send_type? && node.prefix_not?)
|
|
66
101
|
end
|
|
67
102
|
|
|
68
103
|
def modifier_expression?(node)
|
|
69
104
|
node.type?(:if, :while, :until) && node.modifier_form?
|
|
70
105
|
end
|
|
71
106
|
|
|
107
|
+
def command_call_operand?(node)
|
|
108
|
+
if node.assignment? || (node.call_type? && node.assignment_method?)
|
|
109
|
+
command_call_value?(node.children.last)
|
|
110
|
+
elsif node.send_type? && node.prefix_bang?
|
|
111
|
+
command_call_value?(node.receiver)
|
|
112
|
+
else
|
|
113
|
+
command_call_with_do_block?(node)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def command_call_value?(node)
|
|
118
|
+
command_call?(node) || command_call_operand?(node)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def command_call?(node)
|
|
122
|
+
node = node.send_node if node.any_block_type?
|
|
123
|
+
return false unless node.type?(:call, :super, :yield)
|
|
124
|
+
|
|
125
|
+
node.arguments? && !node.parenthesized? && !node.operator_method?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def command_call_with_do_block?(node)
|
|
129
|
+
node = node.receiver while node.call_type? && node.receiver
|
|
130
|
+
return false unless node.any_block_type?
|
|
131
|
+
|
|
132
|
+
node.keywords? && command_call?(node)
|
|
133
|
+
end
|
|
134
|
+
|
|
72
135
|
def chained_calls?(node)
|
|
73
136
|
first_argument = node.first_argument
|
|
74
137
|
first_argument.call_type? && (node.children.last&.children&.count || 0) > 1
|
|
@@ -9,6 +9,11 @@ module RuboCop
|
|
|
9
9
|
# example, mistranslating an array of literals to percent string notation)
|
|
10
10
|
# rather than meant to be part of the resulting strings.
|
|
11
11
|
#
|
|
12
|
+
# When a quoted string contains whitespace, `%w` splits it into separate values,
|
|
13
|
+
# so removing the quotes would not restore the intended string.
|
|
14
|
+
# Such an array is corrected to an array literal instead, as long as its content
|
|
15
|
+
# reads as one with only string elements.
|
|
16
|
+
#
|
|
12
17
|
# @safety
|
|
13
18
|
# The cop is unsafe because the correction changes the values in the array
|
|
14
19
|
# and that might have been done purposely.
|
|
@@ -26,6 +31,12 @@ module RuboCop
|
|
|
26
31
|
#
|
|
27
32
|
# # good
|
|
28
33
|
# %w(foo bar)
|
|
34
|
+
#
|
|
35
|
+
# # bad - the quoted strings are split at whitespace
|
|
36
|
+
# %w('foo bar', 'baz')
|
|
37
|
+
#
|
|
38
|
+
# # good
|
|
39
|
+
# ['foo bar', 'baz']
|
|
29
40
|
class PercentStringArray < Base
|
|
30
41
|
include PercentLiteral
|
|
31
42
|
extend AutoCorrector
|
|
@@ -36,27 +47,69 @@ module RuboCop
|
|
|
36
47
|
|
|
37
48
|
MSG = "Within `%w`/`%W`, quotes and ',' are unnecessary and may be " \
|
|
38
49
|
'unwanted in the resulting strings.'
|
|
50
|
+
MSG_SPLIT_STRING = 'Within `%w`/`%W`, quoted strings are split at whitespace; ' \
|
|
51
|
+
'use an array literal instead.'
|
|
39
52
|
|
|
40
53
|
def on_array(node)
|
|
41
54
|
process(node, '%w', '%W')
|
|
42
55
|
end
|
|
43
56
|
|
|
44
57
|
def on_percent_literal(node)
|
|
45
|
-
|
|
58
|
+
if split_quoted_string?(node)
|
|
59
|
+
register_split_quoted_string(node)
|
|
60
|
+
elsif contains_quotes_or_commas?(node)
|
|
61
|
+
register_quotes_or_commas(node)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
46
64
|
|
|
47
|
-
|
|
48
|
-
node.each_value do |value|
|
|
49
|
-
range = value.source_range
|
|
65
|
+
private
|
|
50
66
|
|
|
51
|
-
|
|
52
|
-
|
|
67
|
+
def split_quoted_string?(node)
|
|
68
|
+
sources = node.values.map { |value| value.source.delete_suffix(',') }
|
|
53
69
|
|
|
54
|
-
|
|
55
|
-
|
|
70
|
+
sources.each_with_index.any? do |source, index|
|
|
71
|
+
next false unless (quote = unclosed_quote(source))
|
|
72
|
+
|
|
73
|
+
sources.drop(index + 1).any? { |rest| closing_quote?(rest, quote) }
|
|
56
74
|
end
|
|
57
75
|
end
|
|
58
76
|
|
|
59
|
-
|
|
77
|
+
def unclosed_quote(source)
|
|
78
|
+
quote = source[0]
|
|
79
|
+
return unless LEADING_QUOTE.match?(quote)
|
|
80
|
+
|
|
81
|
+
quote if source.length > 1 && !source.end_with?(quote)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def closing_quote?(source, quote)
|
|
85
|
+
source.end_with?(quote) && (source.length == 1 || !source.start_with?(quote))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def register_split_quoted_string(node)
|
|
89
|
+
add_offense(node, message: MSG_SPLIT_STRING) do |corrector|
|
|
90
|
+
next unless (array_literal = array_literal_source(node))
|
|
91
|
+
|
|
92
|
+
corrector.replace(node, array_literal)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def array_literal_source(node)
|
|
97
|
+
content = range_between(node.loc.begin.end_pos, node.loc.end.begin_pos).source
|
|
98
|
+
array_literal = "[#{content}]"
|
|
99
|
+
|
|
100
|
+
array_literal if string_array?(parse(array_literal))
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def string_array?(processed_source)
|
|
104
|
+
return false unless processed_source.valid_syntax? && processed_source.comments.empty?
|
|
105
|
+
return false unless (array = processed_source.ast).array_type?
|
|
106
|
+
|
|
107
|
+
array.children.all? { |child| quoted_string?(child) }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def quoted_string?(node)
|
|
111
|
+
node.type?(:str, :dstr) && node.loc?(:begin)
|
|
112
|
+
end
|
|
60
113
|
|
|
61
114
|
def contains_quotes_or_commas?(node)
|
|
62
115
|
node.values.any? do |value|
|
|
@@ -68,6 +121,19 @@ module RuboCop
|
|
|
68
121
|
QUOTES_AND_COMMAS.any? { |pat| literal.match?(pat) }
|
|
69
122
|
end
|
|
70
123
|
end
|
|
124
|
+
|
|
125
|
+
def register_quotes_or_commas(node)
|
|
126
|
+
add_offense(node) do |corrector|
|
|
127
|
+
node.each_value do |value|
|
|
128
|
+
range = value.source_range
|
|
129
|
+
|
|
130
|
+
match = range.source.match(TRAILING_QUOTE)
|
|
131
|
+
corrector.remove_trailing(range, match[0].length) if match
|
|
132
|
+
|
|
133
|
+
corrector.remove_leading(range, 1) if LEADING_QUOTE.match?(range.source)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
71
137
|
end
|
|
72
138
|
end
|
|
73
139
|
end
|
|
@@ -111,12 +111,25 @@ module RuboCop
|
|
|
111
111
|
# offenses, so its directives cannot be judged - they typically
|
|
112
112
|
# prepare the code for the moment the cop gets enabled.
|
|
113
113
|
next if pending_cop_not_run?(cop) || disabled_by_preview?(cop)
|
|
114
|
+
next if excluded_from_run?(cop)
|
|
114
115
|
|
|
115
116
|
each_already_disabled(cop, line_ranges, &block)
|
|
116
117
|
each_line_range(cop, line_ranges, &block)
|
|
117
118
|
end
|
|
118
119
|
end
|
|
119
120
|
|
|
121
|
+
# A cop left out with `--except` produces no offenses in this run, but
|
|
122
|
+
# its directives are still needed in a full run.
|
|
123
|
+
def excluded_from_run?(cop)
|
|
124
|
+
Badge.parse(cop).match_name?(@options[:except])
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def department_partly_excluded_from_run?(department)
|
|
128
|
+
Array(@options[:except]).any? do |name|
|
|
129
|
+
name == department || name.start_with?("#{department}/")
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
120
133
|
def pending_cop_not_run?(cop)
|
|
121
134
|
# `Config#for_cop` corrects wrongly-namespaced names, but a name that
|
|
122
135
|
# is not registered as written never suppresses anything, so it is
|
|
@@ -146,8 +159,8 @@ module RuboCop
|
|
|
146
159
|
comment = directive_comment(line_range)
|
|
147
160
|
next if skip_directive?(comment)
|
|
148
161
|
|
|
149
|
-
|
|
150
|
-
redundant = find_redundant_directive(cop, comment, line_range,
|
|
162
|
+
following_ranges = line_ranges[(line_range_index + 1)..]
|
|
163
|
+
redundant = find_redundant_directive(cop, comment, line_range, following_ranges)
|
|
151
164
|
yield comment, redundant if redundant
|
|
152
165
|
end
|
|
153
166
|
end
|
|
@@ -191,14 +204,33 @@ module RuboCop
|
|
|
191
204
|
directive.push? || directive.pop? || directive.next?
|
|
192
205
|
end
|
|
193
206
|
|
|
194
|
-
def find_redundant_directive(cop, comment, line_range,
|
|
195
|
-
if all_disabled?(comment)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
207
|
+
def find_redundant_directive(cop, comment, line_range, following_ranges)
|
|
208
|
+
return find_redundant_all(line_range, following_ranges.first) if all_disabled?(comment)
|
|
209
|
+
|
|
210
|
+
range = line_range_through_repeated_disables(cop, line_range, following_ranges)
|
|
211
|
+
if department_disabled?(cop, comment)
|
|
212
|
+
find_redundant_department(cop, range)
|
|
199
213
|
else
|
|
200
|
-
find_redundant_cop(cop,
|
|
214
|
+
find_redundant_cop(cop, range)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def line_range_through_repeated_disables(cop, line_range, following_ranges)
|
|
219
|
+
last_range = line_range
|
|
220
|
+
following_ranges.each do |range|
|
|
221
|
+
break unless followed_ranges?(last_range, range) && repeated_disable?(cop, range)
|
|
222
|
+
|
|
223
|
+
last_range = range
|
|
201
224
|
end
|
|
225
|
+
|
|
226
|
+
line_range.begin..last_range.end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def repeated_disable?(cop, range)
|
|
230
|
+
comment = directive_comment(range)
|
|
231
|
+
return false if comment.nil? || skip_directive?(comment)
|
|
232
|
+
|
|
233
|
+
!all_disabled?(comment) && !department_disabled?(cop, comment)
|
|
202
234
|
end
|
|
203
235
|
|
|
204
236
|
# rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
@@ -236,6 +268,9 @@ module RuboCop
|
|
|
236
268
|
end
|
|
237
269
|
|
|
238
270
|
def find_redundant_all(range, next_range)
|
|
271
|
+
# The cops left out with `--except` may have offenses in the range.
|
|
272
|
+
return if @options[:except]
|
|
273
|
+
|
|
239
274
|
# If there's a disable all comment followed by a comment
|
|
240
275
|
# specifically disabling `cop`, we don't report the `all`
|
|
241
276
|
# comment. If the disable all comment is truly redundant, we will
|
|
@@ -247,6 +282,8 @@ module RuboCop
|
|
|
247
282
|
|
|
248
283
|
def find_redundant_department(cop, range)
|
|
249
284
|
department = cop.split('/').first
|
|
285
|
+
return if department_partly_excluded_from_run?(department)
|
|
286
|
+
|
|
250
287
|
offenses = offenses_to_check.select { |offense| offense.cop_name.start_with?(department) }
|
|
251
288
|
add_department_marker(department) if range_with_offense?(range, offenses)
|
|
252
289
|
end
|
|
@@ -76,7 +76,15 @@ module RuboCop
|
|
|
76
76
|
|
|
77
77
|
def register_orphan_pop(directive)
|
|
78
78
|
add_offense(directive.range, message: MSG_ORPHAN_POP) do |corrector|
|
|
79
|
-
corrector
|
|
79
|
+
remove_directive(corrector, directive.range_with_reason, directive.comment)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def remove_directive(corrector, range, comment)
|
|
84
|
+
if range.end.join(comment.source_range.end).source.match?(/\A\s*(?:#|\z)/)
|
|
85
|
+
corrector.remove(range_with_surrounding_space(range, side: :right))
|
|
86
|
+
else
|
|
87
|
+
corrector.replace(range, '#')
|
|
80
88
|
end
|
|
81
89
|
end
|
|
82
90
|
|
|
@@ -89,18 +97,18 @@ module RuboCop
|
|
|
89
97
|
range_of_offense(comment, name),
|
|
90
98
|
message: format(MSG, cop: all_or_name(name))
|
|
91
99
|
) do |corrector|
|
|
92
|
-
corrector
|
|
100
|
+
correct_directive(corrector, directive, cop_names, name)
|
|
93
101
|
end
|
|
94
102
|
end
|
|
95
103
|
end
|
|
96
104
|
|
|
97
|
-
# When every cop on the directive is redundant the whole
|
|
105
|
+
# When every cop on the directive is redundant the whole directive goes, `--` reason
|
|
98
106
|
# included; otherwise just the one cop is spliced out of the list.
|
|
99
|
-
def
|
|
107
|
+
def correct_directive(corrector, directive, cop_names, name)
|
|
100
108
|
if directive.match?(cop_names)
|
|
101
|
-
|
|
109
|
+
remove_directive(corrector, directive.range_with_reason, directive.comment)
|
|
102
110
|
else
|
|
103
|
-
range_with_comma(comment, name)
|
|
111
|
+
corrector.remove(range_with_comma(directive.comment, name))
|
|
104
112
|
end
|
|
105
113
|
end
|
|
106
114
|
|