forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant_allow_nil.rb
111 lines (90 loc) · 3.1 KB
/
redundant_allow_nil.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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Checks Rails model validations for a redundant `allow_nil` when
# `allow_blank` is present.
#
# @example
# # bad
# validates :x, length: { is: 5 }, allow_nil: true, allow_blank: true
#
# # bad
# validates :x, length: { is: 5 }, allow_nil: false, allow_blank: true
#
# # bad
# validates :x, length: { is: 5 }, allow_nil: false, allow_blank: false
#
# # good
# validates :x, length: { is: 5 }, allow_blank: true
#
# # good
# validates :x, length: { is: 5 }, allow_blank: false
#
# # good
# # Here, `nil` is valid but `''` is not
# validates :x, length: { is: 5 }, allow_nil: true, allow_blank: false
#
class RedundantAllowNil < Cop
include RangeHelp
MSG_SAME =
'`allow_nil` is redundant when `allow_blank` has the same value.'
MSG_ALLOW_NIL_FALSE =
'`allow_nil: false` is redundant when `allow_blank` is true.'
def on_send(node)
return unless node.method_name == :validates
allow_nil, allow_blank = find_allow_nil_and_allow_blank(node)
return unless allow_nil && allow_blank
allow_nil_val = allow_nil.children.last
allow_blank_val = allow_blank.children.last
offense(allow_nil_val, allow_blank_val, allow_nil)
end
def autocorrect(node)
prv_sib = previous_sibling(node)
nxt_sib = next_sibling(node)
lambda do |corrector|
if nxt_sib
corrector.remove(range_between(node_beg(node), node_beg(nxt_sib)))
elsif prv_sib
corrector.remove(range_between(node_end(prv_sib), node_end(node)))
else
corrector.remove(node.loc.expression)
end
end
end
private
def offense(allow_nil_val, allow_blank_val, allow_nil)
if allow_nil_val.type == allow_blank_val.type
add_offense(allow_nil, message: MSG_SAME)
elsif allow_nil_val.false_type? && allow_blank_val.true_type?
add_offense(allow_nil, message: MSG_ALLOW_NIL_FALSE)
end
end
def find_allow_nil_and_allow_blank(node)
allow_nil = nil
allow_blank = nil
node.each_descendant do |descendant|
next unless descendant.pair_type?
key = descendant.children.first.source
allow_nil = descendant if key == 'allow_nil'
allow_blank = descendant if key == 'allow_blank'
break if allow_nil && allow_blank
end
[allow_nil, allow_blank]
end
def previous_sibling(node)
node.parent.children[node.sibling_index - 1]
end
def next_sibling(node)
node.parent.children[node.sibling_index + 1]
end
def node_beg(node)
node.loc.expression.begin_pos
end
def node_end(node)
node.loc.expression.end_pos
end
end
end
end
end