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.



9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
# File 'lib/syntax_tree/node.rb', line 9533

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



9522
9523
9524
# File 'lib/syntax_tree/node.rb', line 9522

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9531
9532
9533
# File 'lib/syntax_tree/node.rb', line 9531

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



9528
9529
9530
# File 'lib/syntax_tree/node.rb', line 9528

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



9525
9526
9527
# File 'lib/syntax_tree/node.rb', line 9525

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9547
9548
9549
# File 'lib/syntax_tree/node.rb', line 9547

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

#child_nodesObject Also known as: deconstruct



9551
9552
9553
# File 'lib/syntax_tree/node.rb', line 9551

def child_nodes
  [arguments, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



9557
9558
9559
9560
9561
9562
9563
9564
9565
# File 'lib/syntax_tree/node.rb', line 9557

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

#format(q) ⇒ Object



9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
# File 'lib/syntax_tree/node.rb', line 9567

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