Class: SyntaxTree::BlockArg

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

Overview

BlockArg represents declaring a block parameter on a method definition.

def method(&block); end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, location:, comments: []) ⇒ BlockArg

Returns a new instance of BlockArg.



2706
2707
2708
2709
2710
# File 'lib/syntax_tree.rb', line 2706

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2704
2705
2706
# File 'lib/syntax_tree.rb', line 2704

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2701
2702
2703
# File 'lib/syntax_tree.rb', line 2701

def location
  @location
end

#nameObject (readonly)

nil | Ident

the name of the block argument



2698
2699
2700
# File 'lib/syntax_tree.rb', line 2698

def name
  @name
end

Instance Method Details

#child_nodesObject



2712
2713
2714
# File 'lib/syntax_tree.rb', line 2712

def child_nodes
  [name]
end

#format(q) ⇒ Object



2716
2717
2718
2719
# File 'lib/syntax_tree.rb', line 2716

def format(q)
  q.text("&")
  q.format(name) if name
end

#pretty_print(q) ⇒ Object



2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
# File 'lib/syntax_tree.rb', line 2721

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

    if name
      q.breakable
      q.pp(name)
    end

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

#to_json(*opts) ⇒ Object



2734
2735
2736
2737
2738
# File 'lib/syntax_tree.rb', line 2734

def to_json(*opts)
  { type: :blockarg, name: name, loc: location, cmts: comments }.to_json(
    *opts
  )
end