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
8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 |
# File 'lib/syntax_tree.rb', line 8513 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
8505 8506 8507 |
# File 'lib/syntax_tree.rb', line 8505 def block @block end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
8511 8512 8513 |
# File 'lib/syntax_tree.rb', line 8511 def comments @comments end |
#keyword_rest ⇒ Object (readonly)
- nil | :nil | KwRestParam
-
the optional keyword rest parameter
8502 8503 8504 |
# File 'lib/syntax_tree.rb', line 8502 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [ Ident, nil | untyped
-
]] any keyword parameters and their
optional default values
8499 8500 8501 |
# File 'lib/syntax_tree.rb', line 8499 def keywords @keywords end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
8508 8509 8510 |
# File 'lib/syntax_tree.rb', line 8508 def location @location end |
#optionals ⇒ Object (readonly)
- Array[ [ Ident, untyped
-
]] any optional parameters and their default
values
8487 8488 8489 |
# File 'lib/syntax_tree.rb', line 8487 def optionals @optionals end |
#posts ⇒ Object (readonly)
- Array[ Ident ]
-
any positional parameters that exist after a rest
parameter
8495 8496 8497 |
# File 'lib/syntax_tree.rb', line 8495 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Ident ]
-
any required parameters
8483 8484 8485 |
# File 'lib/syntax_tree.rb', line 8483 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | ArgsForward | ExcessedComma | RestParam
-
the optional rest
parameter
8491 8492 8493 |
# File 'lib/syntax_tree.rb', line 8491 def rest @rest end |
Instance Method Details
#child_nodes ⇒ Object
8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 |
# File 'lib/syntax_tree.rb', line 8546 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.
8539 8540 8541 8542 8543 8544 |
# File 'lib/syntax_tree.rb', line 8539 def empty? requireds.empty? && optionals.empty? && !rest && posts.empty? && keywords.empty? && !keyword_rest && !block end |
#format(q) ⇒ Object
8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 |
# File 'lib/syntax_tree.rb', line 8558 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 q.nest(0) do q.seplist(parts) { |part| q.format(part) } q.format(rest) if rest && rest.is_a?(ExcessedComma) end end |
#pretty_print(q) ⇒ Object
8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 |
# File 'lib/syntax_tree.rb', line 8580 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
8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 |
# File 'lib/syntax_tree.rb', line 8644 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 |