class SyntaxTree::When
When
represents a when
clause in a case
chain.
case value when predicate end
Constants
- 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.
Attributes
Args
-
the arguments to the when clause
Statements
-
the expressions to be executed
Public Class Methods
Source
# File lib/syntax_tree/node.rb, line 11851 def initialize(arguments:, statements:, consequent:, location:) @arguments = arguments @statements = statements @consequent = consequent @location = location @comments = [] end
Public Instance Methods
Source
# File lib/syntax_tree/node.rb, line 11943 def ===(other) other.is_a?(When) && arguments === other.arguments && statements === other.statements && consequent === other.consequent end
Source
# File lib/syntax_tree/node.rb, line 11859 def accept(visitor) visitor.visit_when(self) end
Source
# File lib/syntax_tree/node.rb, line 11863 def child_nodes [arguments, statements, consequent] end
Also aliased as: deconstruct
Source
# File lib/syntax_tree/node.rb, line 11867 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
Source
# File lib/syntax_tree/node.rb, line 11882 def deconstruct_keys(_keys) { arguments: arguments, statements: statements, consequent: consequent, location: location, comments: comments } end
Source
# File lib/syntax_tree/node.rb, line 11908 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