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.



6502
6503
6504
6505
6506
6507
6508
# File 'lib/syntax_tree/node.rb', line 6502

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



6500
6501
6502
# File 'lib/syntax_tree/node.rb', line 6500

def comments
  @comments
end

#falsyObject (readonly)

Node

the expression to be executed if the predicate is falsy



6497
6498
6499
# File 'lib/syntax_tree/node.rb', line 6497

def falsy
  @falsy
end

#predicateObject (readonly)

Node

the expression to be checked



6491
6492
6493
# File 'lib/syntax_tree/node.rb', line 6491

def predicate
  @predicate
end

#truthyObject (readonly)

Node

the expression to be executed if the predicate is truthy



6494
6495
6496
# File 'lib/syntax_tree/node.rb', line 6494

def truthy
  @truthy
end

Instance Method Details

#===(other) ⇒ Object



6576
6577
6578
6579
# File 'lib/syntax_tree/node.rb', line 6576

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

#accept(visitor) ⇒ Object



6510
6511
6512
# File 'lib/syntax_tree/node.rb', line 6510

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

#child_nodesObject Also known as: deconstruct



6514
6515
6516
# File 'lib/syntax_tree/node.rb', line 6514

def child_nodes
  [predicate, truthy, falsy]
end

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



6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
# File 'lib/syntax_tree/node.rb', line 6518

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



6533
6534
6535
6536
6537
6538
6539
6540
6541
# File 'lib/syntax_tree/node.rb', line 6533

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

#format(q) ⇒ Object



6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
# File 'lib/syntax_tree/node.rb', line 6543

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