Class: SyntaxTree::Elsif
- Inherits:
-
Object
- Object
- SyntaxTree::Elsif
- Defined in:
- lib/syntax_tree.rb
Overview
Elsif represents another clause in an if or unless chain.
if variable
elsif other_variable
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | Elsif | Else
-
the next clause in the chain.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif
constructor
A new instance of Elsif.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif
5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 |
# File 'lib/syntax_tree.rb', line 5340 def initialize( predicate:, statements:, consequent:, location:, comments: [] ) @predicate = predicate @statements = statements @consequent = consequent @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5338 5339 5340 |
# File 'lib/syntax_tree.rb', line 5338 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Elsif | Else
-
the next clause in the chain
5332 5333 5334 |
# File 'lib/syntax_tree.rb', line 5332 def consequent @consequent end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
5335 5336 5337 |
# File 'lib/syntax_tree.rb', line 5335 def location @location end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
5326 5327 5328 |
# File 'lib/syntax_tree.rb', line 5326 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
5329 5330 5331 |
# File 'lib/syntax_tree.rb', line 5329 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
5354 5355 5356 |
# File 'lib/syntax_tree.rb', line 5354 def child_nodes [predicate, statements, consequent] end |
#format(q) ⇒ Object
5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 |
# File 'lib/syntax_tree.rb', line 5358 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 |
#pretty_print(q) ⇒ Object
5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 |
# File 'lib/syntax_tree.rb', line 5381 def pretty_print(q) q.group(2, "(", ")") do q.text("elsif") q.breakable q.pp(predicate) q.breakable q.pp(statements) if consequent q.breakable q.pp(consequent) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 |
# File 'lib/syntax_tree.rb', line 5400 def to_json(*opts) { type: :elsif, pred: predicate, stmts: statements, cons: consequent, loc: location, cmts: comments }.to_json(*opts) end |