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.



11336
11337
11338
11339
11340
11341
11342
# File 'lib/syntax_tree/node.rb', line 11336

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



11334
11335
11336
# File 'lib/syntax_tree/node.rb', line 11334

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



11331
11332
11333
# File 'lib/syntax_tree/node.rb', line 11331

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11328
11329
11330
# File 'lib/syntax_tree/node.rb', line 11328

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



11381
11382
11383
11384
# File 'lib/syntax_tree/node.rb', line 11381

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

#accept(visitor) ⇒ Object



11344
11345
11346
# File 'lib/syntax_tree/node.rb', line 11344

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

#child_nodesObject Also known as: deconstruct



11348
11349
11350
# File 'lib/syntax_tree/node.rb', line 11348

def child_nodes
  [predicate, statements, consequent]
end

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



11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
# File 'lib/syntax_tree/node.rb', line 11352

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



11367
11368
11369
11370
11371
11372
11373
11374
11375
# File 'lib/syntax_tree/node.rb', line 11367

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

#format(q) ⇒ Object



11377
11378
11379
# File 'lib/syntax_tree/node.rb', line 11377

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)


11387
11388
11389
# File 'lib/syntax_tree/node.rb', line 11387

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