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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of When.



11848
11849
11850
11851
11852
11853
11854
# File 'lib/syntax_tree/node.rb', line 11848

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



11837
11838
11839
# File 'lib/syntax_tree/node.rb', line 11837

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11846
11847
11848
# File 'lib/syntax_tree/node.rb', line 11846

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



11843
11844
11845
# File 'lib/syntax_tree/node.rb', line 11843

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



11840
11841
11842
# File 'lib/syntax_tree/node.rb', line 11840

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



11940
11941
11942
11943
# File 'lib/syntax_tree/node.rb', line 11940

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

#accept(visitor) ⇒ Object



11856
11857
11858
# File 'lib/syntax_tree/node.rb', line 11856

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

#child_nodesObject Also known as: deconstruct



11860
11861
11862
# File 'lib/syntax_tree/node.rb', line 11860

def child_nodes
  [arguments, statements, consequent]
end

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



11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
# File 'lib/syntax_tree/node.rb', line 11864

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



11879
11880
11881
11882
11883
11884
11885
11886
11887
# File 'lib/syntax_tree/node.rb', line 11879

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

#format(q) ⇒ Object



11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
# File 'lib/syntax_tree/node.rb', line 11905

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