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.



12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
# File 'lib/syntax_tree.rb', line 12941

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



12927
12928
12929
# File 'lib/syntax_tree.rb', line 12927

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12939
12940
12941
# File 'lib/syntax_tree.rb', line 12939

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



12933
12934
12935
# File 'lib/syntax_tree.rb', line 12933

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



12936
12937
12938
# File 'lib/syntax_tree.rb', line 12936

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



12930
12931
12932
# File 'lib/syntax_tree.rb', line 12930

def statements
  @statements
end

Instance Method Details

#child_nodesObject



12955
12956
12957
# File 'lib/syntax_tree.rb', line 12955

def child_nodes
  [arguments, statements, consequent]
end

#format(q) ⇒ Object



12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
# File 'lib/syntax_tree.rb', line 12959

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



12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
# File 'lib/syntax_tree.rb', line 12989

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



13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
# File 'lib/syntax_tree.rb', line 13008

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