Skip to content

Commit

Permalink
Ensure there's a newline in the end of generated files (#387)
Browse files Browse the repository at this point in the history
* Ensure there's a newline in the end of generated files

We must manually add it, as `Code.format_string!` doesn't add a newline
to end of file, nor does protoc.

* Update lib/protobuf/protoc/generator/util.ex

Co-authored-by: Andrea Leopardi <[email protected]>

---------

Co-authored-by: Andrea Leopardi <[email protected]>
  • Loading branch information
v0idpwn and whatyouhide authored Jan 4, 2025
1 parent 2653f60 commit 914422d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/protobuf/protoc/generator/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ defmodule Protobuf.Protoc.Generator.Util do

@spec format(String.t()) :: String.t()
def format(code) when is_binary(code) do
code
|> Code.format_string!(locals_without_parens: @locals_without_parens)
|> IO.iodata_to_binary()
formatted_code =
code
|> Code.format_string!(locals_without_parens: @locals_without_parens)
|> IO.iodata_to_binary()

# As neither Code.format_string!/2 nor protoc automatically adds a newline
# at end of files, we must add ourselves if not present.
if String.ends_with?(formatted_code, "\n"), do: formatted_code, else: formatted_code <> "\n"
end

@spec pad_comment(String.t(), non_neg_integer()) :: String.t()
Expand Down
2 changes: 1 addition & 1 deletion test/protobuf/protoc/generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Protobuf.Protoc.GeneratorTest do
desc = %Google.Protobuf.FileDescriptorProto{name: "name.proto"}

assert Generator.generate(ctx, desc) ==
{nil, [%CodeGeneratorResponse.File{name: "name.pb.ex", content: ""}]}
{nil, [%CodeGeneratorResponse.File{name: "name.pb.ex", content: "\n"}]}
end

test "uses the package prefix" do
Expand Down

0 comments on commit 914422d

Please sign in to comment.