Class: SyntaxTree::Elsif

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

Overview

Elsif represents another clause in an if or unless chain.

if variable
elsif other_variable
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: []) ⇒ Elsif

Returns a new instance of Elsif.



4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
# File 'lib/syntax_tree/node.rb', line 4005

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



4003
4004
4005
# File 'lib/syntax_tree/node.rb', line 4003

def comments
  @comments
end

#consequent ⇒ Object (readonly)

[nil | Elsif | Else] the next clause in the chain



4000
4001
4002
# File 'lib/syntax_tree/node.rb', line 4000

def consequent
  @consequent
end

#predicate ⇒ Object (readonly)

[untyped] the expression to be checked



3994
3995
3996
# File 'lib/syntax_tree/node.rb', line 3994

def predicate
  @predicate
end

#statements ⇒ Object (readonly)

[Statements] the expressions to be executed



3997
3998
3999
# File 'lib/syntax_tree/node.rb', line 3997

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4019
4020
4021
# File 'lib/syntax_tree/node.rb', line 4019

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

#child_nodes ⇒ Object Also known as: deconstruct



4023
4024
4025
# File 'lib/syntax_tree/node.rb', line 4023

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



4029
4030
4031
4032
4033
4034
4035
4036
4037
# File 'lib/syntax_tree/node.rb', line 4029

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

#format(q) ⇒ Object



4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
# File 'lib/syntax_tree/node.rb', line 4039

def format(q)
  q.group do
    q.group do
      q.text("elsif ")
      q.nest("elsif".length - 1) { q.format(predicate) }
    end

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.group do
        q.breakable(force: true)
        q.format(consequent)
      end
    end
  end
end