Skip to content

Commit 238f5a1

Browse files
authored
Fix route code lens when there are no classes in the stack (#407)
1 parent 459b186 commit 238f5a1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/ruby_lsp/ruby_lsp_rails/code_lens.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ def on_class_node_leave(node)
160160

161161
sig { returns(T.nilable(T::Boolean)) }
162162
def controller?
163-
class_name, superclass_name = T.must(@constant_name_stack.last)
164-
class_name.end_with?("Controller") && superclass_name&.end_with?("Controller")
163+
class_name, superclass_name = @constant_name_stack.last
164+
return false unless class_name && superclass_name
165+
166+
class_name.end_with?("Controller") && superclass_name.end_with?("Controller")
165167
end
166168

167169
sig { params(node: Prism::DefNode).void }

test/ruby_lsp_rails/code_lens_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ def index
303303
assert_match("config/routes.rb", path)
304304
end
305305

306+
test "doesn't break when analyzing a file without a class" do
307+
response = generate_code_lens_for_source(<<~RUBY)
308+
def index
309+
end
310+
RUBY
311+
312+
assert_empty(response)
313+
end
314+
306315
private
307316

308317
attr_reader :ruby

0 commit comments

Comments
 (0)