gazayas / masamune-ast

A covenience wrapper around Prism, a Ruby source code parser
MIT License
13 stars 1 forks source link

Order results when retrieving multiple types #32

Closed gazayas closed 1 year ago

gazayas commented 1 year ago
def all_methods
  method_definitions + method_calls
end

Since DataNode.order_results_by_position is fired within method_definitions and method_calls separately, it doesn't order the total result. You can see this in the README with the following code.

code = <<CODE
ary = [1, 2, 3]
ary.sum.times do |n|
  puts n
end

def foo
end
foo
foo # Call again
CODE

msmn = Masamune::AbstractSyntaxTree.new(code)
msmn.all_methods
#=> [{:position=>[6, 4], :token=>"foo"},
#=> {:position=>[2, 4], :token=>"sum"},
#=> {:position=>[2, 8], :token=>"times"},
#=> {:position=>[8, 0], :token=>"foo"},
#=> {:position=>[9, 0], :token=>"foo"}]

I want to give developers the option to retrieve DataNode instance objects as results if they want to (as opposed to the {position: ..., token: ...} hash), so I will probably implement that first, then order the results depending on what class the object is.