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

Defined Under Namespace

Classes: Separator

Constant Summary collapse

SEPARATOR =

We’re going to keep a single instance of this separator around so we don’t have to allocate a new one every time we format a when clause.

Separator.new.freeze

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:) ⇒ When



11663
11664
11665
11666
11667
11668
11669
# File 'lib/syntax_tree/node.rb', line 11663

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

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments to the when clause



11652
11653
11654
# File 'lib/syntax_tree/node.rb', line 11652

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11661
11662
11663
# File 'lib/syntax_tree/node.rb', line 11661

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



11658
11659
11660
# File 'lib/syntax_tree/node.rb', line 11658

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



11655
11656
11657
# File 'lib/syntax_tree/node.rb', line 11655

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



11755
11756
11757
11758
# File 'lib/syntax_tree/node.rb', line 11755

def ===(other)
  other.is_a?(When) && arguments === other.arguments &&
    statements === other.statements && consequent === other.consequent
end

#accept(visitor) ⇒ Object



11671
11672
11673
# File 'lib/syntax_tree/node.rb', line 11671

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

#child_nodesObject Also known as: deconstruct



11675
11676
11677
# File 'lib/syntax_tree/node.rb', line 11675

def child_nodes
  [arguments, statements, consequent]
end

#copy(arguments: nil, statements: nil, consequent: nil, location: nil) ⇒ Object



11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
# File 'lib/syntax_tree/node.rb', line 11679

def copy(arguments: nil, statements: nil, consequent: nil, location: nil)
  node =
    When.new(
      arguments: arguments || self.arguments,
      statements: statements || self.statements,
      consequent: consequent || self.consequent,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11694
11695
11696
11697
11698
11699
11700
11701
11702
# File 'lib/syntax_tree/node.rb', line 11694

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

#format(q) ⇒ Object



11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
# File 'lib/syntax_tree/node.rb', line 11720

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
          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
        q.text(" then") if last.is_a?(RangeNode) && !last.right
      end
    end

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end

    if consequent
      q.breakable_force
      q.format(consequent)
    end
  end
end