Class: SyntaxTree::IfMod

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

Overview

IfMod represents the modifier form of an if statement.

expression if predicate

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(statement:, predicate:, location:, comments: []) ⇒ IfMod

Returns a new instance of IfMod.



6111
6112
6113
6114
6115
6116
# File 'lib/syntax_tree/node.rb', line 6111

def initialize(statement:, predicate:, location:, comments: [])
  @statement = statement
  @predicate = predicate
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6109
6110
6111
# File 'lib/syntax_tree/node.rb', line 6109

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



6106
6107
6108
# File 'lib/syntax_tree/node.rb', line 6106

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



6103
6104
6105
# File 'lib/syntax_tree/node.rb', line 6103

def statement
  @statement
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6118
6119
6120
# File 'lib/syntax_tree/node.rb', line 6118

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



6133
6134
6135
# File 'lib/syntax_tree/node.rb', line 6133

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

#pretty_print(q) ⇒ Object



6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
# File 'lib/syntax_tree/node.rb', line 6137

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

    q.breakable
    q.pp(statement)

    q.breakable
    q.pp(predicate)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



6151
6152
6153
6154
6155
6156
6157
6158
6159
# File 'lib/syntax_tree/node.rb', line 6151

def to_json(*opts)
  {
    type: :if_mod,
    stmt: statement,
    pred: predicate,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end