diff --git a/apps/common/lib/lexical/ast/analysis.ex b/apps/common/lib/lexical/ast/analysis.ex index 14e19d032..35e4c0d72 100644 --- a/apps/common/lib/lexical/ast/analysis.ex +++ b/apps/common/lib/lexical/ast/analysis.ex @@ -333,7 +333,7 @@ defmodule Lexical.Ast.Analysis do end end - defp fetch_alias_as(options) do + defp fetch_alias_as(options) when is_list(options) do alias_as = Enum.find_value(options, fn {{:__block__, _, [:as]}, {:__aliases__, _, [alias_as]}} -> alias_as @@ -345,4 +345,9 @@ defmodule Lexical.Ast.Analysis do _ -> {:ok, alias_as} end end + + # When the `as` section is incomplete, like: `alias Foo, a` + defp fetch_alias_as(_) do + :error + end end diff --git a/apps/common/test/lexical/ast_test.exs b/apps/common/test/lexical/ast_test.exs index 93b46f2e7..05d70e913 100644 --- a/apps/common/test/lexical/ast_test.exs +++ b/apps/common/test/lexical/ast_test.exs @@ -296,6 +296,17 @@ defmodule Lexical.AstTest do refute analysis.ast assert {:error, _} = analysis.parse_error end + + test "creates an analysis from a document with incomplete `as` section" do + code = ~q[ + defmodule Invalid do + alias Foo, a + end + ] + + assert %Analysis{} = analysis = analyze(code) + assert {:defmodule, _, _} = analysis.ast + end end defp ast(s) do