Class: SyntaxTree::BlockNode
Overview
Block represents passing a block to a method call using the do and end keywords or the { and } operators.
method do |value|
end
method { |value| }
Defined Under Namespace
Classes: BlockOpenFormatter
Instance Attribute Summary collapse
-
#block_var ⇒ Object
readonly
- nil | BlockVar
-
the optional variable declaration within this block.
-
#bodystmt ⇒ Object
readonly
- BodyStmt | Statements
-
the expressions to be executed within this block.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#opening ⇒ Object
readonly
- LBrace | Kw
-
the left brace or the do keyword that opens this block.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #arity ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(opening: nil, block_var: nil, bodystmt: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(opening:, block_var:, bodystmt:, location:) ⇒ BlockNode
constructor
A new instance of BlockNode.
- #keywords? ⇒ Boolean
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(opening:, block_var:, bodystmt:, location:) ⇒ BlockNode
Returns a new instance of BlockNode.
4362 4363 4364 4365 4366 4367 4368 |
# File 'lib/syntax_tree/node.rb', line 4362 def initialize(opening:, block_var:, bodystmt:, location:) @opening = opening @block_var = block_var @bodystmt = bodystmt @location = location @comments = [] end |
Instance Attribute Details
#block_var ⇒ Object (readonly)
- nil | BlockVar
-
the optional variable declaration within this block
4354 4355 4356 |
# File 'lib/syntax_tree/node.rb', line 4354 def block_var @block_var end |
#bodystmt ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed within this block
4357 4358 4359 |
# File 'lib/syntax_tree/node.rb', line 4357 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4360 4361 4362 |
# File 'lib/syntax_tree/node.rb', line 4360 def comments @comments end |
#opening ⇒ Object (readonly)
- LBrace | Kw
-
the left brace or the do keyword that opens this block
4351 4352 4353 |
# File 'lib/syntax_tree/node.rb', line 4351 def opening @opening end |
Instance Method Details
#===(other) ⇒ Object
4435 4436 4437 4438 |
# File 'lib/syntax_tree/node.rb', line 4435 def ===(other) other.is_a?(BlockNode) && opening === other.opening && block_var === other.block_var && bodystmt === other.bodystmt end |
#accept(visitor) ⇒ Object
4370 4371 4372 |
# File 'lib/syntax_tree/node.rb', line 4370 def accept(visitor) visitor.visit_block(self) end |
#arity ⇒ Object
4444 4445 4446 4447 4448 4449 4450 4451 |
# File 'lib/syntax_tree/node.rb', line 4444 def arity case block_var when BlockVar block_var.params.arity else 0..0 end end |
#child_nodes ⇒ Object Also known as: deconstruct
4374 4375 4376 |
# File 'lib/syntax_tree/node.rb', line 4374 def child_nodes [opening, block_var, bodystmt] end |
#copy(opening: nil, block_var: nil, bodystmt: nil, location: nil) ⇒ Object
4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 |
# File 'lib/syntax_tree/node.rb', line 4378 def copy(opening: nil, block_var: nil, bodystmt: nil, location: nil) node = BlockNode.new( opening: opening || self.opening, block_var: block_var || self.block_var, bodystmt: bodystmt || self.bodystmt, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
4393 4394 4395 4396 4397 4398 4399 4400 4401 |
# File 'lib/syntax_tree/node.rb', line 4393 def deconstruct_keys(_keys) { opening: opening, block_var: block_var, bodystmt: bodystmt, location: location, comments: comments } end |
#format(q) ⇒ Object
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 |
# File 'lib/syntax_tree/node.rb', line 4403 def format(q) # If this is nested anywhere inside of a Command or CommandCall node, then # we can't change which operators we're using for the bounds of the block. break_opening, break_closing, flat_opening, flat_closing = if unchangeable_bounds?(q) block_close = keywords? ? "end" : "}" [opening.value, block_close, opening.value, block_close] elsif forced_do_end_bounds?(q) %w[do end do end] elsif forced_brace_bounds?(q) %w[{ } { }] else %w[do end { }] end # If the receiver of this block a Command or CommandCall node, then there # are no parentheses around the arguments to that command, so we need to # break the block. case q.parent when nil, Command, CommandCall q.break_parent format_break(q, break_opening, break_closing) return end q.group do q .if_break { format_break(q, break_opening, break_closing) } .if_flat { format_flat(q, flat_opening, flat_closing) } end end |
#keywords? ⇒ Boolean
4440 4441 4442 |
# File 'lib/syntax_tree/node.rb', line 4440 def keywords? opening.is_a?(Kw) end |