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.



2968
2969
2970
2971
2972
2973
2974
# File 'lib/syntax_tree.rb', line 2968

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



2957
2958
2959
# File 'lib/syntax_tree.rb', line 2957

def block_var
  @block_var
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2966
2967
2968
# File 'lib/syntax_tree.rb', line 2966

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this block



2954
2955
2956
# File 'lib/syntax_tree.rb', line 2954

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



2963
2964
2965
# File 'lib/syntax_tree.rb', line 2963

def location
  @location
end

#statementsObject (readonly)

Statements

the list of expressions to evaluate within the block



2960
2961
2962
# File 'lib/syntax_tree.rb', line 2960

def statements
  @statements
end

Instance Method Details

#child_nodesObject



2976
2977
2978
# File 'lib/syntax_tree.rb', line 2976

def child_nodes
  [lbrace, block_var, statements]
end

#format(q) ⇒ Object



2980
2981
2982
# File 'lib/syntax_tree.rb', line 2980

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

#pretty_print(q) ⇒ Object



2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
# File 'lib/syntax_tree.rb', line 2984

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



3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
# File 'lib/syntax_tree.rb', line 3000

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