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.



3983
3984
3985
3986
3987
3988
# File 'lib/syntax_tree/node.rb', line 3983

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



3981
3982
3983
# File 'lib/syntax_tree/node.rb', line 3981

def comments
  @comments
end

#keywordObject (readonly)

Kw

the else keyword



3975
3976
3977
# File 'lib/syntax_tree/node.rb', line 3975

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



3978
3979
3980
# File 'lib/syntax_tree/node.rb', line 3978

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



3990
3991
3992
# File 'lib/syntax_tree/node.rb', line 3990

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [keyword, statements]
end

#deconstruct_keys(_keys) ⇒ Object



4000
4001
4002
4003
4004
4005
4006
4007
# File 'lib/syntax_tree/node.rb', line 4000

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

#format(q) ⇒ Object



4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
# File 'lib/syntax_tree/node.rb', line 4009

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