Skip to content

Commit 47de5a0

Browse files
committed
Do not add new line on all empty mix format
1 parent f85fb63 commit 47de5a0

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

lib/mix/lib/mix/tasks/format.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,11 @@ defmodule Mix.Tasks.Format do
544544
defp stdin_or_wildcard("-"), do: [:stdin]
545545
defp stdin_or_wildcard(path), do: path |> Path.expand() |> Path.wildcard(match_dot: true)
546546

547-
defp elixir_format("", _formatter_opts), do: ""
548-
549547
defp elixir_format(content, formatter_opts) do
550-
IO.iodata_to_binary([Code.format_string!(content, formatter_opts), ?\n])
548+
case Code.format_string!(content, formatter_opts) do
549+
[] -> ""
550+
_ -> IO.iodata_to_binary([Code.format_string!(content, formatter_opts), ?\n])
551+
end
551552
end
552553

553554
defp find_sigils_from_plugins(plugin, formatter_opts) do

lib/mix/test/mix/tasks/format_test.exs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ defmodule Mix.Tasks.FormatTest do
1515
end
1616
end
1717

18-
test "doesn't format empty lines into line breaks", context do
18+
test "doesn't format empty files into line breaks", context do
1919
in_tmp(context.test, fn ->
2020
File.write!("a.exs", "")
21-
2221
Mix.Tasks.Format.run(["a.exs"])
23-
2422
assert File.read!("a.exs") == ""
23+
24+
File.write!("b.exs", " \n \n")
25+
Mix.Tasks.Format.run(["b.exs"])
26+
assert File.read!("b.exs") == ""
2527
end)
2628
end
2729

0 commit comments

Comments
 (0)