Class: SyntaxTree::When
Overview
When represents a when clause in a case chain.
case value
when predicate
end
Defined Under Namespace
Classes: Separator
Constant Summary collapse
- SEPARATOR =
We’re going to keep a single instance of this separator around so we don’t have to allocate a new one every time we format a when clause.
Separator.new.freeze
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
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(arguments: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(arguments:, statements:, consequent:, location:) ⇒ When
constructor
A new instance of When.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(arguments:, statements:, consequent:, location:) ⇒ When
11693 11694 11695 11696 11697 11698 11699 |
# File 'lib/syntax_tree/node.rb', line 11693 def initialize(arguments:, statements:, consequent:, location:) @arguments = arguments @statements = statements @consequent = consequent @location = location @comments = [] end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
- Args
-
the arguments to the when clause
11682 11683 11684 |
# File 'lib/syntax_tree/node.rb', line 11682 def arguments @arguments end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
11691 11692 11693 |
# File 'lib/syntax_tree/node.rb', line 11691 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Else | When
-
the next clause in the chain
11688 11689 11690 |
# File 'lib/syntax_tree/node.rb', line 11688 def consequent @consequent end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
11685 11686 11687 |
# File 'lib/syntax_tree/node.rb', line 11685 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
11785 11786 11787 11788 |
# File 'lib/syntax_tree/node.rb', line 11785 def ===(other) other.is_a?(When) && arguments === other.arguments && statements === other.statements && consequent === other.consequent end |
#accept(visitor) ⇒ Object
11701 11702 11703 |
# File 'lib/syntax_tree/node.rb', line 11701 def accept(visitor) visitor.visit_when(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
11705 11706 11707 |
# File 'lib/syntax_tree/node.rb', line 11705 def child_nodes [arguments, statements, consequent] end |
#copy(arguments: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 |
# File 'lib/syntax_tree/node.rb', line 11709 def copy(arguments: nil, statements: nil, consequent: nil, location: nil) node = When.new( arguments: arguments || self.arguments, statements: statements || self.statements, consequent: consequent || self.consequent, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
11724 11725 11726 11727 11728 11729 11730 11731 11732 |
# File 'lib/syntax_tree/node.rb', line 11724 def deconstruct_keys(_keys) { arguments: arguments, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 |
# File 'lib/syntax_tree/node.rb', line 11750 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 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 q.text(" then") if last.is_a?(RangeNode) && !last.right end end unless statements.empty? q.indent do q.breakable_force q.format(statements) end end if consequent q.breakable_force q.format(consequent) end end end |