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)
}
// 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"`,
"\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 +",
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)