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-03-28 23:49:52 +0000
commit5b68cb65d3385edbd42fd19484a9e9be7fadbec7 (patch)
tree08a15ba421531536294360dc586b4d526689bb34 /src/cmd/gofmt
parentd2cb5b78e0783e142f16dd399339b35be14f8fd9 (diff)
downloadgo-5b68cb65d3385edbd42fd19484a9e9be7fadbec7.tar.gz
go-5b68cb65d3385edbd42fd19484a9e9be7fadbec7.zip
go/ast: fix SortImports to handle block comments
The current algorithm only assumed line comments which always appear at the end of an import spec. This caused block comments which can appear before a spec to be attached to the previous spec. So while mapping a comment to an import spec, we maintain additional information on whether the comment is supposed to appear on the left or right of the spec. And we also take into account the possibility of "//line" comments in the source. So we use unadjusted line numbers. While at it, added some more testcases from tools/go/ast/astutil/imports_test.go Fixes #18929 Change-Id: If920426641702a8a93904b2ec1d3455749169f69 Reviewed-on: https://go-review.googlesource.com/c/go/+/162337 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r--src/cmd/gofmt/testdata/import.golden60
-rw-r--r--src/cmd/gofmt/testdata/import.input62
2 files changed, 122 insertions, 0 deletions
diff --git a/src/cmd/gofmt/testdata/import.golden b/src/cmd/gofmt/testdata/import.golden
index 51d7be79df..f7d742e3e8 100644
--- a/src/cmd/gofmt/testdata/import.golden
+++ b/src/cmd/gofmt/testdata/import.golden
@@ -1,3 +1,4 @@
+// package comment
package main
import (
@@ -20,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"
@@ -124,3 +129,58 @@ 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"
+)
diff --git a/src/cmd/gofmt/testdata/import.input b/src/cmd/gofmt/testdata/import.input
index 9a4b09dbf9..6e3a3a3bed 100644
--- a/src/cmd/gofmt/testdata/import.input
+++ b/src/cmd/gofmt/testdata/import.input
@@ -1,3 +1,4 @@
+// package comment
package main
import (
@@ -20,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"
@@ -129,3 +134,60 @@ 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"
+)