Class: SyntaxTree::IfOp

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

IfOp represents a ternary clause.

predicate ? truthy : falsy

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(predicate:, truthy:, falsy:, location:) ⇒ IfOp

Returns a new instance of IfOp.



6572
6573
6574
6575
6576
6577
6578
# File 'lib/syntax_tree/node.rb', line 6572

def initialize(predicate:, truthy:, falsy:, location:)
  @predicate = predicate
  @truthy = truthy
  @falsy = falsy
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6570
6571
6572
# File 'lib/syntax_tree/node.rb', line 6570

def comments
  @comments
end

#falsyObject (readonly)

Node

the expression to be executed if the predicate is falsy



6567
6568
6569
# File 'lib/syntax_tree/node.rb', line 6567

def falsy
  @falsy
end

#predicateObject (readonly)

Node

the expression to be checked



6561
6562
6563
# File 'lib/syntax_tree/node.rb', line 6561

def predicate
  @predicate
end

#truthyObject (readonly)

Node

the expression to be executed if the predicate is truthy



6564
6565
6566
# File 'lib/syntax_tree/node.rb', line 6564

def truthy
  @truthy
end

Instance Method Details

#===(other) ⇒ Object



6646
6647
6648
6649
# File 'lib/syntax_tree/node.rb', line 6646

def ===(other)
  other.is_a?(IfOp) && predicate === other.predicate &&
    truthy === other.truthy && falsy === other.falsy
end

#accept(visitor) ⇒ Object



6580
6581
6582
# File 'lib/syntax_tree/node.rb', line 6580

def accept(visitor)
  visitor.visit_if_op(self)
end

#child_nodesObject Also known as: deconstruct



6584
6585
6586
# File 'lib/syntax_tree/node.rb', line 6584

def child_nodes
  [predicate, truthy, falsy]
end

#copy(predicate: nil, truthy: nil, falsy: nil, location: nil) ⇒ Object



6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
# File 'lib/syntax_tree/node.rb', line 6588

def copy(predicate: nil, truthy: nil, falsy: nil, location: nil)
  node =
    IfOp.new(
      predicate: predicate || self.predicate,
      truthy: truthy || self.truthy,
      falsy: falsy || self.falsy,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



6603
6604
6605
6606
6607
6608
6609
6610
6611
# File 'lib/syntax_tree/node.rb', line 6603

def deconstruct_keys(_keys)
  {
    predicate: predicate,
    truthy: truthy,
    falsy: falsy,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
# File 'lib/syntax_tree/node.rb', line 6613

def format(q)
  force_flat = [
    AliasNode,
    Assign,
    Break,
    Command,
    CommandCall,
    Heredoc,
    IfNode,
    IfOp,
    Lambda,
    MAssign,
    Next,
    OpAssign,
    RescueMod,
    ReturnNode,
    Super,
    Undef,
    UnlessNode,
    VoidStmt,
    YieldNode,
    ZSuper
  ]

  if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
       force_flat.include?(falsy.class)
    q.group { format_flat(q) }
    return
  end

  q.group { q.if_break { format_break(q) }.if_flat { format_flat(q) } }
end