rubocop 1.26.0 → 1.26.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +3 -3
- data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -2
- data/lib/rubocop/cop/mixin/line_length_help.rb +17 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -1
- data/lib/rubocop/cop/style/select_by_regexp.rb +6 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +50 -12
- data/lib/rubocop/cop/style/unless_else.rb +4 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10e6b8905d81b2204fd3d0bf60d3278cd902d0cefc2ed387bf2c317062b46034
|
4
|
+
data.tar.gz: f27011464d465728f1fa470c85c4779c2b148b3cdc872247c5a25d421f269076
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 215ee2bb6f0699fdedcd82c7795c40e2350c33fdfbd48117dbb2fdcaa957bcbff9a65ad9c95b9666d4b9c70d6a2e2d8b6ec6904c3c3f01cbbfa8fee2319c9518
|
7
|
+
data.tar.gz: 0b33b7c3aecf2b7482e19cdbf1a94a56f21d3cb14035c68c62ce0572385edb34a59faa1ea5fb21d9504b04e18f797b0419c6ade76ff70604311b9f899e28b39d
|
@@ -7,9 +7,9 @@ module RuboCop
|
|
7
7
|
# This cop checks for redundant `sort` method to `Dir.glob` and `Dir[]`.
|
8
8
|
#
|
9
9
|
# @safety
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
10
|
+
# This cop is unsafe, in case of having a file and a directory with
|
11
|
+
# identical names, since directory will be loaded before the file, which
|
12
|
+
# will break `exe/files.rb` that rely on `exe.rb` file.
|
13
13
|
#
|
14
14
|
# @example
|
15
15
|
#
|
@@ -147,13 +147,14 @@ module RuboCop
|
|
147
147
|
# will be ignored.
|
148
148
|
return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
|
149
149
|
|
150
|
-
correction = node.value.inspect
|
150
|
+
correction = node.value.inspect
|
151
|
+
correction = correction.delete_prefix(':') if node.parent.colon?
|
151
152
|
return if properly_quoted?(node.source, correction)
|
152
153
|
|
153
154
|
register_offense(
|
154
155
|
node,
|
155
156
|
correction: correction,
|
156
|
-
message: format(MSG, correction: "#{correction}:")
|
157
|
+
message: format(MSG, correction: node.parent.colon? ? "#{correction}:" : correction)
|
157
158
|
)
|
158
159
|
end
|
159
160
|
|
@@ -39,12 +39,7 @@ module RuboCop
|
|
39
39
|
pos + indentation_difference(line)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
# This allows for URIs that are wrapped in quotes or parens to be handled properly
|
44
|
-
# while not allowing additional words to be added after the URL.
|
45
|
-
if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
|
46
|
-
end_position += match.offset(0).last
|
47
|
-
end
|
42
|
+
end_position = extend_uri_end_position(line, end_position)
|
48
43
|
|
49
44
|
return nil if begin_position < max_line_length && end_position < max_line_length
|
50
45
|
|
@@ -65,6 +60,22 @@ module RuboCop
|
|
65
60
|
(line.index(/[^\t]/) || 0) * (tab_indentation_width - 1)
|
66
61
|
end
|
67
62
|
|
63
|
+
def extend_uri_end_position(line, end_position)
|
64
|
+
# Extend the end position YARD comments with linked URLs of the form {<uri> <title>}
|
65
|
+
if line&.match(/{(\s|\S)*}$/)
|
66
|
+
match = line[end_position..line_length(line)]&.match(/(\s|\S)*}/)
|
67
|
+
end_position += match.offset(0).last
|
68
|
+
end
|
69
|
+
|
70
|
+
# Extend the end position until the start of the next word, if any.
|
71
|
+
# This allows for URIs that are wrapped in quotes or parens to be handled properly
|
72
|
+
# while not allowing additional words to be added after the URL.
|
73
|
+
if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
|
74
|
+
end_position += match.offset(0).last
|
75
|
+
end
|
76
|
+
end_position
|
77
|
+
end
|
78
|
+
|
68
79
|
def tab_indentation_width
|
69
80
|
config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
|
70
81
|
config.for_cop('Layout/IndentationWidth')['Width']
|
@@ -29,7 +29,9 @@ module RuboCop
|
|
29
29
|
# b c { block }. <-- b is indented relative to a
|
30
30
|
# d <-- d is indented relative to a
|
31
31
|
def left_hand_side(lhs)
|
32
|
-
|
32
|
+
while lhs.parent&.send_type? && lhs.parent.loc.dot && !lhs.parent.assignment_method?
|
33
|
+
lhs = lhs.parent
|
34
|
+
end
|
33
35
|
lhs
|
34
36
|
end
|
35
37
|
|
@@ -69,6 +69,11 @@ module RuboCop
|
|
69
69
|
}
|
70
70
|
PATTERN
|
71
71
|
|
72
|
+
# @!method env_const?(node)
|
73
|
+
def_node_matcher :env_const?, <<~PATTERN
|
74
|
+
(const {nil? cbase} :ENV)
|
75
|
+
PATTERN
|
76
|
+
|
72
77
|
# @!method calls_lvar?(node, name)
|
73
78
|
def_node_matcher :calls_lvar?, <<~PATTERN
|
74
79
|
{
|
@@ -94,7 +99,7 @@ module RuboCop
|
|
94
99
|
def receiver_allowed?(node)
|
95
100
|
return false unless node
|
96
101
|
|
97
|
-
node.hash_type? || creates_hash?(node)
|
102
|
+
node.hash_type? || creates_hash?(node) || env_const?(node)
|
98
103
|
end
|
99
104
|
|
100
105
|
def register_offense(node, block_node, regexp)
|
@@ -15,6 +15,11 @@ module RuboCop
|
|
15
15
|
# end
|
16
16
|
# end
|
17
17
|
#
|
18
|
+
# # bad
|
19
|
+
# if condition_b
|
20
|
+
# do_something
|
21
|
+
# end if condition_a
|
22
|
+
#
|
18
23
|
# # good
|
19
24
|
# if condition_a && condition_b
|
20
25
|
# do_something
|
@@ -26,12 +31,21 @@ module RuboCop
|
|
26
31
|
# do_something if condition_b
|
27
32
|
# end
|
28
33
|
#
|
34
|
+
# # bad
|
35
|
+
# if condition_b
|
36
|
+
# do_something
|
37
|
+
# end if condition_a
|
38
|
+
#
|
29
39
|
# @example AllowModifier: true
|
30
40
|
# # good
|
31
41
|
# if condition_a
|
32
42
|
# do_something if condition_b
|
33
43
|
# end
|
34
44
|
#
|
45
|
+
# # good
|
46
|
+
# if condition_b
|
47
|
+
# do_something
|
48
|
+
# end if condition_a
|
35
49
|
class SoleNestedConditional < Base
|
36
50
|
include RangeHelp
|
37
51
|
extend AutoCorrector
|
@@ -47,7 +61,7 @@ module RuboCop
|
|
47
61
|
|
48
62
|
if_branch = node.if_branch
|
49
63
|
return if use_variable_assignment_in_condition?(node.condition, if_branch)
|
50
|
-
return unless offending_branch?(if_branch)
|
64
|
+
return unless offending_branch?(node, if_branch)
|
51
65
|
|
52
66
|
message = format(MSG, conditional_type: node.keyword)
|
53
67
|
add_offense(if_branch.loc.keyword, message: message) do |corrector|
|
@@ -72,13 +86,13 @@ module RuboCop
|
|
72
86
|
end
|
73
87
|
end
|
74
88
|
|
75
|
-
def offending_branch?(branch)
|
89
|
+
def offending_branch?(node, branch)
|
76
90
|
return false unless branch
|
77
91
|
|
78
92
|
branch.if_type? &&
|
79
93
|
!branch.else? &&
|
80
94
|
!branch.ternary? &&
|
81
|
-
!(branch.modifier_form? && allow_modifier?)
|
95
|
+
!((node.modifier_form? || branch.modifier_form?) && allow_modifier?)
|
82
96
|
end
|
83
97
|
|
84
98
|
def autocorrect(corrector, node, if_branch)
|
@@ -86,6 +100,14 @@ module RuboCop
|
|
86
100
|
corrector.wrap(node.condition, '(', ')')
|
87
101
|
end
|
88
102
|
|
103
|
+
if outer_condition_modify_form?(node, if_branch)
|
104
|
+
autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
105
|
+
else
|
106
|
+
autocorrect_outer_condition_basic(corrector, node, if_branch)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def autocorrect_outer_condition_basic(corrector, node, if_branch)
|
89
111
|
correct_from_unless_to_if(corrector, node) if node.unless?
|
90
112
|
|
91
113
|
and_operator = if_branch.unless? ? ' && !' : ' && '
|
@@ -97,11 +119,17 @@ module RuboCop
|
|
97
119
|
end
|
98
120
|
end
|
99
121
|
|
100
|
-
def
|
122
|
+
def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
123
|
+
correct_from_unless_to_if(corrector, if_branch, is_modify_form: true) if if_branch.unless?
|
124
|
+
correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
|
125
|
+
end
|
126
|
+
|
127
|
+
def correct_from_unless_to_if(corrector, node, is_modify_form: false)
|
101
128
|
corrector.replace(node.loc.keyword, 'if')
|
102
129
|
|
103
130
|
condition = node.condition
|
104
|
-
if condition.send_type? && condition.comparison_method? && !condition.parenthesized?
|
131
|
+
if (condition.send_type? && condition.comparison_method? && !condition.parenthesized?) ||
|
132
|
+
(is_modify_form && wrap_condition?(condition))
|
105
133
|
corrector.wrap(node.condition, '!(', ')')
|
106
134
|
else
|
107
135
|
corrector.insert_before(node.condition, '!')
|
@@ -113,7 +141,7 @@ module RuboCop
|
|
113
141
|
correct_outer_condition(corrector, outer_condition)
|
114
142
|
|
115
143
|
condition = if_branch.condition
|
116
|
-
corrector.insert_after(outer_condition,
|
144
|
+
corrector.insert_after(outer_condition, "#{and_operator}#{replace_condition(condition)}")
|
117
145
|
|
118
146
|
range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
|
119
147
|
corrector.remove(range_with_surrounding_space(range: range, newlines: false))
|
@@ -129,6 +157,16 @@ module RuboCop
|
|
129
157
|
corrector.wrap(if_branch.condition, '(', ')') if wrap_condition?(if_branch.condition)
|
130
158
|
end
|
131
159
|
|
160
|
+
def correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
|
161
|
+
condition = if_branch.condition
|
162
|
+
corrector.insert_before(condition,
|
163
|
+
"#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
|
164
|
+
|
165
|
+
corrector.remove(node.condition.loc.expression)
|
166
|
+
corrector.remove(range_with_surrounding_space(range: node.loc.keyword, newlines: false))
|
167
|
+
corrector.replace(if_branch.loc.keyword, 'if')
|
168
|
+
end
|
169
|
+
|
132
170
|
def correct_for_comment(corrector, node, if_branch)
|
133
171
|
return if config.for_cop('Style/IfUnlessModifier')['Enabled']
|
134
172
|
|
@@ -165,17 +203,17 @@ module RuboCop
|
|
165
203
|
(node.send_type? && node.arguments.any? && !node.parenthesized?)
|
166
204
|
end
|
167
205
|
|
168
|
-
def
|
169
|
-
|
170
|
-
"#{and_operator}(#{condition.source})"
|
171
|
-
else
|
172
|
-
"#{and_operator}#{condition.source}"
|
173
|
-
end
|
206
|
+
def replace_condition(condition)
|
207
|
+
wrap_condition?(condition) ? "(#{condition.source})" : condition.source
|
174
208
|
end
|
175
209
|
|
176
210
|
def allow_modifier?
|
177
211
|
cop_config['AllowModifier']
|
178
212
|
end
|
213
|
+
|
214
|
+
def outer_condition_modify_form?(node, if_branch)
|
215
|
+
node.condition.loc.expression.begin_pos > if_branch.condition.loc.expression.begin_pos
|
216
|
+
end
|
179
217
|
end
|
180
218
|
end
|
181
219
|
end
|
@@ -32,10 +32,14 @@ module RuboCop
|
|
32
32
|
body_range = range_between_condition_and_else(node, node.condition)
|
33
33
|
else_range = range_between_else_and_end(node)
|
34
34
|
|
35
|
+
next if part_of_ignored_node?(node)
|
36
|
+
|
35
37
|
corrector.replace(node.loc.keyword, 'if')
|
36
38
|
corrector.replace(body_range, else_range.source)
|
37
39
|
corrector.replace(else_range, body_range.source)
|
38
40
|
end
|
41
|
+
|
42
|
+
ignore_node(node)
|
39
43
|
end
|
40
44
|
|
41
45
|
def range_between_condition_and_else(node, condition)
|
data/lib/rubocop/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.26.
|
4
|
+
version: 1.26.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-03-
|
13
|
+
date: 2022-03-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: parallel
|