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.



5359
5360
5361
5362
5363
5364
5365
# File 'lib/syntax_tree/node.rb', line 5359

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



5357
5358
5359
# File 'lib/syntax_tree/node.rb', line 5357

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



5354
5355
5356
# File 'lib/syntax_tree/node.rb', line 5354

def falsy
  @falsy
end

#predicateObject (readonly)

untyped

the expression to be checked



5348
5349
5350
# File 'lib/syntax_tree/node.rb', line 5348

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



5351
5352
5353
# File 'lib/syntax_tree/node.rb', line 5351

def truthy
  @truthy
end

Instance Method Details

#accept(visitor) ⇒ Object



5367
5368
5369
# File 'lib/syntax_tree/node.rb', line 5367

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

#child_nodesObject Also known as: deconstruct



5371
5372
5373
# File 'lib/syntax_tree/node.rb', line 5371

def child_nodes
  [predicate, truthy, falsy]
end

#deconstruct_keys(_keys) ⇒ Object



5377
5378
5379
5380
5381
5382
5383
5384
5385
# File 'lib/syntax_tree/node.rb', line 5377

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

#format(q) ⇒ Object



5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
# File 'lib/syntax_tree/node.rb', line 5387

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