From: Robert Griesemer Date: Wed, 30 Sep 2015 00:34:32 +0000 (-0700) Subject: go/format: handle whitespace-only input correctly X-Git-Tag: go1.6beta1~950 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=829cc349c5990ecba52141bd978a99ee1571bc08;p=gostls13.git go/format: handle whitespace-only input correctly Applied identical change to cmd/gofmt/internal.go. Fixes #11275. Change-Id: Icb4bf0460c94c9e2830dd0d62c69376774cbda30 Reviewed-on: https://go-review.googlesource.com/15154 Reviewed-by: Alan Donovan --- diff --git a/src/cmd/gofmt/internal.go b/src/cmd/gofmt/internal.go index fc7f976af9..f764b10ebb 100644 --- a/src/cmd/gofmt/internal.go +++ b/src/cmd/gofmt/internal.go @@ -149,7 +149,17 @@ func format( if err != nil { return nil, err } - res = append(res, sourceAdj(buf.Bytes(), cfg.Indent)...) + out := sourceAdj(buf.Bytes(), cfg.Indent) + + // If the adjusted output is empty, the source + // was empty but (possibly) for white space. + // The result is the incoming source. + if len(out) == 0 { + return src, nil + } + + // Otherwise, append output to leading space. + res = append(res, out...) // Determine and append trailing space. i = len(src) diff --git a/src/go/format/format_test.go b/src/go/format/format_test.go index 000c611aa2..b5817a5dd1 100644 --- a/src/go/format/format_test.go +++ b/src/go/format/format_test.go @@ -72,6 +72,7 @@ func TestSource(t *testing.T) { } // Test cases that are expected to fail are marked by the prefix "ERROR". +// The formatted result must look the same as the input for successful tests. var tests = []string{ // declaration lists `import "go/format"`, @@ -91,11 +92,23 @@ var tests = []string{ "\n\t\t\n\n\t\t\tx := 0\n\t\t\tconst s = `\n\t\tfoo\n`\n\n\n", // no indentation removed inside raw strings // comments - "i := 5 /* Comment */", // Issue 5551. - "\ta()\n//line :1", // Issue 11276. - "\t//xxx\n\ta()\n//line :2", // Issue 11276. - "\ta() //line :1\n\tb()\n", // Issue 11276. - "x := 0\n//line :1\n//line :2", // Issue 11276. + "/* Comment */", + "\t/* Comment */ ", + "\n/* Comment */ ", + "i := 5 /* Comment */", // issue #5551 + "\ta()\n//line :1", // issue #11276 + "\t//xxx\n\ta()\n//line :2", // issue #11276 + "\ta() //line :1\n\tb()\n", // issue #11276 + "x := 0\n//line :1\n//line :2", // issue #11276 + + // whitespace + "", // issue #11275 + " ", // issue #11275 + "\t", // issue #11275 + "\t\t", // issue #11275 + "\n", // issue #11275 + "\n\n", // issue #11275 + "\t\n", // issue #11275 // erroneous programs "ERROR1 + 2 +", diff --git a/src/go/format/internal.go b/src/go/format/internal.go index 2850a43068..9d04878f86 100644 --- a/src/go/format/internal.go +++ b/src/go/format/internal.go @@ -149,7 +149,17 @@ func format( if err != nil { return nil, err } - res = append(res, sourceAdj(buf.Bytes(), cfg.Indent)...) + out := sourceAdj(buf.Bytes(), cfg.Indent) + + // If the adjusted output is empty, the source + // was empty but (possibly) for white space. + // The result is the incoming source. + if len(out) == 0 { + return src, nil + } + + // Otherwise, append output to leading space. + res = append(res, out...) // Determine and append trailing space. i = len(src)