Skip to content

Fix route code lens when there are no classes in the stack #407

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 1 commit into from
Jul 11, 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
6 changes: 4 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rails/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ def on_class_node_leave(node)

sig { returns(T.nilable(T::Boolean)) }
def controller?
class_name, superclass_name = T.must(@constant_name_stack.last)
class_name.end_with?("Controller") && superclass_name&.end_with?("Controller")
class_name, superclass_name = @constant_name_stack.last
return false unless class_name && superclass_name

class_name.end_with?("Controller") && superclass_name.end_with?("Controller")
end

sig { params(node: Prism::DefNode).void }
Expand Down
9 changes: 9 additions & 0 deletions test/ruby_lsp_rails/code_lens_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ def index
assert_match("config/routes.rb", path)
end

test "doesn't break when analyzing a file without a class" do
response = generate_code_lens_for_source(<<~RUBY)
def index
end
RUBY

assert_empty(response)
end

private

attr_reader :ruby
Expand Down
Loading