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.



5276
5277
5278
5279
5280
5281
5282
# File 'lib/syntax_tree/node.rb', line 5276

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



5274
5275
5276
# File 'lib/syntax_tree/node.rb', line 5274

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



5271
5272
5273
# File 'lib/syntax_tree/node.rb', line 5271

def falsy
  @falsy
end

#predicateObject (readonly)

untyped

the expression to be checked



5265
5266
5267
# File 'lib/syntax_tree/node.rb', line 5265

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



5268
5269
5270
# File 'lib/syntax_tree/node.rb', line 5268

def truthy
  @truthy
end

Instance Method Details

#accept(visitor) ⇒ Object



5284
5285
5286
# File 'lib/syntax_tree/node.rb', line 5284

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

#child_nodesObject Also known as: deconstruct



5288
5289
5290
# File 'lib/syntax_tree/node.rb', line 5288

def child_nodes
  [predicate, truthy, falsy]
end

#deconstruct_keys(keys) ⇒ Object



5294
5295
5296
5297
5298
5299
5300
5301
5302
# File 'lib/syntax_tree/node.rb', line 5294

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

#format(q) ⇒ Object



5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
# File 'lib/syntax_tree/node.rb', line 5304

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