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.
8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 |
# File 'lib/syntax_tree.rb', line 8981 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
8973 8974 8975 |
# File 'lib/syntax_tree.rb', line 8973 def block @block end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
8979 8980 8981 |
# File 'lib/syntax_tree.rb', line 8979 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
- nil | :nil | KwRestParam
-
the optional keyword rest parameter
8970 8971 8972 |
# File 'lib/syntax_tree.rb', line 8970 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their
optional default values
8967 8968 8969 |
# File 'lib/syntax_tree.rb', line 8967 def keywords @keywords end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
8976 8977 8978 |
# File 'lib/syntax_tree.rb', line 8976 def location @location end |
#optionals ⇒ Object (readonly)
- Array[ [ Ident, untyped
-
]] any optional parameters and their default
values
8955 8956 8957 |
# File 'lib/syntax_tree.rb', line 8955 def optionals @optionals end |
#posts ⇒ Object (readonly)
- Array[ Ident ]
-
any positional parameters that exist after a rest
parameter
8963 8964 8965 |
# File 'lib/syntax_tree.rb', line 8963 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Ident ]
-
any required parameters
8951 8952 8953 |
# File 'lib/syntax_tree.rb', line 8951 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest
parameter
8959 8960 8961 |
# File 'lib/syntax_tree.rb', line 8959 def rest @rest end |
Instance Method Details
#child_nodes ⇒ Object
9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 |
# File 'lib/syntax_tree.rb', line 9012 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.
9007 9008 9009 9010 |
# File 'lib/syntax_tree.rb', line 9007 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 |
# File 'lib/syntax_tree.rb', line 9024 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].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
9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 |
# File 'lib/syntax_tree.rb', line 9058 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
9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 |
# File 'lib/syntax_tree.rb', line 9122 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 |