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

Fix inference for compact namespace declarations #2569

Merged
merged 1 commit into from
Sep 19, 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
8 changes: 6 additions & 2 deletions lib/ruby_lsp/type_inferrer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ def self_receiver_handling(node_context)
return Type.new(node_context.fully_qualified_name) if node_context.surrounding_method

# If we're not inside a method, then we're inside the body of a class or module, which is a singleton
# context
Type.new("#{nesting.join("::")}::<Class:#{nesting.last}>")
# context.
#
# If the class/module definition is using compact style (e.g.: `class Foo::Bar`), then we need to split the name
# into its individual parts to build the correct singleton name
parts = nesting.flat_map { |part| part.split("::") }
Type.new("#{parts.join("::")}::<Class:#{parts.last}>")
end

sig do
Expand Down
34 changes: 34 additions & 0 deletions test/type_inferrer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,40 @@ def test_infer_lambda_literal
assert_equal("Proc", @type_inferrer.infer_receiver_type(node_context).name)
end

def test_infer_self_type_for_compact_namespace
node_context = index_and_locate(<<~RUBY, { line: 1, character: 3 })
class Admin::User
validates
end
RUBY

assert_equal("Admin::User::<Class:User>", @type_inferrer.infer_receiver_type(node_context).name)
end

def test_infer_self_type_for_compact_namespace_inside_method
node_context = index_and_locate(<<~RUBY, { line: 2, character: 4 })
class Admin::User
def foo
hello
end
end
RUBY

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

def test_infer_self_type_for_compact_namespace_inside_singleton_method
node_context = index_and_locate(<<~RUBY, { line: 2, character: 4 })
class Admin::User
def self.foo
hello
end
end
RUBY

assert_equal("Admin::User::<Class:User>", @type_inferrer.infer_receiver_type(node_context).name)
end

private

def index_and_locate(source, position)
Expand Down
Loading