Skip to content

Commit

Permalink
Fix Code.Fragment.surround_context/2 for submodules of non-aliases (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zachallaun authored and josevalim committed Aug 24, 2023
1 parent def65ab commit fb03792
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/elixir/lib/code/fragment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ defmodule Code.Fragment do
{{:alias, acc}, offset} ->
build_surround({:alias, acc}, reversed, line, offset)

{{:alias, parent, acc}, offset} ->
build_surround({:alias, parent, acc}, reversed, line, offset)

{{:struct, acc}, offset} ->
build_surround({:struct, acc}, reversed, line, offset)

Expand Down
48 changes: 28 additions & 20 deletions lib/elixir/test/elixir/code_fragment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -755,17 +755,21 @@ defmodule CodeFragmentTest do
end: {1, 11}
}

assert CF.surround_context("__MODULE__.Foo", {1, 12}) == %{
context: {:alias, {:local_or_var, ~c"__MODULE__"}, ~c"Foo"},
begin: {1, 1},
end: {1, 15}
}
for i <- 1..15 do
assert CF.surround_context("__MODULE__.Foo", {1, i}) == %{
context: {:alias, {:local_or_var, ~c"__MODULE__"}, ~c"Foo"},
begin: {1, 1},
end: {1, 15}
}
end

assert CF.surround_context("__MODULE__.Foo.Sub", {1, 16}) == %{
context: {:alias, {:local_or_var, ~c"__MODULE__"}, ~c"Foo.Sub"},
begin: {1, 1},
end: {1, 19}
}
for i <- 1..19 do
assert CF.surround_context("__MODULE__.Foo.Sub", {1, i}) == %{
context: {:alias, {:local_or_var, ~c"__MODULE__"}, ~c"Foo.Sub"},
begin: {1, 1},
end: {1, 19}
}
end

assert CF.surround_context("%__MODULE__{}", {1, 5}) == %{
context: {:struct, {:local_or_var, ~c"__MODULE__"}},
Expand Down Expand Up @@ -811,17 +815,21 @@ defmodule CodeFragmentTest do
end

test "attribute submodules" do
assert CF.surround_context("@some.Foo", {1, 8}) == %{
context: {:alias, {:module_attribute, ~c"some"}, ~c"Foo"},
begin: {1, 1},
end: {1, 10}
}
for i <- 1..10 do
assert CF.surround_context("@some.Foo", {1, i}) == %{
context: {:alias, {:module_attribute, ~c"some"}, ~c"Foo"},
begin: {1, 1},
end: {1, 10}
}
end

assert CF.surround_context("@some.Foo.Sub", {1, 12}) == %{
context: {:alias, {:module_attribute, ~c"some"}, ~c"Foo.Sub"},
begin: {1, 1},
end: {1, 14}
}
for i <- 1..14 do
assert CF.surround_context("@some.Foo.Sub", {1, i}) == %{
context: {:alias, {:module_attribute, ~c"some"}, ~c"Foo.Sub"},
begin: {1, 1},
end: {1, 14}
}
end

assert CF.surround_context("%@some{}", {1, 5}) == %{
context: {:struct, {:module_attribute, ~c"some"}},
Expand Down

0 comments on commit fb03792

Please sign in to comment.