Class: SyntaxTree::Else

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



5289
5290
5291
5292
5293
# File 'lib/syntax_tree.rb', line 5289

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



5287
5288
5289
# File 'lib/syntax_tree.rb', line 5287

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5284
5285
5286
# File 'lib/syntax_tree.rb', line 5284

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



5281
5282
5283
# File 'lib/syntax_tree.rb', line 5281

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5295
5296
5297
# File 'lib/syntax_tree.rb', line 5295

def child_nodes
  [statements]
end

#format(q) ⇒ Object



5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
# File 'lib/syntax_tree.rb', line 5299

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



5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
# File 'lib/syntax_tree.rb', line 5312

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



5323
5324
5325
5326
5327
# File 'lib/syntax_tree.rb', line 5323

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