From 1ff76bec2121c49cae1d3aa6d55b99a1e3194aa8 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pryimak Date: Mon, 15 Jun 2020 13:58:45 +0300 Subject: [PATCH] Skip replacing comments without imports --- reviser/reviser.go | 2 +- reviser/reviser_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/reviser/reviser.go b/reviser/reviser.go index 36e15ea..231b619 100644 --- a/reviser/reviser.go +++ b/reviser/reviser.go @@ -201,7 +201,7 @@ func clearImportDocs(f *ast.File, importsPositions []*importPosition) { } } - if len(importsComments) > 0 { + if len(f.Imports) > 0 { f.Comments = importsComments } } diff --git a/reviser/reviser_test.go b/reviser/reviser_test.go index 65788f4..e56b7ca 100644 --- a/reviser/reviser_test.go +++ b/reviser/reviser_test.go @@ -293,6 +293,44 @@ import ( ) // nolint:gomnd +`, + wantChange: false, + wantErr: false, + }, + + { + name: "success no changes by imports and comments", + args: args{ + projectName: "github.com/incu6us/goimports-reviser", + filePath: "./testdata/example.go", + fileContent: `package testdata + +import ( + "context" + "database/sql" + "fmt" + + _ "github.com/lib/pq" // configure database/sql with postgres driver + "github.com/pkg/errors" + "go.uber.org/fx" + + "github.com/incu6us/goimports-reviser/pkg/somepkg" +) +`, + }, + want: `package testdata + +import ( + "context" + "database/sql" + "fmt" + + _ "github.com/lib/pq" // configure database/sql with postgres driver + "github.com/pkg/errors" + "go.uber.org/fx" + + "github.com/incu6us/goimports-reviser/pkg/somepkg" +) `, wantChange: false, wantErr: false,