class SyntaxTree::When

When represents a when clause in a case chain.

case value
when predicate
end

Constants

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.

Attributes

arguments[R]
Args

the arguments to the when clause

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

consequent[R]
nil | Else | When

the next clause in the chain

statements[R]
Statements

the expressions to be executed

Public Class Methods

new(arguments:, statements:, consequent:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 11832
def initialize(arguments:, statements:, consequent:, location:)
  @arguments = arguments
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 11924
def ===(other)
  other.is_a?(When) && arguments === other.arguments &&
    statements === other.statements && consequent === other.consequent
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 11840
def accept(visitor)
  visitor.visit_when(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 11844
def child_nodes
  [arguments, statements, consequent]
end
Also aliased as: deconstruct
copy(arguments: nil, statements: nil, consequent: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 11848
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()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 11863
def deconstruct_keys(_keys)
  {
    arguments: arguments,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 11889
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