Class: SyntaxTree::Case

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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(value:, consequent:, location:, comments: []) ⇒ Case

Returns a new instance of Case.



3072
3073
3074
3075
3076
3077
# File 'lib/syntax_tree.rb', line 3072

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3070
3071
3072
# File 'lib/syntax_tree.rb', line 3070

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



3064
3065
3066
# File 'lib/syntax_tree.rb', line 3064

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



3067
3068
3069
# File 'lib/syntax_tree.rb', line 3067

def location
  @location
end

#valueObject (readonly)

nil | untyped

optional value being switched on



3061
3062
3063
# File 'lib/syntax_tree.rb', line 3061

def value
  @value
end

Instance Method Details

#child_nodesObject



3079
3080
3081
# File 'lib/syntax_tree.rb', line 3079

def child_nodes
  [value, consequent]
end

#format(q) ⇒ Object



3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
# File 'lib/syntax_tree.rb', line 3083

def format(q)
  q.group(0, "case", "end") do
    if value
      q.text(" ")
      q.format(value)
    end

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

#pretty_print(q) ⇒ Object



3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
# File 'lib/syntax_tree.rb', line 3096

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

    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



3112
3113
3114
3115
3116
3117
3118
3119
3120
# File 'lib/syntax_tree.rb', line 3112

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