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.



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

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



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

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



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

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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