Class: SyntaxTree::Elsif

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Elsif represents another clause in an if or unless chain.

if variable
elsif other_variable
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif



5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
# File 'lib/syntax_tree.rb', line 5340

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5338
5339
5340
# File 'lib/syntax_tree.rb', line 5338

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



5332
5333
5334
# File 'lib/syntax_tree.rb', line 5332

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



5335
5336
5337
# File 'lib/syntax_tree.rb', line 5335

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



5326
5327
5328
# File 'lib/syntax_tree.rb', line 5326

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5329
5330
5331
# File 'lib/syntax_tree.rb', line 5329

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5354
5355
5356
# File 'lib/syntax_tree.rb', line 5354

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
# File 'lib/syntax_tree.rb', line 5358

def format(q)
  q.group do
    q.group do
      q.text("elsif ")
      q.nest("elsif".length - 1) { q.format(predicate) }
    end

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

    if consequent
      q.group do
        q.breakable(force: true)
        q.format(consequent)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
# File 'lib/syntax_tree.rb', line 5381

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

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

#to_json(*opts) ⇒ Object



5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
# File 'lib/syntax_tree.rb', line 5400

def to_json(*opts)
  {
    type: :elsif,
    pred: predicate,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end