lib/rubocop/cop/rails/redundant_allow_nil.rb in rubocop-rails-2.8.1 vs lib/rubocop/cop/rails/redundant_allow_nil.rb in rubocop-rails-2.9.0

- old
+ new

@@ -24,71 +24,69 @@ # # # good # # Here, `nil` is valid but `''` is not # validates :x, length: { is: 5 }, allow_nil: true, allow_blank: false # - class RedundantAllowNil < Cop + class RedundantAllowNil < Base include RangeHelp + extend AutoCorrector 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?(:validates) + RESTRICT_ON_SEND = %i[validates].freeze + def on_send(node) 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) + if allow_nil_val.type == allow_blank_val.type + register_offense(allow_nil, MSG_SAME) + elsif allow_nil_val.false_type? && allow_blank_val.true_type? + register_offense(allow_nil, MSG_ALLOW_NIL_FALSE) + end end - def autocorrect(node) - prv_sib = previous_sibling(node) - nxt_sib = next_sibling(node) + private - lambda do |corrector| + def register_offense(allow_nil, message) + add_offense(allow_nil, message: message) do |corrector| + prv_sib = previous_sibling(allow_nil) + nxt_sib = next_sibling(allow_nil) + if nxt_sib - corrector.remove(range_between(node_beg(node), node_beg(nxt_sib))) + corrector.remove(range_between(node_beg(allow_nil), node_beg(nxt_sib))) elsif prv_sib - corrector.remove(range_between(node_end(prv_sib), node_end(node))) + corrector.remove(range_between(node_end(prv_sib), node_end(allow_nil))) else - corrector.remove(node.loc.expression) + corrector.remove(allow_nil.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 + allow_nil, allow_blank = nil - node.each_descendant do |descendant| - next unless descendant.pair_type? + node.each_child_node do |child_node| + if child_node.pair_type? + key = child_node.children.first.source - key = descendant.children.first.source + allow_nil = child_node if key == 'allow_nil' + allow_blank = child_node if key == 'allow_blank' + end + return [allow_nil, allow_blank] if allow_nil && allow_blank - allow_nil = descendant if key == 'allow_nil' - allow_blank = descendant if key == 'allow_blank' - - break if allow_nil && allow_blank + found_in_children_nodes = find_allow_nil_and_allow_blank(child_node) + return found_in_children_nodes if found_in_children_nodes end - [allow_nil, allow_blank] + nil end def previous_sibling(node) node.parent.children[node.sibling_index - 1] end