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
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(constant: nil, keywords: nil, keyword_rest: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, keywords:, keyword_rest:, location:) ⇒ 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:) ⇒ HshPtn
Returns a new instance of HshPtn.
5941 5942 5943 5944 5945 5946 5947 |
# File 'lib/syntax_tree/node.rb', line 5941 def initialize(constant:, keywords:, keyword_rest:, location:) @constant = constant @keywords = keywords @keyword_rest = keyword_rest @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5939 5940 5941 |
# File 'lib/syntax_tree/node.rb', line 5939 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | untyped
-
the optional constant wrapper
5929 5930 5931 |
# File 'lib/syntax_tree/node.rb', line 5929 def constant @constant end |
#keyword_rest ⇒ Object (readonly)
- nil | VarField
-
an optional parameter to gather up all remaining keywords
5936 5937 5938 |
# File 'lib/syntax_tree/node.rb', line 5936 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
5933 5934 5935 |
# File 'lib/syntax_tree/node.rb', line 5933 def keywords @keywords end |
Instance Method Details
#===(other) ⇒ Object
6034 6035 6036 6037 6038 6039 6040 6041 |
# File 'lib/syntax_tree/node.rb', line 6034 def ===(other) other.is_a?(HshPtn) && constant === other.constant && keywords.length == other.keywords.length && keywords .zip(other.keywords) .all? { |left, right| ArrayMatch.call(left, right) } && keyword_rest === other.keyword_rest end |
#accept(visitor) ⇒ Object
5949 5950 5951 |
# File 'lib/syntax_tree/node.rb', line 5949 def accept(visitor) visitor.visit_hshptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5953 5954 5955 |
# File 'lib/syntax_tree/node.rb', line 5953 def child_nodes [constant, *keywords.flatten(1), keyword_rest] end |
#copy(constant: nil, keywords: nil, keyword_rest: nil, location: nil) ⇒ Object
5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 |
# File 'lib/syntax_tree/node.rb', line 5957 def copy(constant: nil, keywords: nil, keyword_rest: nil, location: nil) node = HshPtn.new( constant: constant || self.constant, keywords: keywords || self.keywords, keyword_rest: keyword_rest || self.keyword_rest, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
5972 5973 5974 5975 5976 5977 5978 5979 5980 |
# File 'lib/syntax_tree/node.rb', line 5972 def deconstruct_keys(_keys) { constant: constant, keywords: keywords, keyword_rest: keyword_rest, location: location, comments: comments } end |
#format(q) ⇒ Object
5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 |
# File 'lib/syntax_tree/node.rb', line 5982 def format(q) parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) } parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest nested = PATTERNS.include?(q.parent.class) # 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_empty format_contents(q, parts, nested) end q.breakable_empty 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 !nested && parts.size == 1 format_contents(q, parts, nested) 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_space format_contents(q, parts, nested) end if q.target_ruby_version < Formatter::SemanticVersion.new("2.7.3") q.text(" }") else q.breakable_space q.text("}") end end end |