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.



6481
6482
6483
6484
6485
6486
6487
# File 'lib/syntax_tree/node.rb', line 6481

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



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

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



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

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6526
6527
6528
6529
# File 'lib/syntax_tree/node.rb', line 6526

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

#accept(visitor) ⇒ Object



6489
6490
6491
# File 'lib/syntax_tree/node.rb', line 6489

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

#child_nodesObject Also known as: deconstruct



6493
6494
6495
# File 'lib/syntax_tree/node.rb', line 6493

def child_nodes
  [predicate, statements, consequent]
end

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



6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
# File 'lib/syntax_tree/node.rb', line 6497

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



6512
6513
6514
6515
6516
6517
6518
6519
6520
# File 'lib/syntax_tree/node.rb', line 6512

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

#format(q) ⇒ Object



6522
6523
6524
# File 'lib/syntax_tree/node.rb', line 6522

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)


6532
6533
6534
# File 'lib/syntax_tree/node.rb', line 6532

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