rubocop 1.57.2 → 1.60.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +4 -4
- data/config/default.yml +46 -3
- data/lib/rubocop/config.rb +0 -2
- data/lib/rubocop/config_loader.rb +0 -1
- data/lib/rubocop/config_obsoletion.rb +11 -8
- data/lib/rubocop/config_validator.rb +0 -2
- data/lib/rubocop/cop/base.rb +6 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
- data/lib/rubocop/cop/exclude_limit.rb +1 -1
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
- data/lib/rubocop/cop/internal_affairs/example_description.rb +4 -4
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
- data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +22 -7
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
- data/lib/rubocop/cop/layout/redundant_line_break.rb +7 -2
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +50 -20
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -4
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -3
- data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
- data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
- data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +43 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -4
- data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +7 -2
- data/lib/rubocop/cop/lint/syntax.rb +6 -3
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +14 -1
- data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +12 -4
- data/lib/rubocop/cop/naming/constant_name.rb +1 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/security/open.rb +2 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +127 -17
- data/lib/rubocop/cop/style/array_first_last.rb +64 -0
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
- data/lib/rubocop/cop/style/case_like_if.rb +4 -4
- data/lib/rubocop/cop/style/class_check.rb +1 -0
- data/lib/rubocop/cop/style/collection_compact.rb +18 -8
- data/lib/rubocop/cop/style/combinable_loops.rb +13 -7
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/date_time.rb +5 -4
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
- data/lib/rubocop/cop/style/each_with_object.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +3 -14
- data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
- data/lib/rubocop/cop/style/hash_each_methods.rb +105 -11
- data/lib/rubocop/cop/style/hash_except.rb +2 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +4 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +6 -5
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +39 -2
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +3 -2
- data/lib/rubocop/cop/style/map_to_hash.rb +17 -7
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +14 -5
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -3
- data/lib/rubocop/cop/style/next.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
- data/lib/rubocop/cop/style/redundant_argument.rb +3 -2
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +3 -3
- data/lib/rubocop/cop/style/redundant_each.rb +7 -4
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +10 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +33 -10
- data/lib/rubocop/cop/style/redundant_return.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +17 -2
- data/lib/rubocop/cop/style/redundant_sort.rb +9 -8
- data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
- data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
- data/lib/rubocop/cop/style/sample.rb +2 -1
- data/lib/rubocop/cop/style/select_by_regexp.rb +7 -6
- data/lib/rubocop/cop/style/self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +8 -0
- data/lib/rubocop/cop/style/single_argument_dig.rb +5 -2
- data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
- data/lib/rubocop/cop/style/string_chars.rb +1 -0
- data/lib/rubocop/cop/style/strip.rb +7 -4
- data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +36 -0
- data/lib/rubocop/cop/style/unpack_first.rb +11 -14
- data/lib/rubocop/cops_documentation_generator.rb +11 -1
- data/lib/rubocop/ext/regexp_node.rb +9 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
- data/lib/rubocop/formatter/html_formatter.rb +1 -2
- data/lib/rubocop/formatter/json_formatter.rb +0 -1
- data/lib/rubocop/formatter.rb +1 -1
- data/lib/rubocop/lsp/routes.rb +1 -1
- data/lib/rubocop/options.rb +0 -8
- data/lib/rubocop/result_cache.rb +0 -1
- data/lib/rubocop/rspec/shared_contexts.rb +6 -0
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/server/cache.rb +1 -2
- data/lib/rubocop/server/client_command/exec.rb +0 -1
- data/lib/rubocop/server/server_command/exec.rb +0 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +4 -0
- metadata +15 -10
- /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
@@ -84,7 +84,7 @@ module RuboCop
|
|
84
84
|
# are considered.
|
85
85
|
return if node.method?(:eval) && !valid_eval_receiver?(node.receiver)
|
86
86
|
|
87
|
-
code = node.
|
87
|
+
code = node.first_argument
|
88
88
|
return unless code && (code.str_type? || code.dstr_type?)
|
89
89
|
|
90
90
|
check_location(node, code)
|
@@ -128,17 +128,6 @@ module RuboCop
|
|
128
128
|
node.method?(:eval) ? node.arguments.size >= 2 : true
|
129
129
|
end
|
130
130
|
|
131
|
-
# FIXME: It's a Style/ConditionalAssignment's false positive.
|
132
|
-
# rubocop:disable Style/ConditionalAssignment
|
133
|
-
def with_lineno?(node)
|
134
|
-
if node.method?(:eval)
|
135
|
-
node.arguments.size == 4
|
136
|
-
else
|
137
|
-
node.arguments.size == 3
|
138
|
-
end
|
139
|
-
end
|
140
|
-
# rubocop:enable Style/ConditionalAssignment
|
141
|
-
|
142
131
|
def add_offense_for_incorrect_line(method_name, line_node, sign, line_diff)
|
143
132
|
expected = expected_line(sign, line_diff)
|
144
133
|
message = format(MSG_INCORRECT_LINE,
|
@@ -165,7 +154,7 @@ module RuboCop
|
|
165
154
|
end
|
166
155
|
|
167
156
|
def check_line(node, code)
|
168
|
-
line_node = node.
|
157
|
+
line_node = node.last_argument
|
169
158
|
line_diff = line_difference(line_node, code)
|
170
159
|
if line_diff.zero?
|
171
160
|
add_offense_for_same_line(node, line_node)
|
@@ -227,7 +216,7 @@ module RuboCop
|
|
227
216
|
end
|
228
217
|
|
229
218
|
def missing_line(node, code)
|
230
|
-
line_diff = line_difference(node.
|
219
|
+
line_diff = line_difference(node.last_argument, code)
|
231
220
|
sign = line_diff.positive? ? :+ : :-
|
232
221
|
expected_line(sign, line_diff)
|
233
222
|
end
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
|
31
31
|
# @!method exact_regexp_match(node)
|
32
32
|
def_node_matcher :exact_regexp_match, <<~PATTERN
|
33
|
-
(
|
33
|
+
(call
|
34
34
|
_ {:=~ :=== :!~ :match :match?}
|
35
35
|
(regexp
|
36
36
|
(str $_)
|
@@ -49,6 +49,7 @@ module RuboCop
|
|
49
49
|
corrector.replace(node, prefer)
|
50
50
|
end
|
51
51
|
end
|
52
|
+
alias on_csend on_send
|
52
53
|
|
53
54
|
private
|
54
55
|
|
@@ -86,7 +86,7 @@ module RuboCop
|
|
86
86
|
|
87
87
|
def extract_block_name(def_node)
|
88
88
|
if def_node.block_argument?
|
89
|
-
def_node.
|
89
|
+
def_node.last_argument.name
|
90
90
|
else
|
91
91
|
'block'
|
92
92
|
end
|
@@ -127,7 +127,7 @@ module RuboCop
|
|
127
127
|
end
|
128
128
|
|
129
129
|
def insert_argument(node, corrector, block_name)
|
130
|
-
last_arg = node.
|
130
|
+
last_arg = node.last_argument
|
131
131
|
arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
|
132
132
|
replacement = " &#{block_name}"
|
133
133
|
replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
|
@@ -17,10 +17,16 @@ module RuboCop
|
|
17
17
|
# @example
|
18
18
|
# # bad
|
19
19
|
# hash.keys.each { |k| p k }
|
20
|
-
# hash.
|
20
|
+
# hash.each { |k, unused_value| p k }
|
21
21
|
#
|
22
22
|
# # good
|
23
23
|
# hash.each_key { |k| p k }
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# hash.values.each { |v| p v }
|
27
|
+
# hash.each { |unused_key, v| p v }
|
28
|
+
#
|
29
|
+
# # good
|
24
30
|
# hash.each_value { |v| p v }
|
25
31
|
#
|
26
32
|
# @example AllowedReceivers: ['execute']
|
@@ -33,25 +39,59 @@ module RuboCop
|
|
33
39
|
extend AutoCorrector
|
34
40
|
|
35
41
|
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
|
42
|
+
UNUSED_BLOCK_ARG_MSG = "#{MSG.chop} and remove the unused `%<unused_code>s` block argument."
|
43
|
+
ARRAY_CONVERTER_METHODS = %i[assoc flatten rassoc sort sort_by to_a].freeze
|
36
44
|
|
37
45
|
# @!method kv_each(node)
|
38
46
|
def_node_matcher :kv_each, <<~PATTERN
|
39
|
-
({block numblock} $(
|
47
|
+
({block numblock} $(call (call _ ${:keys :values}) :each) ...)
|
48
|
+
PATTERN
|
49
|
+
|
50
|
+
# @!method each_arguments(node)
|
51
|
+
def_node_matcher :each_arguments, <<~PATTERN
|
52
|
+
(block (call _ :each)(args $_key $_value) ...)
|
40
53
|
PATTERN
|
41
54
|
|
42
55
|
# @!method kv_each_with_block_pass(node)
|
43
56
|
def_node_matcher :kv_each_with_block_pass, <<~PATTERN
|
44
|
-
(
|
57
|
+
(call $(call _ ${:keys :values}) :each (block_pass (sym _)))
|
45
58
|
PATTERN
|
46
59
|
|
47
60
|
def on_block(node)
|
61
|
+
return unless handleable?(node)
|
62
|
+
|
48
63
|
kv_each(node) do |target, method|
|
49
|
-
register_kv_offense(target, method)
|
64
|
+
register_kv_offense(target, method) and return
|
50
65
|
end
|
51
|
-
end
|
52
66
|
|
67
|
+
return unless (key, value = each_arguments(node))
|
68
|
+
|
69
|
+
check_unused_block_args(node, key, value)
|
70
|
+
end
|
53
71
|
alias on_numblock on_block
|
54
72
|
|
73
|
+
# rubocop:disable Metrics/AbcSize
|
74
|
+
def check_unused_block_args(node, key, value)
|
75
|
+
return if node.body.nil?
|
76
|
+
|
77
|
+
value_unused = unused_block_arg_exist?(node, value)
|
78
|
+
key_unused = unused_block_arg_exist?(node, key)
|
79
|
+
return if value_unused && key_unused
|
80
|
+
|
81
|
+
if value_unused
|
82
|
+
message = message('each_key', node.method_name, value.source)
|
83
|
+
unused_range = key.source_range.end.join(value.source_range.end)
|
84
|
+
|
85
|
+
register_each_args_offense(node, message, 'each_key', unused_range)
|
86
|
+
elsif key_unused
|
87
|
+
message = message('each_value', node.method_name, key.source)
|
88
|
+
unused_range = key.source_range.begin.join(value.source_range.begin)
|
89
|
+
|
90
|
+
register_each_args_offense(node, message, 'each_value', unused_range)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
# rubocop:enable Metrics/AbcSize
|
94
|
+
|
55
95
|
def on_block_pass(node)
|
56
96
|
kv_each_with_block_pass(node.parent) do |target, method|
|
57
97
|
register_kv_with_block_pass_offense(node, target, method)
|
@@ -60,27 +100,81 @@ module RuboCop
|
|
60
100
|
|
61
101
|
private
|
62
102
|
|
103
|
+
def handleable?(node)
|
104
|
+
return false if use_array_converter_method_as_preceding?(node)
|
105
|
+
return false unless (root_receiver = root_receiver(node))
|
106
|
+
|
107
|
+
!root_receiver.literal? || root_receiver.hash_type?
|
108
|
+
end
|
109
|
+
|
63
110
|
def register_kv_offense(target, method)
|
64
111
|
return unless (parent_receiver = target.receiver.receiver)
|
65
112
|
return if allowed_receiver?(parent_receiver)
|
66
113
|
|
67
|
-
|
114
|
+
current = target.receiver.loc.selector.join(target.source_range.end).source
|
115
|
+
|
116
|
+
add_offense(kv_range(target), message: format_message(method, current)) do |corrector|
|
68
117
|
correct_key_value_each(target, corrector)
|
69
118
|
end
|
70
119
|
end
|
71
120
|
|
121
|
+
def unused_block_arg_exist?(node, block_arg)
|
122
|
+
lvar_sources = node.body.each_descendant(:lvar).map(&:source)
|
123
|
+
|
124
|
+
if block_arg.mlhs_type?
|
125
|
+
block_arg.each_descendant(:arg, :restarg).all? do |block_arg|
|
126
|
+
lvar_sources.none?(block_arg.source.delete_prefix('*'))
|
127
|
+
end
|
128
|
+
else
|
129
|
+
lvar_sources.none?(block_arg.source.delete_prefix('*'))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def message(prefer, method_name, unused_code)
|
134
|
+
format(
|
135
|
+
UNUSED_BLOCK_ARG_MSG, prefer: prefer, current: method_name, unused_code: unused_code
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
def register_each_args_offense(node, message, prefer, unused_range)
|
140
|
+
add_offense(node, message: message) do |corrector|
|
141
|
+
corrector.replace(node.send_node.loc.selector, prefer)
|
142
|
+
corrector.remove(unused_range)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
72
146
|
def register_kv_with_block_pass_offense(node, target, method)
|
73
147
|
return unless (parent_receiver = node.parent.receiver.receiver)
|
74
148
|
return if allowed_receiver?(parent_receiver)
|
75
149
|
|
76
|
-
range = target.loc.selector.
|
77
|
-
|
150
|
+
range = target.loc.selector.join(node.parent.loc.selector.end)
|
151
|
+
|
152
|
+
add_offense(range, message: format_message(method, range.source)) do |corrector|
|
78
153
|
corrector.replace(range, "each_#{method[0..-2]}")
|
79
154
|
end
|
80
155
|
end
|
81
156
|
|
82
|
-
def
|
83
|
-
|
157
|
+
def use_array_converter_method_as_preceding?(node)
|
158
|
+
return false unless (preceding_method = node.children.first.children.first)
|
159
|
+
unless preceding_method.call_type? ||
|
160
|
+
preceding_method.block_type? || preceding_method.numblock_type?
|
161
|
+
return false
|
162
|
+
end
|
163
|
+
|
164
|
+
ARRAY_CONVERTER_METHODS.include?(preceding_method.method_name)
|
165
|
+
end
|
166
|
+
|
167
|
+
def root_receiver(node)
|
168
|
+
receiver = node.receiver
|
169
|
+
if receiver&.receiver
|
170
|
+
root_receiver(receiver)
|
171
|
+
else
|
172
|
+
receiver
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def format_message(method_name, current)
|
177
|
+
format(MSG, prefer: "each_#{method_name[0..-2]}", current: current)
|
84
178
|
end
|
85
179
|
|
86
180
|
def check_argument(variable)
|
@@ -103,7 +197,7 @@ module RuboCop
|
|
103
197
|
name = "each_#{node.receiver.method_name.to_s.chop}"
|
104
198
|
return correct_implicit(node, corrector, name) unless receiver
|
105
199
|
|
106
|
-
new_source = receiver.source + "
|
200
|
+
new_source = receiver.source + "#{node.loc.dot.source}#{name}"
|
107
201
|
corrector.replace(node, new_source)
|
108
202
|
end
|
109
203
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
# @!method bad_method_with_poro?(node)
|
44
44
|
def_node_matcher :bad_method_with_poro?, <<~PATTERN
|
45
45
|
(block
|
46
|
-
(
|
46
|
+
(call _ _)
|
47
47
|
(args
|
48
48
|
$(arg _)
|
49
49
|
(arg _))
|
@@ -86,6 +86,7 @@ module RuboCop
|
|
86
86
|
corrector.replace(range, preferred_method)
|
87
87
|
end
|
88
88
|
end
|
89
|
+
alias on_csend on_send
|
89
90
|
|
90
91
|
private
|
91
92
|
|
@@ -158,7 +158,10 @@ module RuboCop
|
|
158
158
|
if head.assignment?
|
159
159
|
# The `send` node is used instead of the `indexasgn` node, so `name` cannot be used.
|
160
160
|
# https://github.com/rubocop/rubocop-ast/blob/v1.29.0/lib/rubocop/ast/node/indexasgn_node.rb
|
161
|
-
|
161
|
+
#
|
162
|
+
# FIXME: It would be better to update `RuboCop::AST::OpAsgnNode` or its subclasses to
|
163
|
+
# handle `self.foo ||= value` as a solution, instead of using `head.node_parts[0].to_s`.
|
164
|
+
assigned_value = head.send_type? ? head.receiver.source : head.node_parts[0].to_s
|
162
165
|
|
163
166
|
return if condition_variable == assigned_value
|
164
167
|
end
|
@@ -60,17 +60,17 @@ module RuboCop
|
|
60
60
|
# @!method inverse_candidate?(node)
|
61
61
|
def_node_matcher :inverse_candidate?, <<~PATTERN
|
62
62
|
{
|
63
|
-
(send $(
|
64
|
-
(send ({block numblock} $(
|
65
|
-
(send (begin $(
|
63
|
+
(send $(call $(...) $_ $...) :!)
|
64
|
+
(send ({block numblock} $(call $(...) $_) $...) :!)
|
65
|
+
(send (begin $(call $(...) $_ $...)) :!)
|
66
66
|
}
|
67
67
|
PATTERN
|
68
68
|
|
69
69
|
# @!method inverse_block?(node)
|
70
70
|
def_node_matcher :inverse_block?, <<~PATTERN
|
71
|
-
({block numblock} $(
|
71
|
+
({block numblock} $(call (...) $_) ... { $(call ... :!)
|
72
72
|
$(send (...) {:!= :!~} ...)
|
73
|
-
(begin ... $(
|
73
|
+
(begin ... $(call ... :!))
|
74
74
|
(begin ... $(send (...) {:!= :!~} ...))
|
75
75
|
})
|
76
76
|
PATTERN
|
@@ -87,6 +87,7 @@ module RuboCop
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
|
+
alias on_csend on_send
|
90
91
|
|
91
92
|
def on_block(node)
|
92
93
|
inverse_block?(node) do |_method_call, method, block|
|
@@ -51,7 +51,7 @@ module RuboCop
|
|
51
51
|
class InvertibleUnlessCondition < Base
|
52
52
|
extend AutoCorrector
|
53
53
|
|
54
|
-
MSG = '
|
54
|
+
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
|
55
55
|
|
56
56
|
def on_if(node)
|
57
57
|
return unless node.unless?
|
@@ -59,7 +59,10 @@ module RuboCop
|
|
59
59
|
condition = node.condition
|
60
60
|
return unless invertible?(condition)
|
61
61
|
|
62
|
-
|
62
|
+
message = format(MSG, prefer: "#{node.inverse_keyword} #{preferred_condition(condition)}",
|
63
|
+
current: "#{node.keyword} #{condition.source}")
|
64
|
+
|
65
|
+
add_offense(node, message: message) do |corrector|
|
63
66
|
corrector.replace(node.loc.keyword, node.inverse_keyword)
|
64
67
|
autocorrect(corrector, condition)
|
65
68
|
end
|
@@ -88,6 +91,40 @@ module RuboCop
|
|
88
91
|
(argument.const_type? && argument.short_name.to_s.upcase != argument.short_name.to_s)
|
89
92
|
end
|
90
93
|
|
94
|
+
def preferred_condition(node)
|
95
|
+
case node.type
|
96
|
+
when :begin then "(#{preferred_condition(node.children.first)})"
|
97
|
+
when :send then preferred_send_condition(node)
|
98
|
+
when :or, :and then preferred_logical_condition(node)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def preferred_send_condition(node)
|
103
|
+
receiver_source = node.receiver.source
|
104
|
+
return receiver_source if node.method?(:!)
|
105
|
+
|
106
|
+
inverse_method_name = inverse_methods[node.method_name]
|
107
|
+
return "#{receiver_source}.#{inverse_method_name}" unless node.arguments?
|
108
|
+
|
109
|
+
argument_list = node.arguments.map(&:source).join(', ')
|
110
|
+
if node.operator_method?
|
111
|
+
return "#{receiver_source} #{inverse_method_name} #{argument_list}"
|
112
|
+
end
|
113
|
+
|
114
|
+
if node.parenthesized?
|
115
|
+
return "#{receiver_source}.#{inverse_method_name}(#{argument_list})"
|
116
|
+
end
|
117
|
+
|
118
|
+
"#{receiver_source}.#{inverse_method_name} #{argument_list}"
|
119
|
+
end
|
120
|
+
|
121
|
+
def preferred_logical_condition(node)
|
122
|
+
preferred_lhs = preferred_condition(node.lhs)
|
123
|
+
preferred_rhs = preferred_condition(node.rhs)
|
124
|
+
|
125
|
+
"#{preferred_lhs} #{node.inverse_operator} #{preferred_rhs}"
|
126
|
+
end
|
127
|
+
|
91
128
|
def autocorrect(corrector, node)
|
92
129
|
case node.type
|
93
130
|
when :begin
|
@@ -44,9 +44,9 @@ module RuboCop
|
|
44
44
|
|
45
45
|
# @!method map_and_compact?(node)
|
46
46
|
def_node_matcher :map_and_compact?, <<~RUBY
|
47
|
-
(
|
47
|
+
(call
|
48
48
|
(block
|
49
|
-
(
|
49
|
+
(call _ :map)
|
50
50
|
(args
|
51
51
|
$(arg _))
|
52
52
|
{
|
@@ -85,6 +85,7 @@ module RuboCop
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|
88
|
+
alias on_csend on_send
|
88
89
|
|
89
90
|
private
|
90
91
|
|
@@ -34,21 +34,25 @@ module RuboCop
|
|
34
34
|
|
35
35
|
minimum_target_ruby_version 2.6
|
36
36
|
|
37
|
-
MSG = 'Pass a block to `to_h` instead of calling `%<method>s
|
37
|
+
MSG = 'Pass a block to `to_h` instead of calling `%<method>s%<dot>sto_h`.'
|
38
38
|
RESTRICT_ON_SEND = %i[to_h].freeze
|
39
39
|
|
40
|
-
# @!method map_to_h
|
41
|
-
def_node_matcher :map_to_h
|
40
|
+
# @!method map_to_h(node)
|
41
|
+
def_node_matcher :map_to_h, <<~PATTERN
|
42
42
|
{
|
43
|
-
$(
|
44
|
-
$(
|
43
|
+
$(call ({block numblock} $(call _ {:map :collect}) ...) :to_h)
|
44
|
+
$(call $(call _ {:map :collect} (block_pass sym)) :to_h)
|
45
45
|
}
|
46
46
|
PATTERN
|
47
47
|
|
48
|
+
def self.autocorrect_incompatible_with
|
49
|
+
[Layout::SingleLineBlockChain]
|
50
|
+
end
|
51
|
+
|
48
52
|
def on_send(node)
|
49
|
-
return unless (to_h_node, map_node = map_to_h
|
53
|
+
return unless (to_h_node, map_node = map_to_h(node))
|
50
54
|
|
51
|
-
message = format(MSG, method: map_node.loc.selector.source)
|
55
|
+
message = format(MSG, method: map_node.loc.selector.source, dot: to_h_node.loc.dot.source)
|
52
56
|
add_offense(map_node.loc.selector, message: message) do |corrector|
|
53
57
|
# If the `to_h` call already has a block, do not autocorrect.
|
54
58
|
next if to_h_node.block_node
|
@@ -56,15 +60,21 @@ module RuboCop
|
|
56
60
|
autocorrect(corrector, to_h_node, map_node)
|
57
61
|
end
|
58
62
|
end
|
63
|
+
alias on_csend on_send
|
59
64
|
|
60
65
|
private
|
61
66
|
|
67
|
+
# rubocop:disable Metrics/AbcSize
|
62
68
|
def autocorrect(corrector, to_h, map)
|
63
69
|
removal_range = range_between(to_h.loc.dot.begin_pos, to_h.loc.selector.end_pos)
|
64
70
|
|
65
71
|
corrector.remove(range_with_surrounding_space(removal_range, side: :left))
|
72
|
+
if (map_dot = map.loc.dot)
|
73
|
+
corrector.replace(map_dot, to_h.loc.dot.source)
|
74
|
+
end
|
66
75
|
corrector.replace(map.loc.selector, 'to_h')
|
67
76
|
end
|
77
|
+
# rubocop:enable Metrics/AbcSize
|
68
78
|
end
|
69
79
|
end
|
70
80
|
end
|
@@ -86,6 +86,7 @@ module RuboCop
|
|
86
86
|
|
87
87
|
def legitimate_call_with_parentheses?(node) # rubocop:disable Metrics/PerceivedComplexity
|
88
88
|
call_in_literals?(node) ||
|
89
|
+
node.parent&.when_type? ||
|
89
90
|
call_with_ambiguous_arguments?(node) ||
|
90
91
|
call_in_logical_operators?(node) ||
|
91
92
|
call_in_optional_arguments?(node) ||
|
@@ -126,23 +127,31 @@ module RuboCop
|
|
126
127
|
|
127
128
|
def call_with_ambiguous_arguments?(node) # rubocop:disable Metrics/PerceivedComplexity
|
128
129
|
call_with_braced_block?(node) ||
|
130
|
+
call_in_argument_with_block?(node) ||
|
129
131
|
call_as_argument_or_chain?(node) ||
|
130
132
|
call_in_match_pattern?(node) ||
|
131
133
|
hash_literal_in_arguments?(node) ||
|
132
134
|
node.descendants.any? do |n|
|
133
|
-
n.forwarded_args_type? ||
|
134
|
-
|
135
|
+
n.forwarded_args_type? || n.block_type? ||
|
136
|
+
ambiguous_literal?(n) || logical_operator?(n)
|
135
137
|
end
|
136
138
|
end
|
137
139
|
|
138
140
|
def call_with_braced_block?(node)
|
139
|
-
(node.
|
141
|
+
(node.call_type? || node.super_type?) && node.block_node&.braces?
|
142
|
+
end
|
143
|
+
|
144
|
+
def call_in_argument_with_block?(node)
|
145
|
+
parent = node.parent&.block_type? && node.parent&.parent
|
146
|
+
return false unless parent
|
147
|
+
|
148
|
+
parent.call_type? || parent.super_type? || parent.yield_type?
|
140
149
|
end
|
141
150
|
|
142
151
|
def call_as_argument_or_chain?(node)
|
143
152
|
node.parent &&
|
144
|
-
(
|
145
|
-
|
153
|
+
(node.parent.call_type? || node.parent.super_type? || node.parent.yield_type?) &&
|
154
|
+
!assigned_before?(node.parent, node)
|
146
155
|
end
|
147
156
|
|
148
157
|
def call_in_match_pattern?(node)
|
@@ -218,15 +218,13 @@ module RuboCop
|
|
218
218
|
send(style, node) # call require_parentheses or omit_parentheses
|
219
219
|
end
|
220
220
|
alias on_csend on_send
|
221
|
-
alias on_super on_send
|
222
221
|
alias on_yield on_send
|
223
222
|
|
224
223
|
private
|
225
224
|
|
226
225
|
def args_begin(node)
|
227
226
|
loc = node.loc
|
228
|
-
selector =
|
229
|
-
node.super_type? || node.yield_type? ? loc.keyword : loc.selector
|
227
|
+
selector = node.yield_type? ? loc.keyword : loc.selector
|
230
228
|
|
231
229
|
resize_by = args_parenthesized?(node) ? 2 : 1
|
232
230
|
selector.end.resize(resize_by)
|
@@ -239,7 +237,7 @@ module RuboCop
|
|
239
237
|
def args_parenthesized?(node)
|
240
238
|
return false unless node.arguments.one?
|
241
239
|
|
242
|
-
first_node = node.
|
240
|
+
first_node = node.first_argument
|
243
241
|
first_node.begin_type? && first_node.parenthesized_call?
|
244
242
|
end
|
245
243
|
end
|
@@ -8,6 +8,9 @@ module RuboCop
|
|
8
8
|
# This cop can be customized allowed methods with `AllowedMethods`.
|
9
9
|
# By default, there are no methods to allowed.
|
10
10
|
#
|
11
|
+
# NOTE: This cop allows the use of `it()` without arguments in blocks,
|
12
|
+
# as in `0.times { it() }`, following `Lint/ItWithoutArgumentsInBlock` cop.
|
13
|
+
#
|
11
14
|
# @example
|
12
15
|
# # bad
|
13
16
|
# object.some_method()
|
@@ -30,15 +33,18 @@ module RuboCop
|
|
30
33
|
|
31
34
|
MSG = 'Do not use parentheses for method calls with no arguments.'
|
32
35
|
|
36
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
33
37
|
def on_send(node)
|
34
38
|
return unless !node.arguments? && node.parenthesized?
|
35
39
|
return if ineligible_node?(node)
|
36
40
|
return if default_argument?(node)
|
37
41
|
return if allowed_method_name?(node.method_name)
|
38
42
|
return if same_name_assignment?(node)
|
43
|
+
return if parenthesized_it_method_in_block?(node)
|
39
44
|
|
40
45
|
register_offense(node)
|
41
46
|
end
|
47
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
42
48
|
|
43
49
|
private
|
44
50
|
|
@@ -71,6 +77,20 @@ module RuboCop
|
|
71
77
|
end
|
72
78
|
end
|
73
79
|
|
80
|
+
# Respects `Lint/ItWithoutArgumentsInBlock` cop and the following Ruby 3.3's warning:
|
81
|
+
#
|
82
|
+
# $ ruby -e '0.times { begin; it; end }'
|
83
|
+
# -e:1: warning: `it` calls without arguments will refer to the first block param in
|
84
|
+
# Ruby 3.4; use it() or self.it
|
85
|
+
#
|
86
|
+
def parenthesized_it_method_in_block?(node)
|
87
|
+
return false unless node.method?(:it)
|
88
|
+
return false unless (block_node = node.each_ancestor(:block).first)
|
89
|
+
return false unless block_node.arguments.empty_and_without_delimiters?
|
90
|
+
|
91
|
+
!node.receiver && node.arguments.empty? && !node.block_literal?
|
92
|
+
end
|
93
|
+
|
74
94
|
def any_assignment?(node)
|
75
95
|
node.each_ancestor(*AST::Node::ASSIGNMENTS).any? do |asgn_node|
|
76
96
|
# `obj.method = value` parses as (send ... :method= ...), and will
|
@@ -170,7 +170,7 @@ module RuboCop
|
|
170
170
|
return true if node.arguments.any? do |arg|
|
171
171
|
arg.forward_arg_type? || arg.restarg_type? || arg.kwrestarg_type?
|
172
172
|
end
|
173
|
-
return false unless (last_argument = node.
|
173
|
+
return false unless (last_argument = node.last_argument)
|
174
174
|
|
175
175
|
last_argument.blockarg_type? && last_argument.name.nil?
|
176
176
|
end
|
@@ -54,12 +54,10 @@ module RuboCop
|
|
54
54
|
private
|
55
55
|
|
56
56
|
def offense?(node)
|
57
|
-
node.ternary? && node.multiline?
|
57
|
+
node.ternary? && node.multiline? && node.source != replacement(node)
|
58
58
|
end
|
59
59
|
|
60
60
|
def autocorrect(corrector, node)
|
61
|
-
return unless offense?(node)
|
62
|
-
|
63
61
|
corrector.replace(node, replacement(node))
|
64
62
|
return unless (parent = node.parent)
|
65
63
|
return unless (comments_in_condition = comments_in_condition(node))
|
@@ -4,8 +4,8 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# Checks for redundant dot before operator method call.
|
7
|
-
# The target operator methods are `|`, `^`, `&`,
|
8
|
-
#
|
7
|
+
# The target operator methods are `|`, `^`, `&`, ``<=>``, `==`, `===`, `=~`, `>`, `>=`, `<`,
|
8
|
+
# ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `~`, `!`, `!=`, and `!~`.
|
9
9
|
#
|
10
10
|
# @example
|
11
11
|
#
|