Skip to content

Potential formatter bug - newlines don't seem to be added as expected #13866

@pdgonzalez872

Description

@pdgonzalez872

Elixir and Erlang/OTP versions

latest main, Erlang 27

Operating system

Linux + Mac

Current behavior

Hi!

This came up in the Elixir forum: https://elixir-lang.slack.com/archives/C03EPRA3B/p1727466551251699

I had a little bit of time yesterday after Jason (@axelson) shared his snippet, so I chatted with him and played around with the code.

The following test case fails but it seems like it should pass?

# place it in lib/elixir/test/elixir/code_formatter/general_test.exs
test "with def followed by module attribute" do
  bad = """
  defmodule Example do
    @impl FooA
    def a(), do: :something
    @impl FooB
    def b() do
      :something_else
    end
  end
  """

  good = """
  defmodule Example do
    @impl FooA
    def a(), do: :something

    @impl FooB
    def b() do
      :something_else
    end
  end
  """

  assert_format bad, good
end

with this error (note only one \n):

  1) test blocks with def followed by module attribute (Code.Formatter.GeneralTest)
     lib/elixir/test/elixir/code_formatter/general_test.exs:873
     Assertion with == failed
     code:  assert IO.iodata_to_binary(Code.format_string!(bad, opts)) == result
     left:  "defmodule Example do\n  @impl FooA\n  def a(), do: :something\n  @impl FooB\n  def b() do\n    :something_else\n  end\nend"
     right: "defmodule Example do\n  @impl FooA\n  def a(), do: :something\n\n  @impl FooB\n  def b() do\n    :something_else\n  end\nend"
     stacktrace:
       lib/elixir/test/elixir/code_formatter/general_test.exs:897: (test)

I have a commit that makes the above pass, I'll push it up shortly.

Expected behavior

The test should pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions