Class: Ruby::Merge::RubyHashLiteralProjector

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/merge.rb

Overview

Projects Ruby hash literals into source-preserving merge nodes.

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ RubyHashLiteralProjector

Returns a new instance of RubyHashLiteralProjector.



2464
2465
2466
# File 'lib/ruby/merge.rb', line 2464

def initialize(source)
  @source = source.to_s
end

Instance Method Details

#callObject

Raises:

  • (ArgumentError)


2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
# File 'lib/ruby/merge.rb', line 2468

def call
  tree = TreeHaver.parser_for(:ruby, backend_type: :tree_sitter).parse(source)
  root = if tree.respond_to?(:parse_result)
           tree.parse_result.value
         else
           tree.root_node
         end
  hash_node = root_hash_node(root)
  raise ArgumentError, 'expected Ruby hash literal' unless hash_node?(hash_node)

  project_hash_node(hash_node)
end