Skip to content

Commit

Permalink
Handled formatting invalid module names (#781)
Browse files Browse the repository at this point in the history
* Handled formatting invalid module names

I'm working in a project that generates invalid module names, which
would crash lexical due to lexical using Module.split, which causes a
crash when it gets an invalid module name.

Switched to use `String.split` instead.

* DOH Pr fix

* Update projects/lexical_shared/test/lexical/formats_test.exs

Co-authored-by: Cameron Duley <Cameron.Duley99@gmail.com>

---------

Co-authored-by: Cameron Duley <Cameron.Duley99@gmail.com>
  • Loading branch information
scohen and Moosieus authored Jul 10, 2024
1 parent 4c85267 commit 6012ca6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions projects/lexical_shared/lib/lexical/formats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ defmodule Lexical.Formats do
string_name = Atom.to_string(module_name)

if String.contains?(string_name, ".") do
module_name
|> Module.split()
|> Enum.join(".")
case string_name do
"Elixir." <> rest -> rest
other -> other
end
else
# erlang module_name
":#{string_name}"
Expand Down
8 changes: 8 additions & 0 deletions projects/lexical_shared/test/lexical/formats_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ defmodule Lexical.FormatsTest do
test "it correctly handles an erlang module name" do
assert ":ets" == Formats.module(:ets)
end

test "it drops any `Elixir.` prefix" do
assert "Kernel.SpecialForms" == Formats.module(Elixir.Kernel.SpecialForms)
end

test "it correctly handles an invalid elixir module" do
assert "This.Is.Not.A.Module" == Formats.module(:"This.Is.Not.A.Module")
end
end

describe "formatting time" do
Expand Down

0 comments on commit 6012ca6

Please sign in to comment.