mirror of https://github.com/golang/go.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 <adonovan@google.com>
This commit is contained in:
parent
a4fc3512ba
commit
829cc349c5
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 +",
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue