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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of IfOp.



5622
5623
5624
5625
5626
5627
5628
# File 'lib/syntax_tree/node.rb', line 5622

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5620
5621
5622
# File 'lib/syntax_tree/node.rb', line 5620

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



5617
5618
5619
# File 'lib/syntax_tree/node.rb', line 5617

def falsy
  @falsy
end

#predicateObject (readonly)

untyped

the expression to be checked



5611
5612
5613
# File 'lib/syntax_tree/node.rb', line 5611

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



5614
5615
5616
# File 'lib/syntax_tree/node.rb', line 5614

def truthy
  @truthy
end

Instance Method Details

#accept(visitor) ⇒ Object



5630
5631
5632
# File 'lib/syntax_tree/node.rb', line 5630

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

#child_nodesObject Also known as: deconstruct



5634
5635
5636
# File 'lib/syntax_tree/node.rb', line 5634

def child_nodes
  [predicate, truthy, falsy]
end

#deconstruct_keys(_keys) ⇒ Object



5640
5641
5642
5643
5644
5645
5646
5647
5648
# File 'lib/syntax_tree/node.rb', line 5640

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

#format(q) ⇒ Object



5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
# File 'lib/syntax_tree/node.rb', line 5650

def format(q)
  force_flat = [
    Alias, Assign, Break, Command, CommandCall, Heredoc, If, IfMod, IfOp,
    Lambda, MAssign, Next, OpAssign, RescueMod, Return, Return0, Super,
    Undef, Unless, UnlessMod, UntilMod, VarAlias, VoidStmt, WhileMod, Yield,
    Yield0, 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