rubocop 1.72.2 → 1.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/config/default.yml +21 -11
- data/config/internal_affairs.yml +16 -0
- data/lib/rubocop/config_loader_resolver.rb +2 -2
- data/lib/rubocop/config_validator.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +26 -1
- data/lib/rubocop/cop/layout/line_length.rb +3 -3
- data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -14
- data/lib/rubocop/cop/lint/float_comparison.rb +1 -6
- data/lib/rubocop/cop/lint/literal_as_condition.rb +104 -7
- data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +23 -2
- data/lib/rubocop/cop/lint/void.rb +6 -0
- data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
- data/lib/rubocop/cop/mixin/trailing_comma.rb +12 -0
- data/lib/rubocop/cop/naming/variable_name.rb +64 -6
- data/lib/rubocop/cop/style/accessor_grouping.rb +19 -5
- data/lib/rubocop/cop/style/endless_method.rb +163 -18
- data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +34 -0
- data/lib/rubocop/cop/style/redundant_format.rb +23 -11
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
- data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cops_documentation_generator.rb +12 -1
- data/lib/rubocop/plugin/load_error.rb +1 -1
- data/lib/rubocop/plugin.rb +9 -2
- data/lib/rubocop/rspec/shared_contexts.rb +15 -0
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +0 -1
- metadata +4 -5
- data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
@@ -4,6 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
# Common methods shared by Style/TrailingCommaInArguments,
|
6
6
|
# Style/TrailingCommaInArrayLiteral and Style/TrailingCommaInHashLiteral
|
7
|
+
# rubocop:disable Metrics/ModuleLength
|
7
8
|
module TrailingComma
|
8
9
|
include ConfigurableEnforcedStyle
|
9
10
|
include RangeHelp
|
@@ -57,6 +58,8 @@ module RuboCop
|
|
57
58
|
', unless each item is on its own line'
|
58
59
|
when :consistent_comma
|
59
60
|
', unless items are split onto multiple lines'
|
61
|
+
when :diff_comma
|
62
|
+
', unless that item immediately precedes a newline'
|
60
63
|
else
|
61
64
|
''
|
62
65
|
end
|
@@ -68,6 +71,8 @@ module RuboCop
|
|
68
71
|
multiline?(node) && no_elements_on_same_line?(node)
|
69
72
|
when :consistent_comma
|
70
73
|
multiline?(node) && !method_name_and_arguments_on_same_line?(node)
|
74
|
+
when :diff_comma
|
75
|
+
multiline?(node) && last_item_precedes_newline?(node)
|
71
76
|
else
|
72
77
|
false
|
73
78
|
end
|
@@ -130,6 +135,12 @@ module RuboCop
|
|
130
135
|
range1.last_line == range2.line
|
131
136
|
end
|
132
137
|
|
138
|
+
def last_item_precedes_newline?(node)
|
139
|
+
after_last_item =
|
140
|
+
range_between(node.children.last.source_range.end_pos, node.loc.end.begin_pos)
|
141
|
+
after_last_item.source =~ /\A,?\s*\n/
|
142
|
+
end
|
143
|
+
|
133
144
|
def avoid_comma(kind, comma_begin_pos, extra_info)
|
134
145
|
range = range_between(comma_begin_pos, comma_begin_pos + 1)
|
135
146
|
article = kind.include?('array') ? 'an' : 'a'
|
@@ -205,5 +216,6 @@ module RuboCop
|
|
205
216
|
false
|
206
217
|
end
|
207
218
|
end
|
219
|
+
# rubocop:enable Metrics/ModuleLength
|
208
220
|
end
|
209
221
|
end
|
@@ -3,8 +3,15 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Naming
|
6
|
-
#
|
7
|
-
#
|
6
|
+
# Checks that the configured style (snake_case or camelCase) is used for all variable names.
|
7
|
+
# This includes local variables, instance variables, class variables, method arguments
|
8
|
+
# (positional, keyword, rest or block), and block arguments.
|
9
|
+
#
|
10
|
+
# The cop can also be configured to forbid using specific names for variables, using
|
11
|
+
# `ForbiddenIdentifiers` or `ForbiddenPatterns`. In addition to the above, this applies
|
12
|
+
# to global variables as well.
|
13
|
+
#
|
14
|
+
# Method definitions and method calls are not affected by this cop.
|
8
15
|
#
|
9
16
|
# @example EnforcedStyle: snake_case (default)
|
10
17
|
# # bad
|
@@ -26,7 +33,21 @@ module RuboCop
|
|
26
33
|
#
|
27
34
|
# @example AllowedPatterns: ['_v\d+\z']
|
28
35
|
# # good (with EnforcedStyle: camelCase)
|
29
|
-
#
|
36
|
+
# release_v1 = true
|
37
|
+
#
|
38
|
+
# @example ForbiddenIdentifiers: ['fooBar']
|
39
|
+
# # bad (in all cases)
|
40
|
+
# fooBar = 1
|
41
|
+
# @fooBar = 1
|
42
|
+
# @@fooBar = 1
|
43
|
+
# $fooBar = 1
|
44
|
+
#
|
45
|
+
# @example ForbiddenPatterns: ['_v\d+\z']
|
46
|
+
# # bad (in all cases)
|
47
|
+
# release_v1 = true
|
48
|
+
# @release_v1 = true
|
49
|
+
# @@release_v1 = true
|
50
|
+
# $release_v1 = true
|
30
51
|
#
|
31
52
|
class VariableName < Base
|
32
53
|
include AllowedIdentifiers
|
@@ -34,16 +55,21 @@ module RuboCop
|
|
34
55
|
include AllowedPattern
|
35
56
|
|
36
57
|
MSG = 'Use %<style>s for variable names.'
|
58
|
+
MSG_FORBIDDEN = '`%<identifier>s` is forbidden, use another name instead.'
|
37
59
|
|
38
60
|
def valid_name?(node, name, given_style = style)
|
39
61
|
super || matches_allowed_pattern?(name)
|
40
62
|
end
|
41
63
|
|
42
64
|
def on_lvasgn(node)
|
43
|
-
return unless node.name
|
44
|
-
return if allowed_identifier?(
|
65
|
+
return unless (name = node.name)
|
66
|
+
return if allowed_identifier?(name)
|
45
67
|
|
46
|
-
|
68
|
+
if forbidden_name?(name)
|
69
|
+
register_forbidden_name(node)
|
70
|
+
else
|
71
|
+
check_name(node, name, node.loc.name)
|
72
|
+
end
|
47
73
|
end
|
48
74
|
alias on_ivasgn on_lvasgn
|
49
75
|
alias on_cvasgn on_lvasgn
|
@@ -56,11 +82,43 @@ module RuboCop
|
|
56
82
|
alias on_blockarg on_lvasgn
|
57
83
|
alias on_lvar on_lvasgn
|
58
84
|
|
85
|
+
# Only forbidden names are checked for global variable assignment
|
86
|
+
def on_gvasgn(node)
|
87
|
+
return unless (name = node.name)
|
88
|
+
return unless forbidden_name?(name)
|
89
|
+
|
90
|
+
register_forbidden_name(node)
|
91
|
+
end
|
92
|
+
|
59
93
|
private
|
60
94
|
|
61
95
|
def message(style)
|
62
96
|
format(MSG, style: style)
|
63
97
|
end
|
98
|
+
|
99
|
+
def forbidden_identifiers
|
100
|
+
cop_config.fetch('ForbiddenIdentifiers', [])
|
101
|
+
end
|
102
|
+
|
103
|
+
def forbidden_patterns
|
104
|
+
cop_config.fetch('ForbiddenPatterns', [])
|
105
|
+
end
|
106
|
+
|
107
|
+
def matches_forbidden_pattern?(name)
|
108
|
+
forbidden_patterns.any? { |pattern| Regexp.new(pattern).match?(name) }
|
109
|
+
end
|
110
|
+
|
111
|
+
def forbidden_name?(name)
|
112
|
+
name = name.to_s.delete(SIGILS)
|
113
|
+
|
114
|
+
(forbidden_identifiers.any? && forbidden_identifiers.include?(name)) ||
|
115
|
+
(forbidden_patterns.any? && matches_forbidden_pattern?(name))
|
116
|
+
end
|
117
|
+
|
118
|
+
def register_forbidden_name(node)
|
119
|
+
message = format(MSG_FORBIDDEN, identifier: node.name)
|
120
|
+
add_offense(node.loc.name, message: message)
|
121
|
+
end
|
64
122
|
end
|
65
123
|
end
|
66
124
|
end
|
@@ -139,12 +139,16 @@ module RuboCop
|
|
139
139
|
style == :separated
|
140
140
|
end
|
141
141
|
|
142
|
+
def groupable_sibling_accessor?(node, sibling)
|
143
|
+
sibling.attribute_accessor? &&
|
144
|
+
sibling.method?(node.method_name) &&
|
145
|
+
node_visibility(sibling) == node_visibility(node) &&
|
146
|
+
groupable_accessor?(sibling) && !previous_line_comment?(sibling)
|
147
|
+
end
|
148
|
+
|
142
149
|
def groupable_sibling_accessors(send_node)
|
143
150
|
send_node.parent.each_child_node(:send).select do |sibling|
|
144
|
-
|
145
|
-
sibling.method?(send_node.method_name) &&
|
146
|
-
node_visibility(sibling) == node_visibility(send_node) &&
|
147
|
-
groupable_accessor?(sibling) && !previous_line_comment?(sibling)
|
151
|
+
groupable_sibling_accessor?(send_node, sibling)
|
148
152
|
end
|
149
153
|
end
|
150
154
|
|
@@ -155,13 +159,23 @@ module RuboCop
|
|
155
159
|
|
156
160
|
def preferred_accessors(node)
|
157
161
|
if grouped_style?
|
162
|
+
return if skip_for_grouping?(node)
|
163
|
+
|
158
164
|
accessors = groupable_sibling_accessors(node)
|
159
|
-
|
165
|
+
if node.loc == accessors.first.loc || skip_for_grouping?(accessors.first)
|
166
|
+
group_accessors(node, accessors)
|
167
|
+
end
|
160
168
|
else
|
161
169
|
separate_accessors(node)
|
162
170
|
end
|
163
171
|
end
|
164
172
|
|
173
|
+
# Group after constants
|
174
|
+
def skip_for_grouping?(node)
|
175
|
+
node.right_siblings.any?(&:casgn_type?) &&
|
176
|
+
node.right_siblings.any? { |n| n.send_type? && groupable_sibling_accessor?(node, n) }
|
177
|
+
end
|
178
|
+
|
165
179
|
def group_accessors(node, accessors)
|
166
180
|
accessor_names = accessors.flat_map { |accessor| accessor.arguments.map(&:source) }.uniq
|
167
181
|
|
@@ -5,8 +5,11 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# Checks for endless methods.
|
7
7
|
#
|
8
|
-
# It can enforce
|
9
|
-
#
|
8
|
+
# It can enforce endless method definitions whenever possible or with single line methods.
|
9
|
+
# It can also disallow multiline endless method definitions or all endless definitions.
|
10
|
+
#
|
11
|
+
# `require_single_line` style enforces endless method definitions for single line methods.
|
12
|
+
# `require_always` style enforces endless method definitions for single statement methods.
|
10
13
|
#
|
11
14
|
# Other method definition types are not considered by this cop.
|
12
15
|
#
|
@@ -15,36 +18,116 @@ module RuboCop
|
|
15
18
|
# * allow_single_line (default) - only single line endless method definitions are allowed.
|
16
19
|
# * allow_always - all endless method definitions are allowed.
|
17
20
|
# * disallow - all endless method definitions are disallowed.
|
21
|
+
# * require_single_line - endless method definitions are required for single line methods.
|
22
|
+
# * require_always - all endless method definitions are required.
|
18
23
|
#
|
19
24
|
# NOTE: Incorrect endless method definitions will always be
|
20
25
|
# corrected to a multi-line definition.
|
21
26
|
#
|
22
27
|
# @example EnforcedStyle: allow_single_line (default)
|
28
|
+
# # bad, multi-line endless method
|
29
|
+
# def my_method = x.foo
|
30
|
+
# .bar
|
31
|
+
# .baz
|
32
|
+
#
|
23
33
|
# # good
|
24
|
-
# def my_method
|
34
|
+
# def my_method
|
35
|
+
# x
|
36
|
+
# end
|
25
37
|
#
|
26
|
-
# #
|
27
|
-
# def my_method
|
28
|
-
#
|
29
|
-
#
|
38
|
+
# # good
|
39
|
+
# def my_method = x
|
40
|
+
#
|
41
|
+
# # good
|
42
|
+
# def my_method
|
43
|
+
# x.foo
|
44
|
+
# .bar
|
45
|
+
# .baz
|
46
|
+
# end
|
30
47
|
#
|
31
48
|
# @example EnforcedStyle: allow_always
|
32
49
|
# # good
|
33
|
-
# def my_method
|
50
|
+
# def my_method
|
51
|
+
# x
|
52
|
+
# end
|
34
53
|
#
|
35
54
|
# # good
|
36
|
-
# def my_method
|
37
|
-
#
|
38
|
-
#
|
55
|
+
# def my_method = x
|
56
|
+
#
|
57
|
+
# # good
|
58
|
+
# def my_method = x.foo
|
59
|
+
# .bar
|
60
|
+
# .baz
|
61
|
+
#
|
62
|
+
# # good
|
63
|
+
# def my_method
|
64
|
+
# x.foo
|
65
|
+
# .bar
|
66
|
+
# .baz
|
67
|
+
# end
|
39
68
|
#
|
40
69
|
# @example EnforcedStyle: disallow
|
41
70
|
# # bad
|
42
|
-
# def my_method
|
71
|
+
# def my_method = x
|
72
|
+
#
|
73
|
+
# # bad
|
74
|
+
# def my_method = x.foo
|
75
|
+
# .bar
|
76
|
+
# .baz
|
77
|
+
#
|
78
|
+
# # good
|
79
|
+
# def my_method
|
80
|
+
# x
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# # good
|
84
|
+
# def my_method
|
85
|
+
# x.foo
|
86
|
+
# .bar
|
87
|
+
# .baz
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# @example EnforcedStyle: require_single_line
|
91
|
+
# # bad
|
92
|
+
# def my_method
|
93
|
+
# x
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# # bad
|
97
|
+
# def my_method = x.foo
|
98
|
+
# .bar
|
99
|
+
# .baz
|
100
|
+
#
|
101
|
+
# # good
|
102
|
+
# def my_method = x
|
103
|
+
#
|
104
|
+
# # good
|
105
|
+
# def my_method
|
106
|
+
# x.foo
|
107
|
+
# .bar
|
108
|
+
# .baz
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# @example EnforcedStyle: require_always
|
112
|
+
# # bad
|
113
|
+
# def my_method
|
114
|
+
# x
|
115
|
+
# end
|
43
116
|
#
|
44
117
|
# # bad
|
45
|
-
# def my_method
|
46
|
-
#
|
47
|
-
#
|
118
|
+
# def my_method
|
119
|
+
# x.foo
|
120
|
+
# .bar
|
121
|
+
# .baz
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# # good
|
125
|
+
# def my_method = x
|
126
|
+
#
|
127
|
+
# # good
|
128
|
+
# def my_method = x.foo
|
129
|
+
# .bar
|
130
|
+
# .baz
|
48
131
|
#
|
49
132
|
class EndlessMethod < Base
|
50
133
|
include ConfigurableEnforcedStyle
|
@@ -57,12 +140,21 @@ module RuboCop
|
|
57
140
|
CORRECTION_STYLES = %w[multiline single_line].freeze
|
58
141
|
MSG = 'Avoid endless method definitions.'
|
59
142
|
MSG_MULTI_LINE = 'Avoid endless method definitions with multiple lines.'
|
143
|
+
MSG_REQUIRE_SINGLE = 'Use endless method definitions for single line methods.'
|
144
|
+
MSG_REQUIRE_ALWAYS = 'Use endless method definitions.'
|
60
145
|
|
61
146
|
def on_def(node)
|
62
|
-
if
|
63
|
-
|
64
|
-
|
147
|
+
return if node.assignment_method?
|
148
|
+
|
149
|
+
case style
|
150
|
+
when :allow_single_line, :allow_always
|
65
151
|
handle_allow_style(node)
|
152
|
+
when :disallow
|
153
|
+
handle_disallow_style(node)
|
154
|
+
when :require_single_line
|
155
|
+
handle_require_single_line_style(node)
|
156
|
+
when :require_always
|
157
|
+
handle_require_always_style(node)
|
66
158
|
end
|
67
159
|
end
|
68
160
|
|
@@ -77,11 +169,64 @@ module RuboCop
|
|
77
169
|
end
|
78
170
|
end
|
79
171
|
|
172
|
+
def handle_require_single_line_style(node)
|
173
|
+
if node.endless? && !node.single_line?
|
174
|
+
add_offense(node, message: MSG_MULTI_LINE) do |corrector|
|
175
|
+
correct_to_multiline(corrector, node)
|
176
|
+
end
|
177
|
+
elsif !node.endless? && can_be_made_endless?(node) && node.body.single_line?
|
178
|
+
return if too_long_when_made_endless?(node)
|
179
|
+
|
180
|
+
add_offense(node, message: MSG_REQUIRE_SINGLE) do |corrector|
|
181
|
+
corrector.replace(node, endless_replacement(node))
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def handle_require_always_style(node)
|
187
|
+
return if node.endless? || !can_be_made_endless?(node)
|
188
|
+
return if too_long_when_made_endless?(node)
|
189
|
+
|
190
|
+
add_offense(node, message: MSG_REQUIRE_ALWAYS) do |corrector|
|
191
|
+
corrector.replace(node, endless_replacement(node))
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
80
195
|
def handle_disallow_style(node)
|
81
196
|
return unless node.endless?
|
82
197
|
|
83
198
|
add_offense(node) { |corrector| correct_to_multiline(corrector, node) }
|
84
199
|
end
|
200
|
+
|
201
|
+
def correct_to_multiline(corrector, node)
|
202
|
+
replacement = <<~RUBY.strip
|
203
|
+
def #{node.method_name}#{arguments(node)}
|
204
|
+
#{node.body.source}
|
205
|
+
end
|
206
|
+
RUBY
|
207
|
+
|
208
|
+
corrector.replace(node, replacement)
|
209
|
+
end
|
210
|
+
|
211
|
+
def endless_replacement(node)
|
212
|
+
<<~RUBY.strip
|
213
|
+
def #{node.method_name}#{arguments(node)} = #{node.body.source}
|
214
|
+
RUBY
|
215
|
+
end
|
216
|
+
|
217
|
+
def arguments(node, missing = '')
|
218
|
+
node.arguments.any? ? node.arguments.source : missing
|
219
|
+
end
|
220
|
+
|
221
|
+
def can_be_made_endless?(node)
|
222
|
+
node.body && !node.body.begin_type? && !node.body.kwbegin_type?
|
223
|
+
end
|
224
|
+
|
225
|
+
def too_long_when_made_endless?(node)
|
226
|
+
return false unless config.cop_enabled?('Layout/LineLength')
|
227
|
+
|
228
|
+
endless_replacement(node).length > config.for_cop('Layout/LineLength')['Max']
|
229
|
+
end
|
85
230
|
end
|
86
231
|
end
|
87
232
|
end
|
@@ -36,7 +36,7 @@ module RuboCop
|
|
36
36
|
include RangeHelp
|
37
37
|
extend AutoCorrector
|
38
38
|
|
39
|
-
MSG = 'Use `\\` instead of
|
39
|
+
MSG = 'Use `\\` instead of `%<operator>s` to concatenate multiline strings.'
|
40
40
|
CONCAT_TOKEN_TYPES = %i[tPLUS tLSHFT].freeze
|
41
41
|
SIMPLE_STRING_TOKEN_TYPE = :tSTRING
|
42
42
|
COMPLEX_STRING_BEGIN_TOKEN = :tSTRING_BEG
|
@@ -61,14 +61,20 @@ module RuboCop
|
|
61
61
|
successor = tokens[index + 2]
|
62
62
|
|
63
63
|
return unless eligible_token_set?(predecessor, operator, successor)
|
64
|
-
|
65
64
|
return if same_line?(operator, successor)
|
66
65
|
|
67
66
|
next_successor = token_after_last_string(successor, index)
|
68
|
-
|
69
67
|
return unless eligible_next_successor?(next_successor)
|
70
68
|
|
71
|
-
|
69
|
+
register_offense(operator)
|
70
|
+
end
|
71
|
+
|
72
|
+
def register_offense(operator)
|
73
|
+
message = format(MSG, operator: operator.text)
|
74
|
+
|
75
|
+
add_offense(operator.pos, message: message) do |corrector|
|
76
|
+
autocorrect(corrector, operator.pos)
|
77
|
+
end
|
72
78
|
end
|
73
79
|
|
74
80
|
def autocorrect(corrector, operator_range)
|
@@ -41,7 +41,7 @@ module RuboCop
|
|
41
41
|
return if ignored_node?(node)
|
42
42
|
|
43
43
|
return unless (receiver = node.receiver)
|
44
|
-
return unless receiver.any_block_type? && receiver.
|
44
|
+
return unless receiver.any_block_type? && receiver.keywords?
|
45
45
|
|
46
46
|
range = range_between(receiver.loc.end.begin_pos, node.source_range.end_pos)
|
47
47
|
|
@@ -42,6 +42,22 @@ module RuboCop
|
|
42
42
|
# c
|
43
43
|
# end
|
44
44
|
#
|
45
|
+
# # bad
|
46
|
+
# a.nil? ? true : a
|
47
|
+
#
|
48
|
+
# # good
|
49
|
+
# a.nil? || a
|
50
|
+
#
|
51
|
+
# # bad
|
52
|
+
# if a.nil?
|
53
|
+
# true
|
54
|
+
# else
|
55
|
+
# a
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# # good
|
59
|
+
# a.nil? || a
|
60
|
+
#
|
45
61
|
class RedundantCondition < Base
|
46
62
|
include CommentsHelp
|
47
63
|
include RangeHelp
|
@@ -128,6 +144,16 @@ module RuboCop
|
|
128
144
|
# end
|
129
145
|
return true if condition == if_branch
|
130
146
|
|
147
|
+
# e.g.
|
148
|
+
# a.nil? ? true : a
|
149
|
+
# or
|
150
|
+
# if a.nil?
|
151
|
+
# true
|
152
|
+
# else
|
153
|
+
# a
|
154
|
+
# end
|
155
|
+
return true if if_branch_is_true_type_and_else_is_not?(node)
|
156
|
+
|
131
157
|
# e.g.
|
132
158
|
# if foo
|
133
159
|
# @value = foo
|
@@ -146,6 +172,12 @@ module RuboCop
|
|
146
172
|
!use_hash_key_access?(if_branch)
|
147
173
|
end
|
148
174
|
|
175
|
+
def if_branch_is_true_type_and_else_is_not?(node)
|
176
|
+
return false unless node.ternary? || node.if?
|
177
|
+
|
178
|
+
node.if_branch&.true_type? && node.else_branch && !node.else_branch.true_type?
|
179
|
+
end
|
180
|
+
|
149
181
|
def branches_have_assignment?(node)
|
150
182
|
_condition, if_branch, else_branch = *node # rubocop:disable InternalAffairs/NodeDestructuring
|
151
183
|
|
@@ -194,6 +226,8 @@ module RuboCop
|
|
194
226
|
argument_source = if_branch.first_argument.source
|
195
227
|
|
196
228
|
"#{if_branch.receiver.source} #{if_branch.method_name} (#{argument_source}"
|
229
|
+
elsif if_branch.true_type?
|
230
|
+
if_branch.parent.condition.source
|
197
231
|
else
|
198
232
|
if_branch.source
|
199
233
|
end
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
47
47
|
class RedundantFormat < Base
|
48
48
|
extend AutoCorrector
|
49
49
|
|
50
|
-
MSG = '
|
50
|
+
MSG = 'Use `%<prefer>s` directly instead of `%<method_name>s`.'
|
51
51
|
|
52
52
|
RESTRICT_ON_SEND = %i[format sprintf].to_set.freeze
|
53
53
|
ACCEPTABLE_LITERAL_TYPES = %i[str dstr sym dsym numeric boolean nil].freeze
|
@@ -72,10 +72,20 @@ module RuboCop
|
|
72
72
|
(pair (sym %1) $_)
|
73
73
|
PATTERN
|
74
74
|
|
75
|
+
# @!method splatted_arguments?(node)
|
76
|
+
def_node_matcher :splatted_arguments?, <<~PATTERN
|
77
|
+
(send _ %RESTRICT_ON_SEND <{
|
78
|
+
splat
|
79
|
+
(hash <kwsplat ...>)
|
80
|
+
} ...>)
|
81
|
+
PATTERN
|
82
|
+
|
75
83
|
def on_send(node)
|
76
84
|
format_without_additional_args?(node) do |value|
|
77
|
-
|
78
|
-
|
85
|
+
replacement = value.source
|
86
|
+
|
87
|
+
add_offense(node, message: message(node, replacement)) do |corrector|
|
88
|
+
corrector.replace(node, replacement)
|
79
89
|
end
|
80
90
|
return
|
81
91
|
end
|
@@ -85,8 +95,8 @@ module RuboCop
|
|
85
95
|
|
86
96
|
private
|
87
97
|
|
88
|
-
def message(node)
|
89
|
-
format(MSG, method_name: node.method_name)
|
98
|
+
def message(node, prefer)
|
99
|
+
format(MSG, prefer: prefer, method_name: node.method_name)
|
90
100
|
end
|
91
101
|
|
92
102
|
def detect_unnecessary_fields(node)
|
@@ -96,7 +106,7 @@ module RuboCop
|
|
96
106
|
arguments = node.arguments[1..]
|
97
107
|
|
98
108
|
return unless string && arguments.any?
|
99
|
-
return if
|
109
|
+
return if splatted_arguments?(node)
|
100
110
|
|
101
111
|
register_all_fields_literal(node, string, arguments)
|
102
112
|
end
|
@@ -104,9 +114,11 @@ module RuboCop
|
|
104
114
|
def register_all_fields_literal(node, string, arguments)
|
105
115
|
return unless all_fields_literal?(string, arguments.dup)
|
106
116
|
|
107
|
-
|
108
|
-
|
109
|
-
|
117
|
+
formatted_string = format(string, *argument_values(arguments))
|
118
|
+
replacement = quote(formatted_string, node)
|
119
|
+
|
120
|
+
add_offense(node, message: message(node, replacement)) do |corrector|
|
121
|
+
corrector.replace(node, replacement)
|
110
122
|
end
|
111
123
|
end
|
112
124
|
|
@@ -119,7 +131,7 @@ module RuboCop
|
|
119
131
|
next if sequence.percent?
|
120
132
|
|
121
133
|
hash = arguments.detect(&:hash_type?)
|
122
|
-
argument = find_argument(sequence, arguments, hash)
|
134
|
+
next unless (argument = find_argument(sequence, arguments, hash))
|
123
135
|
next unless matching_argument?(sequence, argument)
|
124
136
|
|
125
137
|
count += 1
|
@@ -160,7 +172,7 @@ module RuboCop
|
|
160
172
|
end
|
161
173
|
|
162
174
|
def numeric?(argument)
|
163
|
-
argument
|
175
|
+
argument.type?(:numeric, :str) ||
|
164
176
|
rational_number?(argument) ||
|
165
177
|
complex_number?(argument)
|
166
178
|
end
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
58
58
|
# rubocop:disable Metrics/AbcSize
|
59
59
|
def on_lvasgn(node)
|
60
60
|
return unless (rhs = node.rhs)
|
61
|
-
return unless rhs.
|
61
|
+
return unless rhs.type?(:any_block, :call) && method_returning_self?(rhs.method_name)
|
62
62
|
return unless (receiver = rhs.receiver)
|
63
63
|
|
64
64
|
receiver_type = ASSIGNMENT_TYPE_TO_RECEIVER_TYPE[node.type]
|
@@ -8,9 +8,9 @@ module RuboCop
|
|
8
8
|
#
|
9
9
|
# Endless methods added in Ruby 3.0 are also accepted by this cop.
|
10
10
|
#
|
11
|
-
# If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line`
|
12
|
-
# `
|
13
|
-
# methods if there is only one statement in the body.
|
11
|
+
# If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line`, `allow_always`,
|
12
|
+
# `require_single_line`, or `require_always`, single-line methods will be autocorrected
|
13
|
+
# to endless methods if there is only one statement in the body.
|
14
14
|
#
|
15
15
|
# @example
|
16
16
|
# # bad
|