Class: SyntaxTree::When

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

Overview

When represents a when clause in a case chain.

case value
when predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When

Returns a new instance of When.



9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
# File 'lib/syntax_tree/node.rb', line 9697

def initialize(
  arguments:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @arguments = arguments
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments to the when clause



9686
9687
9688
# File 'lib/syntax_tree/node.rb', line 9686

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9695
9696
9697
# File 'lib/syntax_tree/node.rb', line 9695

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



9692
9693
9694
# File 'lib/syntax_tree/node.rb', line 9692

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



9689
9690
9691
# File 'lib/syntax_tree/node.rb', line 9689

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9711
9712
9713
# File 'lib/syntax_tree/node.rb', line 9711

def accept(visitor)
  visitor.visit_when(self)
end

#child_nodesObject Also known as: deconstruct



9715
9716
9717
# File 'lib/syntax_tree/node.rb', line 9715

def child_nodes
  [arguments, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



9721
9722
9723
9724
9725
9726
9727
9728
9729
# File 'lib/syntax_tree/node.rb', line 9721

def deconstruct_keys(_keys)
  {
    arguments: arguments,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
# File 'lib/syntax_tree/node.rb', line 9731

def format(q)
  keyword = "when "

  q.group do
    q.group do
      q.text(keyword)
      q.nest(keyword.length) do
        if arguments.comments.any?
          q.format(arguments)
        else
          separator = -> { q.group { q.comma_breakable } }
          q.seplist(arguments.parts, separator) { |part| q.format(part) }
        end

        # Very special case here. If you're inside of a when clause and the
        # last argument to the predicate is and endless range, then you are
        # forced to use the "then" keyword to make it parse properly.
        last = arguments.parts.last
        if (last.is_a?(Dot2) || last.is_a?(Dot3)) && !last.right
          q.text(" then")
        end
      end
    end

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end