Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess types for chained invocations #2637

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/ruby_lsp/type_inferrer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ def infer_receiver_for_call_node(node, node_context)

Type.new("#{parts.join("::")}::#{last}::<Class:#{last}>")
else
raw_receiver = node.receiver&.slice

raw_receiver = if receiver.is_a?(Prism::CallNode)
receiver.message
else
receiver&.slice
end

if raw_receiver
guessed_name = raw_receiver
Expand Down
11 changes: 11 additions & 0 deletions test/type_inferrer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ class User
assert_equal("User", @type_inferrer.infer_receiver_type(node_context).name)
end

def test_infer_guessed_types_for_chained_method_calls
node_context = index_and_locate(<<~RUBY, { line: 3, character: 15 })
class User
end

something.user.name
RUBY

assert_equal("User", @type_inferrer.infer_receiver_type(node_context).name)
end

def test_infer_guessed_types_for_instance_variable_receiver
node_context = index_and_locate(<<~RUBY, { line: 4, character: 6 })
class User
Expand Down
Loading