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.



7136
7137
7138
7139
7140
7141
7142
# File 'lib/syntax_tree.rb', line 7136

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



7134
7135
7136
# File 'lib/syntax_tree.rb', line 7134

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



7128
7129
7130
# File 'lib/syntax_tree.rb', line 7128

def falsy
  @falsy
end

#locationObject (readonly)

Location

the location of this node



7131
7132
7133
# File 'lib/syntax_tree.rb', line 7131

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



7122
7123
7124
# File 'lib/syntax_tree.rb', line 7122

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



7125
7126
7127
# File 'lib/syntax_tree.rb', line 7125

def truthy
  @truthy
end

Instance Method Details

#child_nodesObject



7144
7145
7146
# File 'lib/syntax_tree.rb', line 7144

def child_nodes
  [predicate, truthy, falsy]
end

#format(q) ⇒ Object



7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
# File 'lib/syntax_tree.rb', line 7148

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



7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
# File 'lib/syntax_tree.rb', line 7164

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



7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
# File 'lib/syntax_tree.rb', line 7181

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