diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 36bfb03c8..3d1600698 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -3,13 +3,14 @@ package formatter import ( "flag" "fmt" - "github.com/google/go-jsonnet/internal/testutils" "io" "io/ioutil" "path/filepath" "regexp" "strings" "testing" + + "github.com/google/go-jsonnet/internal/testutils" ) var update = flag.Bool("update", false, "update .golden files") @@ -42,7 +43,7 @@ func runTest(t *testing.T, test *formatterTest, changedGoldensList *ChangedGolde input := read(test.input) var outBuilder strings.Builder - output, err := Format(test.name, string(input), Options{}) + output, err := Format(test.name, string(input), DefaultOptions()) if err != nil { errWriter := ErrorWriter{ Writer: &outBuilder, diff --git a/formatter/testdata/empty_comment.fmt.golden b/formatter/testdata/empty_comment.fmt.golden new file mode 100644 index 000000000..13e494310 --- /dev/null +++ b/formatter/testdata/empty_comment.fmt.golden @@ -0,0 +1,2 @@ +// +{} diff --git a/formatter/testdata/empty_comment.jsonnet b/formatter/testdata/empty_comment.jsonnet new file mode 100644 index 000000000..447077299 --- /dev/null +++ b/formatter/testdata/empty_comment.jsonnet @@ -0,0 +1,2 @@ +# +{} diff --git a/internal/formatter/enforce_comment_style.go b/internal/formatter/enforce_comment_style.go index f508e9a5c..f8302a681 100644 --- a/internal/formatter/enforce_comment_style.go +++ b/internal/formatter/enforce_comment_style.go @@ -38,7 +38,7 @@ func (c *EnforceCommentStyle) FodderElement(p pass.ASTPass, element *ast.FodderE *comment = "#" + (*comment)[2:] } if c.Options.CommentStyle == CommentStyleSlash && (*comment)[0] == '#' { - if !c.seenFirstFodder && (*comment)[1] == '!' { + if !c.seenFirstFodder && len(*comment) > 1 && (*comment)[1] == '!' { return } *comment = "//" + (*comment)[1:]