Skip to content

Commit 829cc34

Browse files
committed
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 <[email protected]>
1 parent a4fc351 commit 829cc34

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/cmd/gofmt/internal.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,17 @@ func format(
149149
if err != nil {
150150
return nil, err
151151
}
152-
res = append(res, sourceAdj(buf.Bytes(), cfg.Indent)...)
152+
out := sourceAdj(buf.Bytes(), cfg.Indent)
153+
154+
// If the adjusted output is empty, the source
155+
// was empty but (possibly) for white space.
156+
// The result is the incoming source.
157+
if len(out) == 0 {
158+
return src, nil
159+
}
160+
161+
// Otherwise, append output to leading space.
162+
res = append(res, out...)
153163

154164
// Determine and append trailing space.
155165
i = len(src)

src/go/format/format_test.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestSource(t *testing.T) {
7272
}
7373

7474
// Test cases that are expected to fail are marked by the prefix "ERROR".
75+
// The formatted result must look the same as the input for successful tests.
7576
var tests = []string{
7677
// declaration lists
7778
`import "go/format"`,
@@ -91,11 +92,23 @@ var tests = []string{
9192
"\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
9293

9394
// comments
94-
"i := 5 /* Comment */", // Issue 5551.
95-
"\ta()\n//line :1", // Issue 11276.
96-
"\t//xxx\n\ta()\n//line :2", // Issue 11276.
97-
"\ta() //line :1\n\tb()\n", // Issue 11276.
98-
"x := 0\n//line :1\n//line :2", // Issue 11276.
95+
"/* Comment */",
96+
"\t/* Comment */ ",
97+
"\n/* Comment */ ",
98+
"i := 5 /* Comment */", // issue #5551
99+
"\ta()\n//line :1", // issue #11276
100+
"\t//xxx\n\ta()\n//line :2", // issue #11276
101+
"\ta() //line :1\n\tb()\n", // issue #11276
102+
"x := 0\n//line :1\n//line :2", // issue #11276
103+
104+
// whitespace
105+
"", // issue #11275
106+
" ", // issue #11275
107+
"\t", // issue #11275
108+
"\t\t", // issue #11275
109+
"\n", // issue #11275
110+
"\n\n", // issue #11275
111+
"\t\n", // issue #11275
99112

100113
// erroneous programs
101114
"ERROR1 + 2 +",

src/go/format/internal.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,17 @@ func format(
149149
if err != nil {
150150
return nil, err
151151
}
152-
res = append(res, sourceAdj(buf.Bytes(), cfg.Indent)...)
152+
out := sourceAdj(buf.Bytes(), cfg.Indent)
153+
154+
// If the adjusted output is empty, the source
155+
// was empty but (possibly) for white space.
156+
// The result is the incoming source.
157+
if len(out) == 0 {
158+
return src, nil
159+
}
160+
161+
// Otherwise, append output to leading space.
162+
res = append(res, out...)
153163

154164
// Determine and append trailing space.
155165
i = len(src)

0 commit comments

Comments
 (0)