1
0
mirror of https://github.com/golang/go synced 2024-10-01 01:48:32 -06:00

Revert "imports: support repairing import grouping/ordering"

This reverts commit CL 116795 12a7c317e8.

Reason for revert: mangles comments in imports. See comments
on issue golang/go#20818.

Updates golang/go#20818

Change-Id: Iff82f8dc310dceb982b48d82b26176ea279fef10
Reviewed-on: https://go-review.googlesource.com/c/144339
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Brad Fitzpatrick 2018-10-24 15:31:02 +00:00
parent 40a48ad93f
commit a2dc47679d
2 changed files with 19 additions and 147 deletions

View File

@ -514,6 +514,7 @@ c = fmt.Printf
import (
"fmt"
"gu"
"github.com/foo/bar"
@ -604,11 +605,15 @@ var _, _, _, _, _ = fmt.Errorf, io.Copy, strings.Contains, renamed_bar.A, B
import (
"fmt"
"io"
"strings"
renamed_bar "github.com/foo/bar"
"io"
. "github.com/foo/baz"
"strings"
_ "github.com/foo/qux"
)
@ -1044,149 +1049,6 @@ var _ = fmt.Printf
`,
},
// golang.org/issue/20818
{
name: "sort_all_groups",
in: `package p
import (
"testing"
"github.com/Sirupsen/logrus"
"context"
)
var _, _, _ = testing.T, logrus.Entry, context.Context
`,
out: `package p
import (
"context"
"testing"
"github.com/Sirupsen/logrus"
)
var _, _, _ = testing.T, logrus.Entry, context.Context
`,
},
// golang.org/issue/20818
{
name: "sort_many_groups",
in: `package p
import (
"testing"
"k8s.io/apimachinery/pkg/api/meta"
"fmt"
"github.com/pkg/errors"
"golang.org/x/tools/cover"
"github.com/sirupsen/logrus"
"context"
)
var _, _, _, _, _, _ = testing.T, logrus.Entry, context.Context, meta.AnyGroup, fmt.Printf, errors.Frame
`,
out: `package p
import (
"context"
"fmt"
"testing"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
)
var _, _, _, _, _, _ = testing.T, logrus.Entry, context.Context, meta.AnyGroup, fmt.Printf, errors.Frame
`,
},
// golang.org/issue/20818
{
name: "sort_all_groups_with_blanks_and_comments",
in: `package p
import (
"testing"
"k8s.io/apimachinery/pkg/api/meta"
// a comment for the "fmt" package (#26921: they are broken, should be fixed)
"fmt"
"github.com/pkg/errors" // some comment
"golang.org/x/tools/cover"
"github.com/sirupsen/logrus"
"context"
)
var _, _, _, _, _, _ = testing.T, logrus.Entry, context.Context, meta.AnyGroup, fmt.Printf, errors.Frame
`,
out: `package p
import (
"context"
"fmt"
"testing"
"github.com/pkg/errors" // some comment
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta" // a comment for the "fmt" package (#26921: they are broken, should be fixed)
)
var _, _, _, _, _, _ = testing.T, logrus.Entry, context.Context, meta.AnyGroup, fmt.Printf, errors.Frame
`,
},
{
name: "sort_all_groups_with_local_packages",
in: `package p
import (
"local/foo"
"testing"
"k8s.io/apimachinery/pkg/api/meta"
"fmt"
"github.com/pkg/errors"
"github.com/local/bar"
"golang.org/x/tools/cover"
"github.com/sirupsen/logrus"
"context"
)
var _, _, _, _, _, _ = testing.T, logrus.Entry, context.Context, meta.AnyGroup, fmt.Printf, errors.Frame
var _, _ = foo.Foo, bar.Bar
`,
out: `package p
import (
"context"
"fmt"
"testing"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
"github.com/local/bar"
"local/foo"
)
var _, _, _, _, _, _ = testing.T, logrus.Entry, context.Context, meta.AnyGroup, fmt.Printf, errors.Frame
var _, _ = foo.Foo, bar.Bar
`,
},
{
name: "cryptorand_preferred_easy_possible",
in: `package p

View File

@ -34,8 +34,18 @@ func sortImports(fset *token.FileSet, f *ast.File) {
continue
}
// Sort and regroup all imports.
sortSpecs(fset, f, d.Specs)
// Identify and sort runs of specs on successive lines.
i := 0
specs := d.Specs[:0]
for j, s := range d.Specs {
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
// j begins a new run. End this one.
specs = append(specs, sortSpecs(fset, f, d.Specs[i:j])...)
i = j
}
}
specs = append(specs, sortSpecs(fset, f, d.Specs[i:])...)
d.Specs = specs
// Deduping can leave a blank line before the rparen; clean that up.
if len(d.Specs) > 0 {