Skip to content

Commit

Permalink
Fix route code lens when there are no classes in the stack (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Jul 11, 2024
1 parent 459b186 commit 238f5a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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

0 comments on commit 238f5a1

Please sign in to comment.