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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(predicate:, statements:, consequent:, location:) ⇒ Elsif

Returns a new instance of Elsif.



4875
4876
4877
4878
4879
4880
4881
# File 'lib/syntax_tree/node.rb', line 4875

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



4873
4874
4875
# File 'lib/syntax_tree/node.rb', line 4873

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4870
4871
4872
# File 'lib/syntax_tree/node.rb', line 4870

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



4864
4865
4866
# File 'lib/syntax_tree/node.rb', line 4864

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4867
4868
4869
# File 'lib/syntax_tree/node.rb', line 4867

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



4939
4940
4941
4942
# File 'lib/syntax_tree/node.rb', line 4939

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

#accept(visitor) ⇒ Object



4883
4884
4885
# File 'lib/syntax_tree/node.rb', line 4883

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

#child_nodesObject Also known as: deconstruct



4887
4888
4889
# File 'lib/syntax_tree/node.rb', line 4887

def child_nodes
  [predicate, statements, consequent]
end

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



4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
# File 'lib/syntax_tree/node.rb', line 4891

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



4906
4907
4908
4909
4910
4911
4912
4913
4914
# File 'lib/syntax_tree/node.rb', line 4906

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

#format(q) ⇒ Object



4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
# File 'lib/syntax_tree/node.rb', line 4916

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