fmt: Trim trailing whitespace in comments (#6163)

Not sure if there's a better way to accomplish this, but
it seems to get the job done.

Fixes #6161

Signed-off-by: Anders Eknert <anders@styra.com>
This commit is contained in:
Anders Eknert
2023-08-17 16:28:08 +02:00
committed by GitHub
parent 700e2c97de
commit dc1a876c52
3 changed files with 27 additions and 0 deletions
+11
View File
@@ -11,6 +11,7 @@ import (
"regexp"
"sort"
"strings"
"unicode"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/internal/future"
@@ -270,6 +271,8 @@ func (w *writer) writeModule(module *ast.Module, o fmtOpts) {
return locLess(others[i], others[j])
})
comments = trimTrailingWhitespaceInComments(comments)
comments = w.writePackage(pkg, comments)
var imports []*ast.Import
var rules []*ast.Rule
@@ -288,6 +291,14 @@ func (w *writer) writeModule(module *ast.Module, o fmtOpts) {
}
}
func trimTrailingWhitespaceInComments(comments []*ast.Comment) []*ast.Comment {
for _, c := range comments {
c.Text = bytes.TrimRightFunc(c.Text, unicode.IsSpace)
}
return comments
}
func (w *writer) writePackage(pkg *ast.Package, comments []*ast.Comment) []*ast.Comment {
comments = w.insertComments(comments, pkg.Location)
+8
View File
@@ -0,0 +1,8 @@
package p
# this is a comment with trailing whitespace
allow {
# another comment with trailing whitespace
1 == 1
}
@@ -0,0 +1,8 @@
package p
# this is a comment with trailing whitespace
allow {
# another comment with trailing whitespace
1 == 1
}