Class: SyntaxTree::IfOp

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

Overview

IfOp represents a ternary clause.

predicate ? truthy : falsy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of IfOp.



7004
7005
7006
7007
7008
7009
7010
# File 'lib/syntax_tree.rb', line 7004

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



7002
7003
7004
# File 'lib/syntax_tree.rb', line 7002

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



6996
6997
6998
# File 'lib/syntax_tree.rb', line 6996

def falsy
  @falsy
end

#locationObject (readonly)

Location

the location of this node



6999
7000
7001
# File 'lib/syntax_tree.rb', line 6999

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6990
6991
6992
# File 'lib/syntax_tree.rb', line 6990

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



6993
6994
6995
# File 'lib/syntax_tree.rb', line 6993

def truthy
  @truthy
end

Instance Method Details

#child_nodesObject



7012
7013
7014
# File 'lib/syntax_tree.rb', line 7012

def child_nodes
  [predicate, truthy, falsy]
end

#format(q) ⇒ Object



7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
# File 'lib/syntax_tree.rb', line 7016

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

#pretty_print(q) ⇒ Object



7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
# File 'lib/syntax_tree.rb', line 7032

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("ifop")

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(truthy)

    q.breakable
    q.pp(falsy)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
# File 'lib/syntax_tree.rb', line 7049

def to_json(*opts)
  {
    type: :ifop,
    pred: predicate,
    tthy: truthy,
    flsy: falsy,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end