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:, comments: []) ⇒ Elsif

Returns a new instance of Elsif.



4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
# File 'lib/syntax_tree/node.rb', line 4042

def initialize(
  predicate:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4040
4041
4042
# File 'lib/syntax_tree/node.rb', line 4040

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4037
4038
4039
# File 'lib/syntax_tree/node.rb', line 4037

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4031
4032
4033
# File 'lib/syntax_tree/node.rb', line 4031

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4034
4035
4036
# File 'lib/syntax_tree/node.rb', line 4034

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4056
4057
4058
# File 'lib/syntax_tree/node.rb', line 4056

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

#child_nodesObject Also known as: deconstruct



4060
4061
4062
# File 'lib/syntax_tree/node.rb', line 4060

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



4066
4067
4068
4069
4070
4071
4072
4073
4074
# File 'lib/syntax_tree/node.rb', line 4066

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

#format(q) ⇒ Object



4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
# File 'lib/syntax_tree/node.rb', line 4076

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: true)
        q.format(statements)
      end
    end

    if consequent
      q.group do
        q.breakable(force: true)
        q.format(consequent)
      end
    end
  end
end