Class: SyntaxTree::Elsif

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

Overview

Elsif represents another clause in an if or unless chain.

if variable
elsif other_variable
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Elsif.



4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
# File 'lib/syntax_tree/node.rb', line 4075

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



4073
4074
4075
# File 'lib/syntax_tree/node.rb', line 4073

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4070
4071
4072
# File 'lib/syntax_tree/node.rb', line 4070

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4064
4065
4066
# File 'lib/syntax_tree/node.rb', line 4064

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4067
4068
4069
# File 'lib/syntax_tree/node.rb', line 4067

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4089
4090
4091
# File 'lib/syntax_tree/node.rb', line 4089

def accept(visitor)
  visitor.visit_elsif(self)
end

#child_nodesObject Also known as: deconstruct



4093
4094
4095
# File 'lib/syntax_tree/node.rb', line 4093

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



4099
4100
4101
4102
4103
4104
4105
4106
4107
# File 'lib/syntax_tree/node.rb', line 4099

def deconstruct_keys(_keys)
  {
    predicate: predicate,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
# File 'lib/syntax_tree/node.rb', line 4109

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