Skip to content

Commit

Permalink
Fix incorrect ranges for fn and -> (#117)
Browse files Browse the repository at this point in the history
Fixes #116
  • Loading branch information
doorgan committed Jan 4, 2024
1 parent dca8e9d commit c1251a3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
35 changes: 34 additions & 1 deletion lib/sourceror/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,31 @@ defmodule Sourceror.Range do
get_range(first)
end

# Stabs without args
# Stabs without right args
# a ->
defp do_get_range({:->, stab_meta, [left, {:__block__, right_meta, [nil]} = right]}) do
end_pos =
if right_meta[:column] == stab_meta[:column] do
right_meta
|> Keyword.take([:line, :column])
|> Keyword.update!(:column, &(&1 + 2))
else
get_range(right).end
end

start_pos =
case left do
[] ->
Keyword.take(stab_meta, [:line, :column])

_ ->
get_range(left).start
end

%{start: start_pos, end: end_pos}
end

# Stabs without left args
# -> b
defp do_get_range({:->, meta, [[], right]}) do
start_pos = Keyword.take(meta, [:line, :column])
Expand Down Expand Up @@ -425,6 +449,15 @@ defmodule Sourceror.Range do
end
end

defp get_range_for_node_with_closing_line({:fn, _, _} = quoted) do
start_position = Sourceror.get_start_position(quoted)
end_position = Sourceror.get_end_position(quoted)

end_position = Keyword.update!(end_position, :column, &(&1 + 3))

%{start: start_position, end: end_position}
end

defp get_range_for_node_with_closing_line({_, meta, _} = quoted) do
start_position = Sourceror.get_start_position(quoted)
end_position = Sourceror.get_end_position(quoted)
Expand Down
42 changes: 42 additions & 0 deletions test/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,48 @@ defmodule SourcerorTest.RangeTest do
}
end

test "stab without body" do
{:fn, _, [stab]} = Sourceror.parse_string!(~S"fn -> end")

assert Sourceror.Range.get_range(stab) == %{
start: [line: 1, column: 4],
end: [line: 1, column: 6]
}

{:fn, _, [stab]} =
Sourceror.parse_string!(~S"""
fn a ->
end
""")

assert Sourceror.Range.get_range(stab) == %{
start: [line: 1, column: 4],
end: [line: 1, column: 8]
}
end

test "anonymous functions" do
assert to_range(~S"fn -> :ok end") == %{
start: [line: 1, column: 1],
end: [line: 1, column: 14]
}

assert to_range(~S"""
fn ->
:ok
end
""") == %{start: [line: 1, column: 1], end: [line: 3, column: 4]}

assert to_range(~S"""
fn -> end
""") == %{start: [line: 1, column: 1], end: [line: 1, column: 10]}

assert to_range(~S"""
fn ->
end
""") == %{start: [line: 1, column: 1], end: [line: 2, column: 4]}
end

test "qualified tuples" do
assert to_range(~S/Foo.{Bar, Baz}/) == %{
start: [line: 1, column: 1],
Expand Down

0 comments on commit c1251a3

Please sign in to comment.