rubocop 1.18.1 → 1.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +24 -6
- data/lib/rubocop.rb +4 -0
- data/lib/rubocop/cli.rb +18 -0
- data/lib/rubocop/config_loader_resolver.rb +22 -7
- data/lib/rubocop/config_validator.rb +18 -5
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
- data/lib/rubocop/cop/layout/class_structure.rb +5 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +10 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +36 -17
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +21 -9
- data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
- data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +6 -1
- data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +10 -7
- data/lib/rubocop/cop/mixin/require_library.rb +59 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +18 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +31 -0
- data/lib/rubocop/cop/style/comment_annotation.rb +50 -6
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +8 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
- data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
- data/lib/rubocop/cop/style/missing_else.rb +7 -0
- data/lib/rubocop/cop/style/mutable_constant.rb +6 -8
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +88 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +32 -24
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +25 -15
- data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
- data/lib/rubocop/cop/style/word_array.rb +20 -2
- data/lib/rubocop/cop/util.rb +7 -2
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +11 -5
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
# incorrect registering of keywords (eg. `review`) inside a paragraph as an
|
13
13
|
# annotation.
|
14
14
|
#
|
15
|
-
# @example
|
15
|
+
# @example RequireColon: true (default)
|
16
16
|
# # bad
|
17
17
|
# # TODO make better
|
18
18
|
#
|
@@ -36,14 +36,36 @@ module RuboCop
|
|
36
36
|
#
|
37
37
|
# # good
|
38
38
|
# # OPTIMIZE: does not work
|
39
|
+
#
|
40
|
+
# @example RequireColon: false
|
41
|
+
# # bad
|
42
|
+
# # TODO: make better
|
43
|
+
#
|
44
|
+
# # good
|
45
|
+
# # TODO make better
|
46
|
+
#
|
47
|
+
# # bad
|
48
|
+
# # fixme does not work
|
49
|
+
#
|
50
|
+
# # good
|
51
|
+
# # FIXME does not work
|
52
|
+
#
|
53
|
+
# # bad
|
54
|
+
# # Optimize does not work
|
55
|
+
#
|
56
|
+
# # good
|
57
|
+
# # OPTIMIZE does not work
|
39
58
|
class CommentAnnotation < Base
|
40
59
|
include AnnotationComment
|
41
60
|
include RangeHelp
|
42
61
|
extend AutoCorrector
|
43
62
|
|
44
|
-
|
45
|
-
|
46
|
-
|
63
|
+
MSG_COLON_STYLE = 'Annotation keywords like `%<keyword>s` should be all ' \
|
64
|
+
'upper case, followed by a colon, and a space, ' \
|
65
|
+
'then a note describing the problem.'
|
66
|
+
MSG_SPACE_STYLE = 'Annotation keywords like `%<keyword>s` should be all ' \
|
67
|
+
'upper case, followed by a space, ' \
|
68
|
+
'then a note describing the problem.'
|
47
69
|
MISSING_NOTE = 'Annotation comment, with keyword `%<keyword>s`, is missing a note.'
|
48
70
|
|
49
71
|
def on_new_investigation
|
@@ -63,13 +85,15 @@ module RuboCop
|
|
63
85
|
private
|
64
86
|
|
65
87
|
def register_offense(range, note, first_word)
|
88
|
+
message = requires_colon? ? MSG_COLON_STYLE : MSG_SPACE_STYLE
|
89
|
+
|
66
90
|
add_offense(
|
67
91
|
range,
|
68
|
-
message: format(note ?
|
92
|
+
message: format(note ? message : MISSING_NOTE, keyword: first_word)
|
69
93
|
) do |corrector|
|
70
94
|
next if note.nil?
|
71
95
|
|
72
|
-
corrector
|
96
|
+
correct_offense(corrector, range, first_word)
|
73
97
|
end
|
74
98
|
end
|
75
99
|
|
@@ -92,8 +116,28 @@ module RuboCop
|
|
92
116
|
end
|
93
117
|
|
94
118
|
def correct_annotation?(first_word, colon, space, note)
|
119
|
+
return correct_colon_annotation?(first_word, colon, space, note) if requires_colon?
|
120
|
+
|
121
|
+
correct_space_annotation?(first_word, colon, space, note)
|
122
|
+
end
|
123
|
+
|
124
|
+
def correct_colon_annotation?(first_word, colon, space, note)
|
95
125
|
keyword?(first_word) && (colon && space && note || !colon && !note)
|
96
126
|
end
|
127
|
+
|
128
|
+
def correct_space_annotation?(first_word, colon, space, note)
|
129
|
+
keyword?(first_word) && (!colon && space && note || !colon && !note)
|
130
|
+
end
|
131
|
+
|
132
|
+
def correct_offense(corrector, range, first_word)
|
133
|
+
return corrector.replace(range, "#{first_word.upcase}: ") if requires_colon?
|
134
|
+
|
135
|
+
corrector.replace(range, "#{first_word.upcase} ")
|
136
|
+
end
|
137
|
+
|
138
|
+
def requires_colon?
|
139
|
+
cop_config['RequireColon']
|
140
|
+
end
|
97
141
|
end
|
98
142
|
end
|
99
143
|
end
|
@@ -12,6 +12,7 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# Auto-correction removes comments from `end` keyword and keeps comments
|
14
14
|
# for `class`, `module`, `def` and `begin` above the keyword.
|
15
|
+
# It is marked as unsafe auto-correction as it may remove meaningful comments.
|
15
16
|
#
|
16
17
|
# @example
|
17
18
|
# # bad
|
@@ -50,7 +51,7 @@ module RuboCop
|
|
50
51
|
|
51
52
|
def on_new_investigation
|
52
53
|
processed_source.comments.each do |comment|
|
53
|
-
next unless (match = line(comment).match(/(?<keyword>\S+).*#/))
|
54
|
+
next unless offensive?(comment) && (match = line(comment).match(/(?<keyword>\S+).*#/))
|
54
55
|
|
55
56
|
register_offense(comment, match[:keyword])
|
56
57
|
end
|
@@ -26,7 +26,7 @@ module RuboCop
|
|
26
26
|
# `when` nodes contain the entire branch including the condition.
|
27
27
|
# We only need the contents of the branch, not the condition.
|
28
28
|
def expand_when_branches(when_branches)
|
29
|
-
when_branches.map
|
29
|
+
when_branches.map(&:body)
|
30
30
|
end
|
31
31
|
|
32
32
|
def tail(branch)
|
@@ -272,6 +272,16 @@ module RuboCop
|
|
272
272
|
check_node(node, branches)
|
273
273
|
end
|
274
274
|
|
275
|
+
def on_case_match(node)
|
276
|
+
return unless style == :assign_to_condition
|
277
|
+
return unless node.else_branch
|
278
|
+
|
279
|
+
in_pattern_branches = expand_when_branches(node.in_pattern_branches)
|
280
|
+
branches = [*in_pattern_branches, node.else_branch]
|
281
|
+
|
282
|
+
check_node(node, branches)
|
283
|
+
end
|
284
|
+
|
275
285
|
private
|
276
286
|
|
277
287
|
def check_assignment_to_condition(node)
|
@@ -297,7 +307,7 @@ module RuboCop
|
|
297
307
|
end
|
298
308
|
|
299
309
|
# @!method candidate_condition?(node)
|
300
|
-
def_node_matcher :candidate_condition?, '[{if case} !#allowed_ternary?]'
|
310
|
+
def_node_matcher :candidate_condition?, '[{if case case_match} !#allowed_ternary?]'
|
301
311
|
|
302
312
|
def allowed_ternary?(assignment)
|
303
313
|
assignment.if_type? && assignment.ternary? && !include_ternary?
|
@@ -319,7 +329,7 @@ module RuboCop
|
|
319
329
|
end
|
320
330
|
|
321
331
|
def move_assignment_outside_condition(corrector, node)
|
322
|
-
if node.case_type?
|
332
|
+
if node.case_type? || node.case_match_type?
|
323
333
|
CaseCorrector.correct(corrector, self, node)
|
324
334
|
elsif node.ternary?
|
325
335
|
TernaryCorrector.correct(corrector, node)
|
@@ -333,7 +343,7 @@ module RuboCop
|
|
333
343
|
|
334
344
|
if ternary_condition?(condition)
|
335
345
|
TernaryCorrector.move_assignment_inside_condition(corrector, node)
|
336
|
-
elsif condition.case_type?
|
346
|
+
elsif condition.case_type? || condition.case_match_type?
|
337
347
|
CaseCorrector.move_assignment_inside_condition(corrector, node)
|
338
348
|
elsif condition.if_type?
|
339
349
|
IfCorrector.move_assignment_inside_condition(corrector, node)
|
@@ -631,7 +641,11 @@ module RuboCop
|
|
631
641
|
end
|
632
642
|
|
633
643
|
def extract_branches(case_node)
|
634
|
-
when_branches =
|
644
|
+
when_branches = if case_node.case_type?
|
645
|
+
expand_when_branches(case_node.when_branches)
|
646
|
+
else
|
647
|
+
expand_when_branches(case_node.in_pattern_branches)
|
648
|
+
end
|
635
649
|
|
636
650
|
[when_branches, case_node.else_branch]
|
637
651
|
end
|
@@ -36,13 +36,7 @@ module RuboCop
|
|
36
36
|
next unless comment.text.scan(/# rubocop:(?:disable|todo)/).size > 1
|
37
37
|
|
38
38
|
add_offense(comment) do |corrector|
|
39
|
-
|
40
|
-
'# rubocop:disable'
|
41
|
-
else
|
42
|
-
'# rubocop:todo'
|
43
|
-
end
|
44
|
-
|
45
|
-
corrector.replace(comment, comment.text[/#{prefix} \S+/])
|
39
|
+
corrector.replace(comment, comment.text.gsub(%r{ # rubocop:(disable|todo)}, ','))
|
46
40
|
end
|
47
41
|
end
|
48
42
|
end
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
# RUBY
|
44
44
|
#
|
45
45
|
# This cop works only when a string literal is given as a code string.
|
46
|
-
# No
|
46
|
+
# No offense is reported if a string variable is given as below:
|
47
47
|
#
|
48
48
|
# @example
|
49
49
|
# # not checked
|
@@ -93,18 +93,43 @@ module RuboCop
|
|
93
93
|
|
94
94
|
def add_block_argument(node, corrector)
|
95
95
|
if node.arguments?
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
elsif node.call_type? || node.zsuper_type?
|
102
|
-
corrector.insert_after(node, '(&block)')
|
96
|
+
insert_argument(node, corrector)
|
97
|
+
elsif empty_arguments?(node)
|
98
|
+
corrector.replace(node.arguments, '(&block)')
|
99
|
+
elsif call_like?(node)
|
100
|
+
correct_call_node(node, corrector)
|
103
101
|
else
|
104
102
|
corrector.insert_after(node.loc.name, '(&block)')
|
105
103
|
end
|
106
104
|
end
|
107
105
|
|
106
|
+
def empty_arguments?(node)
|
107
|
+
# Is there an arguments node with only parentheses?
|
108
|
+
node.arguments.is_a?(RuboCop::AST::Node) && node.arguments.loc.begin
|
109
|
+
end
|
110
|
+
|
111
|
+
def call_like?(node)
|
112
|
+
node.call_type? || node.zsuper_type? || node.super_type?
|
113
|
+
end
|
114
|
+
|
115
|
+
def insert_argument(node, corrector)
|
116
|
+
last_arg = node.arguments.last
|
117
|
+
arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
|
118
|
+
replacement = ' &block'
|
119
|
+
replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
|
120
|
+
corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
|
121
|
+
end
|
122
|
+
|
123
|
+
def correct_call_node(node, corrector)
|
124
|
+
corrector.insert_after(node, '(&block)')
|
125
|
+
return unless node.parenthesized?
|
126
|
+
|
127
|
+
args_begin = Util.args_begin(node)
|
128
|
+
args_end = Util.args_end(node)
|
129
|
+
range = range_between(args_begin.begin_pos, args_end.end_pos)
|
130
|
+
corrector.remove(range)
|
131
|
+
end
|
132
|
+
|
108
133
|
def block_body_range(block_node, send_node)
|
109
134
|
range_between(send_node.loc.expression.end_pos, block_node.loc.end.end_pos)
|
110
135
|
end
|
@@ -5,11 +5,17 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop is designed to help you transition from mutable string literals
|
7
7
|
# to frozen string literals.
|
8
|
-
# It will add the
|
9
|
-
# files to enable frozen string literals. Frozen string literals may be
|
8
|
+
# It will add the `# frozen_string_literal: true` magic comment to the top
|
9
|
+
# of files to enable frozen string literals. Frozen string literals may be
|
10
10
|
# default in future Ruby. The comment will be added below a shebang and
|
11
11
|
# encoding comment.
|
12
12
|
#
|
13
|
+
# Note that the cop will ignore files where the comment exists but is set
|
14
|
+
# to `false` instead of `true`.
|
15
|
+
#
|
16
|
+
# To require a blank line after this comment, please see
|
17
|
+
# `Layout/EmptyLineAfterMagicComment` cop.
|
18
|
+
#
|
13
19
|
# @example EnforcedStyle: always (default)
|
14
20
|
# # The `always` style will always add the frozen string literal comment
|
15
21
|
# # to a file, regardless of the Ruby version or if `freeze` or `<<` are
|
@@ -27,11 +27,8 @@ module RuboCop
|
|
27
27
|
# {a: 1, b: 2}.transform_keys { |k| k.to_s }
|
28
28
|
class HashTransformKeys < Base
|
29
29
|
include HashTransformMethod
|
30
|
-
extend TargetRubyVersion
|
31
30
|
extend AutoCorrector
|
32
31
|
|
33
|
-
minimum_target_ruby_version 2.5
|
34
|
-
|
35
32
|
# @!method on_bad_each_with_object(node)
|
36
33
|
def_node_matcher :on_bad_each_with_object, <<~PATTERN
|
37
34
|
(block
|
@@ -7,6 +7,22 @@ module RuboCop
|
|
7
7
|
# each branch of a conditional expression. Such expressions should normally
|
8
8
|
# be placed outside the conditional expression - before or after it.
|
9
9
|
#
|
10
|
+
# This cop is marked unsafe auto-correction as the order of method invocations
|
11
|
+
# must be guaranteed in the following case:
|
12
|
+
#
|
13
|
+
# [source,ruby]
|
14
|
+
# ----
|
15
|
+
# if method_that_modifies_global_state # 1
|
16
|
+
# method_that_relies_on_global_state # 2
|
17
|
+
# foo # 3
|
18
|
+
# else
|
19
|
+
# method_that_relies_on_global_state # 2
|
20
|
+
# bar # 3
|
21
|
+
# end
|
22
|
+
# ----
|
23
|
+
#
|
24
|
+
# In such a case, auto-correction may change the invocation order.
|
25
|
+
#
|
10
26
|
# NOTE: The cop is poorly named and some people might think that it actually
|
11
27
|
# checks for duplicated conditional branches. The name will probably be changed
|
12
28
|
# in a future major RuboCop release.
|
@@ -124,21 +140,30 @@ module RuboCop
|
|
124
140
|
return if branches.any?(&:nil?)
|
125
141
|
|
126
142
|
tails = branches.map { |branch| tail(branch) }
|
127
|
-
check_expressions(node, tails, :after_condition) if duplicated_expressions?(tails)
|
143
|
+
check_expressions(node, tails, :after_condition) if duplicated_expressions?(node, tails)
|
128
144
|
|
129
145
|
heads = branches.map { |branch| head(branch) }
|
130
|
-
check_expressions(node, heads, :before_condition) if duplicated_expressions?(heads)
|
146
|
+
check_expressions(node, heads, :before_condition) if duplicated_expressions?(node, heads)
|
131
147
|
end
|
132
148
|
|
133
|
-
def duplicated_expressions?(expressions)
|
134
|
-
|
149
|
+
def duplicated_expressions?(node, expressions)
|
150
|
+
unique_expressions = expressions.uniq
|
151
|
+
return false unless expressions.size >= 1 && unique_expressions.one?
|
152
|
+
|
153
|
+
unique_expression = unique_expressions.first
|
154
|
+
return true unless unique_expression.assignment?
|
155
|
+
|
156
|
+
lhs = unique_expression.child_nodes.first
|
157
|
+
node.condition.child_nodes.none? { |n| n.source == lhs.source if n.variable? }
|
135
158
|
end
|
136
159
|
|
137
|
-
def check_expressions(node, expressions, insert_position)
|
160
|
+
def check_expressions(node, expressions, insert_position) # rubocop:disable Metrics/MethodLength
|
138
161
|
inserted_expression = false
|
139
162
|
|
140
163
|
expressions.each do |expression|
|
141
164
|
add_offense(expression) do |corrector|
|
165
|
+
next if node.if_type? && node.ternary?
|
166
|
+
|
142
167
|
range = range_by_whole_lines(expression.source_range, include_final_newline: true)
|
143
168
|
corrector.remove(range)
|
144
169
|
next if inserted_expression
|
@@ -96,7 +96,7 @@ module RuboCop
|
|
96
96
|
MSG_MISSING = 'Use def with parentheses when there are parameters.'
|
97
97
|
|
98
98
|
def on_def(node)
|
99
|
-
return if node
|
99
|
+
return if forced_parentheses?(node)
|
100
100
|
|
101
101
|
args = node.arguments
|
102
102
|
|
@@ -129,6 +129,15 @@ module RuboCop
|
|
129
129
|
corrector.insert_after(arguments_range, ')')
|
130
130
|
end
|
131
131
|
|
132
|
+
def forced_parentheses?(node)
|
133
|
+
# Regardless of style, parentheses are necessary for:
|
134
|
+
# 1. Endless methods
|
135
|
+
# 2. Argument lists containing a `forward-arg` (`...`)
|
136
|
+
# Removing the parens would be a syntax error here.
|
137
|
+
|
138
|
+
node.endless? || node.arguments.any?(&:forward_arg_type?)
|
139
|
+
end
|
140
|
+
|
132
141
|
def require_parentheses?(args)
|
133
142
|
style == :require_parentheses ||
|
134
143
|
(style == :require_no_parentheses_except_multiline && args.multiline?)
|
@@ -5,6 +5,9 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# Checks for `if` expressions that do not have an `else` branch.
|
7
7
|
#
|
8
|
+
# NOTE: Pattern matching is allowed to have no `else` branch because unlike `if` and `case`,
|
9
|
+
# it raises `NoMatchingPatternError` if the pattern doesn't match and without having `else`.
|
10
|
+
#
|
8
11
|
# Supported styles are: if, case, both.
|
9
12
|
#
|
10
13
|
# @example EnforcedStyle: if
|
@@ -114,6 +117,10 @@ module RuboCop
|
|
114
117
|
check(node)
|
115
118
|
end
|
116
119
|
|
120
|
+
def on_case_match(node)
|
121
|
+
# do nothing.
|
122
|
+
end
|
123
|
+
|
117
124
|
private
|
118
125
|
|
119
126
|
def check(node)
|
@@ -61,13 +61,12 @@ module RuboCop
|
|
61
61
|
|
62
62
|
def on_casgn(node)
|
63
63
|
_scope, _const_name, value = *node
|
64
|
-
|
65
|
-
|
64
|
+
if value.nil? # This is only the case for `CONST += ...` or similarg66
|
65
|
+
parent = node.parent
|
66
|
+
return unless parent.or_asgn_type? # We only care about `CONST ||= ...`
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
return unless lhs&.casgn_type?
|
68
|
+
value = parent.children.last
|
69
|
+
end
|
71
70
|
|
72
71
|
on_assignment(value)
|
73
72
|
end
|
@@ -118,14 +117,13 @@ module RuboCop
|
|
118
117
|
end
|
119
118
|
|
120
119
|
def mutable_literal?(value)
|
121
|
-
return false if value.nil?
|
122
120
|
return false if frozen_regexp_or_range_literals?(value)
|
123
121
|
|
124
122
|
value.mutable_literal?
|
125
123
|
end
|
126
124
|
|
127
125
|
def immutable_literal?(node)
|
128
|
-
|
126
|
+
frozen_regexp_or_range_literals?(node) || node.immutable_literal?
|
129
127
|
end
|
130
128
|
|
131
129
|
def frozen_string_literal?(node)
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for places where conditional branch makes redundant self-assignment.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# foo = condition ? bar : foo
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# foo = bar if condition
|
15
|
+
#
|
16
|
+
# # bad
|
17
|
+
# foo = condition ? foo : bar
|
18
|
+
#
|
19
|
+
# # good
|
20
|
+
# foo = bar unless condition
|
21
|
+
#
|
22
|
+
class RedundantSelfAssignmentBranch < Base
|
23
|
+
include RangeHelp
|
24
|
+
extend AutoCorrector
|
25
|
+
|
26
|
+
MSG = 'Remove the self-assignment branch.'
|
27
|
+
|
28
|
+
# @!method bad_method?(node)
|
29
|
+
def_node_matcher :bad_method?, <<~PATTERN
|
30
|
+
(send nil? :bad_method ...)
|
31
|
+
PATTERN
|
32
|
+
|
33
|
+
def on_lvasgn(node)
|
34
|
+
variable, expression = *node
|
35
|
+
return unless expression&.if_type?
|
36
|
+
return unless expression.ternary? || expression.else?
|
37
|
+
|
38
|
+
if_branch = expression.if_branch
|
39
|
+
else_branch = expression.else_branch
|
40
|
+
|
41
|
+
if self_assign?(variable, if_branch)
|
42
|
+
register_offense(expression, if_branch, else_branch, 'unless')
|
43
|
+
elsif self_assign?(variable, else_branch)
|
44
|
+
register_offense(expression, else_branch, if_branch, 'if')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
alias on_ivasgn on_lvasgn
|
49
|
+
alias on_cvasgn on_lvasgn
|
50
|
+
alias on_gvasgn on_lvasgn
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def self_assign?(variable, branch)
|
55
|
+
variable.to_s == branch&.source
|
56
|
+
end
|
57
|
+
|
58
|
+
def register_offense(if_node, offense_branch, opposite_branch, keyword)
|
59
|
+
add_offense(offense_branch) do |corrector|
|
60
|
+
if if_node.ternary?
|
61
|
+
replacement = "#{opposite_branch.source} #{keyword} #{if_node.condition.source}"
|
62
|
+
corrector.replace(if_node, replacement)
|
63
|
+
else
|
64
|
+
if_node_loc = if_node.loc
|
65
|
+
|
66
|
+
range = range_by_whole_lines(offense_branch.source_range, include_final_newline: true)
|
67
|
+
corrector.remove(range)
|
68
|
+
range = range_by_whole_lines(if_node_loc.else, include_final_newline: true)
|
69
|
+
corrector.remove(range)
|
70
|
+
|
71
|
+
autocorrect_if_condition(corrector, if_node, if_node_loc, keyword)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def autocorrect_if_condition(corrector, if_node, if_node_loc, keyword)
|
77
|
+
else_branch = if_node.else_branch
|
78
|
+
|
79
|
+
if else_branch.respond_to?(:elsif?) && else_branch.elsif?
|
80
|
+
corrector.replace(if_node.condition, else_branch.condition.source)
|
81
|
+
else
|
82
|
+
corrector.replace(if_node_loc.keyword, keyword)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|