Skip to content

Commit

Permalink
Only write one trailing newline at end of file
Browse files Browse the repository at this point in the history
opa fmt should only add one newline at the end of the file

Fixes open-policy-agent#1032

Signed-off-by: Kim Christensen <kimworking@gmail.com>
  • Loading branch information
kichristensen committed Oct 24, 2018
1 parent 002383c commit 0442bd8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ func (w *writer) writeModule(module *ast.Module) {
comments = w.writeRules(rules, comments)
}

for _, c := range comments {
for i, c := range comments {
w.writeLine(c.String())
if i == len(comments)-1 {
w.write("\n")
}
}
}

Expand Down Expand Up @@ -837,6 +840,9 @@ func (w *writer) startLine() {
panic("currently in a line")
}
w.inline = true
if w.buf.Len() > 0 {
w.write("\n")
}
for i := 0; i < w.level; i++ {
w.write(w.indent)
}
Expand All @@ -853,7 +859,6 @@ func (w *writer) endLine() {
w.beforeEnd = nil
}
w.delay = false
w.write("\n")
}

// beforeLineEnd registers a comment to be printed at the end of the current line.
Expand Down
10 changes: 5 additions & 5 deletions repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,15 @@ func TestShow(t *testing.T) {

repl.OneShot(ctx, `package repl_test`)
repl.OneShot(ctx, "show")
assertREPLText(t, buffer, "package repl_test\n\n")
assertREPLText(t, buffer, "package repl_test\n")
buffer.Reset()

repl.OneShot(ctx, "import input.xyz")
repl.OneShot(ctx, "show")

expected := `package repl_test
import input.xyz` + "\n\n"
import input.xyz` + "\n"
assertREPLText(t, buffer, expected)
buffer.Reset()

Expand All @@ -340,7 +340,7 @@ import input.xyz` + "\n\n"
expected = `package repl_test
import data.foo as bar
import input.xyz` + "\n\n"
import input.xyz` + "\n"
assertREPLText(t, buffer, expected)
buffer.Reset()

Expand All @@ -357,14 +357,14 @@ import input.xyz
p[1]
p[2]` + "\n\n"
p[2]` + "\n"
assertREPLText(t, buffer, expected)
buffer.Reset()

repl.OneShot(ctx, "package abc")
repl.OneShot(ctx, "show")

assertREPLText(t, buffer, "package abc\n\n")
assertREPLText(t, buffer, "package abc\n")
buffer.Reset()

repl.OneShot(ctx, "package repl_test")
Expand Down

0 comments on commit 0442bd8

Please sign in to comment.