Added Ruby::Node#not
This is a shortcut for "!foo". Using this method one doesn't have to worry about how the "!" operator binds. For example, this: !foo.or(bar) would be parsed/evaluated as this: !(foo.or(bar)) when instead we want it to be this: (!foo).or(bar) Using explicit parenthesis leads to ugly code, so now we can do this instead: foo.not.or(bar)
This commit is contained in:
parent
4698e98632
commit
07b52fb48a
|
@ -84,6 +84,17 @@ module Oga
|
|||
Node.new(:or, [self, other])
|
||||
end
|
||||
|
||||
##
|
||||
# Returns a node that evaluates to its inverse.
|
||||
#
|
||||
# For example, a variable `foo` would be turned into `!foo`.
|
||||
#
|
||||
# @return [Oga::Ruby::Node]
|
||||
#
|
||||
def not
|
||||
!self
|
||||
end
|
||||
|
||||
##
|
||||
# Returns a node for Ruby's "is_a?" method.
|
||||
#
|
||||
|
|
|
@ -302,7 +302,7 @@ module Oga
|
|||
.followed_by(throw_message(:skip_children))
|
||||
end
|
||||
|
||||
next_check = (!check).or(parent != doc_node.parent).if_true do
|
||||
next_check = check.not.or(parent != doc_node.parent).if_true do
|
||||
send_message(:next)
|
||||
end
|
||||
|
||||
|
@ -343,7 +343,7 @@ module Oga
|
|||
.followed_by(throw_message(:skip_children))
|
||||
end
|
||||
|
||||
next_check = (!check).if_true { send_message(:next) }
|
||||
next_check = check.not.if_true { send_message(:next) }
|
||||
|
||||
match_node = backup_variable(node, doc_node) do
|
||||
process(ast, node, &block).if_true do
|
||||
|
|
|
@ -61,6 +61,16 @@ describe Oga::Ruby::Node do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#not' do
|
||||
it 'returns a send Node' do
|
||||
node = described_class.new(:lit, %w{foo})
|
||||
inverted = node.not
|
||||
|
||||
inverted.type.should == :send
|
||||
inverted.to_a.should == [node, '!']
|
||||
end
|
||||
end
|
||||
|
||||
describe '#is_a?' do
|
||||
it 'returns a is_a? call Node' do
|
||||
left = described_class.new(:lit, %w{number})
|
||||
|
|
Loading…
Reference in New Issue