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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of If.



6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
# File 'lib/syntax_tree/node.rb', line 6070

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



6068
6069
6070
# File 'lib/syntax_tree/node.rb', line 6068

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



6062
6063
6064
# File 'lib/syntax_tree/node.rb', line 6062

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



6065
6066
6067
# File 'lib/syntax_tree/node.rb', line 6065

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6056
6057
6058
# File 'lib/syntax_tree/node.rb', line 6056

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6059
6060
6061
# File 'lib/syntax_tree/node.rb', line 6059

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6084
6085
6086
# File 'lib/syntax_tree/node.rb', line 6084

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



6090
6091
6092
6093
6094
6095
6096
6097
6098
# File 'lib/syntax_tree/node.rb', line 6090

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

#format(q) ⇒ Object



6100
6101
6102
# File 'lib/syntax_tree/node.rb', line 6100

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

#pretty_print(q) ⇒ Object



6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
# File 'lib/syntax_tree/node.rb', line 6104

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



6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
# File 'lib/syntax_tree/node.rb', line 6123

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