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.
6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 |
# File 'lib/syntax_tree/node.rb', line 6608 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
6603 6604 6605 |
# File 'lib/syntax_tree/node.rb', line 6603 def block @block end |
#comments ⇒ Object (readonly)
[Array[ Comment | EmbDoc ]] the comments attached to this node
6606 6607 6608 |
# File 'lib/syntax_tree/node.rb', line 6606 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
[nil | :nil | KwRestParam] the optional keyword rest parameter
6600 6601 6602 |
# File 'lib/syntax_tree/node.rb', line 6600 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
[Array[ [ Ident, nil | untyped ] ]] any keyword parameters and their optional default values
6597 6598 6599 |
# File 'lib/syntax_tree/node.rb', line 6597 def keywords @keywords end |
#optionals ⇒ Object (readonly)
[Array[ [ Ident, untyped ] ]] any optional parameters and their default values
6585 6586 6587 |
# File 'lib/syntax_tree/node.rb', line 6585 def optionals @optionals end |
#posts ⇒ Object (readonly)
[Array[ Ident ]] any positional parameters that exist after a rest parameter
6593 6594 6595 |
# File 'lib/syntax_tree/node.rb', line 6593 def posts @posts end |
#requireds ⇒ Object (readonly)
[Array[ Ident ]] any required parameters
6581 6582 6583 |
# File 'lib/syntax_tree/node.rb', line 6581 def requireds @requireds end |
#rest ⇒ Object (readonly)
[nil | ArgsForward | ExcessedComma | RestParam] the optional rest parameter
6589 6590 6591 |
# File 'lib/syntax_tree/node.rb', line 6589 def rest @rest end |
Instance Method Details
#accept(visitor) ⇒ Object
6639 6640 6641 |
# File 'lib/syntax_tree/node.rb', line 6639 def accept(visitor) visitor.visit_params(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 |
# File 'lib/syntax_tree/node.rb', line 6643 def child_nodes [ *requireds, *optionals.flatten(1), rest, *posts, *keywords.flatten(1), (keyword_rest if keyword_rest != :nil), block ] end |
#deconstruct_keys(keys) ⇒ Object
6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 |
# File 'lib/syntax_tree/node.rb', line 6657 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.
6634 6635 6636 6637 |
# File 'lib/syntax_tree/node.rb', line 6634 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 |
# File 'lib/syntax_tree/node.rb', line 6671 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) || parts.empty? q.nest(0, &contents) else q.group(0, "(", ")") do q.indent do q.breakable("") contents.call end q.breakable("") end end end |