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.
4908 4909 4910 4911 4912 4913 4914 |
# File 'lib/syntax_tree/node.rb', line 4908 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
4906 4907 4908 |
# File 'lib/syntax_tree/node.rb', line 4906 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | untyped
-
the optional constant wrapper
4896 4897 4898 |
# File 'lib/syntax_tree/node.rb', line 4896 def constant @constant end |
#keyword_rest ⇒ Object (readonly)
- nil | VarField
-
an optional parameter to gather up all remaining keywords
4903 4904 4905 |
# File 'lib/syntax_tree/node.rb', line 4903 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
4900 4901 4902 |
# File 'lib/syntax_tree/node.rb', line 4900 def keywords @keywords end |
Instance Method Details
#accept(visitor) ⇒ Object
4916 4917 4918 |
# File 'lib/syntax_tree/node.rb', line 4916 def accept(visitor) visitor.visit_hshptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
4920 4921 4922 |
# File 'lib/syntax_tree/node.rb', line 4920 def child_nodes [constant, *keywords.flatten(1), keyword_rest] end |
#deconstruct_keys(keys) ⇒ Object
4926 4927 4928 4929 4930 4931 4932 4933 4934 |
# File 'lib/syntax_tree/node.rb', line 4926 def deconstruct_keys(keys) { constant: constant, keywords: keywords, keyword_rest: keyword_rest, location: location, comments: comments } end |
#format(q) ⇒ Object
4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 |
# File 'lib/syntax_tree/node.rb', line 4936 def format(q) parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) } parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest contents = -> do q.group { q.seplist(parts) { |part| q.format(part, stackable: false) } } # If there isn't a constant, and there's a blank keyword_rest, then we # have an plain ** that needs to have a `then` after it in order to # parse correctly on the next parse. q.text(" then") if !constant && keyword_rest && keyword_rest.value.nil? end if constant q.format(constant) q.group(0, "[", "]", &contents) return end if parts.empty? q.text("{}") elsif PATTERNS.include?(q.parent.class) q.text("{ ") contents.call q.text(" }") else contents.call end end |