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.



4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
# File 'lib/syntax_tree/node.rb', line 4114

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



4112
4113
4114
# File 'lib/syntax_tree/node.rb', line 4112

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4109
4110
4111
# File 'lib/syntax_tree/node.rb', line 4109

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4103
4104
4105
# File 'lib/syntax_tree/node.rb', line 4103

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4106
4107
4108
# File 'lib/syntax_tree/node.rb', line 4106

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4128
4129
4130
# File 'lib/syntax_tree/node.rb', line 4128

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

#child_nodesObject Also known as: deconstruct



4132
4133
4134
# File 'lib/syntax_tree/node.rb', line 4132

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



4138
4139
4140
4141
4142
4143
4144
4145
4146
# File 'lib/syntax_tree/node.rb', line 4138

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

#format(q) ⇒ Object



4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
# File 'lib/syntax_tree/node.rb', line 4148

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