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.



4878
4879
4880
4881
4882
4883
4884
# File 'lib/syntax_tree/node.rb', line 4878

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



4876
4877
4878
# File 'lib/syntax_tree/node.rb', line 4876

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



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

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



4942
4943
4944
4945
# File 'lib/syntax_tree/node.rb', line 4942

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



4890
4891
4892
# File 'lib/syntax_tree/node.rb', line 4890

def child_nodes
  [predicate, statements, consequent]
end

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



4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
# File 'lib/syntax_tree/node.rb', line 4894

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



4909
4910
4911
4912
4913
4914
4915
4916
4917
# File 'lib/syntax_tree/node.rb', line 4909

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

#format(q) ⇒ Object



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

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