rubocop 1.34.1 → 1.36.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 +1 -1
- data/config/default.yml +33 -0
- data/lib/rubocop/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +0 -0
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +32 -2
- data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
- data/lib/rubocop/cop/generator/require_file_injector.rb +2 -2
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +16 -12
- data/lib/rubocop/cop/layout/block_end_newline.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
- data/lib/rubocop/cop/layout/end_of_line.rb +4 -4
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +5 -1
- data/lib/rubocop/cop/layout/line_length.rb +4 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +25 -9
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +31 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +4 -0
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +6 -6
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +9 -3
- data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
- data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
- data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +15 -2
- data/lib/rubocop/cop/lint/unreachable_loop.rb +8 -2
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +8 -6
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +2 -0
- data/lib/rubocop/cop/mixin/allowed_methods.rb +10 -5
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +13 -5
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +76 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -6
- data/lib/rubocop/cop/mixin/method_complexity.rb +4 -4
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +2 -3
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +2 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +77 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/case_equality.rb +40 -10
- data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +41 -6
- data/lib/rubocop/cop/style/each_with_object.rb +39 -8
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/guard_clause.rb +27 -16
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
- data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
- data/lib/rubocop/cop/style/next.rb +3 -5
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/object_then.rb +2 -0
- data/lib/rubocop/cop/style/perl_backrefs.rb +22 -1
- data/lib/rubocop/cop/style/proc.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
- data/lib/rubocop/cop/style/redundant_self.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
- data/lib/rubocop/cop/style/safe_navigation.rb +4 -2
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +0 -2
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +5 -4
- data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/feature_loader.rb +6 -2
- data/lib/rubocop/formatter/html_formatter.rb +2 -2
- data/lib/rubocop/runner.rb +4 -0
- data/lib/rubocop/server/cache.rb +11 -8
- data/lib/rubocop/version.rb +3 -2
- data/lib/rubocop.rb +3 -2
- metadata +9 -7
@@ -14,7 +14,7 @@ module RuboCop
|
|
14
14
|
{(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
|
15
15
|
PATTERN
|
16
16
|
|
17
|
-
def on_block(node)
|
17
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
18
18
|
on_bad_each_with_object(node) do |*match|
|
19
19
|
handle_possible_offense(node, match, 'each_with_object')
|
20
20
|
end
|
@@ -68,6 +68,8 @@ module RuboCop
|
|
68
68
|
# `transform_values` if value transformation uses key.
|
69
69
|
return if captures.transformation_uses_both_args?
|
70
70
|
|
71
|
+
return unless captures.use_transformed_argname?
|
72
|
+
|
71
73
|
message = "Prefer `#{new_method_name}` over `#{match_desc}`."
|
72
74
|
add_offense(node, message: message) do |corrector|
|
73
75
|
correction = prepare_correction(node)
|
@@ -113,11 +115,7 @@ module RuboCop
|
|
113
115
|
end
|
114
116
|
|
115
117
|
# Internal helper class to hold match data
|
116
|
-
Captures = Struct.new(
|
117
|
-
:transformed_argname,
|
118
|
-
:transforming_body_expr,
|
119
|
-
:unchanged_body_expr
|
120
|
-
) do
|
118
|
+
Captures = Struct.new(:transformed_argname, :transforming_body_expr, :unchanged_body_expr) do
|
121
119
|
def noop_transformation?
|
122
120
|
transforming_body_expr.lvar_type? &&
|
123
121
|
transforming_body_expr.children == [transformed_argname]
|
@@ -126,6 +124,12 @@ module RuboCop
|
|
126
124
|
def transformation_uses_both_args?
|
127
125
|
transforming_body_expr.descendants.include?(unchanged_body_expr)
|
128
126
|
end
|
127
|
+
|
128
|
+
def use_transformed_argname?
|
129
|
+
transforming_body_expr.each_descendant(:lvar).any? do |node|
|
130
|
+
node.source == transformed_argname.to_s
|
131
|
+
end
|
132
|
+
end
|
129
133
|
end
|
130
134
|
|
131
135
|
# Internal helper class to hold autocorrect data
|
@@ -29,14 +29,14 @@ module RuboCop
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
alias on_numblock on_block
|
33
|
+
|
32
34
|
private
|
33
35
|
|
34
36
|
# @!method define_method?(node)
|
35
37
|
def_node_matcher :define_method?, <<~PATTERN
|
36
|
-
(block
|
37
|
-
(send nil? :define_method ({sym str} $_))
|
38
|
-
args
|
39
|
-
_)
|
38
|
+
({block numblock}
|
39
|
+
(send nil? :define_method ({sym str} $_)) _ _)
|
40
40
|
PATTERN
|
41
41
|
|
42
42
|
def check_complexity(node, method_name)
|
@@ -57,7 +57,7 @@ module RuboCop
|
|
57
57
|
end
|
58
58
|
|
59
59
|
if left_parenthesis && style == :special_inside_parentheses
|
60
|
-
return [left_parenthesis.column + 1, :
|
60
|
+
return [left_parenthesis.column + 1, :first_column_after_left_parenthesis]
|
61
61
|
end
|
62
62
|
|
63
63
|
[left_brace.source_line =~ /\S/, :start_of_line]
|
@@ -70,9 +70,8 @@ module RuboCop
|
|
70
70
|
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
71
71
|
end
|
72
72
|
|
73
|
-
def range_by_whole_lines(range, include_final_newline: false
|
74
|
-
|
75
|
-
|
73
|
+
def range_by_whole_lines(range, include_final_newline: false,
|
74
|
+
buffer: @processed_source.buffer)
|
76
75
|
last_line = buffer.source_line(range.last_line)
|
77
76
|
end_offset = last_line.length - range.last_column
|
78
77
|
end_offset += 1 if include_final_newline
|
@@ -72,10 +72,10 @@ module RuboCop
|
|
72
72
|
PATTERN
|
73
73
|
|
74
74
|
def allowed_conditional_expression_on_rhs?(node)
|
75
|
-
node&.if_type? &&
|
75
|
+
node&.if_type? && contains_constant?(node)
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
78
|
+
def contains_constant?(node)
|
79
79
|
node.branches.any?(&:const_type?)
|
80
80
|
end
|
81
81
|
end
|
@@ -9,6 +9,11 @@ module RuboCop
|
|
9
9
|
# Applications of visibility methods to symbols can be controlled
|
10
10
|
# using AllowModifiersOnSymbols config.
|
11
11
|
#
|
12
|
+
# @safety
|
13
|
+
# Autocorrection is not safe, because the visibility of dynamically
|
14
|
+
# defined methods can vary depending on the state determined by
|
15
|
+
# the group access modifier.
|
16
|
+
#
|
12
17
|
# @example EnforcedStyle: group (default)
|
13
18
|
# # bad
|
14
19
|
# class Foo
|
@@ -63,7 +68,10 @@ module RuboCop
|
|
63
68
|
#
|
64
69
|
# end
|
65
70
|
class AccessModifierDeclarations < Base
|
71
|
+
extend AutoCorrector
|
72
|
+
|
66
73
|
include ConfigurableEnforcedStyle
|
74
|
+
include RangeHelp
|
67
75
|
|
68
76
|
GROUP_STYLE_MESSAGE = [
|
69
77
|
'`%<access_modifier>s` should not be',
|
@@ -88,7 +96,10 @@ module RuboCop
|
|
88
96
|
return if allow_modifiers_on_symbols?(node)
|
89
97
|
|
90
98
|
if offense?(node)
|
91
|
-
add_offense(node.loc.selector)
|
99
|
+
add_offense(node.loc.selector) do |corrector|
|
100
|
+
autocorrect(corrector, node)
|
101
|
+
end
|
102
|
+
opposite_style_detected
|
92
103
|
else
|
93
104
|
correct_style_detected
|
94
105
|
end
|
@@ -96,6 +107,23 @@ module RuboCop
|
|
96
107
|
|
97
108
|
private
|
98
109
|
|
110
|
+
def autocorrect(corrector, node)
|
111
|
+
case style
|
112
|
+
when :group
|
113
|
+
def_node = find_corresponding_def_node(node)
|
114
|
+
return unless def_node
|
115
|
+
|
116
|
+
remove_node(corrector, def_node)
|
117
|
+
remove_node(corrector, node)
|
118
|
+
insert_def(corrector, node, def_node.source)
|
119
|
+
when :inline
|
120
|
+
remove_node(corrector, node)
|
121
|
+
select_grouped_def_nodes(node).each do |grouped_def_node|
|
122
|
+
insert_inline_modifier(corrector, grouped_def_node, node.method_name)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
99
127
|
def allow_modifiers_on_symbols?(node)
|
100
128
|
cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node)
|
101
129
|
end
|
@@ -130,6 +158,54 @@ module RuboCop
|
|
130
158
|
format(INLINE_STYLE_MESSAGE, access_modifier: access_modifier)
|
131
159
|
end
|
132
160
|
end
|
161
|
+
|
162
|
+
def find_corresponding_def_node(node)
|
163
|
+
if access_modifier_with_symbol?(node)
|
164
|
+
method_name = node.arguments.first.value
|
165
|
+
node.parent.each_child_node(:def).find do |child|
|
166
|
+
child.method?(method_name)
|
167
|
+
end
|
168
|
+
else
|
169
|
+
node.arguments.first
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def find_argument_less_modifier_node(node)
|
174
|
+
node.parent.each_child_node(:send).find do |child|
|
175
|
+
child.method?(node.method_name) && child.arguments.empty?
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def select_grouped_def_nodes(node)
|
180
|
+
node.right_siblings.take_while do |sibling|
|
181
|
+
!(sibling.send_type? && sibling.bare_access_modifier_declaration?)
|
182
|
+
end.select(&:def_type?)
|
183
|
+
end
|
184
|
+
|
185
|
+
def insert_def(corrector, node, source)
|
186
|
+
argument_less_modifier_node = find_argument_less_modifier_node(node)
|
187
|
+
if argument_less_modifier_node
|
188
|
+
corrector.insert_after(argument_less_modifier_node, "\n\n#{source}")
|
189
|
+
else
|
190
|
+
corrector.insert_before(
|
191
|
+
node.each_ancestor(:block, :class, :module).first.location.end,
|
192
|
+
"#{node.method_name}\n\n#{source}\n"
|
193
|
+
)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def insert_inline_modifier(corrector, node, modifier_name)
|
198
|
+
corrector.insert_before(node, "#{modifier_name} ")
|
199
|
+
end
|
200
|
+
|
201
|
+
def remove_node(corrector, node)
|
202
|
+
corrector.remove(
|
203
|
+
range_by_whole_lines(
|
204
|
+
node.location.expression,
|
205
|
+
include_final_newline: true
|
206
|
+
)
|
207
|
+
)
|
208
|
+
end
|
133
209
|
end
|
134
210
|
end
|
135
211
|
end
|
@@ -73,11 +73,11 @@ module RuboCop
|
|
73
73
|
{
|
74
74
|
(send _ _
|
75
75
|
(splat (lvar %1))
|
76
|
-
(block-pass (lvar %2)))
|
76
|
+
(block-pass {(lvar %2) nil?}))
|
77
77
|
(send _ _
|
78
78
|
(splat (lvar %1))
|
79
79
|
(hash (kwsplat (lvar %3)))
|
80
|
-
(block-pass (lvar %2)))
|
80
|
+
(block-pass {(lvar %2) nil?}))
|
81
81
|
}
|
82
82
|
PATTERN
|
83
83
|
|
@@ -7,6 +7,9 @@ module RuboCop
|
|
7
7
|
#
|
8
8
|
# If `AllowOnConstant` option is enabled, the cop will ignore violations when the receiver of
|
9
9
|
# the case equality operator is a constant.
|
10
|
+
|
11
|
+
# If `AllowOnSelfClass` option is enabled, the cop will ignore violations when the receiver of
|
12
|
+
# the case equality operator is `self.class`. Note intermediate variables are not accepted.
|
10
13
|
#
|
11
14
|
# @example
|
12
15
|
# # bad
|
@@ -26,6 +29,14 @@ module RuboCop
|
|
26
29
|
# # good
|
27
30
|
# Array === something
|
28
31
|
#
|
32
|
+
# @example AllowOnSelfClass: false (default)
|
33
|
+
# # bad
|
34
|
+
# self.class === something
|
35
|
+
#
|
36
|
+
# @example AllowOnSelfClass: true
|
37
|
+
# # good
|
38
|
+
# self.class === something
|
39
|
+
#
|
29
40
|
class CaseEquality < Base
|
30
41
|
extend AutoCorrector
|
31
42
|
|
@@ -33,7 +44,10 @@ module RuboCop
|
|
33
44
|
RESTRICT_ON_SEND = %i[===].freeze
|
34
45
|
|
35
46
|
# @!method case_equality?(node)
|
36
|
-
def_node_matcher :case_equality?, '(send $#
|
47
|
+
def_node_matcher :case_equality?, '(send $#offending_receiver? :=== $_)'
|
48
|
+
|
49
|
+
# @!method self_class?(node)
|
50
|
+
def_node_matcher :self_class?, '(send (self) :class)'
|
37
51
|
|
38
52
|
def on_send(node)
|
39
53
|
case_equality?(node) do |lhs, rhs|
|
@@ -48,12 +62,11 @@ module RuboCop
|
|
48
62
|
|
49
63
|
private
|
50
64
|
|
51
|
-
def
|
52
|
-
if cop_config.fetch('AllowOnConstant', false)
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
65
|
+
def offending_receiver?(node)
|
66
|
+
return false if node&.const_type? && cop_config.fetch('AllowOnConstant', false)
|
67
|
+
return false if self_class?(node) && cop_config.fetch('AllowOnSelfClass', false)
|
68
|
+
|
69
|
+
true
|
57
70
|
end
|
58
71
|
|
59
72
|
def replacement(lhs, rhs)
|
@@ -66,12 +79,29 @@ module RuboCop
|
|
66
79
|
#
|
67
80
|
# So here is noop.
|
68
81
|
when :begin
|
69
|
-
|
70
|
-
"#{lhs.source}.include?(#{rhs.source})" if child&.range_type?
|
82
|
+
begin_replacement(lhs, rhs)
|
71
83
|
when :const
|
72
|
-
|
84
|
+
const_replacement(lhs, rhs)
|
85
|
+
when :send
|
86
|
+
send_replacement(lhs, rhs)
|
73
87
|
end
|
74
88
|
end
|
89
|
+
|
90
|
+
def begin_replacement(lhs, rhs)
|
91
|
+
return unless lhs.children.first&.range_type?
|
92
|
+
|
93
|
+
"#{lhs.source}.include?(#{rhs.source})"
|
94
|
+
end
|
95
|
+
|
96
|
+
def const_replacement(lhs, rhs)
|
97
|
+
"#{rhs.source}.is_a?(#{lhs.source})"
|
98
|
+
end
|
99
|
+
|
100
|
+
def send_replacement(lhs, rhs)
|
101
|
+
return unless self_class?(lhs)
|
102
|
+
|
103
|
+
"#{rhs.source}.is_a?(#{lhs.source})"
|
104
|
+
end
|
75
105
|
end
|
76
106
|
end
|
77
107
|
end
|
@@ -70,7 +70,7 @@ module RuboCop
|
|
70
70
|
|
71
71
|
def on_sclass(node)
|
72
72
|
return unless def_self_style?
|
73
|
-
return unless node.identifier.
|
73
|
+
return unless node.identifier.self_type?
|
74
74
|
return unless all_methods_public?(node)
|
75
75
|
|
76
76
|
add_offense(node, message: MSG_SCLASS) do |corrector|
|
@@ -80,6 +80,7 @@ module RuboCop
|
|
80
80
|
|
81
81
|
def on_defs(node)
|
82
82
|
return if def_self_style?
|
83
|
+
return unless node.receiver.self_type?
|
83
84
|
|
84
85
|
message = format(MSG, preferred: 'class << self')
|
85
86
|
add_offense(node, message: message)
|
@@ -66,6 +66,8 @@ module RuboCop
|
|
66
66
|
add_offense(node) if same_collection_looping?(node, node.left_sibling)
|
67
67
|
end
|
68
68
|
|
69
|
+
alias on_numblock on_block
|
70
|
+
|
69
71
|
def on_for(node)
|
70
72
|
return unless node.parent&.begin_type?
|
71
73
|
|
@@ -82,7 +84,7 @@ module RuboCop
|
|
82
84
|
end
|
83
85
|
|
84
86
|
def same_collection_looping?(node, sibling)
|
85
|
-
sibling&.block_type? &&
|
87
|
+
(sibling&.block_type? || sibling&.numblock_type?) &&
|
86
88
|
sibling.send_node.method?(node.method_name) &&
|
87
89
|
sibling.receiver == node.receiver &&
|
88
90
|
sibling.send_node.arguments == node.send_node.arguments
|
@@ -27,15 +27,15 @@ module RuboCop
|
|
27
27
|
|
28
28
|
MSG = 'Use `Integer#times` for a simple loop which iterates a fixed number of times.'
|
29
29
|
|
30
|
-
def on_block(node)
|
31
|
-
return unless
|
30
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
31
|
+
return unless offending?(node)
|
32
32
|
|
33
33
|
send_node = node.send_node
|
34
34
|
|
35
35
|
range = send_node.receiver.source_range.join(send_node.loc.selector)
|
36
36
|
|
37
37
|
add_offense(range) do |corrector|
|
38
|
-
range_type, min, max =
|
38
|
+
range_type, min, max = each_range(node)
|
39
39
|
|
40
40
|
max += 1 if range_type == :irange
|
41
41
|
|
@@ -45,9 +45,44 @@ module RuboCop
|
|
45
45
|
|
46
46
|
private
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
def offending?(node)
|
49
|
+
each_range_with_zero_origin?(node) || each_range_without_block_argument?(node)
|
50
|
+
end
|
51
|
+
|
52
|
+
# @!method each_range(node)
|
53
|
+
def_node_matcher :each_range, <<~PATTERN
|
54
|
+
(block
|
55
|
+
(send
|
56
|
+
(begin
|
57
|
+
(${irange erange}
|
58
|
+
(int $_) (int $_)))
|
59
|
+
:each)
|
60
|
+
(args ...)
|
61
|
+
...)
|
62
|
+
PATTERN
|
63
|
+
|
64
|
+
# @!method each_range_with_zero_origin?(node)
|
65
|
+
def_node_matcher :each_range_with_zero_origin?, <<~PATTERN
|
66
|
+
(block
|
67
|
+
(send
|
68
|
+
(begin
|
69
|
+
({irange erange}
|
70
|
+
(int 0) (int _)))
|
71
|
+
:each)
|
72
|
+
(args ...)
|
73
|
+
...)
|
74
|
+
PATTERN
|
75
|
+
|
76
|
+
# @!method each_range_without_block_argument?(node)
|
77
|
+
def_node_matcher :each_range_without_block_argument?, <<~PATTERN
|
78
|
+
(block
|
79
|
+
(send
|
80
|
+
(begin
|
81
|
+
({irange erange}
|
82
|
+
(int _) (int _)))
|
83
|
+
:each)
|
84
|
+
(args)
|
85
|
+
...)
|
51
86
|
PATTERN
|
52
87
|
end
|
53
88
|
end
|
@@ -23,13 +23,8 @@ module RuboCop
|
|
23
23
|
MSG = 'Use `each_with_object` instead of `%<method>s`.'
|
24
24
|
METHODS = %i[inject reduce].freeze
|
25
25
|
|
26
|
-
# @!method each_with_object_candidate?(node)
|
27
|
-
def_node_matcher :each_with_object_candidate?, <<~PATTERN
|
28
|
-
(block $(send _ {:inject :reduce} _) $_ $_)
|
29
|
-
PATTERN
|
30
|
-
|
31
26
|
def on_block(node)
|
32
|
-
|
27
|
+
each_with_object_block_candidate?(node) do |method, args, body|
|
33
28
|
_, method_name, method_arg = *method
|
34
29
|
return if simple_method_arg?(method_arg)
|
35
30
|
|
@@ -40,14 +35,38 @@ module RuboCop
|
|
40
35
|
|
41
36
|
message = format(MSG, method: method_name)
|
42
37
|
add_offense(method.loc.selector, message: message) do |corrector|
|
43
|
-
|
38
|
+
autocorrect_block(corrector, node, return_value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def on_numblock(node)
|
44
|
+
each_with_object_numblock_candidate?(node) do |method, body|
|
45
|
+
_, method_name, method_arg = *method
|
46
|
+
return if simple_method_arg?(method_arg)
|
47
|
+
|
48
|
+
return unless return_value(body)&.source == '_1'
|
49
|
+
|
50
|
+
message = format(MSG, method: method_name)
|
51
|
+
add_offense(method.loc.selector, message: message) do |corrector|
|
52
|
+
autocorrect_numblock(corrector, node)
|
44
53
|
end
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
48
57
|
private
|
49
58
|
|
50
|
-
|
59
|
+
# @!method each_with_object_block_candidate?(node)
|
60
|
+
def_node_matcher :each_with_object_block_candidate?, <<~PATTERN
|
61
|
+
(block $(send _ {:inject :reduce} _) $_ $_)
|
62
|
+
PATTERN
|
63
|
+
|
64
|
+
# @!method each_with_object_numblock_candidate?(node)
|
65
|
+
def_node_matcher :each_with_object_numblock_candidate?, <<~PATTERN
|
66
|
+
(numblock $(send _ {:inject :reduce} _) 2 $_)
|
67
|
+
PATTERN
|
68
|
+
|
69
|
+
def autocorrect_block(corrector, node, return_value)
|
51
70
|
corrector.replace(node.send_node.loc.selector, 'each_with_object')
|
52
71
|
|
53
72
|
first_arg, second_arg = *node.arguments
|
@@ -62,6 +81,18 @@ module RuboCop
|
|
62
81
|
end
|
63
82
|
end
|
64
83
|
|
84
|
+
def autocorrect_numblock(corrector, node)
|
85
|
+
corrector.replace(node.send_node.loc.selector, 'each_with_object')
|
86
|
+
|
87
|
+
# We don't remove the return value to avoid a clobbering error.
|
88
|
+
node.body.each_descendant do |var|
|
89
|
+
next unless var.lvar_type?
|
90
|
+
|
91
|
+
corrector.replace(var, '_2') if var.source == '_1'
|
92
|
+
corrector.replace(var, '_1') if var.source == '_2'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
65
96
|
def simple_method_arg?(method_arg)
|
66
97
|
method_arg&.basic_literal?
|
67
98
|
end
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
|
29
29
|
MSG = 'Omit pipes for the empty block parameters.'
|
30
30
|
|
31
|
-
def on_block(node)
|
31
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
32
32
|
send_node = node.send_node
|
33
33
|
check(node) unless send_node.send_type? && send_node.lambda_literal?
|
34
34
|
end
|
@@ -6,6 +6,10 @@ module RuboCop
|
|
6
6
|
# Use a guard clause instead of wrapping the code inside a conditional
|
7
7
|
# expression
|
8
8
|
#
|
9
|
+
# A condition with an `elsif` or `else` branch is allowed unless
|
10
|
+
# one of `return`, `break`, `next`, `raise`, or `fail` is used
|
11
|
+
# in the body of the conditional expression.
|
12
|
+
#
|
9
13
|
# @example
|
10
14
|
# # bad
|
11
15
|
# def test
|
@@ -50,34 +54,41 @@ module RuboCop
|
|
50
54
|
#
|
51
55
|
# @example AllowConsecutiveConditionals: false (default)
|
52
56
|
# # bad
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
57
|
+
# def test
|
58
|
+
# if foo?
|
59
|
+
# work
|
60
|
+
# end
|
56
61
|
#
|
57
|
-
#
|
58
|
-
#
|
62
|
+
# if bar? # <- reports an offense
|
63
|
+
# work
|
64
|
+
# end
|
59
65
|
# end
|
60
66
|
#
|
61
67
|
# @example AllowConsecutiveConditionals: true
|
62
68
|
# # good
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
69
|
+
# def test
|
70
|
+
# if foo?
|
71
|
+
# work
|
72
|
+
# end
|
66
73
|
#
|
67
|
-
#
|
68
|
-
#
|
74
|
+
# if bar?
|
75
|
+
# work
|
76
|
+
# end
|
69
77
|
# end
|
70
78
|
#
|
71
79
|
# # bad
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
80
|
+
# def test
|
81
|
+
# if foo?
|
82
|
+
# work
|
83
|
+
# end
|
75
84
|
#
|
76
|
-
#
|
85
|
+
# do_something
|
77
86
|
#
|
78
|
-
#
|
79
|
-
#
|
87
|
+
# if bar? # <- reports an offense
|
88
|
+
# work
|
89
|
+
# end
|
80
90
|
# end
|
91
|
+
#
|
81
92
|
class GuardClause < Base
|
82
93
|
include MinBodyLength
|
83
94
|
include StatementModifier
|
@@ -35,13 +35,15 @@ module RuboCop
|
|
35
35
|
|
36
36
|
# @!method kv_each(node)
|
37
37
|
def_node_matcher :kv_each, <<~PATTERN
|
38
|
-
(block $(send (send _ ${:keys :values}) :each) ...)
|
38
|
+
({block numblock} $(send (send _ ${:keys :values}) :each) ...)
|
39
39
|
PATTERN
|
40
40
|
|
41
41
|
def on_block(node)
|
42
42
|
register_kv_offense(node)
|
43
43
|
end
|
44
44
|
|
45
|
+
alias on_numblock on_block
|
46
|
+
|
45
47
|
private
|
46
48
|
|
47
49
|
def register_kv_offense(node)
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
# * always - forces use of the 3.1 syntax (e.g. {foo:})
|
29
29
|
# * never - forces use of explicit hash literal value
|
30
30
|
# * either - accepts both shorthand and explicit use of hash literal value
|
31
|
+
# * consistent - like "always", but will avoid mixing styles in a single hash
|
31
32
|
#
|
32
33
|
# @example EnforcedStyle: ruby19 (default)
|
33
34
|
# # bad
|
@@ -89,6 +90,20 @@ module RuboCop
|
|
89
90
|
# # good
|
90
91
|
# {foo:, bar:}
|
91
92
|
#
|
93
|
+
# @example EnforcedShorthandSyntax: consistent
|
94
|
+
#
|
95
|
+
# # bad
|
96
|
+
# {foo: , bar: bar}
|
97
|
+
#
|
98
|
+
# # good
|
99
|
+
# {foo:, bar:}
|
100
|
+
#
|
101
|
+
# # bad
|
102
|
+
# {foo: , bar: baz}
|
103
|
+
#
|
104
|
+
# # good
|
105
|
+
# {foo: foo, bar: baz}
|
106
|
+
#
|
92
107
|
class HashSyntax < Base
|
93
108
|
include ConfigurableEnforcedStyle
|
94
109
|
include HashShorthandSyntax
|
@@ -104,6 +119,8 @@ module RuboCop
|
|
104
119
|
|
105
120
|
return if pairs.empty?
|
106
121
|
|
122
|
+
on_hash_for_mixed_shorthand(node)
|
123
|
+
|
107
124
|
if style == :hash_rockets || force_hash_rockets?(pairs)
|
108
125
|
hash_rockets_check(pairs)
|
109
126
|
elsif style == :ruby19_no_mixed_keys
|