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.



2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
# File 'lib/syntax_tree.rb', line 2636

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



2634
2635
2636
# File 'lib/syntax_tree.rb', line 2634

def comments
  @comments
end

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



2625
2626
2627
# File 'lib/syntax_tree.rb', line 2625

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



2628
2629
2630
# File 'lib/syntax_tree.rb', line 2628

def ensure_clause
  @ensure_clause
end

#locationObject (readonly)

Location

the location of this node



2631
2632
2633
# File 'lib/syntax_tree.rb', line 2631

def location
  @location
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



2622
2623
2624
# File 'lib/syntax_tree.rb', line 2622

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



2619
2620
2621
# File 'lib/syntax_tree.rb', line 2619

def statements
  @statements
end

Instance Method Details

#bind(start_char, end_char) ⇒ Object



2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
# File 'lib/syntax_tree.rb', line 2652

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



2683
2684
2685
# File 'lib/syntax_tree.rb', line 2683

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

#empty?Boolean

Returns:

  • (Boolean)


2679
2680
2681
# File 'lib/syntax_tree.rb', line 2679

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

#format(q) ⇒ Object



2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
# File 'lib/syntax_tree.rb', line 2687

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



2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
# File 'lib/syntax_tree.rb', line 2716

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



2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
# File 'lib/syntax_tree.rb', line 2742

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