Skip to content

Commit

Permalink
Chains resolve identical names with different contexts (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
castwide committed Feb 1, 2025
1 parent f86dc1c commit 56558f2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.51.3
- Chains resolve identical names with different contexts (#679)

## 0.51.2 - February 1, 2025
- Fix exception from parser when anonymous block forwarding is used (#740)
- Parameterized Object types
Expand Down
2 changes: 1 addition & 1 deletion lib/solargraph/pin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def proxy return_type
end

def identity
@identity ||= "#{closure.context.namespace}|#{name}"
@identity ||= "#{closure.path}|#{name}"
end

def inspect
Expand Down
3 changes: 3 additions & 0 deletions lib/solargraph/source/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def infer_first_defined pins, context, api_map, locals
pins.each do |pin|
# Avoid infinite recursion
next if @@inference_stack.include?(pin.identity)

@@inference_stack.push pin.identity
type = pin.typify(api_map)
@@inference_stack.pop
Expand All @@ -143,10 +144,12 @@ def infer_first_defined pins, context, api_map, locals
if possibles.empty?
# Limit method inference recursion
return ComplexType::UNDEFINED if @@inference_depth >= 10 && pins.first.is_a?(Pin::Method)

@@inference_depth += 1
pins.each do |pin|
# Avoid infinite recursion
next if @@inference_stack.include?(pin.identity)

@@inference_stack.push pin.identity
type = pin.probe(api_map)
@@inference_stack.pop
Expand Down
2 changes: 1 addition & 1 deletion lib/solargraph/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Solargraph
VERSION = '0.51.2'
VERSION = '0.51.3'
end
18 changes: 18 additions & 0 deletions spec/source_map/clip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1545,4 +1545,22 @@ def foo
pin_names = api_map.clip_at('test.rb', [8, 5]).complete.pins.map(&:name)
expect(pin_names).to include('some_method')
end

it 'resolves name conflicts in pin identities' do
source = Solargraph::Source.load_string(%(
class A
def x
"string"
end
end
a = A.new
x = a.x
x
), 'test.rb')
api_map = Solargraph::ApiMap.new.map(source)
clip = api_map.clip_at('test.rb', [9, 7])
type = clip.infer
expect(type.tag).to eq('String')
end
end

0 comments on commit 56558f2

Please sign in to comment.