Class: SyntaxTree::BraceBlock

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

Overview

BraceBlock represents passing a block to a method call using the { } operators.

method { |variable| variable + 1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lbrace:, block_var:, statements:, location:, comments: []) ⇒ BraceBlock

Returns a new instance of BraceBlock.



2760
2761
2762
2763
2764
2765
2766
# File 'lib/syntax_tree.rb', line 2760

def initialize(lbrace:, block_var:, statements:, location:, comments: [])
  @lbrace = lbrace
  @block_var = block_var
  @statements = statements
  @location = location
  @comments = comments
end

Instance Attribute Details

#block_varObject (readonly)

nil | BlockVar

the optional set of parameters to the block



2749
2750
2751
# File 'lib/syntax_tree.rb', line 2749

def block_var
  @block_var
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2758
2759
2760
# File 'lib/syntax_tree.rb', line 2758

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this block



2746
2747
2748
# File 'lib/syntax_tree.rb', line 2746

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



2755
2756
2757
# File 'lib/syntax_tree.rb', line 2755

def location
  @location
end

#statementsObject (readonly)

Statements

the list of expressions to evaluate within the block



2752
2753
2754
# File 'lib/syntax_tree.rb', line 2752

def statements
  @statements
end

Instance Method Details

#child_nodesObject



2768
2769
2770
# File 'lib/syntax_tree.rb', line 2768

def child_nodes
  [lbrace, block_var, statements]
end

#format(q) ⇒ Object



2772
2773
2774
# File 'lib/syntax_tree.rb', line 2772

def format(q)
  BlockFormatter.new(self, lbrace, statements).format(q)
end

#pretty_print(q) ⇒ Object



2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
# File 'lib/syntax_tree.rb', line 2776

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

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

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
# File 'lib/syntax_tree.rb', line 2792

def to_json(*opts)
  {
    type: :brace_block,
    lbrace: lbrace,
    block_var: block_var,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end