forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant_condition.rb
139 lines (116 loc) · 3.52 KB
/
redundant_condition.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for unnecessary conditional expressions.
#
# @example
# # bad
# a = b ? b : c
#
# # good
# a = b || c
#
# @example
# # bad
# if b
# b
# else
# c
# end
#
# # good
# b || c
#
# # good
# if b
# b
# elsif cond
# c
# end
#
class RedundantCondition < Base
include RangeHelp
extend AutoCorrector
MSG = 'Use double pipes `||` instead.'
REDUNDANT_CONDITION = 'This condition is not needed.'
def on_if(node)
return if node.elsif_conditional?
return unless offense?(node)
message = message(node)
add_offense(range_of_offense(node), message: message) do |corrector|
if node.ternary?
correct_ternary(corrector, node)
elsif node.modifier_form? || !node.else_branch
corrector.replace(node, node.if_branch.source)
else
corrected = make_ternary_form(node)
corrector.replace(node, corrected)
end
end
end
private
def message(node)
if node.modifier_form? || !node.else_branch
REDUNDANT_CONDITION
else
MSG
end
end
def range_of_offense(node)
return node.loc.expression unless node.ternary?
range_between(node.loc.question.begin_pos, node.loc.colon.end_pos)
end
def offense?(node)
condition, if_branch, else_branch = *node
return false if use_if_branch?(else_branch) || use_hash_key_assignment?(else_branch)
condition == if_branch && !node.elsif? && (
node.ternary? ||
!else_branch.instance_of?(AST::Node) ||
else_branch.single_line?
)
end
def use_if_branch?(else_branch)
else_branch&.if_type?
end
def use_hash_key_assignment?(else_branch)
else_branch&.send_type? && else_branch&.method?(:[]=)
end
def else_source(else_branch)
if require_parentheses?(else_branch)
"(#{else_branch.source})"
elsif without_argument_parentheses_method?(else_branch)
"#{else_branch.method_name}(#{else_branch.arguments.map(&:source).join(', ')})"
else
else_branch.source
end
end
def make_ternary_form(node)
_condition, if_branch, else_branch = *node
ternary_form = [if_branch.source,
else_source(else_branch)].join(' || ')
if node.parent&.send_type?
"(#{ternary_form})"
else
ternary_form
end
end
def correct_ternary(corrector, node)
corrector.replace(range_of_offense(node), '||')
return unless node.else_branch.range_type?
corrector.wrap(node.else_branch, '(', ')')
end
def require_parentheses?(node)
node.basic_conditional? &&
node.modifier_form? ||
node.range_type? ||
node.rescue_type? ||
node.respond_to?(:semantic_operator?) && node.semantic_operator?
end
def without_argument_parentheses_method?(node)
node.send_type? && !node.arguments.empty? && !node.parenthesized?
end
end
end
end
end