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
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
Returns a new instance of HshPtn.
5035 5036 5037 5038 5039 5040 5041 |
# File 'lib/syntax_tree/node.rb', line 5035 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
5033 5034 5035 |
# File 'lib/syntax_tree/node.rb', line 5033 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | untyped
-
the optional constant wrapper
5023 5024 5025 |
# File 'lib/syntax_tree/node.rb', line 5023 def constant @constant end |
#keyword_rest ⇒ Object (readonly)
- nil | VarField
-
an optional parameter to gather up all remaining keywords
5030 5031 5032 |
# File 'lib/syntax_tree/node.rb', line 5030 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
5027 5028 5029 |
# File 'lib/syntax_tree/node.rb', line 5027 def keywords @keywords end |
Instance Method Details
#accept(visitor) ⇒ Object
5043 5044 5045 |
# File 'lib/syntax_tree/node.rb', line 5043 def accept(visitor) visitor.visit_hshptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5047 5048 5049 |
# File 'lib/syntax_tree/node.rb', line 5047 def child_nodes [constant, *keywords.flatten(1), keyword_rest] end |
#deconstruct_keys(_keys) ⇒ Object
5053 5054 5055 5056 5057 5058 5059 5060 5061 |
# File 'lib/syntax_tree/node.rb', line 5053 def deconstruct_keys(_keys) { constant: constant, keywords: keywords, keyword_rest: keyword_rest, location: location, comments: comments } end |
#format(q) ⇒ Object
5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 |
# File 'lib/syntax_tree/node.rb', line 5063 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 there is a constant, we're going to format to have the constant name # first and then use brackets. if constant q.group do q.format(constant) q.text("[") q.indent do q.breakable("") contents.call end q.breakable("") q.text("]") end return end # If there's nothing at all, then we're going to use empty braces. if parts.empty? q.text("{}") return end # If there's only one pair, then we'll just print the contents provided # we're not inside another pattern. if !PATTERNS.include?(q.parent.class) && parts.size == 1 contents.call return end # Otherwise, we're going to always use braces to make it clear it's a hash # pattern. q.group do q.text("{") q.indent do q.breakable contents.call end q.breakable q.text("}") end end |