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

Returns a new instance of Elsif.



5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
# File 'lib/syntax_tree.rb', line 5377

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



5375
5376
5377
# File 'lib/syntax_tree.rb', line 5375

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



5369
5370
5371
# File 'lib/syntax_tree.rb', line 5369

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



5372
5373
5374
# File 'lib/syntax_tree.rb', line 5372

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



5363
5364
5365
# File 'lib/syntax_tree.rb', line 5363

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5366
5367
5368
# File 'lib/syntax_tree.rb', line 5366

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5391
5392
5393
# File 'lib/syntax_tree.rb', line 5391

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
# File 'lib/syntax_tree.rb', line 5395

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



5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
# File 'lib/syntax_tree.rb', line 5418

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



5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
# File 'lib/syntax_tree.rb', line 5437

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