Skip to content

Commit bd6c527

Browse files
committed
avoid storing anonymous function AST in CallInfo
1 parent 0e6278a commit bd6c527

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/elixir_sense/core/compiler/state.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ defmodule ElixirSense.Core.Compiler.State do
348348
column + Keyword.get(meta, :column_correction, 0)
349349
end
350350

351+
# For anonymous function calls, func can be an AST tuple (e.g., {:fn, [], []})
352+
# Replace it with nil to avoid issues in consumers
353+
func =
354+
case func do
355+
f when is_atom(f) -> f
356+
{:attribute, _} = attr -> attr
357+
{:variable, _, _} = var -> var
358+
_ -> nil
359+
end
360+
351361
call = %CallInfo{mod: mod, func: func, arity: arity, position: {line, column}, kind: kind}
352362

353363
calls =

lib/elixir_sense/core/state/call_info.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ defmodule ElixirSense.Core.State.CallInfo do
55
@type t :: %ElixirSense.Core.State.CallInfo{
66
arity: non_neg_integer | nil,
77
position: {non_neg_integer, pos_integer | nil},
8-
func: atom,
9-
mod: module | {:attribute, atom},
8+
func: atom | {:attribute, atom} | {:variable, atom, any} | nil,
9+
mod: module | {:attribute, atom} | {:variable, atom, any} | nil,
1010
kind: atom
1111
}
1212
defstruct arity: 0,

test/elixir_sense/core/metadata_builder_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8954,6 +8954,24 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
89548954
} = state.calls
89558955
end
89568956

8957+
test "registers calls capture expression anonymous" do
8958+
state =
8959+
"""
8960+
defmodule NyModule do
8961+
def func do
8962+
(& 1 + &1).(1)
8963+
end
8964+
end
8965+
"""
8966+
|> string_to_state
8967+
8968+
# The anonymous function call should have func: nil instead of a tuple
8969+
assert Enum.any?(
8970+
state.calls[3],
8971+
&match?(%CallInfo{arity: 1, func: nil, kind: :anonymous_function}, &1)
8972+
)
8973+
end
8974+
89578975
test "registers calls on ex_unit DSL" do
89588976
state =
89598977
"""

0 commit comments

Comments
 (0)