Class: SyntaxTree::Case

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Case represents the beginning of a case chain.

case value
when 1
  "one"
when 2
  "two"
else
  "number"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword:, value:, consequent:, location:, comments: []) ⇒ Case

Returns a new instance of Case.



2857
2858
2859
2860
2861
2862
2863
# File 'lib/syntax_tree/node.rb', line 2857

def initialize(keyword:, value:, consequent:, location:, comments: [])
  @keyword = keyword
  @value = value
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2855
2856
2857
# File 'lib/syntax_tree/node.rb', line 2855

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



2849
2850
2851
# File 'lib/syntax_tree/node.rb', line 2849

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



2843
2844
2845
# File 'lib/syntax_tree/node.rb', line 2843

def keyword
  @keyword
end

#locationObject (readonly)

Location

the location of this node



2852
2853
2854
# File 'lib/syntax_tree/node.rb', line 2852

def location
  @location
end

#valueObject (readonly)

nil | untyped

optional value being switched on



2846
2847
2848
# File 'lib/syntax_tree/node.rb', line 2846

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2865
2866
2867
# File 'lib/syntax_tree/node.rb', line 2865

def child_nodes
  [keyword, value, consequent]
end

#deconstruct_keys(keys) ⇒ Object



2871
2872
2873
2874
2875
2876
2877
2878
2879
# File 'lib/syntax_tree/node.rb', line 2871

def deconstruct_keys(keys)
  {
    keyword: keyword,
    value: value,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
# File 'lib/syntax_tree/node.rb', line 2881

def format(q)
  q.group do
    q.format(keyword)

    if value
      q.text(" ")
      q.format(value)
    end

    q.breakable(force: true)
    q.format(consequent)
    q.breakable(force: true)

    q.text("end")
  end
end

#pretty_print(q) ⇒ Object



2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
# File 'lib/syntax_tree/node.rb', line 2898

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

    q.breakable
    q.pp(keyword)

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

    q.breakable
    q.pp(consequent)

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

#to_json(*opts) ⇒ Object



2917
2918
2919
2920
2921
2922
2923
2924
2925
# File 'lib/syntax_tree/node.rb', line 2917

def to_json(*opts)
  {
    type: :case,
    value: value,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end