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

Constructor Details

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

Returns a new instance of Else.



4495
4496
4497
4498
4499
# File 'lib/syntax_tree/node.rb', line 4495

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



4493
4494
4495
# File 'lib/syntax_tree/node.rb', line 4493

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be executed



4490
4491
4492
# File 'lib/syntax_tree/node.rb', line 4490

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



4501
4502
4503
# File 'lib/syntax_tree/node.rb', line 4501

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



4507
4508
4509
# File 'lib/syntax_tree/node.rb', line 4507

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

#format(q) ⇒ Object



4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
# File 'lib/syntax_tree/node.rb', line 4511

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



4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
# File 'lib/syntax_tree/node.rb', line 4524

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



4535
4536
4537
4538
4539
# File 'lib/syntax_tree/node.rb', line 4535

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