Class: SyntaxTree::If

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

Overview

If represents the first clause in an if chain.

if predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of If.



5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
# File 'lib/syntax_tree/node.rb', line 5866

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



5864
5865
5866
# File 'lib/syntax_tree/node.rb', line 5864

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



5861
5862
5863
# File 'lib/syntax_tree/node.rb', line 5861

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



5855
5856
5857
# File 'lib/syntax_tree/node.rb', line 5855

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5858
5859
5860
# File 'lib/syntax_tree/node.rb', line 5858

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



5880
5881
5882
# File 'lib/syntax_tree/node.rb', line 5880

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



5886
5887
5888
5889
5890
5891
5892
5893
5894
# File 'lib/syntax_tree/node.rb', line 5886

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

#format(q) ⇒ Object



5896
5897
5898
# File 'lib/syntax_tree/node.rb', line 5896

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

#pretty_print(q) ⇒ Object



5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
# File 'lib/syntax_tree/node.rb', line 5900

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

    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



5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
# File 'lib/syntax_tree/node.rb', line 5919

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