Class: SyntaxTree::HshPtn

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

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

#commentsObject (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

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4896
4897
4898
# File 'lib/syntax_tree/node.rb', line 4896

def constant
  @constant
end

#keyword_restObject (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

#keywordsObject (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_nodesObject 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