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.
-
#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 (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ 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.
11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 |
# File 'lib/syntax_tree/node.rb', line 11310 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
11296 11297 11298 |
# File 'lib/syntax_tree/node.rb', line 11296 def arguments @arguments end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
11308 11309 11310 |
# File 'lib/syntax_tree/node.rb', line 11308 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Else | When
-
the next clause in the chain
11302 11303 11304 |
# File 'lib/syntax_tree/node.rb', line 11302 def consequent @consequent end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
11305 11306 11307 |
# File 'lib/syntax_tree/node.rb', line 11305 def location @location end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
11299 11300 11301 |
# File 'lib/syntax_tree/node.rb', line 11299 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
11324 11325 11326 |
# File 'lib/syntax_tree/node.rb', line 11324 def child_nodes [arguments, statements, consequent] end |
#deconstruct_keys(keys) ⇒ Object
11330 11331 11332 11333 11334 11335 11336 11337 11338 |
# File 'lib/syntax_tree/node.rb', line 11330 def deconstruct_keys(keys) { arguments: arguments, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 |
# File 'lib/syntax_tree/node.rb', line 11340 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
11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 |
# File 'lib/syntax_tree/node.rb', line 11378 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
11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 |
# File 'lib/syntax_tree/node.rb', line 11397 def to_json(*opts) { type: :when, args: arguments, stmts: statements, cons: consequent, loc: location, cmts: comments }.to_json(*opts) end |