Class: SyntaxTree::UnlessNode

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

Overview

Unless represents the first clause in an unless chain.

unless predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(predicate:, statements:, consequent:, location:) ⇒ UnlessNode

Returns a new instance of UnlessNode.



11273
11274
11275
11276
11277
11278
11279
# File 'lib/syntax_tree/node.rb', line 11273

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11271
11272
11273
# File 'lib/syntax_tree/node.rb', line 11271

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



11268
11269
11270
# File 'lib/syntax_tree/node.rb', line 11268

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



11262
11263
11264
# File 'lib/syntax_tree/node.rb', line 11262

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11265
11266
11267
# File 'lib/syntax_tree/node.rb', line 11265

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



11318
11319
11320
11321
# File 'lib/syntax_tree/node.rb', line 11318

def ===(other)
  other.is_a?(UnlessNode) && predicate === other.predicate &&
    statements === other.statements && consequent === other.consequent
end

#accept(visitor) ⇒ Object



11281
11282
11283
# File 'lib/syntax_tree/node.rb', line 11281

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

#child_nodesObject Also known as: deconstruct



11285
11286
11287
# File 'lib/syntax_tree/node.rb', line 11285

def child_nodes
  [predicate, statements, consequent]
end

#copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object



11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
# File 'lib/syntax_tree/node.rb', line 11289

def copy(predicate: nil, statements: nil, consequent: nil, location: nil)
  node =
    UnlessNode.new(
      predicate: predicate || self.predicate,
      statements: statements || self.statements,
      consequent: consequent || self.consequent,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11304
11305
11306
11307
11308
11309
11310
11311
11312
# File 'lib/syntax_tree/node.rb', line 11304

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

#format(q) ⇒ Object



11314
11315
11316
# File 'lib/syntax_tree/node.rb', line 11314

def format(q)
  ConditionalFormatter.new("unless", self).format(q)
end

#modifier?Boolean

Checks if the node was originally found in the modifier form.

Returns:

  • (Boolean)


11324
11325
11326
# File 'lib/syntax_tree/node.rb', line 11324

def modifier?
  predicate.location.start_char > statements.location.start_char
end