elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Code formatter Error with @spec and Map Literal

Open pedrosnk opened this issue 9 years ago • 0 comments

Environment

  • Elixir & Erlang versions (elixir --version): Erlang 20.1 Elixir master
  • Operating system: OSX

Current behavior

When formatting an Map inside an spec function definition it removes the parentheses from the key (when multiple) causing it to fail compilation

Code sample

defmodule Foo do
  @spec foo(%{(String.t() | atom) => any}) :: any
  def foo(value), do: value
end

running $ mix format it becomes

defmodule Foo do
  @spec foo(%{String.t() | atom => any}) :: any
  def foo(value), do: value
end

Which is invalid because of the map with multiple values as keys (string or atom)

Easier way to reproduce:

$ echo "defmodule Foo do
              @spec bar(%{(String.t | atom) => any}) :: any
              def bar(argument), do: argument
              end
              " | mix format -
# defmodule Foo do
#   @spec bar(%{String.t() | atom => any}) :: any
#   def bar(argument), do: argument
# end

To see the error

$  echo "defmodule Foo do
              @spec bar(%{(String.t | atom) => any}) :: any
              def bar(argument), do: argument
              end
              " | mix format --check-equivalent -
# ** (Mix) mix format failed due to --check-equivalent.
# The following files were not equivalent:
#
#   * stdin

Expected behavior

Investigating the code I got into Code.Formatter to_algebra here. block_to_algebra/4 seems to be generating the wrong algebra for spec code, removing the parenthesis. I tried to dig a little deeper but got sucked there. If it is a trivial bug to fix I can attempt to fix it later.

pedrosnk avatar Oct 18 '17 14:10 pedrosnk