Skip to content

Commit 612e6ac

Browse files
committed
fix: prevent infinite loop when typing out anonymous function
`fn ->)` is a common intermediate state when writing anonymous function and when the editor closes the bracket, putting the cursor inside. This was causing an infinite loop, because it did not consume a token. This is because of special handling of some tokens. Same happened to :eof.
1 parent 4679425 commit 612e6ac

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/spitfire.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@ defmodule Spitfire do
21802180

21812181
{ast, parser} =
21822182
cond do
2183-
current_token(parser) == :-> and peek_token(parser) == :end ->
2183+
current_token(parser) == :-> and peek_token(parser) in [:")", :end, :eof] ->
21842184
parser = next_token(parser)
21852185
{ast, parser}
21862186

test/spitfire_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,13 @@ defmodule SpitfireTest do
23482348
end)
23492349
end
23502350

2351+
test "fn -> followed by closing delimiter does not hang" do
2352+
assert {:error, _ast, _errors} = Spitfire.parse("fn ->)")
2353+
assert {:error, _ast, _errors} = Spitfire.parse("fn ->")
2354+
assert {:error, _ast, _errors} = Spitfire.parse("Enum.map(fn ->)")
2355+
assert {:error, _ast, _errors} = Spitfire.parse("fn ->\n)")
2356+
end
2357+
23512358
test "missing bitstring brackets" do
23522359
code = """
23532360
<<one::

0 commit comments

Comments
 (0)