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.



5252
5253
5254
5255
5256
# File 'lib/syntax_tree.rb', line 5252

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



5250
5251
5252
# File 'lib/syntax_tree.rb', line 5250

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5247
5248
5249
# File 'lib/syntax_tree.rb', line 5247

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



5244
5245
5246
# File 'lib/syntax_tree.rb', line 5244

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5258
5259
5260
# File 'lib/syntax_tree.rb', line 5258

def child_nodes
  [statements]
end

#format(q) ⇒ Object



5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
# File 'lib/syntax_tree.rb', line 5262

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



5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
# File 'lib/syntax_tree.rb', line 5275

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



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

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