From d9dd0f909bdcde77e382014fe2469f87dca563f0 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pryimak Date: Mon, 25 May 2020 21:23:40 +0300 Subject: [PATCH] leave comment blocks before 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 71fb768..576a596 100644 --- a/reviser/reviser.go +++ b/reviser/reviser.go @@ -281,7 +281,7 @@ type importPosition struct { } func (p *importPosition) IsInRange(comment *ast.CommentGroup) bool { - if p.Start >= comment.Pos() || comment.Pos() <= p.End { + if p.Start <= comment.Pos() && comment.Pos() <= p.End { return true } diff --git a/reviser/reviser_test.go b/reviser/reviser_test.go index 93beb22..8c244eb 100644 --- a/reviser/reviser_test.go +++ b/reviser/reviser_test.go @@ -421,6 +421,44 @@ import ( _ "github.com/incu6us/goimports-reviser/testdata/innderpkg" // custom package ) +// nolint:gomnd +func main() { + _ = fmt.Println("test") +} +`, + wantChange: true, + wantErr: false, + }, + { + name: "success with comments before imports", + args: args{ + projectName: "github.com/incu6us/goimports-reviser", + filePath: "./testdata/example.go", + fileContent: `// Some comments are here +package testdata + +// test +import ( + "fmt" //fmt package + _ "github.com/incu6us/goimports-reviser/testdata/innderpkg" //custom package +) + +// nolint:gomnd +func main(){ + _ = fmt.Println("test") +} +`, + }, + want: `// Some comments are here +package testdata + +// test +import ( + "fmt" // fmt package + + _ "github.com/incu6us/goimports-reviser/testdata/innderpkg" // custom package +) + // nolint:gomnd func main() { _ = fmt.Println("test")