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.



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

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



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

def comments
  @comments
end

#falsyObject (readonly)

[untyped] the expression to be executed if the predicate is falsy



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

def falsy
  @falsy
end

#predicateObject (readonly)

[untyped] the expression to be checked



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

def predicate
  @predicate
end

#truthyObject (readonly)

[untyped] the expression to be executed if the predicate is truthy



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

def truthy
  @truthy
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [predicate, truthy, falsy]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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