Class: SyntaxTree::For

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

Overview

For represents using a for loop.

for value in list do
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(index:, collection:, statements:, location:, comments: []) ⇒ For

Returns a new instance of For.



5200
5201
5202
5203
5204
5205
5206
# File 'lib/syntax_tree/node.rb', line 5200

def initialize(index:, collection:, statements:, location:, comments: [])
  @index = index
  @collection = collection
  @statements = statements
  @location = location
  @comments = comments
end

Instance Attribute Details

#collectionObject (readonly)

untyped

the object being enumerated in the loop



5192
5193
5194
# File 'lib/syntax_tree/node.rb', line 5192

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5198
5199
5200
# File 'lib/syntax_tree/node.rb', line 5198

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5189
5190
5191
# File 'lib/syntax_tree/node.rb', line 5189

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



5195
5196
5197
# File 'lib/syntax_tree/node.rb', line 5195

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



5208
5209
5210
# File 'lib/syntax_tree/node.rb', line 5208

def child_nodes
  [index, collection, statements]
end

#deconstruct_keys(keys) ⇒ Object



5214
5215
5216
5217
5218
5219
5220
5221
5222
# File 'lib/syntax_tree/node.rb', line 5214

def deconstruct_keys(keys)
  {
    index: index,
    collection: collection,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
# File 'lib/syntax_tree/node.rb', line 5224

def format(q)
  q.group do
    q.text("for ")
    q.group { q.format(index) }
    q.text(" in ")
    q.format(collection)

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    q.breakable(force: true)
    q.text("end")
  end
end

#pretty_print(q) ⇒ Object



5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
# File 'lib/syntax_tree/node.rb', line 5243

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

    q.breakable
    q.pp(index)

    q.breakable
    q.pp(collection)

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
# File 'lib/syntax_tree/node.rb', line 5260

def to_json(*opts)
  {
    type: :for,
    index: index,
    collection: collection,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end