Class: SyntaxTree::Unless

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



10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
# File 'lib/syntax_tree/node.rb', line 10602

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



10600
10601
10602
# File 'lib/syntax_tree/node.rb', line 10600

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



10594
10595
10596
# File 'lib/syntax_tree/node.rb', line 10594

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



10597
10598
10599
# File 'lib/syntax_tree/node.rb', line 10597

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



10588
10589
10590
# File 'lib/syntax_tree/node.rb', line 10588

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



10591
10592
10593
# File 'lib/syntax_tree/node.rb', line 10591

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10616
10617
10618
# File 'lib/syntax_tree/node.rb', line 10616

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



10622
10623
10624
10625
10626
10627
10628
10629
10630
# File 'lib/syntax_tree/node.rb', line 10622

def deconstruct_keys(keys)
  {
    predicate: predicate,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



10632
10633
10634
# File 'lib/syntax_tree/node.rb', line 10632

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

#pretty_print(q) ⇒ Object



10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
# File 'lib/syntax_tree/node.rb', line 10636

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



10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
# File 'lib/syntax_tree/node.rb', line 10655

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