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.



6484
6485
6486
6487
6488
6489
6490
# File 'lib/syntax_tree/node.rb', line 6484

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



6482
6483
6484
# File 'lib/syntax_tree/node.rb', line 6482

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



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

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6529
6530
6531
6532
# File 'lib/syntax_tree/node.rb', line 6529

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



6496
6497
6498
# File 'lib/syntax_tree/node.rb', line 6496

def child_nodes
  [predicate, statements, consequent]
end

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



6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
# File 'lib/syntax_tree/node.rb', line 6500

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



6515
6516
6517
6518
6519
6520
6521
6522
6523
# File 'lib/syntax_tree/node.rb', line 6515

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

#format(q) ⇒ Object



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

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)


6535
6536
6537
# File 'lib/syntax_tree/node.rb', line 6535

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