Class: SyntaxTree::When
Overview
When represents a when clause in a case chain.
case value
when predicate
end
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
- Args
-
the arguments to the when clause.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | Else | When
-
the next clause in the chain.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When
constructor
A new instance of When.
Methods inherited from Node
Constructor Details
#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When
Returns a new instance of When.
9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 |
# File 'lib/syntax_tree/node.rb', line 9421 def initialize( arguments:, statements:, consequent:, location:, comments: [] ) @arguments = arguments @statements = statements @consequent = consequent @location = location @comments = comments end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
- Args
-
the arguments to the when clause
9410 9411 9412 |
# File 'lib/syntax_tree/node.rb', line 9410 def arguments @arguments end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9419 9420 9421 |
# File 'lib/syntax_tree/node.rb', line 9419 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Else | When
-
the next clause in the chain
9416 9417 9418 |
# File 'lib/syntax_tree/node.rb', line 9416 def consequent @consequent end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
9413 9414 9415 |
# File 'lib/syntax_tree/node.rb', line 9413 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
9435 9436 9437 |
# File 'lib/syntax_tree/node.rb', line 9435 def accept(visitor) visitor.visit_when(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9439 9440 9441 |
# File 'lib/syntax_tree/node.rb', line 9439 def child_nodes [arguments, statements, consequent] end |
#deconstruct_keys(keys) ⇒ Object
9445 9446 9447 9448 9449 9450 9451 9452 9453 |
# File 'lib/syntax_tree/node.rb', line 9445 def deconstruct_keys(keys) { arguments: arguments, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 |
# File 'lib/syntax_tree/node.rb', line 9455 def format(q) keyword = "when " q.group do q.group do q.text(keyword) q.nest(keyword.length) do if arguments.comments.any? q.format(arguments) else separator = -> { q.group { q.comma_breakable } } q.seplist(arguments.parts, separator) { |part| q.format(part) } end # Very special case here. If you're inside of a when clause and the # last argument to the predicate is and endless range, then you are # forced to use the "then" keyword to make it parse properly. last = arguments.parts.last if (last.is_a?(Dot2) || last.is_a?(Dot3)) && !last.right q.text(" then") end end end unless statements.empty? q.indent do q.breakable(force: true) q.format(statements) end end if consequent q.breakable(force: true) q.format(consequent) end end end |