Skip to content

Commit 52099ff

Browse files
mkaputmhanberg
andauthored
fix: preserve fn newline metadata after semicolons (#112)
Capture newline metadata for anonymous functions when the separator is a semicolon token that carries newline count. This aligns cases like `fn ;\n -> :ok end` with Elixir metadata while avoiding incorrect `newlines: 0` for same-line semicolons. Adds deterministic regression assertions in the property-regression test block. --------- Co-authored-by: Mitchell Hanberg <mitch@mitchellhanberg.com>
1 parent 708da64 commit 52099ff

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/spitfire.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,18 @@ defmodule Spitfire do
21102110
trace "parse_anon_function", trace_meta(parser) do
21112111
meta = current_meta(parser)
21122112

2113-
newlines = get_newlines(parser)
2113+
semicolon_newlines =
2114+
case peek_newlines(parser, :";") do
2115+
nl when is_integer(nl) and nl > 0 -> nl
2116+
_ -> nil
2117+
end
2118+
2119+
newlines =
2120+
case peek_newlines(parser) || semicolon_newlines do
2121+
nil -> []
2122+
nl -> [newlines: nl]
2123+
end
2124+
21142125
parser = parser |> next_token() |> eat_eoe()
21152126

21162127
# fn creates its own stab scope

test/spitfire_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,10 @@ defmodule SpitfireTest do
22972297
# In-match operator (<-) in map keys - should be part of key, not wrap it
22982298
assert Spitfire.parse("%{s\\\\r => 1}") == s2q("%{s\\\\r => 1}")
22992299

2300+
# Fn args with semicolon/newline trivia
2301+
assert Spitfire.parse("fn ;\n -> :ok end") == s2q("fn ;\n -> :ok end")
2302+
assert Spitfire.parse("fn ; -> :ok end") == s2q("fn ; -> :ok end")
2303+
23002304
# Struct type with dot-call target
23012305
assert Spitfire.parse("%e.(){}") == s2q("%e.(){}")
23022306
assert Spitfire.parse("%e.(1){}") == s2q("%e.(1){}")

0 commit comments

Comments
 (0)