Class: SyntaxTree::BlockVar

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

Overview

BlockVar represents the parameters being declared for a block. Effectively this node is everything contained within the pipes. This includes all of the various parameter types, as well as block-local variable declarations.

method do |positional, optional = value, keyword:, █ local|
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, locals:, location:, comments: []) ⇒ BlockVar

Returns a new instance of BlockVar.



2377
2378
2379
2380
2381
2382
# File 'lib/syntax_tree.rb', line 2377

def initialize(params:, locals:, location:, comments: [])
  @params = params
  @locals = locals
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2375
2376
2377
# File 'lib/syntax_tree.rb', line 2375

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2369
2370
2371
# File 'lib/syntax_tree.rb', line 2369

def locals
  @locals
end

#locationObject (readonly)

Location

the location of this node



2372
2373
2374
# File 'lib/syntax_tree.rb', line 2372

def location
  @location
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2366
2367
2368
# File 'lib/syntax_tree.rb', line 2366

def params
  @params
end

Instance Method Details

#child_nodesObject



2384
2385
2386
# File 'lib/syntax_tree.rb', line 2384

def child_nodes
  [params, *locals]
end

#format(q) ⇒ Object



2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
# File 'lib/syntax_tree.rb', line 2388

def format(q)
  q.group(0, "|", "|") do
    doc = q.format(params)
    RemoveBreaks.call(doc)

    if locals.any?
      q.text("; ")
      q.seplist(locals, -> { q.text(", ") }) { |local| q.format(local) }
    end
  end
end

#pretty_print(q) ⇒ Object



2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
# File 'lib/syntax_tree.rb', line 2400

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

    q.breakable
    q.pp(params)

    if locals.any?
      q.breakable
      q.group(2, "(", ")") { q.seplist(locals) { |local| q.pp(local) } }
    end

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

#to_json(*opts) ⇒ Object



2416
2417
2418
2419
2420
2421
2422
2423
2424
# File 'lib/syntax_tree.rb', line 2416

def to_json(*opts)
  {
    type: :block_var,
    params: params,
    locals: locals,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end