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

Returns a new instance of IfNode.



6369
6370
6371
6372
6373
6374
6375
# File 'lib/syntax_tree/node.rb', line 6369

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



6367
6368
6369
# File 'lib/syntax_tree/node.rb', line 6367

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



6364
6365
6366
# File 'lib/syntax_tree/node.rb', line 6364

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



6358
6359
6360
# File 'lib/syntax_tree/node.rb', line 6358

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6361
6362
6363
# File 'lib/syntax_tree/node.rb', line 6361

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6414
6415
6416
6417
# File 'lib/syntax_tree/node.rb', line 6414

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [predicate, statements, consequent]
end

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



6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
# File 'lib/syntax_tree/node.rb', line 6385

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



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

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

#format(q) ⇒ Object



6410
6411
6412
# File 'lib/syntax_tree/node.rb', line 6410

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

#modifier?Boolean

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

Returns:

  • (Boolean)


6420
6421
6422
# File 'lib/syntax_tree/node.rb', line 6420

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