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.



9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
# File 'lib/syntax_tree/node.rb', line 9613

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



9602
9603
9604
# File 'lib/syntax_tree/node.rb', line 9602

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9611
9612
9613
# File 'lib/syntax_tree/node.rb', line 9611

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



9608
9609
9610
# File 'lib/syntax_tree/node.rb', line 9608

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



9605
9606
9607
# File 'lib/syntax_tree/node.rb', line 9605

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9627
9628
9629
# File 'lib/syntax_tree/node.rb', line 9627

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

#child_nodesObject Also known as: deconstruct



9631
9632
9633
# File 'lib/syntax_tree/node.rb', line 9631

def child_nodes
  [arguments, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



9637
9638
9639
9640
9641
9642
9643
9644
9645
# File 'lib/syntax_tree/node.rb', line 9637

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

#format(q) ⇒ Object



9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
# File 'lib/syntax_tree/node.rb', line 9647

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