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

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn

Returns a new instance of HshPtn.



5198
5199
5200
5201
5202
5203
5204
# File 'lib/syntax_tree/node.rb', line 5198

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



5196
5197
5198
# File 'lib/syntax_tree/node.rb', line 5196

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



5186
5187
5188
# File 'lib/syntax_tree/node.rb', line 5186

def constant
  @constant
end

#keyword_restObject (readonly)

nil | VarField

an optional parameter to gather up all remaining keywords



5193
5194
5195
# File 'lib/syntax_tree/node.rb', line 5193

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



5190
5191
5192
# File 'lib/syntax_tree/node.rb', line 5190

def keywords
  @keywords
end

Instance Method Details

#accept(visitor) ⇒ Object



5206
5207
5208
# File 'lib/syntax_tree/node.rb', line 5206

def accept(visitor)
  visitor.visit_hshptn(self)
end

#child_nodesObject Also known as: deconstruct



5210
5211
5212
# File 'lib/syntax_tree/node.rb', line 5210

def child_nodes
  [constant, *keywords.flatten(1), keyword_rest]
end

#deconstruct_keys(_keys) ⇒ Object



5216
5217
5218
5219
5220
5221
5222
5223
5224
# File 'lib/syntax_tree/node.rb', line 5216

def deconstruct_keys(_keys)
  {
    constant: constant,
    keywords: keywords,
    keyword_rest: keyword_rest,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
# File 'lib/syntax_tree/node.rb', line 5226

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 < Gem::Version.new("2.7.3")
      q.text(" }")
    else
      q.breakable_space
      q.text("}")
    end
  end
end