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
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:, comments: []) ⇒ Params
Returns a new instance of Params.
6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 |
# File 'lib/syntax_tree/node.rb', line 6785 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
6780 6781 6782 |
# File 'lib/syntax_tree/node.rb', line 6780 def block @block end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6783 6784 6785 |
# File 'lib/syntax_tree/node.rb', line 6783 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
- nil | :nil | KwRestParam
-
the optional keyword rest parameter
6777 6778 6779 |
# File 'lib/syntax_tree/node.rb', line 6777 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their
optional default values
6774 6775 6776 |
# File 'lib/syntax_tree/node.rb', line 6774 def keywords @keywords end |
#optionals ⇒ Object (readonly)
- Array[ [ Ident, untyped
-
]] any optional parameters and their default
values
6762 6763 6764 |
# File 'lib/syntax_tree/node.rb', line 6762 def optionals @optionals end |
#posts ⇒ Object (readonly)
- Array[ Ident ]
-
any positional parameters that exist after a rest
parameter
6770 6771 6772 |
# File 'lib/syntax_tree/node.rb', line 6770 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Ident ]
-
any required parameters
6758 6759 6760 |
# File 'lib/syntax_tree/node.rb', line 6758 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest
parameter
6766 6767 6768 |
# File 'lib/syntax_tree/node.rb', line 6766 def rest @rest end |
Instance Method Details
#accept(visitor) ⇒ Object
6816 6817 6818 |
# File 'lib/syntax_tree/node.rb', line 6816 def accept(visitor) visitor.visit_params(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 |
# File 'lib/syntax_tree/node.rb', line 6820 def child_nodes [ *requireds, *optionals.flatten(1), rest, *posts, *keywords.flatten(1), (keyword_rest if keyword_rest != :nil), block ] end |
#deconstruct_keys(_keys) ⇒ Object
6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 |
# File 'lib/syntax_tree/node.rb', line 6834 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.
6811 6812 6813 6814 |
# File 'lib/syntax_tree/node.rb', line 6811 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 |
# File 'lib/syntax_tree/node.rb', line 6848 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.is_a?(ExcessedComma) end if ![Def, Defs, DefEndless].include?(q.parent.class) || parts.empty? q.nest(0, &contents) else q.group(0, "(", ")") do q.indent do q.breakable("") contents.call end q.breakable("") end end end |