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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of IfOp.



4753
4754
4755
4756
4757
4758
4759
# File 'lib/syntax_tree/node.rb', line 4753

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



4751
4752
4753
# File 'lib/syntax_tree/node.rb', line 4751

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



4748
4749
4750
# File 'lib/syntax_tree/node.rb', line 4748

def falsy
  @falsy
end

#predicateObject (readonly)

untyped

the expression to be checked



4742
4743
4744
# File 'lib/syntax_tree/node.rb', line 4742

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



4745
4746
4747
# File 'lib/syntax_tree/node.rb', line 4745

def truthy
  @truthy
end

Instance Method Details

#accept(visitor) ⇒ Object



4761
4762
4763
# File 'lib/syntax_tree/node.rb', line 4761

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

#child_nodesObject Also known as: deconstruct



4765
4766
4767
# File 'lib/syntax_tree/node.rb', line 4765

def child_nodes
  [predicate, truthy, falsy]
end

#deconstruct_keys(keys) ⇒ Object



4771
4772
4773
4774
4775
4776
4777
4778
4779
# File 'lib/syntax_tree/node.rb', line 4771

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

#format(q) ⇒ Object



4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
# File 'lib/syntax_tree/node.rb', line 4781

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 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