Skip to content

Add support for class_methods do blocks within concerns #530

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

Merged
merged 10 commits into from
Nov 21, 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
21 changes: 21 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def on_call_node_enter(call_node)
handle_concern_extend(owner, call_node)
when :has_one, :has_many, :belongs_to, :has_and_belongs_to_many
handle_association(owner, call_node)
# for `class_methods do` blocks within concerns
when :class_methods
handle_class_methods(owner, call_node)
end
end

sig do
override.params(
call_node: Prism::CallNode,
).void
end
def on_call_node_leave(call_node)
if call_node.name == :class_methods && call_node.block
@listener.pop_namespace_stack
end
end

Expand Down Expand Up @@ -83,6 +97,13 @@ def handle_concern_extend(owner, call_node)
# Do nothing
end
end

sig { params(owner: RubyIndexer::Entry::Namespace, call_node: Prism::CallNode).void }
def handle_class_methods(owner, call_node)
return unless call_node.block

@listener.add_module("ClassMethods", call_node.location, call_node.location)
end
end
end
end
6 changes: 6 additions & 0 deletions test/dummy/app/models/concerns/verifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ def all_verified
all.select(&:verified?)
end
end

class_methods do
def all_unverified
all.reject(&:verified?)
end
end
end
69 changes: 62 additions & 7 deletions test/ruby_lsp_rails/indexing_enhancement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Rails
class IndexingEnhancementTest < ActiveSupport::TestCase
class << self
# For these tests, it's convenient to have the index fully populated with Rails information, but we don't have
# to reindex on every single example or that will be too slow
# to re-index on every single example or that will be too slow
def populated_index
@index ||= begin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinistock avoiding the memoization solves in test instability here. It's obviously slower though.

A few possible alternatives:

  • Have each test be responsible to removing things it added to the index (maybe compare the state before and after in a teardown to ensure clean-up isn't forgotten)
  • Have some naming convention for things we use in tests, and have them automatically removed from the index in a teardown.
  • Have a way to skip indexing of RBS in index_all (which takes a large portion of the test time).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we just add note to the test and use different names in each example? Trying to clean up all of the index state might be difficult and it will require exposing some internal APIs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually scratch that. We already have an API to delete entries, let's use that on teardown.

index = RubyIndexer::Index.new
Expand All @@ -20,22 +20,77 @@ def populated_index

def setup
@index = self.class.populated_index
@indexable_path = RubyIndexer::IndexablePath.new(nil, "/fake.rb")
end

def teardown
# Prevent state leaking between tests
@index.delete(@indexable_path)
@index.instance_variable_set(:@ancestors, {})
end

test "ClassMethods module inside concerns are automatically extended" do
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
class Post < ActiveRecord::Base
Copy link
Contributor Author

@andyw8 andyw8 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this so that all the code is visible in the tests, rather than having to know what Rails does internally.

@index.index_single(@indexable_path, <<~RUBY)
module Verifiable
extend ActiveSupport::Concern

module ClassMethods
def all_verified; end
end
end

class Post
include Verifiable
end
RUBY

ancestors = @index.linearized_ancestors_of("Post::<Class:Post>")
assert_includes(ancestors, "ActiveRecord::Associations::ClassMethods")
assert_includes(ancestors, "ActiveRecord::Store::ClassMethods")
assert_includes(ancestors, "ActiveRecord::AttributeMethods::ClassMethods")

assert_includes(ancestors, "Verifiable::ClassMethods")
refute_nil(@index.resolve_method("all_verified", "Post::<Class:Post>"))
end

test "class_methods blocks inside concerns are automatically extended via a ClassMethods module" do
@index.index_single(@indexable_path, <<~RUBY)
module Verifiable
extend ActiveSupport::Concern

class_methods do
def all_verified; end
end
end

class Post
include Verifiable
end
RUBY

ancestors = @index.linearized_ancestors_of("Post::<Class:Post>")

assert_includes(ancestors, "Verifiable::ClassMethods")
refute_nil(@index.resolve_method("all_verified", "Post::<Class:Post>"))
end

test "ignores `class_methods` calls without a block" do
@index.index_single(@indexable_path, <<~RUBY)
module Verifiable
extend ActiveSupport::Concern

class_methods
end

class Post
include Verifiable
end
RUBY

ancestors = @index.linearized_ancestors_of("Post::<Class:Post>")

refute_includes(ancestors, "Verifiable::ClassMethods")
end

test "associations" do
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
@index.index_single(@indexable_path, <<~RUBY)
class Post < ActiveRecord::Base
has_one :content
belongs_to :author
Expand Down
Loading