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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of IfOp.



5403
5404
5405
5406
5407
5408
5409
# File 'lib/syntax_tree/node.rb', line 5403

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



5401
5402
5403
# File 'lib/syntax_tree/node.rb', line 5401

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



5398
5399
5400
# File 'lib/syntax_tree/node.rb', line 5398

def falsy
  @falsy
end

#predicateObject (readonly)

untyped

the expression to be checked



5392
5393
5394
# File 'lib/syntax_tree/node.rb', line 5392

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



5395
5396
5397
# File 'lib/syntax_tree/node.rb', line 5395

def truthy
  @truthy
end

Instance Method Details

#accept(visitor) ⇒ Object



5411
5412
5413
# File 'lib/syntax_tree/node.rb', line 5411

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

#child_nodesObject Also known as: deconstruct



5415
5416
5417
# File 'lib/syntax_tree/node.rb', line 5415

def child_nodes
  [predicate, truthy, falsy]
end

#deconstruct_keys(_keys) ⇒ Object



5421
5422
5423
5424
5425
5426
5427
5428
5429
# File 'lib/syntax_tree/node.rb', line 5421

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

#format(q) ⇒ Object



5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
# File 'lib/syntax_tree/node.rb', line 5431

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