Class: SyntaxTree::When

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

Overview

When represents a when clause in a case chain.

case value
when predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of When.



13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
# File 'lib/syntax_tree.rb', line 13022

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



13008
13009
13010
# File 'lib/syntax_tree.rb', line 13008

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13020
13021
13022
# File 'lib/syntax_tree.rb', line 13020

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



13014
13015
13016
# File 'lib/syntax_tree.rb', line 13014

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



13017
13018
13019
# File 'lib/syntax_tree.rb', line 13017

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



13011
13012
13013
# File 'lib/syntax_tree.rb', line 13011

def statements
  @statements
end

Instance Method Details

#child_nodesObject



13036
13037
13038
# File 'lib/syntax_tree.rb', line 13036

def child_nodes
  [arguments, statements, consequent]
end

#format(q) ⇒ Object



13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
# File 'lib/syntax_tree.rb', line 13040

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
      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



13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
# File 'lib/syntax_tree.rb', line 13070

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



13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
# File 'lib/syntax_tree.rb', line 13089

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