Class: SyntaxTree::BlockVar
Overview
BlockVar represents the parameters being declared for a block. Effectively this node is everything contained within the pipes. This includes all of the various parameter types, as well as block-local variable declarations.
method do |positional, optional = value, keyword:, █ local|
end
Defined Under Namespace
Classes: Separator
Constant Summary collapse
- SEPARATOR =
We’ll keep a single instance of this separator around for all block vars to cut down on allocations.
Separator.new.freeze
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#locals ⇒ Object
readonly
- Array[ Ident ]
-
the list of block-local variable declarations.
-
#params ⇒ Object
readonly
- Params
-
the parameters being declared with the block.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
-
#arg0? ⇒ Boolean
When a single required parameter is declared for a block, it gets automatically expanded if the values being yielded into it are an array.
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(params: nil, locals: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(params:, locals:, location:) ⇒ BlockVar
constructor
A new instance of BlockVar.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(params:, locals:, location:) ⇒ BlockVar
Returns a new instance of BlockVar.
2115 2116 2117 2118 2119 2120 |
# File 'lib/syntax_tree/node.rb', line 2115 def initialize(params:, locals:, location:) @params = params @locals = locals @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2113 2114 2115 |
# File 'lib/syntax_tree/node.rb', line 2113 def comments @comments end |
#locals ⇒ Object (readonly)
- Array[ Ident ]
-
the list of block-local variable declarations
2110 2111 2112 |
# File 'lib/syntax_tree/node.rb', line 2110 def locals @locals end |
#params ⇒ Object (readonly)
- Params
-
the parameters being declared with the block
2107 2108 2109 |
# File 'lib/syntax_tree/node.rb', line 2107 def params @params end |
Instance Method Details
#===(other) ⇒ Object
2173 2174 2175 2176 |
# File 'lib/syntax_tree/node.rb', line 2173 def ===(other) other.is_a?(BlockVar) && params === other.params && ArrayMatch.call(locals, other.locals) end |
#accept(visitor) ⇒ Object
2122 2123 2124 |
# File 'lib/syntax_tree/node.rb', line 2122 def accept(visitor) visitor.visit_block_var(self) end |
#arg0? ⇒ Boolean
When a single required parameter is declared for a block, it gets automatically expanded if the values being yielded into it are an array.
2180 2181 2182 2183 2184 |
# File 'lib/syntax_tree/node.rb', line 2180 def arg0? params.requireds.length == 1 && params.optionals.empty? && params.rest.nil? && params.posts.empty? && params.keywords.empty? && params.keyword_rest.nil? && params.block.nil? end |
#child_nodes ⇒ Object Also known as: deconstruct
2126 2127 2128 |
# File 'lib/syntax_tree/node.rb', line 2126 def child_nodes [params, *locals] end |
#copy(params: nil, locals: nil, location: nil) ⇒ Object
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 |
# File 'lib/syntax_tree/node.rb', line 2130 def copy(params: nil, locals: nil, location: nil) node = BlockVar.new( params: params || self.params, locals: locals || self.locals, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
2144 2145 2146 |
# File 'lib/syntax_tree/node.rb', line 2144 def deconstruct_keys(_keys) { params: params, locals: locals, location: location, comments: comments } end |
#format(q) ⇒ Object
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 |
# File 'lib/syntax_tree/node.rb', line 2160 def format(q) q.text("|") q.group do q.remove_breaks(q.format(params)) if locals.any? q.text("; ") q.seplist(locals, SEPARATOR) { |local| q.format(local) } end end q.text("|") end |