Class: SyntaxTree::When
- Inherits:
-
Object
- Object
- SyntaxTree::When
- Defined in:
- lib/syntax_tree.rb
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.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When
constructor
A new instance of When.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When
Returns a new instance of When.
13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 |
# File 'lib/syntax_tree.rb', line 13218 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
13204 13205 13206 |
# File 'lib/syntax_tree.rb', line 13204 def arguments @arguments end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
13216 13217 13218 |
# File 'lib/syntax_tree.rb', line 13216 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Else | When
-
the next clause in the chain
13210 13211 13212 |
# File 'lib/syntax_tree.rb', line 13210 def consequent @consequent end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
13213 13214 13215 |
# File 'lib/syntax_tree.rb', line 13213 def location @location end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
13207 13208 13209 |
# File 'lib/syntax_tree.rb', line 13207 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
13232 13233 13234 |
# File 'lib/syntax_tree.rb', line 13232 def child_nodes [arguments, statements, consequent] end |
#format(q) ⇒ Object
13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 |
# File 'lib/syntax_tree.rb', line 13236 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 |
#pretty_print(q) ⇒ Object
13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 |
# File 'lib/syntax_tree.rb', line 13274 def pretty_print(q) q.group(2, "(", ")") do q.text("when") q.breakable q.pp(arguments) q.breakable q.pp(statements) if consequent q.breakable q.pp(consequent) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 |
# File 'lib/syntax_tree.rb', line 13293 def to_json(*opts) { type: :when, args: arguments, stmts: statements, cons: consequent, loc: location, cmts: comments }.to_json(*opts) end |