Class: SyntaxTree::Params
- Inherits:
-
Object
- Object
- SyntaxTree::Params
- Defined in:
- lib/syntax_tree.rb
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.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#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.
Instance Method Summary collapse
- #child_nodes ⇒ 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:, comments: []) ⇒ Params
constructor
A new instance of Params.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:, comments: []) ⇒ Params
Returns a new instance of Params.
9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 |
# File 'lib/syntax_tree.rb', line 9113 def initialize( requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:, comments: [] ) @requireds = requireds @optionals = optionals @rest = rest @posts = posts @keywords = keywords @keyword_rest = keyword_rest @block = block @location = location @comments = comments end |
Instance Attribute Details
#block ⇒ Object (readonly)
- nil | BlockArg
-
the optional block parameter
9105 9106 9107 |
# File 'lib/syntax_tree.rb', line 9105 def block @block end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9111 9112 9113 |
# File 'lib/syntax_tree.rb', line 9111 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
- nil | :nil | KwRestParam
-
the optional keyword rest parameter
9102 9103 9104 |
# File 'lib/syntax_tree.rb', line 9102 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their
optional default values
9099 9100 9101 |
# File 'lib/syntax_tree.rb', line 9099 def keywords @keywords end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
9108 9109 9110 |
# File 'lib/syntax_tree.rb', line 9108 def location @location end |
#optionals ⇒ Object (readonly)
- Array[ [ Ident, untyped
-
]] any optional parameters and their default
values
9087 9088 9089 |
# File 'lib/syntax_tree.rb', line 9087 def optionals @optionals end |
#posts ⇒ Object (readonly)
- Array[ Ident ]
-
any positional parameters that exist after a rest
parameter
9095 9096 9097 |
# File 'lib/syntax_tree.rb', line 9095 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Ident ]
-
any required parameters
9083 9084 9085 |
# File 'lib/syntax_tree.rb', line 9083 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest
parameter
9091 9092 9093 |
# File 'lib/syntax_tree.rb', line 9091 def rest @rest end |
Instance Method Details
#child_nodes ⇒ Object
9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 |
# File 'lib/syntax_tree.rb', line 9144 def child_nodes [ *requireds, *optionals.flatten(1), rest, *posts, *keywords.flatten(1), (keyword_rest if keyword_rest != :nil), block ] 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.
9139 9140 9141 9142 |
# File 'lib/syntax_tree.rb', line 9139 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 |
# File 'lib/syntax_tree.rb', line 9156 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 contents = -> do q.seplist(parts) { |part| q.format(part) } q.format(rest) if rest && rest.is_a?(ExcessedComma) end if [Def, Defs, DefEndless].include?(q.parent.class) q.group(0, "(", ")") do q.indent do q.breakable("") contents.call end q.breakable("") end else q.nest(0, &contents) end end |
#pretty_print(q) ⇒ Object
9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 |
# File 'lib/syntax_tree.rb', line 9190 def pretty_print(q) q.group(2, "(", ")") do q.text("params") if requireds.any? q.breakable q.group(2, "(", ")") { q.seplist(requireds) { |name| q.pp(name) } } end if optionals.any? q.breakable q.group(2, "(", ")") do q.seplist(optionals) do |(name, default)| q.pp(name) q.text("=") q.group(2) do q.breakable("") q.pp(default) end end end end if rest q.breakable q.pp(rest) end if posts.any? q.breakable q.group(2, "(", ")") { q.seplist(posts) { |value| q.pp(value) } } end if keywords.any? q.breakable q.group(2, "(", ")") do q.seplist(keywords) do |(name, default)| q.pp(name) if default q.text("=") q.group(2) do q.breakable("") q.pp(default) end end end end end if keyword_rest q.breakable q.pp(keyword_rest) end if block q.breakable q.pp(block) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 |
# File 'lib/syntax_tree.rb', line 9254 def to_json(*opts) { type: :params, reqs: requireds, opts: optionals, rest: rest, posts: posts, keywords: keywords, kwrest: keyword_rest, block: block, loc: location, cmts: comments }.to_json(*opts) end |