rubocop 1.73.1 → 1.75.1
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 +64 -10
- data/config/internal_affairs.yml +4 -0
- data/config/obsoletion.yml +3 -1
- data/lib/rubocop/cli.rb +1 -1
- data/lib/rubocop/config.rb +35 -6
- data/lib/rubocop/config_loader.rb +4 -1
- data/lib/rubocop/config_loader_resolver.rb +2 -1
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
- data/lib/rubocop/config_obsoletion.rb +46 -2
- data/lib/rubocop/config_validator.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_type_group.rb +91 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
- data/lib/rubocop/cop/layout/line_length.rb +5 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
- data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +15 -70
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
- data/lib/rubocop/cop/lint/literal_as_condition.rb +4 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +2 -2
- data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +9 -3
- data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
- data/lib/rubocop/cop/lint/return_in_void_context.rb +4 -11
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +8 -1
- data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -0
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +2 -11
- data/lib/rubocop/cop/lint/void.rb +1 -0
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
- data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
- data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
- data/lib/rubocop/cop/mixin/range_help.rb +12 -0
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +64 -8
- data/lib/rubocop/cop/naming/variable_name.rb +6 -19
- data/lib/rubocop/cop/registry.rb +9 -6
- data/lib/rubocop/cop/style/array_intersect.rb +39 -28
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +29 -7
- data/lib/rubocop/cop/style/collection_methods.rb +1 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
- data/lib/rubocop/cop/style/commented_keyword.rb +9 -2
- data/lib/rubocop/cop/style/comparable_between.rb +75 -0
- data/lib/rubocop/cop/style/double_negation.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
- data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
- data/lib/rubocop/cop/style/for.rb +1 -0
- data/lib/rubocop/cop/style/format_string_token.rb +38 -11
- data/lib/rubocop/cop/style/guard_clause.rb +2 -1
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +105 -0
- data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
- data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
- data/lib/rubocop/cop/style/it_block_parameter.rb +100 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
- data/lib/rubocop/cop/style/lambda.rb +1 -0
- data/lib/rubocop/cop/style/map_into_array.rb +1 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -3
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -0
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
- data/lib/rubocop/cop/style/next.rb +44 -0
- data/lib/rubocop/cop/style/object_then.rb +1 -0
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/raise_args.rb +8 -8
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
- data/lib/rubocop/cop/style/redundant_format.rb +10 -3
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
- data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +41 -106
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
- data/lib/rubocop/cop/utils/format_string.rb +5 -2
- data/lib/rubocop/cop/variable_force/scope.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +1 -6
- data/lib/rubocop/cop/variable_force.rb +1 -1
- data/lib/rubocop/directive_comment.rb +1 -1
- data/lib/rubocop/ext/regexp_node.rb +0 -1
- data/lib/rubocop/lsp/runtime.rb +4 -4
- data/lib/rubocop/lsp/stdin_runner.rb +3 -1
- data/lib/rubocop/rspec/cop_helper.rb +4 -1
- data/lib/rubocop/rspec/shared_contexts.rb +20 -0
- data/lib/rubocop/rspec/support.rb +2 -0
- data/lib/rubocop/runner.rb +5 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +14 -7
- data/lib/rubocop.rb +5 -0
- data/lib/ruby_lsp/rubocop/runtime_adapter.rb +20 -2
- metadata +12 -6
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
MSG = 'Avoid multi-line chains of blocks.'
|
29
29
|
|
30
30
|
def on_block(node)
|
31
|
-
node.send_node.each_node(:
|
31
|
+
node.send_node.each_node(:call) do |send_node|
|
32
32
|
receiver = send_node.receiver
|
33
33
|
|
34
34
|
next unless receiver&.any_block_type? && receiver.multiline?
|
@@ -44,6 +44,7 @@ module RuboCop
|
|
44
44
|
end
|
45
45
|
|
46
46
|
alias on_numblock on_block
|
47
|
+
alias on_itblock on_block
|
47
48
|
end
|
48
49
|
end
|
49
50
|
end
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
50
50
|
corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
|
51
51
|
end
|
52
52
|
|
53
|
-
arguments_range = arguments_range(node)
|
53
|
+
arguments_range = range_with_surrounding_space(arguments_range(node), side: :left)
|
54
54
|
# If the method name isn't on the same line as def, move it directly after def
|
55
55
|
if arguments_range.first_line != opening_line(node)
|
56
56
|
corrector.remove(node.loc.name)
|
@@ -66,14 +66,6 @@ module RuboCop
|
|
66
66
|
processed_source[arguments.last_line - 1].strip
|
67
67
|
end
|
68
68
|
|
69
|
-
def arguments_range(node)
|
70
|
-
range = range_between(
|
71
|
-
node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
|
72
|
-
)
|
73
|
-
|
74
|
-
range_with_surrounding_space(range, side: :left)
|
75
|
-
end
|
76
|
-
|
77
69
|
def opening_line(node)
|
78
70
|
node.first_line
|
79
71
|
end
|
@@ -46,6 +46,38 @@ module RuboCop
|
|
46
46
|
# next unless a == 1
|
47
47
|
# puts a
|
48
48
|
# end
|
49
|
+
#
|
50
|
+
# @example AllowConsecutiveConditionals: false (default)
|
51
|
+
# # bad
|
52
|
+
# [1, 2].each do |a|
|
53
|
+
# if a == 1
|
54
|
+
# puts a
|
55
|
+
# end
|
56
|
+
# if a == 2
|
57
|
+
# puts a
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# # good
|
62
|
+
# [1, 2].each do |a|
|
63
|
+
# if a == 1
|
64
|
+
# puts a
|
65
|
+
# end
|
66
|
+
# next unless a == 2
|
67
|
+
# puts a
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# @example AllowConsecutiveConditionals: true
|
71
|
+
# # good
|
72
|
+
# [1, 2].each do |a|
|
73
|
+
# if a == 1
|
74
|
+
# puts a
|
75
|
+
# end
|
76
|
+
# if a == 2
|
77
|
+
# puts a
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
#
|
49
81
|
class Next < Base
|
50
82
|
include ConfigurableEnforcedStyle
|
51
83
|
include MinBodyLength
|
@@ -72,6 +104,7 @@ module RuboCop
|
|
72
104
|
end
|
73
105
|
|
74
106
|
alias on_numblock on_block
|
107
|
+
alias on_itblock on_block
|
75
108
|
|
76
109
|
def on_while(node)
|
77
110
|
check(node)
|
@@ -86,6 +119,9 @@ module RuboCop
|
|
86
119
|
|
87
120
|
offending_node = offense_node(node.body)
|
88
121
|
|
122
|
+
return if allowed_consecutive_conditionals? &&
|
123
|
+
consecutive_conditionals?(offending_node)
|
124
|
+
|
89
125
|
add_offense(offense_location(offending_node)) do |corrector|
|
90
126
|
if offending_node.modifier_form?
|
91
127
|
autocorrect_modifier(corrector, offending_node)
|
@@ -227,6 +263,14 @@ module RuboCop
|
|
227
263
|
|
228
264
|
corrector.remove_leading(buffer.line_range(lineno), adjustment) if adjustment.positive?
|
229
265
|
end
|
266
|
+
|
267
|
+
def consecutive_conditionals?(if_node)
|
268
|
+
if_node.parent&.begin_type? && if_node.left_sibling&.if_type?
|
269
|
+
end
|
270
|
+
|
271
|
+
def allowed_consecutive_conditionals?
|
272
|
+
cop_config.fetch('AllowConsecutiveConditionals', false)
|
273
|
+
end
|
230
274
|
end
|
231
275
|
end
|
232
276
|
end
|
@@ -3,15 +3,15 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks the args passed to `fail` and `raise`.
|
7
|
-
# style (default), it recommends passing the exception class and message
|
8
|
-
# to `raise`, rather than construct an instance of the error. It will
|
9
|
-
# still allow passing just a message, or the construction of an error
|
10
|
-
# with more than one argument.
|
6
|
+
# Checks the args passed to `fail` and `raise`.
|
11
7
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
8
|
+
# Exploded style (default) enforces passing the exception class and message
|
9
|
+
# arguments separately, rather than constructing an instance of the error.
|
10
|
+
#
|
11
|
+
# Compact style enforces constructing an error instance.
|
12
|
+
#
|
13
|
+
# Both styles allow passing just a message, or an error instance when there is more
|
14
|
+
# than one argument.
|
15
15
|
#
|
16
16
|
# The exploded style has an `AllowedCompactTypes` configuration
|
17
17
|
# option that takes an `Array` of exception name Strings.
|
@@ -182,9 +182,8 @@ module RuboCop
|
|
182
182
|
return false unless node.ternary? || node.if?
|
183
183
|
|
184
184
|
cond = node.condition
|
185
|
-
|
186
|
-
|
187
|
-
end
|
185
|
+
return false unless cond.call_type?
|
186
|
+
return false if !cond.predicate_method? || allowed_method?(cond.method_name)
|
188
187
|
|
189
188
|
node.if_branch&.true_type? && node.else_branch && !node.else_branch.true_type?
|
190
189
|
end
|
@@ -20,20 +20,30 @@ module RuboCop
|
|
20
20
|
|
21
21
|
MSG = 'Remove the redundant current directory path.'
|
22
22
|
RESTRICT_ON_SEND = %i[require_relative].freeze
|
23
|
-
|
23
|
+
CURRENT_DIRECTORY_PREFIX = %r{./+}.freeze
|
24
|
+
REDUNDANT_CURRENT_DIRECTORY_PREFIX = /\A#{CURRENT_DIRECTORY_PREFIX}/.freeze
|
24
25
|
|
25
26
|
def on_send(node)
|
26
27
|
return unless (first_argument = node.first_argument)
|
27
|
-
return unless first_argument.
|
28
|
-
return unless (
|
28
|
+
return unless (index = first_argument.source.index(CURRENT_DIRECTORY_PREFIX))
|
29
|
+
return unless (redundant_length = redundant_path_length(first_argument.str_content))
|
29
30
|
|
30
31
|
begin_pos = first_argument.source_range.begin.begin_pos + index
|
31
|
-
|
32
|
+
end_pos = begin_pos + redundant_length
|
33
|
+
range = range_between(begin_pos, end_pos)
|
32
34
|
|
33
35
|
add_offense(range) do |corrector|
|
34
36
|
corrector.remove(range)
|
35
37
|
end
|
36
38
|
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def redundant_path_length(path)
|
43
|
+
return unless (match = path&.match(REDUNDANT_CURRENT_DIRECTORY_PREFIX))
|
44
|
+
|
45
|
+
match[0].length
|
46
|
+
end
|
37
47
|
end
|
38
48
|
end
|
39
49
|
end
|
@@ -5,8 +5,8 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# Checks for calls to `Kernel#format` or `Kernel#sprintf` that are redundant.
|
7
7
|
#
|
8
|
-
# Calling `format` with only a single string argument is redundant,
|
9
|
-
# replaced by the string itself.
|
8
|
+
# Calling `format` with only a single string or constant argument is redundant,
|
9
|
+
# as it can be replaced by the string or constant itself.
|
10
10
|
#
|
11
11
|
# Also looks for `format` calls where the arguments are literals that can be
|
12
12
|
# inlined into a string easily. This applies to the `%s`, `%d`, `%i`, `%u`, and
|
@@ -38,6 +38,13 @@ module RuboCop
|
|
38
38
|
# 'the quick brown fox jumps over the lazy dog.'
|
39
39
|
#
|
40
40
|
# # bad
|
41
|
+
# format(MESSAGE)
|
42
|
+
# sprintf(MESSAGE)
|
43
|
+
#
|
44
|
+
# # good
|
45
|
+
# MESSAGE
|
46
|
+
#
|
47
|
+
# # bad
|
41
48
|
# format('%s %s', 'foo', 'bar')
|
42
49
|
# sprintf('%s %s', 'foo', 'bar')
|
43
50
|
#
|
@@ -54,7 +61,7 @@ module RuboCop
|
|
54
61
|
|
55
62
|
# @!method format_without_additional_args?(node)
|
56
63
|
def_node_matcher :format_without_additional_args?, <<~PATTERN
|
57
|
-
(send {(const {nil? cbase} :Kernel) nil?} %RESTRICT_ON_SEND ${str dstr})
|
64
|
+
(send {(const {nil? cbase} :Kernel) nil?} %RESTRICT_ON_SEND ${str dstr const})
|
58
65
|
PATTERN
|
59
66
|
|
60
67
|
# @!method rational_number?(node)
|
@@ -60,7 +60,7 @@ module RuboCop
|
|
60
60
|
(begin (send !{(str _) array} {:+ :- :* :** :/ :%} {float int}))
|
61
61
|
(begin (send _ {:== :=== :!= :<= :>= :< :>} _))
|
62
62
|
(send _ {:count :length :size} ...)
|
63
|
-
(
|
63
|
+
(any_block (send _ {:count :length :size} ...) ...)
|
64
64
|
}
|
65
65
|
PATTERN
|
66
66
|
end
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
include Parentheses
|
18
18
|
extend AutoCorrector
|
19
19
|
|
20
|
-
ALLOWED_NODE_TYPES = %i[
|
20
|
+
ALLOWED_NODE_TYPES = %i[or send splat kwsplat].freeze
|
21
21
|
|
22
22
|
# @!method square_brackets?(node)
|
23
23
|
def_node_matcher :square_brackets?, <<~PATTERN
|
@@ -162,6 +162,7 @@ module RuboCop
|
|
162
162
|
return if node.semantic_operator? && begin_node.parent
|
163
163
|
return if node.multiline? && allow_in_multiline_conditions?
|
164
164
|
return if ALLOWED_NODE_TYPES.include?(begin_node.parent&.type)
|
165
|
+
return if !node.and_type? && begin_node.parent&.and_type?
|
165
166
|
return if begin_node.parent&.if_type? && begin_node.parent.ternary?
|
166
167
|
|
167
168
|
'a logical expression'
|
@@ -21,6 +21,7 @@ module RuboCop
|
|
21
21
|
|
22
22
|
MSG_BLOCK = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
|
23
23
|
MSG_NUMBLOCK = 'Use `sort` instead of `sort_by { _1 }`.'
|
24
|
+
MSG_ITBLOCK = 'Use `sort` instead of `sort_by { it }`.'
|
24
25
|
|
25
26
|
def on_block(node)
|
26
27
|
redundant_sort_by_block(node) do |send, var_name|
|
@@ -36,7 +37,17 @@ module RuboCop
|
|
36
37
|
redundant_sort_by_numblock(node) do |send|
|
37
38
|
range = sort_by_range(send, node)
|
38
39
|
|
39
|
-
add_offense(range, message:
|
40
|
+
add_offense(range, message: MSG_NUMBLOCK) do |corrector|
|
41
|
+
corrector.replace(range, 'sort')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def on_itblock(node)
|
47
|
+
redundant_sort_by_itblock(node) do |send|
|
48
|
+
range = sort_by_range(send, node)
|
49
|
+
|
50
|
+
add_offense(range, message: MSG_ITBLOCK) do |corrector|
|
40
51
|
corrector.replace(range, 'sort')
|
41
52
|
end
|
42
53
|
end
|
@@ -54,6 +65,11 @@ module RuboCop
|
|
54
65
|
(numblock $(call _ :sort_by) 1 (lvar :_1))
|
55
66
|
PATTERN
|
56
67
|
|
68
|
+
# @!method redundant_sort_by_itblock(node)
|
69
|
+
def_node_matcher :redundant_sort_by_itblock, <<~PATTERN
|
70
|
+
(itblock $(call _ :sort_by) _ (lvar :it))
|
71
|
+
PATTERN
|
72
|
+
|
57
73
|
def sort_by_range(send, node)
|
58
74
|
range_between(send.loc.selector.begin_pos, node.loc.end.end_pos)
|
59
75
|
end
|
@@ -67,11 +67,13 @@ module RuboCop
|
|
67
67
|
node.parent && parentheses?(node.parent)
|
68
68
|
end
|
69
69
|
|
70
|
+
# rubocop:disable Metrics/AbcSize
|
70
71
|
def correct_rescue_block(corrector, node, parenthesized)
|
71
72
|
operation = node.body
|
72
73
|
|
73
74
|
node_indentation, node_offset = indentation_and_offset(node, parenthesized)
|
74
75
|
|
76
|
+
corrector.wrap(operation, '[', ']') if operation.array_type? && !operation.bracketed?
|
75
77
|
corrector.remove(range_between(operation.source_range.end_pos, node.source_range.end_pos))
|
76
78
|
corrector.insert_before(operation, "begin\n#{node_indentation}")
|
77
79
|
corrector.insert_after(heredoc_end(operation) || operation, <<~RESCUE_CLAUSE.chop)
|
@@ -81,6 +83,7 @@ module RuboCop
|
|
81
83
|
#{node_offset}end
|
82
84
|
RESCUE_CLAUSE
|
83
85
|
end
|
86
|
+
# rubocop:enable Metrics/AbcSize
|
84
87
|
|
85
88
|
def indentation_and_offset(node, parenthesized)
|
86
89
|
node_indentation = indentation(node)
|
@@ -59,6 +59,7 @@ module RuboCop
|
|
59
59
|
{
|
60
60
|
(block call (args (arg $_)) ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
|
61
61
|
(numblock call $1 ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
|
62
|
+
(itblock call $_ ${(send _ %REGEXP_METHODS _) match-with-lvasgn})
|
62
63
|
}
|
63
64
|
PATTERN
|
64
65
|
|
@@ -137,6 +138,7 @@ module RuboCop
|
|
137
138
|
return unless (block_arg_name, regexp_method_send_node = regexp_match?(block_node))
|
138
139
|
|
139
140
|
block_arg_name = :"_#{block_arg_name}" if block_node.numblock_type?
|
141
|
+
|
140
142
|
return unless calls_lvar?(regexp_method_send_node, block_arg_name)
|
141
143
|
|
142
144
|
regexp_method_send_node
|
@@ -150,7 +152,8 @@ module RuboCop
|
|
150
152
|
return node.child_nodes.first if node.match_with_lvasgn_type?
|
151
153
|
|
152
154
|
if node.receiver.lvar_type? &&
|
153
|
-
(block.
|
155
|
+
(block.type?(:numblock, :itblock) ||
|
156
|
+
node.receiver.source == block.first_argument.source)
|
154
157
|
node.first_argument
|
155
158
|
elsif node.first_argument.lvar_type?
|
156
159
|
node.receiver
|
@@ -56,11 +56,13 @@ module RuboCop
|
|
56
56
|
end
|
57
57
|
# rubocop:enable Metrics/AbcSize
|
58
58
|
alias on_numblock on_block
|
59
|
+
alias on_itblock on_block
|
59
60
|
|
60
61
|
private
|
61
62
|
|
62
63
|
def do_line(node)
|
63
|
-
if node.
|
64
|
+
if node.type?(:numblock, :itblock) ||
|
65
|
+
node.arguments.children.empty? || node.send_node.lambda_literal?
|
64
66
|
node.loc.begin
|
65
67
|
else
|
66
68
|
node.arguments
|
@@ -96,11 +96,7 @@ module RuboCop
|
|
96
96
|
end
|
97
97
|
|
98
98
|
def autocorrect(corrector, node, if_branch)
|
99
|
-
if node.
|
100
|
-
corrector.wrap(node.condition, '(', ')')
|
101
|
-
end
|
102
|
-
|
103
|
-
if outer_condition_modify_form?(node, if_branch)
|
99
|
+
if node.modifier_form?
|
104
100
|
autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
105
101
|
else
|
106
102
|
autocorrect_outer_condition_basic(corrector, node, if_branch)
|
@@ -108,74 +104,48 @@ module RuboCop
|
|
108
104
|
end
|
109
105
|
|
110
106
|
def autocorrect_outer_condition_basic(corrector, node, if_branch)
|
111
|
-
|
112
|
-
|
113
|
-
outer_condition = node.condition
|
114
|
-
correct_outer_condition(corrector, outer_condition)
|
107
|
+
correct_node(corrector, node)
|
115
108
|
|
116
|
-
and_operator = if_branch.unless? ? ' && !' : ' && '
|
117
109
|
if if_branch.modifier_form?
|
118
|
-
correct_for_guard_condition_style(corrector,
|
110
|
+
correct_for_guard_condition_style(corrector, node, if_branch)
|
119
111
|
else
|
120
|
-
correct_for_basic_condition_style(corrector, node, if_branch
|
112
|
+
correct_for_basic_condition_style(corrector, node, if_branch)
|
121
113
|
correct_for_comment(corrector, node, if_branch)
|
122
114
|
end
|
123
115
|
end
|
124
116
|
|
125
|
-
def
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
def correct_from_unless_to_if(corrector, node, is_modify_form: false)
|
131
|
-
corrector.replace(node.loc.keyword, 'if')
|
132
|
-
|
133
|
-
insert_bang(corrector, node, is_modify_form)
|
117
|
+
def correct_node(corrector, node)
|
118
|
+
corrector.replace(node.loc.keyword, 'if') if node.unless?
|
119
|
+
corrector.replace(node.condition, chainable_condition(node))
|
134
120
|
end
|
135
121
|
|
136
|
-
def correct_for_guard_condition_style(corrector,
|
137
|
-
condition
|
138
|
-
corrector.insert_after(outer_condition, "#{and_operator}#{replace_condition(condition)}")
|
122
|
+
def correct_for_guard_condition_style(corrector, node, if_branch)
|
123
|
+
corrector.insert_after(node.condition, " && #{chainable_condition(if_branch)}")
|
139
124
|
|
140
|
-
range = range_between(
|
125
|
+
range = range_between(
|
126
|
+
if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos
|
127
|
+
)
|
141
128
|
corrector.remove(range_with_surrounding_space(range, newlines: false))
|
142
|
-
corrector.remove(if_branch.loc.keyword)
|
143
129
|
end
|
144
130
|
|
145
|
-
def correct_for_basic_condition_style(corrector, node, if_branch
|
131
|
+
def correct_for_basic_condition_style(corrector, node, if_branch)
|
146
132
|
range = range_between(
|
147
133
|
node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
|
148
134
|
)
|
149
|
-
corrector.replace(range,
|
150
|
-
corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
|
135
|
+
corrector.replace(range, ' && ')
|
151
136
|
|
152
|
-
|
153
|
-
end
|
137
|
+
corrector.replace(if_branch.condition, chainable_condition(if_branch))
|
154
138
|
|
155
|
-
|
156
|
-
# Handle `send` and `block` nodes that need to be wrapped in parens
|
157
|
-
# FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
|
158
|
-
# but wrapping the argument list would be a more ergonomic correction.
|
159
|
-
node_to_check = condition&.any_block_type? ? condition.send_node : condition
|
160
|
-
return unless wrap_condition?(node_to_check)
|
161
|
-
|
162
|
-
if condition.call_type?
|
163
|
-
source = parenthesized_method_arguments(condition)
|
164
|
-
|
165
|
-
corrector.replace(condition, source)
|
166
|
-
else
|
167
|
-
corrector.wrap(condition, '(', ')')
|
168
|
-
end
|
139
|
+
corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
|
169
140
|
end
|
170
141
|
|
171
|
-
def
|
172
|
-
|
173
|
-
corrector.insert_before(condition,
|
174
|
-
"#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
|
142
|
+
def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
143
|
+
correct_node(corrector, if_branch)
|
175
144
|
|
176
|
-
corrector.
|
177
|
-
|
178
|
-
|
145
|
+
corrector.insert_before(if_branch.condition, "#{chainable_condition(node)} && ")
|
146
|
+
|
147
|
+
range = range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos)
|
148
|
+
corrector.remove(range_with_surrounding_space(range, newlines: false))
|
179
149
|
end
|
180
150
|
|
181
151
|
def correct_for_comment(corrector, node, if_branch)
|
@@ -187,67 +157,36 @@ module RuboCop
|
|
187
157
|
corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
|
188
158
|
end
|
189
159
|
|
190
|
-
def
|
191
|
-
|
160
|
+
def chainable_condition(node)
|
161
|
+
wrapped_condition = add_parentheses_if_needed(node.condition)
|
192
162
|
|
193
|
-
|
194
|
-
begin_pos = condition.first_argument.source_range.begin_pos
|
195
|
-
return if end_pos > begin_pos
|
163
|
+
return wrapped_condition if node.if?
|
196
164
|
|
197
|
-
|
198
|
-
corrector.remove(range)
|
199
|
-
corrector.insert_after(range, '(')
|
200
|
-
corrector.insert_after(condition.last_argument, ')')
|
165
|
+
node.condition.and_type? ? "!(#{wrapped_condition})" : "!#{wrapped_condition}"
|
201
166
|
end
|
202
167
|
|
203
|
-
def
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
elsif condition.and_type?
|
210
|
-
insert_bang_for_and(corrector, node)
|
211
|
-
else
|
212
|
-
corrector.insert_before(condition, '!')
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
def insert_bang_for_and(corrector, node)
|
217
|
-
lhs, rhs = *node # rubocop:disable InternalAffairs/NodeDestructuring
|
168
|
+
def add_parentheses_if_needed(condition)
|
169
|
+
# Handle `send` and `block` nodes that need to be wrapped in parens
|
170
|
+
# FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
|
171
|
+
# but wrapping the argument list would be a more ergonomic correction.
|
172
|
+
node_to_check = condition&.any_block_type? ? condition.send_node : condition
|
173
|
+
return condition.source unless add_parentheses?(node_to_check)
|
218
174
|
|
219
|
-
if
|
220
|
-
|
221
|
-
corrector.insert_before(rhs, '!') if rhs
|
175
|
+
if parenthesize_method?(condition)
|
176
|
+
parenthesized_method_arguments(condition)
|
222
177
|
else
|
223
|
-
|
224
|
-
corrector.insert_before(rhs, '!')
|
178
|
+
"(#{condition.source})"
|
225
179
|
end
|
226
180
|
end
|
227
181
|
|
228
|
-
def
|
229
|
-
|
230
|
-
!
|
182
|
+
def parenthesize_method?(node)
|
183
|
+
node.call_type? && node.arguments.any? && !node.parenthesized? &&
|
184
|
+
!node.comparison_method? && !node.operator_method?
|
231
185
|
end
|
232
186
|
|
233
|
-
def
|
234
|
-
|
235
|
-
node.
|
236
|
-
)
|
237
|
-
end
|
238
|
-
|
239
|
-
def wrap_condition?(node)
|
240
|
-
node.operator_keyword? || (node.call_type? && node.arguments.any? && !node.parenthesized?)
|
241
|
-
end
|
242
|
-
|
243
|
-
def replace_condition(condition)
|
244
|
-
return condition.source unless wrap_condition?(condition)
|
245
|
-
|
246
|
-
if condition.call_type? && !condition.comparison_method?
|
247
|
-
parenthesized_method_arguments(condition)
|
248
|
-
else
|
249
|
-
"(#{condition.source})"
|
250
|
-
end
|
187
|
+
def add_parentheses?(node)
|
188
|
+
node.assignment? || (node.operator_keyword? && !node.and_type?) ||
|
189
|
+
(node.call_type? && node.arguments.any? && !node.parenthesized?)
|
251
190
|
end
|
252
191
|
|
253
192
|
def parenthesized_method_arguments(node)
|
@@ -260,10 +199,6 @@ module RuboCop
|
|
260
199
|
def allow_modifier?
|
261
200
|
cop_config['AllowModifier']
|
262
201
|
end
|
263
|
-
|
264
|
-
def outer_condition_modify_form?(node, if_branch)
|
265
|
-
node.condition.source_range.begin_pos > if_branch.condition.source_range.begin_pos
|
266
|
-
end
|
267
202
|
end
|
268
203
|
end
|
269
204
|
end
|
@@ -159,6 +159,7 @@ module RuboCop
|
|
159
159
|
{
|
160
160
|
(block $#symbol_proc_receiver? $(args (arg _var)) (send (lvar _var) $_))
|
161
161
|
(numblock $#symbol_proc_receiver? $1 (send (lvar :_1) $_))
|
162
|
+
(itblock $#symbol_proc_receiver? $_ (send (lvar :it) $_))
|
162
163
|
}
|
163
164
|
PATTERN
|
164
165
|
|
@@ -185,6 +186,7 @@ module RuboCop
|
|
185
186
|
end
|
186
187
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
187
188
|
alias on_numblock on_block
|
189
|
+
alias on_itblock on_block
|
188
190
|
|
189
191
|
def destructuring_block_argument?(argument_node)
|
190
192
|
argument_node.one? && argument_node.source.include?(',')
|
@@ -5,8 +5,11 @@ module RuboCop
|
|
5
5
|
module Utils
|
6
6
|
# Parses {Kernel#sprintf} format strings.
|
7
7
|
class FormatString
|
8
|
+
# Escaping the `#` in `INTERPOLATION` and `TEMPLATE_NAME` is necessary to
|
9
|
+
# avoid a bug in Ruby 3.2.0
|
10
|
+
# See: https://bugs.ruby-lang.org/issues/19379
|
8
11
|
DIGIT_DOLLAR = /(?<arg_number>\d+)\$/.freeze
|
9
|
-
INTERPOLATION =
|
12
|
+
INTERPOLATION = /\#\{.*?\}/.freeze
|
10
13
|
FLAG = /[ #0+-]|#{DIGIT_DOLLAR}/.freeze
|
11
14
|
NUMBER_ARG = /\*#{DIGIT_DOLLAR}?/.freeze
|
12
15
|
NUMBER = /\d+|#{NUMBER_ARG}|#{INTERPOLATION}/.freeze
|
@@ -14,7 +17,7 @@ module RuboCop
|
|
14
17
|
PRECISION = /\.(?<precision>#{NUMBER}?)/.freeze
|
15
18
|
TYPE = /(?<type>[bBdiouxXeEfgGaAcps])/.freeze
|
16
19
|
NAME = /<(?<name>\w+)>/.freeze
|
17
|
-
TEMPLATE_NAME = /(
|
20
|
+
TEMPLATE_NAME = /(?<!\#)\{(?<name>\w+)\}/.freeze
|
18
21
|
|
19
22
|
SEQUENCE = /
|
20
23
|
% (?<type>%)
|