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
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
9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 |
# File 'lib/syntax_tree/node.rb', line 9955 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
9944 9945 9946 |
# File 'lib/syntax_tree/node.rb', line 9944 def arguments @arguments end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9953 9954 9955 |
# File 'lib/syntax_tree/node.rb', line 9953 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Else | When
-
the next clause in the chain
9950 9951 9952 |
# File 'lib/syntax_tree/node.rb', line 9950 def consequent @consequent end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
9947 9948 9949 |
# File 'lib/syntax_tree/node.rb', line 9947 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
9969 9970 9971 |
# File 'lib/syntax_tree/node.rb', line 9969 def accept(visitor) visitor.visit_when(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9973 9974 9975 |
# File 'lib/syntax_tree/node.rb', line 9973 def child_nodes [arguments, statements, consequent] end |
#deconstruct_keys(_keys) ⇒ Object
9979 9980 9981 9982 9983 9984 9985 9986 9987 |
# File 'lib/syntax_tree/node.rb', line 9979 def deconstruct_keys(_keys) { arguments: arguments, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 |
# File 'lib/syntax_tree/node.rb', line 10005 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 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 q.format(statements) end end if consequent q.breakable_force q.format(consequent) end end end |