Class: YARP::IfNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents the use of the ‘if` keyword, either in the block form or the modifier form.

bar if foo
^^^^^^^^^^

if foo then bar end
^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(if_keyword_loc, predicate, statements, consequent, end_keyword_loc, location) ⇒ IfNode

def initialize: (if_keyword_loc: Location?, predicate: Node, statements: StatementsNode?, consequent: Node?, end_keyword_loc: Location?, location: Location) -> void



5111
5112
5113
5114
5115
5116
5117
5118
# File 'lib/yarp/node.rb', line 5111

def initialize(if_keyword_loc, predicate, statements, consequent, end_keyword_loc, location)
  @if_keyword_loc = if_keyword_loc
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#consequentObject (readonly)

attr_reader consequent: Node?



5105
5106
5107
# File 'lib/yarp/node.rb', line 5105

def consequent
  @consequent
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location?



5108
5109
5110
# File 'lib/yarp/node.rb', line 5108

def end_keyword_loc
  @end_keyword_loc
end

#if_keyword_locObject (readonly)

attr_reader if_keyword_loc: Location?



5096
5097
5098
# File 'lib/yarp/node.rb', line 5096

def if_keyword_loc
  @if_keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node



5099
5100
5101
# File 'lib/yarp/node.rb', line 5099

def predicate
  @predicate
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



5102
5103
5104
# File 'lib/yarp/node.rb', line 5102

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



5121
5122
5123
# File 'lib/yarp/node.rb', line 5121

def accept(visitor)
  visitor.visit_if_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



5130
5131
5132
# File 'lib/yarp/node.rb', line 5130

def child_nodes
  [predicate, statements, consequent]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



5135
5136
5137
# File 'lib/yarp/node.rb', line 5135

def comment_targets
  [*if_keyword_loc, predicate, *statements, *consequent, *end_keyword_loc]
end

#copy(**params) ⇒ Object

def copy: (**params) -> IfNode



5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
# File 'lib/yarp/node.rb', line 5140

def copy(**params)
  IfNode.new(
    params.fetch(:if_keyword_loc) { if_keyword_loc },
    params.fetch(:predicate) { predicate },
    params.fetch(:statements) { statements },
    params.fetch(:consequent) { consequent },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



5155
5156
5157
# File 'lib/yarp/node.rb', line 5155

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

#end_keywordObject

def end_keyword: () -> String?



5165
5166
5167
# File 'lib/yarp/node.rb', line 5165

def end_keyword
  end_keyword_loc&.slice
end

#if_keywordObject

def if_keyword: () -> String?



5160
5161
5162
# File 'lib/yarp/node.rb', line 5160

def if_keyword
  if_keyword_loc&.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
# File 'lib/yarp/node.rb', line 5169

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── if_keyword_loc: #{inspector.location(if_keyword_loc)}\n"
  inspector << "├── predicate:\n"
  inspector << inspector.child_node(predicate, "")
  if (statements = self.statements).nil?
    inspector << "├── statements: ∅\n"
  else
    inspector << "├── statements:\n"
    inspector << statements.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (consequent = self.consequent).nil?
    inspector << "├── consequent: ∅\n"
  else
    inspector << "├── consequent:\n"
    inspector << consequent.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
end

#set_newline_flag(newline_marked) ⇒ Object



5125
5126
5127
# File 'lib/yarp/node.rb', line 5125

def set_newline_flag(newline_marked)
  predicate.set_newline_flag(newline_marked)
end