Class: SyntaxTree::Elsif

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

Overview

Elsif represents another clause in an if or unless chain.

if variable
elsif other_variable
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif

Returns a new instance of Elsif.



5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
# File 'lib/syntax_tree.rb', line 5043

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5041
5042
5043
# File 'lib/syntax_tree.rb', line 5041

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



5035
5036
5037
# File 'lib/syntax_tree.rb', line 5035

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



5038
5039
5040
# File 'lib/syntax_tree.rb', line 5038

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



5029
5030
5031
# File 'lib/syntax_tree.rb', line 5029

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5032
5033
5034
# File 'lib/syntax_tree.rb', line 5032

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5057
5058
5059
# File 'lib/syntax_tree.rb', line 5057

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
# File 'lib/syntax_tree.rb', line 5061

def format(q)
  q.group do
    q.group do
      q.text("elsif ")
      q.nest("elsif".length - 1) { q.format(predicate) }
    end

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.group do
        q.breakable(force: true)
        q.format(consequent)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
# File 'lib/syntax_tree.rb', line 5084

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

    q.breakable
    q.pp(predicate)

    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



5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
# File 'lib/syntax_tree.rb', line 5103

def to_json(*opts)
  {
    type: :elsif,
    pred: predicate,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end