Class: SyntaxTree::IfNode

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

If represents the first clause in an if chain.

if predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(predicate:, statements:, consequent:, location:) ⇒ IfNode

Returns a new instance of IfNode.



6429
6430
6431
6432
6433
6434
6435
# File 'lib/syntax_tree/node.rb', line 6429

def initialize(predicate:, statements:, consequent:, location:)
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6427
6428
6429
# File 'lib/syntax_tree/node.rb', line 6427

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



6424
6425
6426
# File 'lib/syntax_tree/node.rb', line 6424

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



6418
6419
6420
# File 'lib/syntax_tree/node.rb', line 6418

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6421
6422
6423
# File 'lib/syntax_tree/node.rb', line 6421

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6474
6475
6476
6477
# File 'lib/syntax_tree/node.rb', line 6474

def ===(other)
  other.is_a?(IfNode) && predicate === other.predicate &&
    statements === other.statements && consequent === other.consequent
end

#accept(visitor) ⇒ Object



6437
6438
6439
# File 'lib/syntax_tree/node.rb', line 6437

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

#child_nodesObject Also known as: deconstruct



6441
6442
6443
# File 'lib/syntax_tree/node.rb', line 6441

def child_nodes
  [predicate, statements, consequent]
end

#copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object



6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
# File 'lib/syntax_tree/node.rb', line 6445

def copy(predicate: nil, statements: nil, consequent: nil, location: nil)
  node =
    IfNode.new(
      predicate: predicate || self.predicate,
      statements: statements || self.statements,
      consequent: consequent || self.consequent,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



6460
6461
6462
6463
6464
6465
6466
6467
6468
# File 'lib/syntax_tree/node.rb', line 6460

def deconstruct_keys(_keys)
  {
    predicate: predicate,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6470
6471
6472
# File 'lib/syntax_tree/node.rb', line 6470

def format(q)
  ConditionalFormatter.new("if", self).format(q)
end

#modifier?Boolean

Checks if the node was originally found in the modifier form.

Returns:

  • (Boolean)


6480
6481
6482
# File 'lib/syntax_tree/node.rb', line 6480

def modifier?
  predicate.location.start_char > statements.location.start_char
end