Skip to content

Commit 9e1aa0b

Browse files
authored
Do not format empty lines into line breaks (#11802)
1 parent 549a867 commit 9e1aa0b

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

lib/elixir/test/elixir/code_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ defmodule CodeTest do
262262
end
263263
end
264264

265+
test "format_string/2 returns empty iodata for empty string" do
266+
assert Code.format_string!("") == []
267+
end
268+
265269
test "ensure_loaded?/1" do
266270
assert Code.ensure_loaded?(__MODULE__)
267271
refute Code.ensure_loaded?(Code.NoFile)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ 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+
547549
defp elixir_format(content, formatter_opts) do
548550
IO.iodata_to_binary([Code.format_string!(content, formatter_opts), ?\n])
549551
end

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ defmodule Mix.Tasks.FormatTest do
1515
end
1616
end
1717

18+
test "doesn't format empty lines into line breaks", context do
19+
in_tmp(context.test, fn ->
20+
File.write!("a.exs", "")
21+
22+
Mix.Tasks.Format.run(["a.exs"])
23+
24+
assert File.read!("a.exs") == ""
25+
end)
26+
end
27+
1828
test "formats the given files", context do
1929
in_tmp(context.test, fn ->
2030
File.write!("a.ex", """

0 commit comments

Comments
 (0)