Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a51d7a

Browse files
committed
Add AndNode
1 parent 17928e9 commit 3a51d7a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

lib/syntax_tree/node.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,75 @@ def var_alias?
551551
end
552552
end
553553

554+
class AndNode < Node
555+
# [Node] the left side of the && operator
556+
attr_reader :left
557+
558+
# [Symbol] the operator
559+
attr_reader :operator
560+
561+
# [Node] the right side of the && operator
562+
attr_reader :right
563+
564+
# [Array[ Comment | EmbDoc ]] the comments attached to this node
565+
attr_reader :comments
566+
567+
def initialize(left:, operator:, right:, location:)
568+
@left = left
569+
@operator = operator
570+
@right = right
571+
@location = location
572+
@comments = []
573+
end
574+
575+
def accept(visitor)
576+
visitor.visit_and(self)
577+
end
578+
579+
def child_nodes
580+
[left, right]
581+
end
582+
583+
def copy(left: nil, operator: nil, right: nil, location: nil)
584+
node = AndNode.new(left: left || self.left, operator: operator || self.operator, right: right || self.right, location: location || self.location)
585+
node.comments.concat(comments.map(&:copy))
586+
node
587+
end
588+
589+
alias deconstruct child_nodes
590+
591+
def deconstruct_keys(_keys)
592+
{
593+
left: left,
594+
operator: operator,
595+
right: right,
596+
location: location,
597+
comments: comments
598+
}
599+
end
600+
601+
def format(q)
602+
left = self.left
603+
604+
q.group do
605+
q.group { q.format(left) }
606+
q.text(" ")
607+
608+
q.group do
609+
q.text(operator.name)
610+
q.indent do
611+
q.breakable_space
612+
q.format(right)
613+
end
614+
end
615+
end
616+
end
617+
618+
def ===(other)
619+
other.is_a?(AndNode) && left === other.left && operator === other.operator && right === other.right
620+
end
621+
end
622+
554623
# ARef represents when you're pulling a value out of a collection at a
555624
# specific index. Put another way, it's any time you're calling the method
556625
# #[].

0 commit comments

Comments
 (0)