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

Constructor Details

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

Returns a new instance of When.



10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
# File 'lib/syntax_tree/node.rb', line 10866

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



10855
10856
10857
# File 'lib/syntax_tree/node.rb', line 10855

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10864
10865
10866
# File 'lib/syntax_tree/node.rb', line 10864

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



10861
10862
10863
# File 'lib/syntax_tree/node.rb', line 10861

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



10858
10859
10860
# File 'lib/syntax_tree/node.rb', line 10858

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10880
10881
10882
# File 'lib/syntax_tree/node.rb', line 10880

def child_nodes
  [arguments, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



10886
10887
10888
10889
10890
10891
10892
10893
10894
# File 'lib/syntax_tree/node.rb', line 10886

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

#format(q) ⇒ Object



10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
# File 'lib/syntax_tree/node.rb', line 10896

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

#pretty_print(q) ⇒ Object



10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
# File 'lib/syntax_tree/node.rb', line 10934

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("when")

    q.breakable
    q.pp(arguments)

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
# File 'lib/syntax_tree/node.rb', line 10953

def to_json(*opts)
  {
    type: :when,
    args: arguments,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end