]> Cypherpunks repositories - gostls13.git/commitdiff
go/format: handle whitespace-only input correctly
authorRobert Griesemer <gri@golang.org>
Wed, 30 Sep 2015 00:34:32 +0000 (17:34 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 30 Sep 2015 16:39:43 +0000 (16:39 +0000)
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>
src/cmd/gofmt/internal.go
src/go/format/format_test.go
src/go/format/internal.go

index fc7f976af9762ad301981de854ee0187bb4f8b8e..f764b10ebb04540fce3aa5dd52ad64106bf3bb4d 100644 (file)
@@ -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)
index 000c611aa25525e318377639cec6b8212a9c9d69..b5817a5dd183ab4b79191f6aca4ab38f27ddf3f1 100644 (file)
@@ -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 +",
index 2850a43068e9bb826c5e2ca71d4110cdb86e5ad8..9d04878f8664a1133e7101a7fea723fa6597350d 100644 (file)
@@ -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)