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.



4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
# File 'lib/syntax_tree/node.rb', line 4223

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



4221
4222
4223
# File 'lib/syntax_tree/node.rb', line 4221

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4218
4219
4220
# File 'lib/syntax_tree/node.rb', line 4218

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4212
4213
4214
# File 'lib/syntax_tree/node.rb', line 4212

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4215
4216
4217
# File 'lib/syntax_tree/node.rb', line 4215

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4237
4238
4239
# File 'lib/syntax_tree/node.rb', line 4237

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

#child_nodesObject Also known as: deconstruct



4241
4242
4243
# File 'lib/syntax_tree/node.rb', line 4241

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



4247
4248
4249
4250
4251
4252
4253
4254
4255
# File 'lib/syntax_tree/node.rb', line 4247

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

#format(q) ⇒ Object



4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
# File 'lib/syntax_tree/node.rb', line 4257

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
        q.format(statements)
      end
    end

    if consequent
      q.group do
        q.breakable_force
        q.format(consequent)
      end
    end
  end
end