aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
diff options
context:
space:
mode:
authorAgniva De Sarker <agnivade@yahoo.co.in>2019-02-13 08:49:52 +0530
committerRobert Griesemer <gri@golang.org>2019-10-28 23:33:26 +0000
commitbf670bd83c849b56426c63b49f28121210a627ca (patch)
tree9767ee757e3f90b745adb599516c7968e9d199c3 /src/cmd/gofmt
parent59a684703913e383c482c1383b2d4958c674248c (diff)
downloadgo-bf670bd83c849b56426c63b49f28121210a627ca.tar.gz
go-bf670bd83c849b56426c63b49f28121210a627ca.zip
go/ast: fix SortImports to handle block comments (take 2)
This is a 2nd attempt at fixing CL 162337 which had an off-by-one error. We were unconditionally getting the position of the start of the next line from the last import without checking whether it is the end of the file or not. Fix the code to check for that and move the testcase added in CL 190523 to the end of the file for it to trigger the issue properly. Fixes #18929 Change-Id: I59e77256e256570b160fea6a17bce9ef49e810df Reviewed-on: https://go-review.googlesource.com/c/go/+/190480 Run-TryBot: Agniva De Sarker <agniva.quicksilver@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r--src/cmd/gofmt/testdata/import.golden73
-rw-r--r--src/cmd/gofmt/testdata/import.input71
2 files changed, 136 insertions, 8 deletions
diff --git a/src/cmd/gofmt/testdata/import.golden b/src/cmd/gofmt/testdata/import.golden
index 29bdc9baf4..1125b70cb7 100644
--- a/src/cmd/gofmt/testdata/import.golden
+++ b/src/cmd/gofmt/testdata/import.golden
@@ -1,3 +1,4 @@
+// package comment
package main
import (
@@ -10,11 +11,6 @@ import (
import (
"fmt"
- "math"
-)
-
-import (
- "fmt"
"math"
@@ -25,6 +21,10 @@ import (
"io"
)
+// We reset the line numbering to test that
+// the formatting works independent of line directives
+//line :19
+
import (
"errors"
"fmt"
@@ -129,3 +129,66 @@ import (
"dedup_by_group"
)
+
+import (
+ "fmt" // for Printf
+ /* comment */ io1 "io"
+ /* comment */ io2 "io"
+ /* comment */ "log"
+)
+
+import (
+ "fmt"
+ /* comment */ io1 "io"
+ /* comment */ io2 "io" // hello
+ "math" /* right side */
+ // end
+)
+
+import (
+ "errors" // for New
+ "fmt"
+ /* comment */ io1 "io" /* before */ // after
+ io2 "io" // another
+ // end
+)
+
+import (
+ "errors" // for New
+ /* left */ "fmt" /* right */
+ "log" // for Fatal
+ /* left */ "math" /* right */
+)
+
+import /* why */ /* comment here? */ (
+ /* comment */ "fmt"
+ "math"
+)
+
+// Reset it again
+//line :100
+
+// Dedup with different import styles
+import (
+ "path"
+ . "path"
+ _ "path"
+ pathpkg "path"
+)
+
+/* comment */
+import (
+ "fmt"
+ "math" // for Abs
+ // This is a new run
+ "errors"
+ "fmt"
+)
+
+// End an import declaration in the same line
+// as the last import. See golang.org/issue/33538.
+// Note: Must be the last (or 2nd last) line of the file.
+import (
+ "fmt"
+ "math"
+)
diff --git a/src/cmd/gofmt/testdata/import.input b/src/cmd/gofmt/testdata/import.input
index 78ab4f6544..040b8722d4 100644
--- a/src/cmd/gofmt/testdata/import.input
+++ b/src/cmd/gofmt/testdata/import.input
@@ -1,3 +1,4 @@
+// package comment
package main
import (
@@ -8,9 +9,6 @@ import (
"io"
)
-import("fmt"
-"math")
-
import (
"fmt"
@@ -23,6 +21,10 @@ import (
"io"
)
+// We reset the line numbering to test that
+// the formatting works independent of line directives
+//line :19
+
import (
"fmt"
"math"
@@ -132,3 +134,66 @@ import (
"dedup_by_group"
)
+
+import (
+ /* comment */ io1 "io"
+ "fmt" // for Printf
+ /* comment */ "log"
+ /* comment */ io2 "io"
+)
+
+import (
+ /* comment */ io2 "io" // hello
+ /* comment */ io1 "io"
+ "math" /* right side */
+ "fmt"
+ // end
+)
+
+import (
+ /* comment */ io1 "io" /* before */ // after
+ "fmt"
+ "errors" // for New
+ io2 "io" // another
+ // end
+)
+
+import (
+ /* left */ "fmt" /* right */
+ "errors" // for New
+ /* left */ "math" /* right */
+ "log" // for Fatal
+)
+
+import /* why */ /* comment here? */ (
+ /* comment */ "fmt"
+ "math"
+)
+
+// Reset it again
+//line :100
+
+// Dedup with different import styles
+import (
+ "path"
+ . "path"
+ _ "path"
+ "path"
+ pathpkg "path"
+)
+
+/* comment */
+import (
+ "math" // for Abs
+ "fmt"
+ // This is a new run
+ "errors"
+ "fmt"
+ "errors"
+)
+
+// End an import declaration in the same line
+// as the last import. See golang.org/issue/33538.
+// Note: Must be the last (or 2nd last) line of the file.
+import("fmt"
+"math") \ No newline at end of file