Skip to content

Commit

Permalink
[incu6us#144] If it is single import, do not convert into an import list
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericwww committed Jan 6, 2024
1 parent 5bf2048 commit 7ac25d6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
11 changes: 11 additions & 0 deletions reviser/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ func rebuildImports(tok token.Token, commentsMetadata map[string]*commentsMetada
var specs []ast.Spec

amountOfGroups := len(imports)
var importSlice []string
for _, group := range imports {
importSlice = append(importSlice, group...)
}
if len(importSlice) == 1 {
return []ast.Spec{
&ast.ImportSpec{
Path: &ast.BasicLit{Value: importWithComment(importSlice[0], commentsMetadata), Kind: tok},
},
}
}
for i, group := range imports {
for _, imprt := range group {
spec := &ast.ImportSpec{
Expand Down
68 changes: 68 additions & 0 deletions reviser/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,27 @@ import (
wantChange: true,
wantErr: false,
},
{
name: "success with single std deps only",
args: args{
projectName: "github.com/incu6us/goimports-reviser",
filePath: "./testdata/example.go",
fileContent: `package testdata
import "log"
// nolint:gomnd
`,
},
want: `package testdata
import "log"
// nolint:gomnd
`,
wantChange: false,
wantErr: false,
},
{
name: "success with third-party deps only",
args: args{
Expand All @@ -297,6 +317,27 @@ import (
wantChange: true,
wantErr: false,
},
{
name: "success with single third-party deps",
args: args{
projectName: "github.com/incu6us/goimports-reviser",
filePath: "./testdata/example.go",
fileContent: `package testdata
import "golang.org/x/exp/slices"
// nolint:gomnd
`,
},
want: `package testdata
import "golang.org/x/exp/slices"
// nolint:gomnd
`,
wantChange: false,
wantErr: false,
},

{
name: "success with project deps only",
Expand Down Expand Up @@ -510,6 +551,33 @@ import (
"errors"
"fmt"
)
`,
wantChange: false,
wantErr: false,
},
{
name: "preserves cgo import with single std deps",
args: args{
projectName: "github.com/incu6us/goimports-reviser",
filePath: "./testdata/cgo_example.go",
fileContent: `package testdata
/*
#include <stdlib.h>
*/
import "C"
import "errors"
`,
},
want: `package testdata
/*
#include <stdlib.h>
*/
import "C"
import "errors"
`,
wantChange: false,
wantErr: false,
Expand Down

0 comments on commit 7ac25d6

Please sign in to comment.