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.



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

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



6458
6459
6460
# File 'lib/syntax_tree/node.rb', line 6458

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



6455
6456
6457
# File 'lib/syntax_tree/node.rb', line 6455

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



6449
6450
6451
# File 'lib/syntax_tree/node.rb', line 6449

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6452
6453
6454
# File 'lib/syntax_tree/node.rb', line 6452

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6505
6506
6507
6508
# File 'lib/syntax_tree/node.rb', line 6505

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

#accept(visitor) ⇒ Object



6468
6469
6470
# File 'lib/syntax_tree/node.rb', line 6468

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

#child_nodesObject Also known as: deconstruct



6472
6473
6474
# File 'lib/syntax_tree/node.rb', line 6472

def child_nodes
  [predicate, statements, consequent]
end

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



6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
# File 'lib/syntax_tree/node.rb', line 6476

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



6491
6492
6493
6494
6495
6496
6497
6498
6499
# File 'lib/syntax_tree/node.rb', line 6491

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

#format(q) ⇒ Object



6501
6502
6503
# File 'lib/syntax_tree/node.rb', line 6501

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)


6511
6512
6513
# File 'lib/syntax_tree/node.rb', line 6511

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