Class: SyntaxTree::HshPtn
Overview
HshPtn represents matching against a hash pattern using the Ruby 2.7+ pattern matching syntax.
case value
in { key: }
end
Defined Under Namespace
Classes: KeywordFormatter, KeywordRestFormatter
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | untyped
-
the optional constant wrapper.
-
#keyword_rest ⇒ Object
readonly
- nil | VarField
-
an optional parameter to gather up all remaining keywords.
-
#keywords ⇒ Object
readonly
- Array[ [Label, untyped
-
]] the set of tuples representing the keywords that should be matched against in the pattern.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
constructor
A new instance of HshPtn.
Methods inherited from Node
Constructor Details
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
Returns a new instance of HshPtn.
4508 4509 4510 4511 4512 4513 4514 |
# File 'lib/syntax_tree/node.rb', line 4508 def initialize(constant:, keywords:, keyword_rest:, location:, comments: []) @constant = constant @keywords = keywords @keyword_rest = keyword_rest @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4506 4507 4508 |
# File 'lib/syntax_tree/node.rb', line 4506 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | untyped
-
the optional constant wrapper
4496 4497 4498 |
# File 'lib/syntax_tree/node.rb', line 4496 def constant @constant end |
#keyword_rest ⇒ Object (readonly)
- nil | VarField
-
an optional parameter to gather up all remaining keywords
4503 4504 4505 |
# File 'lib/syntax_tree/node.rb', line 4503 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [Label, untyped
-
]] the set of tuples representing the keywords
that should be matched against in the pattern
4500 4501 4502 |
# File 'lib/syntax_tree/node.rb', line 4500 def keywords @keywords end |
Instance Method Details
#accept(visitor) ⇒ Object
4516 4517 4518 |
# File 'lib/syntax_tree/node.rb', line 4516 def accept(visitor) visitor.visit_hshptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
4520 4521 4522 |
# File 'lib/syntax_tree/node.rb', line 4520 def child_nodes [constant, *keywords.flatten(1), keyword_rest] end |
#deconstruct_keys(keys) ⇒ Object
4526 4527 4528 4529 4530 4531 4532 4533 4534 |
# File 'lib/syntax_tree/node.rb', line 4526 def deconstruct_keys(keys) { constant: constant, keywords: keywords, keyword_rest: keyword_rest, location: location, comments: comments } end |
#format(q) ⇒ Object
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 |
# File 'lib/syntax_tree/node.rb', line 4536 def format(q) parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) } parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest contents = -> do q.seplist(parts) { |part| q.format(part, stackable: false) } end if constant q.format(constant) q.group(0, "[", "]", &contents) return end parent = q.parent if PATTERNS.include?(parent.class) q.text("{ ") contents.call q.text(" }") else contents.call end end |