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.



4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
# File 'lib/syntax_tree/node.rb', line 4086

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



4084
4085
4086
# File 'lib/syntax_tree/node.rb', line 4084

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



4081
4082
4083
# File 'lib/syntax_tree/node.rb', line 4081

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



4075
4076
4077
# File 'lib/syntax_tree/node.rb', line 4075

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



4078
4079
4080
# File 'lib/syntax_tree/node.rb', line 4078

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4100
4101
4102
# File 'lib/syntax_tree/node.rb', line 4100

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

#child_nodesObject Also known as: deconstruct



4104
4105
4106
# File 'lib/syntax_tree/node.rb', line 4104

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



4110
4111
4112
4113
4114
4115
4116
4117
4118
# File 'lib/syntax_tree/node.rb', line 4110

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

#format(q) ⇒ Object



4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
# File 'lib/syntax_tree/node.rb', line 4120

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