Skip to content

Commit fa2c739

Browse files
committed
Handle incomplete route
1 parent e92c3ed commit fa2c739

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/ruby_lsp/ruby_lsp_rails/server.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ def route_location(name)
9090
return { result: nil }
9191
end
9292

93-
key = T.must(name.match(/(^.+)(_path|_url)$/))[1]
93+
match_data = name.match(/(^.+)(_path|_url)$/)
94+
return { result: nil } unless match_data
95+
96+
key = match_data[1]
9497

9598
# A token could match the _path or _url pattern, but not be an actual route.
9699
route = ::Rails.application.routes.named_routes.get(key)
97-
98-
unless route&.source_location
99-
return { result: nil }
100-
end
100+
return { result: nil } unless route&.source_location
101101

102102
{
103103
result: {

test/ruby_lsp_rails/definition_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ def baz; end
9292
assert_equal(3, response[0].range.end.line)
9393
end
9494

95+
test "handles incomplete routes" do
96+
response = generate_definitions_for_source(<<~RUBY, { line: 0, character: 0 })
97+
_path
98+
RUBY
99+
100+
assert_empty(response)
101+
end
102+
95103
test "provides the definition of a custom route" do
96104
response = generate_definitions_for_source(<<~RUBY, { line: 0, character: 0 })
97105
archive_users_path

0 commit comments

Comments
 (0)