Class: SyntaxTree::IfNode

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

Overview

If represents the first clause in an if chain.

if predicate
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:) ⇒ IfNode



6383
6384
6385
6386
6387
6388
6389
# File 'lib/syntax_tree/node.rb', line 6383

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



6381
6382
6383
# File 'lib/syntax_tree/node.rb', line 6381

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



6378
6379
6380
# File 'lib/syntax_tree/node.rb', line 6378

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



6372
6373
6374
# File 'lib/syntax_tree/node.rb', line 6372

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6375
6376
6377
# File 'lib/syntax_tree/node.rb', line 6375

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6428
6429
6430
6431
# File 'lib/syntax_tree/node.rb', line 6428

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

#accept(visitor) ⇒ Object



6391
6392
6393
# File 'lib/syntax_tree/node.rb', line 6391

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

#child_nodesObject Also known as: deconstruct



6395
6396
6397
# File 'lib/syntax_tree/node.rb', line 6395

def child_nodes
  [predicate, statements, consequent]
end

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



6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
# File 'lib/syntax_tree/node.rb', line 6399

def copy(predicate: nil, statements: nil, consequent: nil, location: nil)
  node =
    IfNode.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



6414
6415
6416
6417
6418
6419
6420
6421
6422
# File 'lib/syntax_tree/node.rb', line 6414

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

#format(q) ⇒ Object



6424
6425
6426
# File 'lib/syntax_tree/node.rb', line 6424

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

#modifier?Boolean

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



6434
6435
6436
# File 'lib/syntax_tree/node.rb', line 6434

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