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.



6944
6945
6946
6947
6948
6949
6950
# File 'lib/syntax_tree.rb', line 6944

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



6942
6943
6944
# File 'lib/syntax_tree.rb', line 6942

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



6936
6937
6938
# File 'lib/syntax_tree.rb', line 6936

def falsy
  @falsy
end

#locationObject (readonly)

Location

the location of this node



6939
6940
6941
# File 'lib/syntax_tree.rb', line 6939

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6930
6931
6932
# File 'lib/syntax_tree.rb', line 6930

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



6933
6934
6935
# File 'lib/syntax_tree.rb', line 6933

def truthy
  @truthy
end

Instance Method Details

#child_nodesObject



6952
6953
6954
# File 'lib/syntax_tree.rb', line 6952

def child_nodes
  [predicate, truthy, falsy]
end

#format(q) ⇒ Object



6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
# File 'lib/syntax_tree.rb', line 6956

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



6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
# File 'lib/syntax_tree.rb', line 6972

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



6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
# File 'lib/syntax_tree.rb', line 6989

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