Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#144] If it is single import, do not convert into an import list #145

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions reviser/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,18 @@ func removeEmptyImportNode(f *ast.File) {
func rebuildImports(tok token.Token, commentsMetadata map[string]*commentsMetadata, imports [][]string) []ast.Spec {
var specs []ast.Spec

amountOfGroups := len(imports)
for i, group := range imports {
if i != 0 && len(group) != 0 && len(specs) != 0 {
spec := &ast.ImportSpec{Path: &ast.BasicLit{Value: "", Kind: token.STRING}}

specs = append(specs, spec)
}
for _, imprt := range group {
spec := &ast.ImportSpec{
Path: &ast.BasicLit{Value: importWithComment(imprt, commentsMetadata), Kind: tok},
}
specs = append(specs, spec)
}
if len(group) != 0 && i != amountOfGroups-1 {
spec := &ast.ImportSpec{Path: &ast.BasicLit{Value: "", Kind: token.STRING}}

specs = append(specs, spec)
}
}

return specs
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