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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of When.



8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
# File 'lib/syntax_tree/node.rb', line 8856

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



8845
8846
8847
# File 'lib/syntax_tree/node.rb', line 8845

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8854
8855
8856
# File 'lib/syntax_tree/node.rb', line 8854

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



8851
8852
8853
# File 'lib/syntax_tree/node.rb', line 8851

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



8848
8849
8850
# File 'lib/syntax_tree/node.rb', line 8848

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



8870
8871
8872
# File 'lib/syntax_tree/node.rb', line 8870

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

#child_nodesObject Also known as: deconstruct



8874
8875
8876
# File 'lib/syntax_tree/node.rb', line 8874

def child_nodes
  [arguments, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



8880
8881
8882
8883
8884
8885
8886
8887
8888
# File 'lib/syntax_tree/node.rb', line 8880

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

#format(q) ⇒ Object



8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
# File 'lib/syntax_tree/node.rb', line 8890

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