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
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #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:, comments: []) ⇒ Params
constructor
A new instance of Params.
Methods inherited from Node
Constructor Details
#initialize(requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:, comments: []) ⇒ Params
Returns a new instance of Params.
6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 |
# File 'lib/syntax_tree/node.rb', line 6065 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
6060 6061 6062 |
# File 'lib/syntax_tree/node.rb', line 6060 def block @block end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6063 6064 6065 |
# File 'lib/syntax_tree/node.rb', line 6063 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
- nil | :nil | KwRestParam
-
the optional keyword rest parameter
6057 6058 6059 |
# File 'lib/syntax_tree/node.rb', line 6057 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their
optional default values
6054 6055 6056 |
# File 'lib/syntax_tree/node.rb', line 6054 def keywords @keywords end |
#optionals ⇒ Object (readonly)
- Array[ [ Ident, untyped
-
]] any optional parameters and their default
values
6042 6043 6044 |
# File 'lib/syntax_tree/node.rb', line 6042 def optionals @optionals end |
#posts ⇒ Object (readonly)
- Array[ Ident ]
-
any positional parameters that exist after a rest
parameter
6050 6051 6052 |
# File 'lib/syntax_tree/node.rb', line 6050 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Ident ]
-
any required parameters
6038 6039 6040 |
# File 'lib/syntax_tree/node.rb', line 6038 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest
parameter
6046 6047 6048 |
# File 'lib/syntax_tree/node.rb', line 6046 def rest @rest end |
Instance Method Details
#accept(visitor) ⇒ Object
6096 6097 6098 |
# File 'lib/syntax_tree/node.rb', line 6096 def accept(visitor) visitor.visit_params(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 |
# File 'lib/syntax_tree/node.rb', line 6100 def child_nodes [ *requireds, *optionals.flatten(1), rest, *posts, *keywords.flatten(1), (keyword_rest if keyword_rest != :nil), block ] end |
#deconstruct_keys(keys) ⇒ Object
6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 |
# File 'lib/syntax_tree/node.rb', line 6114 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.
6091 6092 6093 6094 |
# File 'lib/syntax_tree/node.rb', line 6091 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 |
# File 'lib/syntax_tree/node.rb', line 6128 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 |