rubocop 1.50.2 → 1.52.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/config/default.yml +38 -5
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_obsoletion.rb +2 -2
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +12 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +2 -2
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -2
- data/lib/rubocop/cop/lint/inherit_exception.rb +7 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/missing_super.rb +3 -0
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/useless_assignment.rb +58 -1
- data/lib/rubocop/cop/lint/void.rb +62 -6
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/comments_help.rb +6 -2
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +22 -7
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
- data/lib/rubocop/cop/naming/variable_name.rb +6 -1
- data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/collection_compact.rb +16 -6
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +26 -6
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/copyright.rb +5 -2
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +2 -2
- data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
- data/lib/rubocop/cop/style/guard_clause.rb +2 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
- data/lib/rubocop/cop/style/hash_except.rb +19 -8
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -0
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +9 -5
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +1 -2
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +5 -1
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +11 -5
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
- data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
- data/lib/rubocop/cop/style/semicolon.rb +12 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +2 -2
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/variable_force/assignment.rb +20 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
- data/lib/rubocop/cop/variable_force.rb +1 -0
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/target_ruby.rb +3 -2
- data/lib/rubocop/version.rb +10 -6
- data/lib/rubocop.rb +5 -0
- metadata +13 -8
@@ -41,6 +41,10 @@ module RuboCop
|
|
41
41
|
# do_something(some_array)
|
42
42
|
# end
|
43
43
|
class Void < Base
|
44
|
+
extend AutoCorrector
|
45
|
+
|
46
|
+
include RangeHelp
|
47
|
+
|
44
48
|
OP_MSG = 'Operator `%<op>s` used in void context.'
|
45
49
|
VAR_MSG = 'Variable `%<var>s` used in void context.'
|
46
50
|
LIT_MSG = 'Literal `%<lit>s` used in void context.'
|
@@ -100,31 +104,43 @@ module RuboCop
|
|
100
104
|
def check_void_op(node)
|
101
105
|
return unless node.send_type? && OPERATORS.include?(node.method_name)
|
102
106
|
|
103
|
-
add_offense(node.loc.selector,
|
107
|
+
add_offense(node.loc.selector,
|
108
|
+
message: format(OP_MSG, op: node.method_name)) do |corrector|
|
109
|
+
autocorrect_void_op(corrector, node)
|
110
|
+
end
|
104
111
|
end
|
105
112
|
|
106
113
|
def check_var(node)
|
107
114
|
return unless node.variable? || node.const_type?
|
108
115
|
|
109
|
-
add_offense(node.loc.name,
|
116
|
+
add_offense(node.loc.name,
|
117
|
+
message: format(VAR_MSG, var: node.loc.name.source)) do |corrector|
|
118
|
+
autocorrect_void_var(corrector, node)
|
119
|
+
end
|
110
120
|
end
|
111
121
|
|
112
122
|
def check_literal(node)
|
113
123
|
return if !node.literal? || node.xstr_type? || node.range_type?
|
114
124
|
|
115
|
-
add_offense(node, message: format(LIT_MSG, lit: node.source))
|
125
|
+
add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
|
126
|
+
autocorrect_void_literal(corrector, node)
|
127
|
+
end
|
116
128
|
end
|
117
129
|
|
118
130
|
def check_self(node)
|
119
131
|
return unless node.self_type?
|
120
132
|
|
121
|
-
add_offense(node, message: SELF_MSG)
|
133
|
+
add_offense(node, message: SELF_MSG) do |corrector|
|
134
|
+
autocorrect_void_self(corrector, node)
|
135
|
+
end
|
122
136
|
end
|
123
137
|
|
124
138
|
def check_void_expression(node)
|
125
139
|
return unless node.defined_type? || node.lambda_or_proc?
|
126
140
|
|
127
|
-
add_offense(node, message: format(EXPRESSION_MSG, expression: node.source))
|
141
|
+
add_offense(node, message: format(EXPRESSION_MSG, expression: node.source)) do |corrector|
|
142
|
+
autocorrect_void_expression(corrector, node)
|
143
|
+
end
|
128
144
|
end
|
129
145
|
|
130
146
|
def check_nonmutating(node)
|
@@ -139,7 +155,10 @@ module RuboCop
|
|
139
155
|
"#{method_name}!"
|
140
156
|
end
|
141
157
|
add_offense(node,
|
142
|
-
message: format(NONMUTATING_MSG, method: method_name,
|
158
|
+
message: format(NONMUTATING_MSG, method: method_name,
|
159
|
+
suggest: suggestion)) do |corrector|
|
160
|
+
autocorrect_nonmutating_send(corrector, node, suggestion)
|
161
|
+
end
|
143
162
|
end
|
144
163
|
|
145
164
|
def in_void_context?(node)
|
@@ -149,6 +168,43 @@ module RuboCop
|
|
149
168
|
|
150
169
|
VOID_CONTEXT_TYPES.include?(parent.type) && parent.void_context?
|
151
170
|
end
|
171
|
+
|
172
|
+
def autocorrect_void_op(corrector, node)
|
173
|
+
if node.arguments.empty?
|
174
|
+
corrector.replace(node, node.receiver.source)
|
175
|
+
else
|
176
|
+
corrector.replace(
|
177
|
+
range_with_surrounding_space(range: node.loc.selector, side: :both,
|
178
|
+
newlines: false),
|
179
|
+
"\n"
|
180
|
+
)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def autocorrect_void_var(corrector, node)
|
185
|
+
corrector.remove(range_with_surrounding_space(range: node.loc.name, side: :left))
|
186
|
+
end
|
187
|
+
|
188
|
+
def autocorrect_void_literal(corrector, node)
|
189
|
+
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
|
190
|
+
end
|
191
|
+
|
192
|
+
def autocorrect_void_self(corrector, node)
|
193
|
+
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
|
194
|
+
end
|
195
|
+
|
196
|
+
def autocorrect_void_expression(corrector, node)
|
197
|
+
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
|
198
|
+
end
|
199
|
+
|
200
|
+
def autocorrect_nonmutating_send(corrector, node, suggestion)
|
201
|
+
send_node = if node.send_type?
|
202
|
+
node
|
203
|
+
else
|
204
|
+
node.send_node
|
205
|
+
end
|
206
|
+
corrector.replace(send_node.loc.selector, suggestion)
|
207
|
+
end
|
152
208
|
end
|
153
209
|
end
|
154
210
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# This module encapsulates the ability to allow certain receivers in a cop.
|
6
|
+
module AllowedReceivers
|
7
|
+
def allowed_receiver?(receiver)
|
8
|
+
receiver_name = receiver_name(receiver)
|
9
|
+
|
10
|
+
allowed_receivers.include?(receiver_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def receiver_name(receiver)
|
14
|
+
if receiver.receiver && !receiver.receiver.const_type?
|
15
|
+
return receiver_name(receiver.receiver)
|
16
|
+
end
|
17
|
+
|
18
|
+
if receiver.send_type?
|
19
|
+
if receiver.receiver
|
20
|
+
"#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
|
21
|
+
else
|
22
|
+
receiver.method_name.to_s
|
23
|
+
end
|
24
|
+
else
|
25
|
+
receiver.source
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def allowed_receivers
|
30
|
+
cop_config.fetch('AllowedReceivers', [])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -62,12 +62,16 @@ module RuboCop
|
|
62
62
|
# Returns the end line of a node, which might be a comment and not part of the AST
|
63
63
|
# End line is considered either the line at which another node starts, or
|
64
64
|
# the line at which the parent node ends.
|
65
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
65
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Lint/DuplicateBranch
|
66
66
|
def find_end_line(node)
|
67
67
|
if node.if_type? && node.else?
|
68
68
|
node.loc.else.line
|
69
69
|
elsif node.if_type? && node.ternary?
|
70
70
|
node.else_branch.loc.line
|
71
|
+
elsif node.if_type? && node.elsif?
|
72
|
+
node.each_ancestor(:if).find(&:if?).loc.end.line
|
73
|
+
elsif node.block_type? || node.numblock_type?
|
74
|
+
node.loc.end.line
|
71
75
|
elsif (next_sibling = node.right_sibling) && next_sibling.is_a?(AST::Node)
|
72
76
|
next_sibling.loc.line
|
73
77
|
elsif (parent = node.parent)
|
@@ -76,7 +80,7 @@ module RuboCop
|
|
76
80
|
node.loc.end.line
|
77
81
|
end
|
78
82
|
end
|
79
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
83
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Lint/DuplicateBranch
|
80
84
|
end
|
81
85
|
end
|
82
86
|
end
|
@@ -17,7 +17,8 @@ module RuboCop
|
|
17
17
|
# @safety
|
18
18
|
# This cop relies on the pattern `@instance_var ||= ...`,
|
19
19
|
# but this is sometimes used for other purposes than memoization
|
20
|
-
# so this cop is considered unsafe.
|
20
|
+
# so this cop is considered unsafe. Also, its autocorrection is unsafe
|
21
|
+
# because it may conflict with instance variable names already in use.
|
21
22
|
#
|
22
23
|
# @example EnforcedStyleForLeadingUnderscores: disallowed (default)
|
23
24
|
# # bad
|
@@ -145,6 +146,8 @@ module RuboCop
|
|
145
146
|
# @_foo ||= calculate_expensive_thing
|
146
147
|
# end
|
147
148
|
class MemoizedInstanceVariableName < Base
|
149
|
+
extend AutoCorrector
|
150
|
+
|
148
151
|
include ConfigurableEnforcedStyle
|
149
152
|
|
150
153
|
MSG = 'Memoized variable `%<var>s` does not match ' \
|
@@ -163,6 +166,7 @@ module RuboCop
|
|
163
166
|
PATTERN
|
164
167
|
|
165
168
|
# rubocop:disable Metrics/AbcSize
|
169
|
+
# rubocop:disable Metrics/MethodLength
|
166
170
|
def on_or_asgn(node)
|
167
171
|
lhs, _value = *node
|
168
172
|
return unless lhs.ivasgn_type?
|
@@ -175,14 +179,18 @@ module RuboCop
|
|
175
179
|
|
176
180
|
return if matches?(method_name, lhs)
|
177
181
|
|
182
|
+
suggested_var = suggested_var(method_name)
|
178
183
|
msg = format(
|
179
184
|
message(lhs.children.first.to_s),
|
180
185
|
var: lhs.children.first.to_s,
|
181
|
-
suggested_var: suggested_var
|
186
|
+
suggested_var: suggested_var,
|
182
187
|
method: method_name
|
183
188
|
)
|
184
|
-
add_offense(lhs, message: msg)
|
189
|
+
add_offense(lhs, message: msg) do |corrector|
|
190
|
+
corrector.replace(lhs.loc.name, "@#{suggested_var}")
|
191
|
+
end
|
185
192
|
end
|
193
|
+
# rubocop:enable Metrics/MethodLength
|
186
194
|
# rubocop:enable Metrics/AbcSize
|
187
195
|
|
188
196
|
# @!method defined_memoized?(node, ivar)
|
@@ -205,15 +213,22 @@ module RuboCop
|
|
205
213
|
defined_memoized?(method_node.body, var_name) do |defined_ivar, return_ivar, ivar_assign|
|
206
214
|
return if matches?(method_name, ivar_assign)
|
207
215
|
|
216
|
+
suggested_var = suggested_var(method_name)
|
208
217
|
msg = format(
|
209
218
|
message(var_name.to_s),
|
210
219
|
var: var_name.to_s,
|
211
|
-
suggested_var: suggested_var
|
220
|
+
suggested_var: suggested_var,
|
212
221
|
method: method_name
|
213
222
|
)
|
214
|
-
add_offense(defined_ivar, message: msg)
|
215
|
-
|
216
|
-
|
223
|
+
add_offense(defined_ivar, message: msg) do |corrector|
|
224
|
+
corrector.replace(defined_ivar, "@#{suggested_var}")
|
225
|
+
end
|
226
|
+
add_offense(return_ivar, message: msg) do |corrector|
|
227
|
+
corrector.replace(return_ivar, "@#{suggested_var}")
|
228
|
+
end
|
229
|
+
add_offense(ivar_assign.loc.name, message: msg) do |corrector|
|
230
|
+
corrector.replace(ivar_assign.loc.name, "@#{suggested_var}")
|
231
|
+
end
|
217
232
|
end
|
218
233
|
end
|
219
234
|
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
@@ -82,9 +82,7 @@ module RuboCop
|
|
82
82
|
message = message(node)
|
83
83
|
|
84
84
|
add_offense(range, message: message) do |corrector|
|
85
|
-
corrector
|
86
|
-
|
87
|
-
correct_node(corrector, node.body, offending_name, preferred_name)
|
85
|
+
autocorrect(corrector, node, range, offending_name, preferred_name)
|
88
86
|
end
|
89
87
|
end
|
90
88
|
|
@@ -95,6 +93,16 @@ module RuboCop
|
|
95
93
|
variable.source_range
|
96
94
|
end
|
97
95
|
|
96
|
+
def autocorrect(corrector, node, range, offending_name, preferred_name)
|
97
|
+
corrector.replace(range, preferred_name)
|
98
|
+
correct_node(corrector, node.body, offending_name, preferred_name)
|
99
|
+
return unless (kwbegin_node = node.parent.each_ancestor(:kwbegin).first)
|
100
|
+
|
101
|
+
kwbegin_node.right_siblings.each do |child_node|
|
102
|
+
correct_node(corrector, child_node, offending_name, preferred_name)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
98
106
|
def variable_name_matches?(node, name)
|
99
107
|
if node.masgn_type?
|
100
108
|
node.each_descendant(:lvasgn).any? do |lvasgn_node|
|
@@ -20,9 +20,14 @@ module RuboCop
|
|
20
20
|
# # good
|
21
21
|
# fooBar = 1
|
22
22
|
#
|
23
|
+
# @example AllowedIdentifiers: ['fooBar']
|
24
|
+
# # good (with EnforcedStyle: snake_case)
|
25
|
+
# fooBar = 1
|
26
|
+
#
|
23
27
|
# @example AllowedPatterns: ['_v\d+\z']
|
24
|
-
# # good
|
28
|
+
# # good (with EnforcedStyle: camelCase)
|
25
29
|
# :release_v1
|
30
|
+
#
|
26
31
|
class VariableName < Base
|
27
32
|
include AllowedIdentifiers
|
28
33
|
include ConfigurableNaming
|
@@ -92,6 +92,7 @@ module RuboCop
|
|
92
92
|
comment_line?(processed_source[node.first_line - 2])
|
93
93
|
end
|
94
94
|
|
95
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
95
96
|
def groupable_accessor?(node)
|
96
97
|
return true unless (previous_expression = node.left_siblings.last)
|
97
98
|
|
@@ -104,8 +105,11 @@ module RuboCop
|
|
104
105
|
|
105
106
|
return true unless previous_expression.send_type?
|
106
107
|
|
107
|
-
previous_expression.attribute_accessor? ||
|
108
|
+
previous_expression.attribute_accessor? ||
|
109
|
+
previous_expression.access_modifier? ||
|
110
|
+
node.first_line - previous_expression.last_line > 1 # there is a space between nodes
|
108
111
|
end
|
112
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
109
113
|
|
110
114
|
def class_send_elements(class_node)
|
111
115
|
class_def = class_node.body
|
@@ -24,7 +24,7 @@ module RuboCop
|
|
24
24
|
def on_send(node)
|
25
25
|
return unless node.command?(:attr) && node.arguments?
|
26
26
|
# check only for method definitions in class/module body
|
27
|
-
return if
|
27
|
+
return if allowed_context?(node)
|
28
28
|
|
29
29
|
message = message(node)
|
30
30
|
add_offense(node.loc.selector, message: message) do |corrector|
|
@@ -34,6 +34,16 @@ module RuboCop
|
|
34
34
|
|
35
35
|
private
|
36
36
|
|
37
|
+
def allowed_context?(node)
|
38
|
+
return false unless (class_node = node.each_ancestor(:class, :block).first)
|
39
|
+
|
40
|
+
(!class_node.class_type? && !class_eval?(class_node)) || define_attr_method?(class_node)
|
41
|
+
end
|
42
|
+
|
43
|
+
def define_attr_method?(node)
|
44
|
+
node.each_descendant(:def).any? { |def_node| def_node.method?(:attr) }
|
45
|
+
end
|
46
|
+
|
37
47
|
def autocorrect(corrector, node)
|
38
48
|
attr_name, setter = *node.arguments
|
39
49
|
|
@@ -9,8 +9,8 @@ module RuboCop
|
|
9
9
|
# @safety
|
10
10
|
# It is unsafe by default because false positives may occur in the
|
11
11
|
# `nil` check of block arguments to the receiver object. Additionally,
|
12
|
-
#
|
13
|
-
#
|
12
|
+
# we can't know the type of the receiver object for sure, which may
|
13
|
+
# result in false positives as well.
|
14
14
|
#
|
15
15
|
# For example, `[[1, 2], [3, nil]].reject { |first, second| second.nil? }`
|
16
16
|
# and `[[1, 2], [3, nil]].compact` are not compatible. This will work fine
|
@@ -19,7 +19,9 @@ module RuboCop
|
|
19
19
|
# @example
|
20
20
|
# # bad
|
21
21
|
# array.reject(&:nil?)
|
22
|
+
# array.delete_if(&:nil?)
|
22
23
|
# array.reject { |e| e.nil? }
|
24
|
+
# array.delete_if { |e| e.nil? }
|
23
25
|
# array.select { |e| !e.nil? }
|
24
26
|
#
|
25
27
|
# # good
|
@@ -33,20 +35,25 @@ module RuboCop
|
|
33
35
|
# # good
|
34
36
|
# hash.compact!
|
35
37
|
#
|
38
|
+
# @example AllowedReceivers: ['params']
|
39
|
+
# # good
|
40
|
+
# params.reject(&:nil?)
|
41
|
+
#
|
36
42
|
class CollectionCompact < Base
|
43
|
+
include AllowedReceivers
|
37
44
|
include RangeHelp
|
38
45
|
extend AutoCorrector
|
39
46
|
extend TargetRubyVersion
|
40
47
|
|
41
48
|
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
42
|
-
RESTRICT_ON_SEND = %i[reject reject! select select!].freeze
|
49
|
+
RESTRICT_ON_SEND = %i[reject delete_if reject! select select!].freeze
|
43
50
|
TO_ENUM_METHODS = %i[to_enum lazy].freeze
|
44
51
|
|
45
52
|
minimum_target_ruby_version 2.4
|
46
53
|
|
47
54
|
# @!method reject_method_with_block_pass?(node)
|
48
55
|
def_node_matcher :reject_method_with_block_pass?, <<~PATTERN
|
49
|
-
(send !nil? {:reject :reject!}
|
56
|
+
(send !nil? {:reject :delete_if :reject!}
|
50
57
|
(block_pass
|
51
58
|
(sym :nil?)))
|
52
59
|
PATTERN
|
@@ -55,7 +62,7 @@ module RuboCop
|
|
55
62
|
def_node_matcher :reject_method?, <<~PATTERN
|
56
63
|
(block
|
57
64
|
(send
|
58
|
-
!nil? {:reject :reject!})
|
65
|
+
!nil? {:reject :delete_if :reject!})
|
59
66
|
$(args ...)
|
60
67
|
(send
|
61
68
|
$(lvar _) :nil?))
|
@@ -74,7 +81,10 @@ module RuboCop
|
|
74
81
|
|
75
82
|
def on_send(node)
|
76
83
|
return unless (range = offense_range(node))
|
77
|
-
return if
|
84
|
+
return if allowed_receiver?(node.receiver)
|
85
|
+
if (target_ruby_version <= 3.0 || node.method?(:delete_if)) && to_enum_method?(node)
|
86
|
+
return
|
87
|
+
end
|
78
88
|
|
79
89
|
good = good_method_name(node)
|
80
90
|
message = format(MSG, good: good, bad: range.source)
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for methods invoked via the
|
7
|
-
# of the
|
6
|
+
# Checks for methods invoked via the `::` operator instead
|
7
|
+
# of the `.` operator (like `FileUtils::rmdir` instead of `FileUtils.rmdir`).
|
8
8
|
#
|
9
9
|
# @example
|
10
10
|
# # bad
|
@@ -57,38 +57,58 @@ module RuboCop
|
|
57
57
|
# end
|
58
58
|
#
|
59
59
|
class CombinableLoops < Base
|
60
|
+
extend AutoCorrector
|
61
|
+
|
62
|
+
include RangeHelp
|
63
|
+
|
60
64
|
MSG = 'Combine this loop with the previous loop.'
|
61
65
|
|
62
66
|
def on_block(node)
|
63
67
|
return unless node.parent&.begin_type?
|
64
68
|
return unless collection_looping_method?(node)
|
69
|
+
return unless same_collection_looping_block?(node, node.left_sibling)
|
65
70
|
|
66
|
-
add_offense(node)
|
71
|
+
add_offense(node) do |corrector|
|
72
|
+
combine_with_left_sibling(corrector, node)
|
73
|
+
end
|
67
74
|
end
|
68
75
|
|
69
76
|
alias on_numblock on_block
|
70
77
|
|
71
78
|
def on_for(node)
|
72
79
|
return unless node.parent&.begin_type?
|
80
|
+
return unless same_collection_looping_for?(node, node.left_sibling)
|
73
81
|
|
74
|
-
|
75
|
-
|
82
|
+
add_offense(node) do |corrector|
|
83
|
+
combine_with_left_sibling(corrector, node)
|
84
|
+
end
|
76
85
|
end
|
77
86
|
|
78
87
|
private
|
79
88
|
|
80
89
|
def collection_looping_method?(node)
|
81
|
-
|
82
|
-
method_name = node.method_name.to_s
|
90
|
+
method_name = node.method_name
|
83
91
|
method_name.start_with?('each') || method_name.end_with?('_each')
|
84
92
|
end
|
85
93
|
|
86
|
-
def
|
94
|
+
def same_collection_looping_block?(node, sibling)
|
87
95
|
(sibling&.block_type? || sibling&.numblock_type?) &&
|
88
96
|
sibling.send_node.method?(node.method_name) &&
|
89
97
|
sibling.receiver == node.receiver &&
|
90
98
|
sibling.send_node.arguments == node.send_node.arguments
|
91
99
|
end
|
100
|
+
|
101
|
+
def same_collection_looping_for?(node, sibling)
|
102
|
+
sibling&.for_type? && node.collection == sibling.collection
|
103
|
+
end
|
104
|
+
|
105
|
+
def combine_with_left_sibling(corrector, node)
|
106
|
+
corrector.replace(
|
107
|
+
node.left_sibling.body,
|
108
|
+
"#{node.left_sibling.body.source}\n#{node.body.source}"
|
109
|
+
)
|
110
|
+
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
|
111
|
+
end
|
92
112
|
end
|
93
113
|
end
|
94
114
|
end
|
@@ -350,7 +350,7 @@ module RuboCop
|
|
350
350
|
end
|
351
351
|
|
352
352
|
def ternary_condition?(node)
|
353
|
-
[node, node.children.first].any? { |n| n.if_type? && n.ternary? }
|
353
|
+
[node, node.children.first].compact.any? { |n| n.if_type? && n.ternary? }
|
354
354
|
end
|
355
355
|
|
356
356
|
def lhs_all_match?(branches)
|
@@ -386,7 +386,7 @@ module RuboCop
|
|
386
386
|
def allowed_statements?(branches)
|
387
387
|
return false unless branches.all?
|
388
388
|
|
389
|
-
statements = branches.
|
389
|
+
statements = branches.filter_map { |branch| tail(branch) }
|
390
390
|
|
391
391
|
lhs_all_match?(statements) && statements.none?(&:masgn_type?) &&
|
392
392
|
assignment_types_match?(*statements)
|
@@ -8,8 +8,11 @@ module RuboCop
|
|
8
8
|
# The default regexp for an acceptable copyright notice can be found in
|
9
9
|
# config/default.yml. The default can be changed as follows:
|
10
10
|
#
|
11
|
-
#
|
12
|
-
#
|
11
|
+
# [source,yaml]
|
12
|
+
# ----
|
13
|
+
# Style/Copyright:
|
14
|
+
# Notice: '^Copyright (\(c\) )?2\d{3} Acme Inc'
|
15
|
+
# ----
|
13
16
|
#
|
14
17
|
# This regex string is treated as an unanchored regex. For each file
|
15
18
|
# that RuboCop scans, a comment that matches this regex must be found or
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
# declarations.
|
11
11
|
#
|
12
12
|
# The documentation requirement is annulled if the class or module has
|
13
|
-
# a
|
13
|
+
# a `#:nodoc:` comment next to it. Likewise, `#:nodoc: all` does the
|
14
14
|
# same for all its children.
|
15
15
|
#
|
16
16
|
# @example
|
@@ -57,7 +57,7 @@ module RuboCop
|
|
57
57
|
extend AutoCorrector
|
58
58
|
|
59
59
|
MSG = 'Pass `__FILE__` and `__LINE__` to `%<method_name>s`.'
|
60
|
-
MSG_EVAL = 'Pass a binding, `__FILE__
|
60
|
+
MSG_EVAL = 'Pass a binding, `__FILE__`, and `__LINE__` to `eval`.'
|
61
61
|
MSG_INCORRECT_FILE = 'Incorrect file for `%<method_name>s`; ' \
|
62
62
|
'use `%<expected>s` instead of `%<actual>s`.'
|
63
63
|
MSG_INCORRECT_LINE = 'Incorrect line number for `%<method_name>s`; ' \
|
@@ -210,7 +210,7 @@ module RuboCop
|
|
210
210
|
def add_offense_for_missing_line(node, code)
|
211
211
|
register_offense(node) do |corrector|
|
212
212
|
line_str = missing_line(node, code)
|
213
|
-
corrector.insert_after(node.source_range.end, ", #{line_str}")
|
213
|
+
corrector.insert_after(node.last_argument.source_range.end, ", #{line_str}")
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for exact regexp match inside Regexp literals.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# string =~ /\Astring\z/
|
12
|
+
# string === /\Astring\z/
|
13
|
+
# string.match(/\Astring\z/)
|
14
|
+
# string.match?(/\Astring\z/)
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# string == 'string'
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# string !~ /\Astring\z/
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# string != 'string'
|
24
|
+
#
|
25
|
+
class ExactRegexpMatch < Base
|
26
|
+
extend AutoCorrector
|
27
|
+
|
28
|
+
MSG = 'Use `%<prefer>s`.'
|
29
|
+
RESTRICT_ON_SEND = %i[=~ === !~ match match?].freeze
|
30
|
+
|
31
|
+
# @!method exact_regexp_match(node)
|
32
|
+
def_node_matcher :exact_regexp_match, <<~PATTERN
|
33
|
+
(send
|
34
|
+
_ {:=~ :=== :!~ :match :match?}
|
35
|
+
(regexp
|
36
|
+
(str $_)
|
37
|
+
(regopt)))
|
38
|
+
PATTERN
|
39
|
+
|
40
|
+
def on_send(node)
|
41
|
+
return unless (regexp = exact_regexp_match(node))
|
42
|
+
|
43
|
+
parsed_regexp = Regexp::Parser.parse(regexp)
|
44
|
+
return unless exact_match_pattern?(parsed_regexp)
|
45
|
+
|
46
|
+
prefer = "#{node.receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"
|
47
|
+
|
48
|
+
add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
|
49
|
+
corrector.replace(node, prefer)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def exact_match_pattern?(parsed_regexp)
|
56
|
+
tokens = parsed_regexp.map(&:token)
|
57
|
+
return false unless tokens[0] == :bos && tokens[1] == :literal && tokens[2] == :eos
|
58
|
+
|
59
|
+
!parsed_regexp[1].quantifier
|
60
|
+
end
|
61
|
+
|
62
|
+
def new_method(node)
|
63
|
+
node.method?(:!~) ? '!=' : '=='
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -187,6 +187,8 @@ module RuboCop
|
|
187
187
|
if_branch = node.if_branch
|
188
188
|
else_branch = node.else_branch
|
189
189
|
|
190
|
+
corrector.replace(node.loc.begin, "\n") if node.loc.begin&.is?('then')
|
191
|
+
|
190
192
|
if if_branch&.send_type? && heredoc?(if_branch.last_argument)
|
191
193
|
autocorrect_heredoc_argument(corrector, node, if_branch, else_branch, guard)
|
192
194
|
elsif else_branch&.send_type? && heredoc?(else_branch.last_argument)
|