Class: SyntaxTree::Params
Overview
Params represents defining parameters on a method or lambda.
def method(param) end
Defined Under Namespace
Classes: KeywordFormatter, KeywordRestFormatter, OptionalFormatter
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
- nil | BlockArg
-
the optional block parameter.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#keyword_rest ⇒ Object
readonly
- nil | :nil | KwRestParam
-
the optional keyword rest parameter.
-
#keywords ⇒ Object
readonly
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their optional default values.
-
#optionals ⇒ Object
readonly
- Array[ [ Ident, untyped
-
]] any optional parameters and their default values.
-
#posts ⇒ Object
readonly
- Array[ Ident ]
-
any positional parameters that exist after a rest parameter.
-
#requireds ⇒ Object
readonly
- Array[ Ident ]
-
any required parameters.
-
#rest ⇒ Object
readonly
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest parameter.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(location: nil, requireds: nil, optionals: nil, rest: nil, posts: nil, keywords: nil, keyword_rest: nil, block: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
-
#empty? ⇒ Boolean
Params nodes are the most complicated in the tree.
- #format(q) ⇒ Object
-
#initialize(requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:) ⇒ Params
constructor
A new instance of Params.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:) ⇒ Params
Returns a new instance of Params.
8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 |
# File 'lib/syntax_tree/node.rb', line 8180 def initialize( requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location: ) @requireds = requireds @optionals = optionals @rest = rest @posts = posts @keywords = keywords @keyword_rest = keyword_rest @block = block @location = location @comments = [] end |
Instance Attribute Details
#block ⇒ Object (readonly)
- nil | BlockArg
-
the optional block parameter
8175 8176 8177 |
# File 'lib/syntax_tree/node.rb', line 8175 def block @block end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
8178 8179 8180 |
# File 'lib/syntax_tree/node.rb', line 8178 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
- nil | :nil | KwRestParam
-
the optional keyword rest parameter
8172 8173 8174 |
# File 'lib/syntax_tree/node.rb', line 8172 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their
optional default values
8169 8170 8171 |
# File 'lib/syntax_tree/node.rb', line 8169 def keywords @keywords end |
#optionals ⇒ Object (readonly)
- Array[ [ Ident, untyped
-
]] any optional parameters and their default
values
8157 8158 8159 |
# File 'lib/syntax_tree/node.rb', line 8157 def optionals @optionals end |
#posts ⇒ Object (readonly)
- Array[ Ident ]
-
any positional parameters that exist after a rest
parameter
8165 8166 8167 |
# File 'lib/syntax_tree/node.rb', line 8165 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Ident ]
-
any required parameters
8153 8154 8155 |
# File 'lib/syntax_tree/node.rb', line 8153 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest
parameter
8161 8162 8163 |
# File 'lib/syntax_tree/node.rb', line 8161 def rest @rest end |
Instance Method Details
#===(other) ⇒ Object
8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 |
# File 'lib/syntax_tree/node.rb', line 8305 def ===(other) other.is_a?(Params) && ArrayMatch.call(requireds, other.requireds) && optionals.length == other.optionals.length && optionals .zip(other.optionals) .all? { |left, right| ArrayMatch.call(left, right) } && rest === other.rest && ArrayMatch.call(posts, other.posts) && keywords.length == other.keywords.length && keywords .zip(other.keywords) .all? { |left, right| ArrayMatch.call(left, right) } && keyword_rest === other.keyword_rest && block === other.block end |
#accept(visitor) ⇒ Object
8210 8211 8212 |
# File 'lib/syntax_tree/node.rb', line 8210 def accept(visitor) visitor.visit_params(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 |
# File 'lib/syntax_tree/node.rb', line 8214 def child_nodes [ *requireds, *optionals.flatten(1), rest, *posts, *keywords.flatten(1), (keyword_rest if keyword_rest != :nil), block ] end |
#copy(location: nil, requireds: nil, optionals: nil, rest: nil, posts: nil, keywords: nil, keyword_rest: nil, block: nil) ⇒ Object
8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 |
# File 'lib/syntax_tree/node.rb', line 8226 def copy( location: nil, requireds: nil, optionals: nil, rest: nil, posts: nil, keywords: nil, keyword_rest: nil, block: nil ) node = Params.new( location: location || self.location, requireds: requireds || self.requireds, optionals: optionals || self.optionals, rest: rest || self.rest, posts: posts || self.posts, keywords: keywords || self.keywords, keyword_rest: keyword_rest || self.keyword_rest, block: block || self.block ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 |
# File 'lib/syntax_tree/node.rb', line 8254 def deconstruct_keys(_keys) { location: location, requireds: requireds, optionals: optionals, rest: rest, posts: posts, keywords: keywords, keyword_rest: keyword_rest, block: block, comments: comments } end |
#empty? ⇒ Boolean
Params nodes are the most complicated in the tree. Occasionally you want to know if they are “empty”, which means not having any parameters declared. This logic accesses every kind of parameter and determines if it’s missing.
8205 8206 8207 8208 |
# File 'lib/syntax_tree/node.rb', line 8205 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 |
# File 'lib/syntax_tree/node.rb', line 8268 def format(q) parts = [ *requireds, *optionals.map { |(name, value)| OptionalFormatter.new(name, value) } ] parts << rest if rest && !rest.is_a?(ExcessedComma) parts += [ *posts, *keywords.map { |(name, value)| KeywordFormatter.new(name, value) } ] parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest parts << block if block if parts.empty? q.nest(0) { format_contents(q, parts) } return end if q.parent.is_a?(DefNode) q.nest(0) do q.text("(") q.group do q.indent do q.breakable_empty format_contents(q, parts) end q.breakable_empty end q.text(")") end else q.nest(0) { format_contents(q, parts) } end end |