Class: SyntaxTree::Elsif

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

Overview

Elsif represents another clause in an if or unless chain.

if variable
elsif other_variable
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:) ⇒ Elsif

Returns a new instance of Elsif.



4796
4797
4798
4799
4800
4801
4802
# File 'lib/syntax_tree/node.rb', line 4796

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



4794
4795
4796
# File 'lib/syntax_tree/node.rb', line 4794

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4791
4792
4793
# File 'lib/syntax_tree/node.rb', line 4791

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4785
4786
4787
# File 'lib/syntax_tree/node.rb', line 4785

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4788
4789
4790
# File 'lib/syntax_tree/node.rb', line 4788

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



4860
4861
4862
4863
# File 'lib/syntax_tree/node.rb', line 4860

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

#accept(visitor) ⇒ Object



4804
4805
4806
# File 'lib/syntax_tree/node.rb', line 4804

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

#child_nodesObject Also known as: deconstruct



4808
4809
4810
# File 'lib/syntax_tree/node.rb', line 4808

def child_nodes
  [predicate, statements, consequent]
end

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



4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
# File 'lib/syntax_tree/node.rb', line 4812

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



4827
4828
4829
4830
4831
4832
4833
4834
4835
# File 'lib/syntax_tree/node.rb', line 4827

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

#format(q) ⇒ Object



4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
# File 'lib/syntax_tree/node.rb', line 4837

def format(q)
  q.group do
    q.group do
      q.text("elsif ")
      q.nest("elsif".length - 1) { q.format(predicate) }
    end

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end

    if consequent
      q.group do
        q.breakable_force
        q.format(consequent)
      end
    end
  end
end