Class: SyntaxTree::BraceBlock

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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.



2520
2521
2522
2523
2524
2525
2526
# File 'lib/syntax_tree/node.rb', line 2520

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



2509
2510
2511
# File 'lib/syntax_tree/node.rb', line 2509

def block_var
  @block_var
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2518
2519
2520
# File 'lib/syntax_tree/node.rb', line 2518

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this block



2506
2507
2508
# File 'lib/syntax_tree/node.rb', line 2506

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



2515
2516
2517
# File 'lib/syntax_tree/node.rb', line 2515

def location
  @location
end

#statementsObject (readonly)

Statements

the list of expressions to evaluate within the block



2512
2513
2514
# File 'lib/syntax_tree/node.rb', line 2512

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2528
2529
2530
# File 'lib/syntax_tree/node.rb', line 2528

def child_nodes
  [lbrace, block_var, statements]
end

#deconstruct_keys(keys) ⇒ Object



2534
2535
2536
2537
2538
2539
2540
2541
2542
# File 'lib/syntax_tree/node.rb', line 2534

def deconstruct_keys(keys)
  {
    lbrace: lbrace,
    block_var: block_var,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2544
2545
2546
# File 'lib/syntax_tree/node.rb', line 2544

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

#pretty_print(q) ⇒ Object



2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
# File 'lib/syntax_tree/node.rb', line 2548

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



2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
# File 'lib/syntax_tree/node.rb', line 2564

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