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
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When
Returns a new instance of When.
9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 |
# File 'lib/syntax_tree/node.rb', line 9655 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
9644 9645 9646 |
# File 'lib/syntax_tree/node.rb', line 9644 def arguments @arguments end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9653 9654 9655 |
# File 'lib/syntax_tree/node.rb', line 9653 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Else | When
-
the next clause in the chain
9650 9651 9652 |
# File 'lib/syntax_tree/node.rb', line 9650 def consequent @consequent end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
9647 9648 9649 |
# File 'lib/syntax_tree/node.rb', line 9647 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
9669 9670 9671 |
# File 'lib/syntax_tree/node.rb', line 9669 def accept(visitor) visitor.visit_when(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9673 9674 9675 |
# File 'lib/syntax_tree/node.rb', line 9673 def child_nodes [arguments, statements, consequent] end |
#deconstruct_keys(_keys) ⇒ Object
9679 9680 9681 9682 9683 9684 9685 9686 9687 |
# File 'lib/syntax_tree/node.rb', line 9679 def deconstruct_keys(_keys) { arguments: arguments, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 |
# File 'lib/syntax_tree/node.rb', line 9689 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 |