Class: SyntaxTree::BodyStmt

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

Overview

bodystmt can’t actually determine its bounds appropriately because it doesn’t necessarily know where it started. So the parent node needs to report back down into this one where it goes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt

Returns a new instance of BodyStmt.



2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
# File 'lib/syntax_tree.rb', line 2523

def initialize(
  statements:,
  rescue_clause:,
  else_clause:,
  ensure_clause:,
  location:,
  comments: []
)
  @statements = statements
  @rescue_clause = rescue_clause
  @else_clause = else_clause
  @ensure_clause = ensure_clause
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2521
2522
2523
# File 'lib/syntax_tree.rb', line 2521

def comments
  @comments
end

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



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

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



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

def ensure_clause
  @ensure_clause
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



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

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



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

def statements
  @statements
end

Instance Method Details

#bind(start_char, end_char) ⇒ Object



2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
# File 'lib/syntax_tree.rb', line 2539

def bind(start_char, end_char)
  @location =
    Location.new(
      start_line: location.start_line,
      start_char: start_char,
      end_line: location.end_line,
      end_char: end_char
    )

  parts = [rescue_clause, else_clause, ensure_clause]

  # Here we're going to determine the bounds for the statements
  consequent = parts.compact.first
  statements.bind(
    start_char,
    consequent ? consequent.location.start_char : end_char
  )

  # Next we're going to determine the rescue clause if there is one
  if rescue_clause
    consequent = parts.drop(1).compact.first
    rescue_clause.bind_end(
      consequent ? consequent.location.start_char : end_char
    )
  end
end

#child_nodesObject



2570
2571
2572
# File 'lib/syntax_tree.rb', line 2570

def child_nodes
  [statements, rescue_clause, else_clause, ensure_clause]
end

#empty?Boolean

Returns:

  • (Boolean)


2566
2567
2568
# File 'lib/syntax_tree.rb', line 2566

def empty?
  statements.empty? && !rescue_clause && !else_clause && !ensure_clause
end

#format(q) ⇒ Object



2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
# File 'lib/syntax_tree.rb', line 2574

def format(q)
  q.group do
    q.format(statements) unless statements.empty?

    if rescue_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(rescue_clause)
      end
    end

    if else_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.text("else")
      end
      q.breakable(force: true)
      q.format(else_clause)
    end

    if ensure_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(ensure_clause)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
# File 'lib/syntax_tree.rb', line 2603

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

    q.breakable
    q.pp(statements)

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

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

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

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

#to_json(*opts) ⇒ Object



2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
# File 'lib/syntax_tree.rb', line 2629

def to_json(*opts)
  {
    type: :bodystmt,
    stmts: statements,
    rsc: rescue_clause,
    els: else_clause,
    ens: ensure_clause,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end