Class: Prism::WhileNode

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

Overview

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

bar while foo
^^^^^^^^^^^^^

while foo do bar end
^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword_loc, closing_loc, predicate, statements, flags, location) ⇒ WhileNode

def initialize: (keyword_loc: Location, closing_loc: Location?, predicate: Node, statements: StatementsNode?, flags: Integer, location: Location) -> void



14148
14149
14150
14151
14152
14153
14154
14155
# File 'lib/prism/node.rb', line 14148

def initialize(keyword_loc, closing_loc, predicate, statements, flags, location)
  @keyword_loc = keyword_loc
  @closing_loc = closing_loc
  @predicate = predicate
  @statements = statements
  @flags = flags
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



14136
14137
14138
# File 'lib/prism/node.rb', line 14136

def closing_loc
  @closing_loc
end

#keyword_locObject (readonly)

attr_reader keyword_loc: Location



14133
14134
14135
# File 'lib/prism/node.rb', line 14133

def keyword_loc
  @keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node



14139
14140
14141
# File 'lib/prism/node.rb', line 14139

def predicate
  @predicate
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



14142
14143
14144
# File 'lib/prism/node.rb', line 14142

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



14158
14159
14160
# File 'lib/prism/node.rb', line 14158

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

#begin_modifier?Boolean

def begin_modifier?: () -> bool

Returns:

  • (Boolean)


14215
14216
14217
# File 'lib/prism/node.rb', line 14215

def begin_modifier?
  flags.anybits?(LoopFlags::BEGIN_MODIFIER)
end

#child_nodesObject Also known as: deconstruct

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



14167
14168
14169
# File 'lib/prism/node.rb', line 14167

def child_nodes
  [predicate, statements]
end

#closingObject

def closing: () -> String?



14210
14211
14212
# File 'lib/prism/node.rb', line 14210

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



14180
14181
14182
# File 'lib/prism/node.rb', line 14180

def comment_targets
  [keyword_loc, *closing_loc, predicate, *statements]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14172
14173
14174
14175
14176
14177
# File 'lib/prism/node.rb', line 14172

def compact_child_nodes
  compact = []
  compact << predicate
  compact << statements if statements
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> WhileNode



14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
# File 'lib/prism/node.rb', line 14185

def copy(**params)
  WhileNode.new(
    params.fetch(:keyword_loc) { keyword_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:predicate) { predicate },
    params.fetch(:statements) { statements },
    params.fetch(:flags) { flags },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



14200
14201
14202
# File 'lib/prism/node.rb', line 14200

def deconstruct_keys(keys)
  { keyword_loc: keyword_loc, closing_loc: closing_loc, predicate: predicate, statements: statements, flags: flags, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
# File 'lib/prism/node.rb', line 14219

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n"
  inspector << "├── closing_loc: #{inspector.location(closing_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
  flags = [("begin_modifier" if begin_modifier?)].compact
  inspector << "└── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector.to_str
end

#keywordObject

def keyword: () -> String



14205
14206
14207
# File 'lib/prism/node.rb', line 14205

def keyword
  keyword_loc.slice
end

#set_newline_flag(newline_marked) ⇒ Object



14162
14163
14164
# File 'lib/prism/node.rb', line 14162

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

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



14250
14251
14252
# File 'lib/prism/node.rb', line 14250

def type
  :while_node
end