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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Else.



4642
4643
4644
4645
4646
# File 'lib/syntax_tree/node.rb', line 4642

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4640
4641
4642
# File 'lib/syntax_tree/node.rb', line 4640

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4637
4638
4639
# File 'lib/syntax_tree/node.rb', line 4637

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



4634
4635
4636
# File 'lib/syntax_tree/node.rb', line 4634

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



4648
4649
4650
# File 'lib/syntax_tree/node.rb', line 4648

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



4654
4655
4656
# File 'lib/syntax_tree/node.rb', line 4654

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

#format(q) ⇒ Object



4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
# File 'lib/syntax_tree/node.rb', line 4658

def format(q)
  q.group do
    q.text("else")

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

#pretty_print(q) ⇒ Object



4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
# File 'lib/syntax_tree/node.rb', line 4671

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("else")

    q.breakable
    q.pp(statements)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4682
4683
4684
4685
4686
# File 'lib/syntax_tree/node.rb', line 4682

def to_json(*opts)
  { type: :else, stmts: statements, loc: location, cmts: comments }.to_json(
    *opts
  )
end