Class: SyntaxTree::Unless

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

Overview

Unless represents the first clause in an unless chain.

unless predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Unless.



12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
# File 'lib/syntax_tree.rb', line 12473

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



12471
12472
12473
# File 'lib/syntax_tree.rb', line 12471

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



12465
12466
12467
# File 'lib/syntax_tree.rb', line 12465

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



12468
12469
12470
# File 'lib/syntax_tree.rb', line 12468

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



12459
12460
12461
# File 'lib/syntax_tree.rb', line 12459

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



12462
12463
12464
# File 'lib/syntax_tree.rb', line 12462

def statements
  @statements
end

Instance Method Details

#child_nodesObject



12487
12488
12489
# File 'lib/syntax_tree.rb', line 12487

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



12491
12492
12493
# File 'lib/syntax_tree.rb', line 12491

def format(q)
  ConditionalFormatter.new("unless", self).format(q)
end

#pretty_print(q) ⇒ Object



12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
# File 'lib/syntax_tree.rb', line 12495

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

    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



12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
# File 'lib/syntax_tree.rb', line 12514

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