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.



2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
# File 'lib/syntax_tree.rb', line 2653

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



2651
2652
2653
# File 'lib/syntax_tree.rb', line 2651

def comments
  @comments
end

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



2642
2643
2644
# File 'lib/syntax_tree.rb', line 2642

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



2645
2646
2647
# File 'lib/syntax_tree.rb', line 2645

def ensure_clause
  @ensure_clause
end

#locationObject (readonly)

Location

the location of this node



2648
2649
2650
# File 'lib/syntax_tree.rb', line 2648

def location
  @location
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



2639
2640
2641
# File 'lib/syntax_tree.rb', line 2639

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



2636
2637
2638
# File 'lib/syntax_tree.rb', line 2636

def statements
  @statements
end

Instance Method Details

#bind(start_char, end_char) ⇒ Object



2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
# File 'lib/syntax_tree.rb', line 2669

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



2700
2701
2702
# File 'lib/syntax_tree.rb', line 2700

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

#empty?Boolean

Returns:

  • (Boolean)


2696
2697
2698
# File 'lib/syntax_tree.rb', line 2696

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

#format(q) ⇒ Object



2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
# File 'lib/syntax_tree.rb', line 2704

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



2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
# File 'lib/syntax_tree.rb', line 2733

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



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

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