Class: SyntaxTree::If

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

#pretty_print, #to_json

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ If

Returns a new instance of If.



4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
# File 'lib/syntax_tree/node.rb', line 4697

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4695
4696
4697
# File 'lib/syntax_tree/node.rb', line 4695

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



4692
4693
4694
# File 'lib/syntax_tree/node.rb', line 4692

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4686
4687
4688
# File 'lib/syntax_tree/node.rb', line 4686

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4689
4690
4691
# File 'lib/syntax_tree/node.rb', line 4689

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4711
4712
4713
# File 'lib/syntax_tree/node.rb', line 4711

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

#child_nodesObject Also known as: deconstruct



4715
4716
4717
# File 'lib/syntax_tree/node.rb', line 4715

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



4721
4722
4723
4724
4725
4726
4727
4728
4729
# File 'lib/syntax_tree/node.rb', line 4721

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

#format(q) ⇒ Object



4731
4732
4733
# File 'lib/syntax_tree/node.rb', line 4731

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