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.



6374
6375
6376
6377
6378
6379
6380
# File 'lib/syntax_tree/node.rb', line 6374

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



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

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



6369
6370
6371
# File 'lib/syntax_tree/node.rb', line 6369

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



6386
6387
6388
# File 'lib/syntax_tree/node.rb', line 6386

def child_nodes
  [predicate, statements, consequent]
end

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



6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
# File 'lib/syntax_tree/node.rb', line 6390

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



6405
6406
6407
6408
6409
6410
6411
6412
6413
# File 'lib/syntax_tree/node.rb', line 6405

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

#format(q) ⇒ Object



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

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)


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

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