elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.51k stars 3.38k forks source link

Macro.to_string output does not match formatter output #6984

Closed thecodeboss closed 7 years ago

thecodeboss commented 7 years ago

Environment

Elixir 1.6.0-dev (794928e) (compiled with OTP 20)

* Operating system: 

MacOS High Sierra 10.13


### Current behavior

Running the following code:
```elixir
"""
defmodule Hello do
  def test() do
    1
  end
end
"""
|> Code.string_to_quoted!()
|> Macro.to_string()

The output looks like:

defmodule(Hello) do
  def(test()) do
    1
  end
end

Expected behavior

The output should match the format that the Elixir code formatter uses. In this case:

defmodule Hello do
  def test() do
    1
  end
end

This is definitely a "nice to have" thing, obviously not urgent or required. I couldn't find any other issues related to this one, nor any discussions about whether it has already been considered. My apologies if this is a non-goal.

josevalim commented 7 years ago

Hi @thecodeboss!

They can't match because the formatter is based on a string, which gives us a lot more context, and the macro one relies on the AST. furthermore, Macro.to_string was designed to allow the result to be processed as we convert it to string, which would be tricky in the formatter.

TL;DR - different tools, different outputs.

have a good weekend!

josevalim commented 7 years ago

To further clarify, we are not against improving the output of Macro.to_string, you just shouldn't expect them ever to be the same.