Class: SyntaxTree::Else

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

Overview

Else represents the end of an if, unless, or case chain.

if variable
else
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(keyword:, statements:, location:, comments: []) ⇒ Else

Returns a new instance of Else.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4014
4015
4016
# File 'lib/syntax_tree/node.rb', line 4014

def comments
  @comments
end

#keywordObject (readonly)

Kw

the else keyword



4008
4009
4010
# File 'lib/syntax_tree/node.rb', line 4008

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



4011
4012
4013
# File 'lib/syntax_tree/node.rb', line 4011

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



4027
4028
4029
# File 'lib/syntax_tree/node.rb', line 4027

def child_nodes
  [keyword, statements]
end

#deconstruct_keys(_keys) ⇒ Object



4033
4034
4035
4036
4037
4038
4039
4040
# File 'lib/syntax_tree/node.rb', line 4033

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

#format(q) ⇒ Object



4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
# File 'lib/syntax_tree/node.rb', line 4042

def format(q)
  q.group do
    q.format(keyword)

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