mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
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:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user