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.



4893
4894
4895
4896
4897
4898
4899
# File 'lib/syntax_tree/node.rb', line 4893

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



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

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



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

def consequent
  @consequent
end

#predicateObject (readonly)

Node

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



4957
4958
4959
4960
# File 'lib/syntax_tree/node.rb', line 4957

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

#accept(visitor) ⇒ Object



4901
4902
4903
# File 'lib/syntax_tree/node.rb', line 4901

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

#child_nodesObject Also known as: deconstruct



4905
4906
4907
# File 'lib/syntax_tree/node.rb', line 4905

def child_nodes
  [predicate, statements, consequent]
end

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



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

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



4924
4925
4926
4927
4928
4929
4930
4931
4932
# File 'lib/syntax_tree/node.rb', line 4924

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

#format(q) ⇒ Object



4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
# File 'lib/syntax_tree/node.rb', line 4934

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