rubocop 1.92.3 → 1.93.0
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/README.md +1 -1
- data/config/default.yml +5 -1
- data/lib/rubocop/config.rb +6 -0
- data/lib/rubocop/config_store.rb +1 -0
- data/lib/rubocop/config_validator.rb +45 -1
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +25 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
- data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
- data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +16 -4
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +25 -7
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +14 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +47 -9
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +11 -3
- data/lib/rubocop/cop/lint/number_conversion.rb +10 -0
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +45 -1
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
- data/lib/rubocop/cop/lint/useless_times.rb +40 -12
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +16 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/naming/block_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
- data/lib/rubocop/cop/style/array_coercion.rb +1 -1
- data/lib/rubocop/cop/style/array_intersect.rb +15 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +0 -1
- data/lib/rubocop/cop/style/data_inheritance.rb +78 -9
- data/lib/rubocop/cop/style/dig_chain.rb +13 -2
- data/lib/rubocop/cop/style/each_with_object.rb +21 -5
- data/lib/rubocop/cop/style/empty_class_definition.rb +19 -1
- data/lib/rubocop/cop/style/empty_method.rb +11 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
- data/lib/rubocop/cop/style/file_read.rb +5 -1
- data/lib/rubocop/cop/style/file_write.rb +89 -20
- data/lib/rubocop/cop/style/hash_lookup_method.rb +2 -4
- data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
- data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +4 -11
- data/lib/rubocop/cop/style/non_nil_check.rb +13 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +14 -0
- data/lib/rubocop/cop/style/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +14 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +26 -6
- data/lib/rubocop/cop/style/redundant_each.rb +18 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
- data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -1
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
- data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
- data/lib/rubocop/cop/style/select_by_range.rb +1 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
- data/lib/rubocop/cop/style/self_assignment.rb +0 -4
- data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
- data/lib/rubocop/cop/style/struct_inheritance.rb +68 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +16 -7
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -6
|
@@ -59,7 +59,37 @@ module RuboCop
|
|
|
59
59
|
private
|
|
60
60
|
|
|
61
61
|
def offense?(node)
|
|
62
|
-
node.ternary? && node.multiline?
|
|
62
|
+
return false unless node.ternary? && node.multiline?
|
|
63
|
+
return false if heredoc_body_outside_branch?(node) || outer_heredoc_body_inside?(node)
|
|
64
|
+
|
|
65
|
+
node.source != replacement(node)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def heredoc_body_outside_branch?(node)
|
|
69
|
+
single_line = enforce_single_line_ternary_operator?(node)
|
|
70
|
+
|
|
71
|
+
[node.condition, node.if_branch, node.else_branch].any? do |branch|
|
|
72
|
+
branch.each_node(:any_str).any? do |str|
|
|
73
|
+
next false unless str.heredoc?
|
|
74
|
+
next false if single_line && heredoc_ends_after?(str, node)
|
|
75
|
+
|
|
76
|
+
heredoc_ends_after?(str, branch)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def outer_heredoc_body_inside?(node)
|
|
82
|
+
ternary_range = node.source_range
|
|
83
|
+
|
|
84
|
+
processed_source.ast.each_node(:any_str).any? do |str|
|
|
85
|
+
next false if !str.heredoc? || ternary_range.contains?(str.source_range)
|
|
86
|
+
|
|
87
|
+
ternary_range.overlaps?(str.loc.heredoc_body)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def heredoc_ends_after?(heredoc, node)
|
|
92
|
+
heredoc.loc.heredoc_end.end_pos > node.source_range.end_pos
|
|
63
93
|
end
|
|
64
94
|
|
|
65
95
|
def autocorrect(corrector, node)
|
|
@@ -107,12 +107,20 @@ module RuboCop
|
|
|
107
107
|
|
|
108
108
|
def swap_branches(corrector, node)
|
|
109
109
|
if node.if_branch.nil?
|
|
110
|
-
corrector.remove(
|
|
110
|
+
corrector.remove(else_removal_range(node.loc.else))
|
|
111
111
|
else
|
|
112
112
|
corrector.swap(if_range(node), else_range(node))
|
|
113
113
|
end
|
|
114
114
|
end
|
|
115
115
|
|
|
116
|
+
def else_removal_range(keyword)
|
|
117
|
+
line = keyword.source_line
|
|
118
|
+
return range_by_whole_lines(keyword, include_final_newline: true) if line.strip == 'else'
|
|
119
|
+
|
|
120
|
+
side = keyword.last_column == line.rstrip.length ? :both : :right
|
|
121
|
+
range_with_surrounding_space(keyword, side: side, newlines: false)
|
|
122
|
+
end
|
|
123
|
+
|
|
116
124
|
# Collect the entire if branch, including whitespace and comments
|
|
117
125
|
def if_range(node)
|
|
118
126
|
if node.ternary?
|
|
@@ -48,6 +48,8 @@ module RuboCop
|
|
|
48
48
|
|
|
49
49
|
style_check?(node) do
|
|
50
50
|
add_offense(node.loc.selector) do |corrector|
|
|
51
|
+
next if heredoc_after_receiver?(node)
|
|
52
|
+
|
|
51
53
|
if prefer_comparison?
|
|
52
54
|
autocorrect_to_comparison(corrector, node)
|
|
53
55
|
else
|
|
@@ -65,6 +67,14 @@ module RuboCop
|
|
|
65
67
|
processed_source.contains_comment?(node.receiver.source_range.end.join(node.loc.dot))
|
|
66
68
|
end
|
|
67
69
|
|
|
70
|
+
def heredoc_after_receiver?(node)
|
|
71
|
+
receiver = node.receiver
|
|
72
|
+
return false if receiver.last_line == node.last_line
|
|
73
|
+
|
|
74
|
+
range = receiver.source_range.end.join(node.source_range.end)
|
|
75
|
+
processed_source.tokens_within(range).any? { |token| token.type == :tSTRING_END }
|
|
76
|
+
end
|
|
77
|
+
|
|
68
78
|
def autocorrect_to_comparison(corrector, node)
|
|
69
79
|
range = node.receiver.source_range.end.join(node.loc.selector.end)
|
|
70
80
|
corrector.replace(range, ' == nil')
|
|
@@ -6,10 +6,8 @@ module RuboCop
|
|
|
6
6
|
# Checks for lambdas and procs that always return nil,
|
|
7
7
|
# which can be replaced with an empty lambda or proc instead.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
# would change behavior. A lambda is still reported, since there `return`
|
|
12
|
-
# only exits the lambda itself.
|
|
9
|
+
# In a non-lambda proc, `return` and `break` are non-local exits.
|
|
10
|
+
# They cannot be removed even when their argument is `nil`.
|
|
13
11
|
#
|
|
14
12
|
# @example
|
|
15
13
|
# # bad
|
|
@@ -21,10 +19,6 @@ module RuboCop
|
|
|
21
19
|
#
|
|
22
20
|
# proc { nil }
|
|
23
21
|
#
|
|
24
|
-
# Proc.new do
|
|
25
|
-
# break nil
|
|
26
|
-
# end
|
|
27
|
-
#
|
|
28
22
|
# # good
|
|
29
23
|
# -> {}
|
|
30
24
|
#
|
|
@@ -36,6 +30,7 @@ module RuboCop
|
|
|
36
30
|
# proc {}
|
|
37
31
|
#
|
|
38
32
|
# Proc.new { nil if x }
|
|
33
|
+
# proc { break nil }
|
|
39
34
|
#
|
|
40
35
|
class NilLambda < Base
|
|
41
36
|
extend AutoCorrector
|
|
@@ -51,9 +46,7 @@ module RuboCop
|
|
|
51
46
|
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler -- a lambda whose body is `nil` uses no parameters
|
|
52
47
|
return unless node.lambda_or_proc?
|
|
53
48
|
return unless nil_return?(node.body)
|
|
54
|
-
|
|
55
|
-
# so dropping it changes behavior; only a lambda can omit it safely.
|
|
56
|
-
return if node.body.return_type? && !node.lambda?
|
|
49
|
+
return if node.body.type?(:return, :break) && !node.lambda?
|
|
57
50
|
|
|
58
51
|
message = format(MSG, type: node.lambda? ? 'lambda' : 'proc')
|
|
59
52
|
add_offense(node, message: message) do |corrector|
|
|
@@ -91,6 +91,9 @@ module RuboCop
|
|
|
91
91
|
end
|
|
92
92
|
|
|
93
93
|
def autocorrect(corrector, node)
|
|
94
|
+
receiver = node.method?(:!) ? node.receiver.receiver : node.receiver
|
|
95
|
+
return if receiver && correction_removes_heredoc?(node, receiver)
|
|
96
|
+
|
|
94
97
|
case node.method_name
|
|
95
98
|
when :!=
|
|
96
99
|
autocorrect_comparison(corrector, node)
|
|
@@ -101,6 +104,16 @@ module RuboCop
|
|
|
101
104
|
end
|
|
102
105
|
end
|
|
103
106
|
|
|
107
|
+
def correction_removes_heredoc?(node, receiver)
|
|
108
|
+
return false if node.single_line?
|
|
109
|
+
|
|
110
|
+
# Only the receiver is kept, so a heredoc terminator anywhere else in the
|
|
111
|
+
# node is lost, whether the heredoc was opened in the receiver or before it.
|
|
112
|
+
processed_source.tokens_within(node).any? do |token|
|
|
113
|
+
token.type == :tSTRING_END && !receiver.source_range.contains?(token.pos)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
104
117
|
def unless_and_nil_check?(send_node)
|
|
105
118
|
parent = send_node.parent
|
|
106
119
|
|
|
@@ -29,6 +29,10 @@ module RuboCop
|
|
|
29
29
|
splat kwsplat forwarded_args forwarded_restarg forwarded_kwrestarg block_pass
|
|
30
30
|
].freeze
|
|
31
31
|
|
|
32
|
+
def self.autocorrect_incompatible_with
|
|
33
|
+
[Style::TrailingCommaInArguments]
|
|
34
|
+
end
|
|
35
|
+
|
|
32
36
|
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
33
37
|
def on_send(node)
|
|
34
38
|
return unless (dot = node.loc.dot)
|
|
@@ -39,6 +43,7 @@ module RuboCop
|
|
|
39
43
|
return if invalid_syntax_argument?(rhs)
|
|
40
44
|
|
|
41
45
|
add_offense(dot) do |corrector|
|
|
46
|
+
remove_trailing_comma(corrector, node)
|
|
42
47
|
wrap_in_parentheses_if_chained(corrector, node)
|
|
43
48
|
corrector.replace(dot, ' ')
|
|
44
49
|
|
|
@@ -62,6 +67,15 @@ module RuboCop
|
|
|
62
67
|
INVALID_SYNTAX_ARG_TYPES.include?(type)
|
|
63
68
|
end
|
|
64
69
|
|
|
70
|
+
def remove_trailing_comma(corrector, node)
|
|
71
|
+
return unless node.parenthesized?
|
|
72
|
+
|
|
73
|
+
comma = processed_source.tokens_within(node).find do |token|
|
|
74
|
+
token.begin_pos >= node.last_argument.source_range.end_pos && !token.comment?
|
|
75
|
+
end
|
|
76
|
+
corrector.remove(comma.pos) if comma&.comma?
|
|
77
|
+
end
|
|
78
|
+
|
|
65
79
|
def wrap_in_parentheses_if_chained(corrector, node)
|
|
66
80
|
return unless chained?(node)
|
|
67
81
|
|
|
@@ -56,6 +56,10 @@ module RuboCop
|
|
|
56
56
|
|
|
57
57
|
RESTRICT_ON_SEND = %i[raise fail].freeze
|
|
58
58
|
|
|
59
|
+
def self.autocorrect_incompatible_with
|
|
60
|
+
[Style::TrailingCommaInArguments]
|
|
61
|
+
end
|
|
62
|
+
|
|
59
63
|
def on_send(node)
|
|
60
64
|
return unless node.command?(:raise) || node.command?(:fail)
|
|
61
65
|
|
|
@@ -171,10 +175,21 @@ module RuboCop
|
|
|
171
175
|
|
|
172
176
|
def correct_heredoc_compact_to_exploded(corrector, node)
|
|
173
177
|
new_call = node.first_argument
|
|
178
|
+
remove_trailing_comma(corrector, new_call)
|
|
174
179
|
corrector.replace(range_between(new_call.receiver, new_call.first_argument), ', ')
|
|
175
180
|
corrector.remove(new_call.loc.end) if new_call.loc.end
|
|
176
181
|
end
|
|
177
182
|
|
|
183
|
+
def remove_trailing_comma(corrector, new_call)
|
|
184
|
+
return unless new_call.loc.end
|
|
185
|
+
|
|
186
|
+
argument_end = new_call.first_argument.source_range.end
|
|
187
|
+
token = processed_source.first_token_of(argument_end)
|
|
188
|
+
return unless token.comma?
|
|
189
|
+
|
|
190
|
+
corrector.remove(argument_end.join(token.pos))
|
|
191
|
+
end
|
|
192
|
+
|
|
178
193
|
def correct_raise(node)
|
|
179
194
|
opposite_style_detected
|
|
180
195
|
return if inside_corrected_raise?(node)
|
|
@@ -29,6 +29,10 @@ module RuboCop
|
|
|
29
29
|
|
|
30
30
|
RESTRICT_ON_SEND = %i[new [] Array].freeze
|
|
31
31
|
|
|
32
|
+
def self.autocorrect_incompatible_with
|
|
33
|
+
[Style::TrailingCommaInArguments]
|
|
34
|
+
end
|
|
35
|
+
|
|
32
36
|
# @!method redundant_array_constructor(node)
|
|
33
37
|
def_node_matcher :redundant_array_constructor, <<~PATTERN
|
|
34
38
|
{
|
|
@@ -70,10 +74,20 @@ module RuboCop
|
|
|
70
74
|
replacement = replacement.source_range if replacement.is_a?(AST::Node)
|
|
71
75
|
|
|
72
76
|
add_offense(range) do |corrector|
|
|
77
|
+
next if removes_comment_or_heredoc?(node, replacement)
|
|
78
|
+
|
|
73
79
|
corrector.remove(node.source_range.begin.join(replacement.begin))
|
|
74
80
|
corrector.remove(replacement.end.join(node.source_range.end))
|
|
75
81
|
end
|
|
76
82
|
end
|
|
83
|
+
|
|
84
|
+
def removes_comment_or_heredoc?(node, literal)
|
|
85
|
+
return false if node.single_line?
|
|
86
|
+
|
|
87
|
+
processed_source.tokens_within(node).any? do |token|
|
|
88
|
+
(token.comment? || token.type == :tSTRING_END) && !literal.contains?(token.pos)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
77
91
|
end
|
|
78
92
|
end
|
|
79
93
|
end
|
|
@@ -75,7 +75,6 @@ module RuboCop
|
|
|
75
75
|
|
|
76
76
|
def self.autocorrect_incompatible_with
|
|
77
77
|
[
|
|
78
|
-
Style::BlockDelimiters,
|
|
79
78
|
Style::TrailingCommaInArguments,
|
|
80
79
|
Style::TrailingCommaInArrayLiteral,
|
|
81
80
|
Style::TrailingCommaInHashLiteral
|
|
@@ -163,7 +162,6 @@ module RuboCop
|
|
|
163
162
|
end
|
|
164
163
|
|
|
165
164
|
def end_cannot_join_statement_line?(node)
|
|
166
|
-
return false if use_modifier_form_after_multiline_begin_block?(node)
|
|
167
165
|
return false unless code_after_end_on_its_own_line?(node)
|
|
168
166
|
|
|
169
167
|
range_with_end_line_break(node).begin_pos != statement_end_pos(node)
|
|
@@ -177,14 +175,23 @@ module RuboCop
|
|
|
177
175
|
end
|
|
178
176
|
|
|
179
177
|
def code_after_end_on_its_own_line?(node)
|
|
180
|
-
|
|
181
|
-
return false unless begins_its_line?(end_range)
|
|
178
|
+
return false unless begins_its_line?(node.loc.end)
|
|
182
179
|
|
|
183
|
-
rest_of_line =
|
|
180
|
+
rest_of_line = rest_of_line(closing_range(node)).lstrip
|
|
184
181
|
|
|
185
182
|
!rest_of_line.empty? && !rest_of_line.start_with?('#')
|
|
186
183
|
end
|
|
187
184
|
|
|
185
|
+
def closing_range(node)
|
|
186
|
+
return node.loc.end unless use_modifier_form_after_multiline_begin_block?(node)
|
|
187
|
+
|
|
188
|
+
node.loc.end.join(condition_range(node.parent))
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def rest_of_line(range)
|
|
192
|
+
range.source_buffer.source_line(range.last_line)[range.last_column..]
|
|
193
|
+
end
|
|
194
|
+
|
|
188
195
|
def range_with_end_line_break(node)
|
|
189
196
|
range_with_surrounding_space(node.loc.end, side: :left, newlines: true)
|
|
190
197
|
end
|
|
@@ -270,7 +277,20 @@ module RuboCop
|
|
|
270
277
|
condition_range = condition_range(node.parent)
|
|
271
278
|
|
|
272
279
|
corrector.insert_after(node.children.first, " #{condition_range.source}")
|
|
273
|
-
corrector.remove(
|
|
280
|
+
corrector.remove(modifier_form_closing_removal_range(node))
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def modifier_form_closing_removal_range(node)
|
|
284
|
+
closing_range = closing_range(node)
|
|
285
|
+
|
|
286
|
+
if range_with_end_line_break(node).begin_pos == statement_end_pos(node)
|
|
287
|
+
range_with_surrounding_space(closing_range, side: :left, newlines: true)
|
|
288
|
+
elsif rest_of_line(closing_range).strip.empty?
|
|
289
|
+
range_by_whole_lines(closing_range, include_final_newline: true)
|
|
290
|
+
else
|
|
291
|
+
# Only a comment can follow here, and it stays on its own line.
|
|
292
|
+
range_with_surrounding_space(closing_range, side: :right, newlines: false)
|
|
293
|
+
end
|
|
274
294
|
end
|
|
275
295
|
|
|
276
296
|
def condition_range(node)
|
|
@@ -104,6 +104,9 @@ module RuboCop
|
|
|
104
104
|
end
|
|
105
105
|
|
|
106
106
|
def remove_redundant_each(corrector, range, redundant_node)
|
|
107
|
+
return unless removable_each?(range)
|
|
108
|
+
return unless (range = with_preceding_line_break(range, redundant_node))
|
|
109
|
+
|
|
107
110
|
corrector.remove(range)
|
|
108
111
|
|
|
109
112
|
if redundant_node.method?(:each_with_index)
|
|
@@ -113,6 +116,21 @@ module RuboCop
|
|
|
113
116
|
end
|
|
114
117
|
end
|
|
115
118
|
|
|
119
|
+
def removable_each?(range)
|
|
120
|
+
between = processed_source.tokens_within(range)[1...-1]
|
|
121
|
+
between.empty? || between.map(&:type) == %i[tLPAREN2 tRPAREN]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The block of an `each` that starts its line would be left without a call, so the
|
|
125
|
+
# line break goes too, unless there's a comment before it.
|
|
126
|
+
def with_preceding_line_break(range, redundant_node)
|
|
127
|
+
receiver_end = redundant_node.source_range.end
|
|
128
|
+
return range if receiver_end.end_pos >= range.begin_pos
|
|
129
|
+
|
|
130
|
+
gap = receiver_end.join(range.begin)
|
|
131
|
+
range.join(gap) if gap.source.strip.empty?
|
|
132
|
+
end
|
|
133
|
+
|
|
116
134
|
def receiver_of_dot_call?(node)
|
|
117
135
|
parent = node.parent
|
|
118
136
|
return false unless parent&.call_type?
|
|
@@ -43,7 +43,10 @@ module RuboCop
|
|
|
43
43
|
next if message.nil_type?
|
|
44
44
|
|
|
45
45
|
add_offense(node, message: MSG_1) do |corrector|
|
|
46
|
-
|
|
46
|
+
prefix = exception.source_range.join(message.source_range.begin)
|
|
47
|
+
next if comment_or_heredoc_in?(prefix)
|
|
48
|
+
|
|
49
|
+
corrector.remove(prefix)
|
|
47
50
|
corrector.replace(message, message_source(message))
|
|
48
51
|
end
|
|
49
52
|
end
|
|
@@ -58,17 +61,24 @@ module RuboCop
|
|
|
58
61
|
next if message.nil_type?
|
|
59
62
|
|
|
60
63
|
add_offense(node, message: MSG_2) do |corrector|
|
|
61
|
-
|
|
62
|
-
next if heredoc_body_inside?(new_call, message)
|
|
64
|
+
next if unsafe_to_unwrap?(new_call, message)
|
|
63
65
|
|
|
64
66
|
corrector.replace(new_call, message_source(message))
|
|
65
67
|
end
|
|
66
68
|
end
|
|
67
69
|
end
|
|
68
70
|
|
|
69
|
-
def
|
|
70
|
-
message.
|
|
71
|
-
|
|
71
|
+
def unsafe_to_unwrap?(new_call, message)
|
|
72
|
+
prefix = new_call.source_range.begin.join(message.source_range.begin)
|
|
73
|
+
suffix = message.source_range.end.join(new_call.source_range.end)
|
|
74
|
+
comment_or_heredoc_in?(prefix) || comment_or_heredoc_in?(suffix)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def comment_or_heredoc_in?(range)
|
|
78
|
+
return false if range.single_line?
|
|
79
|
+
|
|
80
|
+
processed_source.tokens_within(range).any? do |token|
|
|
81
|
+
(token.comment? || token.type == :tSTRING_END) && range.contains?(token.pos)
|
|
72
82
|
end
|
|
73
83
|
end
|
|
74
84
|
|
|
@@ -110,12 +110,21 @@ module RuboCop
|
|
|
110
110
|
|
|
111
111
|
MSG = 'Remove unnecessary `initialize` method.'
|
|
112
112
|
MSG_EMPTY = 'Remove unnecessary empty `initialize` method.'
|
|
113
|
+
VALUE_DISCARDING_BODY_TYPES = %i[
|
|
114
|
+
class module sclass block numblock itblock def defs while until for
|
|
115
|
+
].freeze
|
|
116
|
+
VALUE_PASSING_BODY_TYPES = %i[when in_pattern resbody].freeze
|
|
113
117
|
|
|
114
118
|
# @!method initialize_forwards?(node)
|
|
115
119
|
def_node_matcher :initialize_forwards?, <<~PATTERN
|
|
116
120
|
(def _ (args $arg*) $({super zsuper} ...))
|
|
117
121
|
PATTERN
|
|
118
122
|
|
|
123
|
+
# @!method private_definition?(node)
|
|
124
|
+
def_node_matcher :private_definition?, <<~PATTERN
|
|
125
|
+
(send nil? :private def)
|
|
126
|
+
PATTERN
|
|
127
|
+
|
|
119
128
|
def on_def(node)
|
|
120
129
|
return if acceptable?(node)
|
|
121
130
|
|
|
@@ -136,12 +145,28 @@ module RuboCop
|
|
|
136
145
|
|
|
137
146
|
def register_offense(node, message)
|
|
138
147
|
add_offense(node, message: message) do |corrector|
|
|
139
|
-
corrector.remove(
|
|
148
|
+
corrector.remove(removal_range(node))
|
|
140
149
|
end
|
|
141
150
|
end
|
|
142
151
|
|
|
152
|
+
def removal_range(node)
|
|
153
|
+
statement = statement_of(node)
|
|
154
|
+
return shared_line_range(statement) unless own_lines?(statement)
|
|
155
|
+
|
|
156
|
+
range = statement.source_range
|
|
157
|
+
return range_with_surrounding_space(range, side: :right) if trailing_comment(statement)
|
|
158
|
+
|
|
159
|
+
range_by_whole_lines(range, include_final_newline: true)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def statement_of(node)
|
|
163
|
+
private_definition?(node.parent) ? node.parent : node
|
|
164
|
+
end
|
|
165
|
+
|
|
143
166
|
def acceptable?(node)
|
|
144
|
-
|
|
167
|
+
return true unless node.method?(:initialize)
|
|
168
|
+
|
|
169
|
+
forwards?(node) || allow_comments?(node) || !removable?(node)
|
|
145
170
|
end
|
|
146
171
|
|
|
147
172
|
def forwards?(node)
|
|
@@ -154,6 +179,94 @@ module RuboCop
|
|
|
154
179
|
contains_comments?(node) && !comments_contain_disables?(node, name)
|
|
155
180
|
end
|
|
156
181
|
|
|
182
|
+
def removable?(node)
|
|
183
|
+
statement = statement_of(node)
|
|
184
|
+
return false unless statement?(statement)
|
|
185
|
+
|
|
186
|
+
own_lines?(statement) || !shared_line_range(statement).nil?
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def own_lines?(node)
|
|
190
|
+
code_on_lines_of(node) == node.source
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def shared_line_range(node)
|
|
194
|
+
return if heredoc_body_after?(node)
|
|
195
|
+
return unless node.parent&.type?(:begin, :kwbegin)
|
|
196
|
+
|
|
197
|
+
range = sibling_separator_range(node)
|
|
198
|
+
range unless range.nil? || foreign_heredoc_body_in?(range, node)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def sibling_separator_range(node)
|
|
202
|
+
range = node.source_range
|
|
203
|
+
if (following = node.right_sibling) && following.first_line == node.last_line
|
|
204
|
+
range.join(following.source_range.begin)
|
|
205
|
+
elsif (preceding = node.left_sibling) && preceding.last_line == node.first_line
|
|
206
|
+
preceding.source_range.end.join(range)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def heredoc_body_after?(node)
|
|
211
|
+
node.each_descendant(:any_str).any? do |str|
|
|
212
|
+
str.heredoc? && str.loc.heredoc_end.end_pos > node.source_range.end_pos
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# The body of a heredoc opened by another statement would be removed with the range.
|
|
217
|
+
def foreign_heredoc_body_in?(range, node)
|
|
218
|
+
processed_source.ast.each_node(:any_str).any? do |str|
|
|
219
|
+
next false if !str.heredoc? || node.source_range.contains?(str.source_range)
|
|
220
|
+
|
|
221
|
+
range.overlaps?(str.loc.heredoc_body)
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def code_on_lines_of(node)
|
|
226
|
+
lines = range_by_whole_lines(node.source_range)
|
|
227
|
+
comment = trailing_comment(node)
|
|
228
|
+
return lines.source.strip.delete_suffix(';').rstrip unless comment
|
|
229
|
+
|
|
230
|
+
lines.begin.join(comment.source_range.begin).source.strip
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def trailing_comment(node)
|
|
234
|
+
processed_source.comment_at_line(node.last_line)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def statement?(node)
|
|
238
|
+
parent = node.parent
|
|
239
|
+
return true if parent.nil? || discards_value_of?(parent, node)
|
|
240
|
+
|
|
241
|
+
passes_on_value_of?(parent, node) && statement?(parent)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def discards_value_of?(parent, node)
|
|
245
|
+
case parent.type
|
|
246
|
+
when :begin, :kwbegin
|
|
247
|
+
!parent.children.last.equal?(node)
|
|
248
|
+
when :ensure
|
|
249
|
+
parent.branch.equal?(node)
|
|
250
|
+
when *VALUE_DISCARDING_BODY_TYPES
|
|
251
|
+
parent.body.equal?(node)
|
|
252
|
+
else
|
|
253
|
+
false
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def passes_on_value_of?(parent, node)
|
|
258
|
+
case parent.type
|
|
259
|
+
when :begin, :kwbegin, :rescue, :ensure
|
|
260
|
+
true
|
|
261
|
+
when :if, :case, :case_match
|
|
262
|
+
!parent.condition.equal?(node)
|
|
263
|
+
when *VALUE_PASSING_BODY_TYPES
|
|
264
|
+
parent.body.equal?(node)
|
|
265
|
+
else
|
|
266
|
+
false
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
157
270
|
def same_args?(super_node, args)
|
|
158
271
|
return true if super_node.zsuper_type?
|
|
159
272
|
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
|
43
43
|
MSG = 'Prefer `to_s` over string interpolation.'
|
|
44
44
|
|
|
45
45
|
def self.autocorrect_incompatible_with
|
|
46
|
-
[Style::
|
|
46
|
+
[Style::MethodCallWithArgsParentheses]
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def on_dstr(node)
|
|
@@ -101,6 +101,7 @@ module RuboCop
|
|
|
101
101
|
return false unless node.children.one?
|
|
102
102
|
|
|
103
103
|
first_child = node.children.first
|
|
104
|
+
return false unless first_child.last_line == node.last_line
|
|
104
105
|
|
|
105
106
|
variable_interpolation?(first_child) ||
|
|
106
107
|
(first_child.send_type? && !first_child.operator_method?)
|
|
@@ -31,14 +31,14 @@ module RuboCop
|
|
|
31
31
|
minimum_target_ruby_version 3.0
|
|
32
32
|
|
|
33
33
|
def self.autocorrect_incompatible_with
|
|
34
|
-
[Layout::MultilineMethodCallBraceLayout]
|
|
34
|
+
[Layout::MultilineMethodCallBraceLayout, Style::TrailingCommaInArguments]
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
# @!method redundant_unfreeze?(node)
|
|
38
38
|
def_node_matcher :redundant_unfreeze?, <<~PATTERN
|
|
39
39
|
{
|
|
40
40
|
(send dstr_type? {:+@ :dup})
|
|
41
|
-
(send (const nil? :String) :new dstr_type?)
|
|
41
|
+
(send (const {nil? cbase} :String) :new dstr_type?)
|
|
42
42
|
}
|
|
43
43
|
PATTERN
|
|
44
44
|
|
|
@@ -33,6 +33,10 @@ module RuboCop
|
|
|
33
33
|
ESCAPED_QUOTE = /(?<!\\)(?:\\\\)*\\"/.freeze
|
|
34
34
|
UNESCAPED_QUOTE = /(?<!\\)(?:\\\\)*"/.freeze
|
|
35
35
|
|
|
36
|
+
def self.autocorrect_incompatible_with
|
|
37
|
+
[Style::RedundantStringEscape]
|
|
38
|
+
end
|
|
39
|
+
|
|
36
40
|
def on_dstr(node)
|
|
37
41
|
return unless string_literal?(node)
|
|
38
42
|
|
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
|
34
34
|
return unless (regexp = redundant_regexp_constructor(node))
|
|
35
35
|
|
|
36
36
|
add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
|
|
37
|
-
next if ambiguous_slash_regexp?(node, regexp)
|
|
37
|
+
next if ambiguous_slash_regexp?(node, regexp) || drops_heredoc_or_comment?(node, regexp)
|
|
38
38
|
|
|
39
39
|
# Reuse the inner literal's own source so its delimiters are preserved.
|
|
40
40
|
# Forcing `/.../` would break patterns like `%r{foo/bar}` that contain
|
|
@@ -57,6 +57,16 @@ module RuboCop
|
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
def drops_heredoc_or_comment?(node, regexp)
|
|
61
|
+
return false if node.single_line?
|
|
62
|
+
|
|
63
|
+
processed_source.tokens_within(node).any? do |token|
|
|
64
|
+
next false if regexp.source_range.contains?(token.pos)
|
|
65
|
+
|
|
66
|
+
token.comment? || token.type == :tSTRING_END
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
60
70
|
def command?(call)
|
|
61
71
|
call.arguments? && !call.parenthesized? && !call.operator_method? && !call.setter_method?
|
|
62
72
|
end
|