diff options
| author | Robert Griesemer <gri@golang.org> | 2015-09-29 17:34:32 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2015-09-30 16:39:43 +0000 |
| commit | 829cc349c5990ecba52141bd978a99ee1571bc08 (patch) | |
| tree | 8a63f443a149388ac3a0294c047713d3380a0835 /src | |
| parent | a4fc3512ba85125d51e5e27491a6dd331cd2599b (diff) | |
| download | go-829cc349c5990ecba52141bd978a99ee1571bc08.tar.xz | |
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 <adonovan@google.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/gofmt/internal.go | 12 | ||||
| -rw-r--r-- | src/go/format/format_test.go | 23 | ||||
| -rw-r--r-- | src/go/format/internal.go | 12 |
3 files changed, 40 insertions, 7 deletions
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) |
