From 6bece11a067b1e5e3fcede011fcf10c865a20b89 Mon Sep 17 00:00:00 2001 From: Ivan Bezrodnov Date: Mon, 10 Oct 2022 00:20:27 +0600 Subject: [PATCH 1/5] lll skip imports --- pkg/golinters/lll.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index 8b5ebd3cf57f..55979cdfd62f 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -5,6 +5,7 @@ import ( "fmt" "go/token" "os" + "regexp" "strings" "sync" "unicode/utf8" @@ -19,6 +20,9 @@ import ( const lllName = "lll" +var lllMultiImportStartRegexp = regexp.MustCompile(`^import \($`) +var lllSingleImportRegexp = regexp.MustCompile(`^import \".+\"$`) + //nolint:dupl func NewLLL(settings *config.LllSettings) *goanalysis.Linter { var mu sync.Mutex @@ -86,9 +90,33 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r lineNumber := 1 scanner := bufio.NewScanner(f) + importsEnded := false + multiImportEnabled := false for scanner.Scan() { line := scanner.Text() line = strings.ReplaceAll(line, "\t", tabSpaces) + // Skips imports + if !importsEnded { + if lllSingleImportRegexp.MatchString(line) { + lineNumber++ + continue + } + + if lllMultiImportStartRegexp.MatchString(line) { + multiImportEnabled = true + lineNumber++ + continue + } + + if multiImportEnabled && line == ")" { + importsEnded = true + lineNumber++ + continue + } + + importsEnded = true + } + lineLen := utf8.RuneCountInString(line) if lineLen > maxLineLen { res = append(res, result.Issue{ From 9ad980b183e14961c75ba135a6d7992e2698d6a2 Mon Sep 17 00:00:00 2001 From: Ivan Bezrodnov Date: Mon, 10 Oct 2022 09:55:27 +0600 Subject: [PATCH 2/5] remove regexps and fix multiImport condition --- pkg/golinters/lll.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index 55979cdfd62f..de2c56825fa8 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -5,7 +5,6 @@ import ( "fmt" "go/token" "os" - "regexp" "strings" "sync" "unicode/utf8" @@ -20,9 +19,6 @@ import ( const lllName = "lll" -var lllMultiImportStartRegexp = regexp.MustCompile(`^import \($`) -var lllSingleImportRegexp = regexp.MustCompile(`^import \".+\"$`) - //nolint:dupl func NewLLL(settings *config.LllSettings) *goanalysis.Linter { var mu sync.Mutex @@ -97,19 +93,16 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r line = strings.ReplaceAll(line, "\t", tabSpaces) // Skips imports if !importsEnded { - if lllSingleImportRegexp.MatchString(line) { - lineNumber++ - continue - } + if strings.HasPrefix(line, "import") { + if strings.HasSuffix(line, "(") { + multiImportEnabled = true + } - if lllMultiImportStartRegexp.MatchString(line) { - multiImportEnabled = true lineNumber++ continue } - if multiImportEnabled && line == ")" { - importsEnded = true + if multiImportEnabled && line != ")" { lineNumber++ continue } From 56b3f6e9fac41ed38da3798096c2130cde31bcf4 Mon Sep 17 00:00:00 2001 From: Ivan Bezrodnov Date: Tue, 11 Oct 2022 12:58:39 +0600 Subject: [PATCH 3/5] simplify logic; add tests --- pkg/golinters/lll.go | 25 +++++++++++-------------- test/testdata/configs/lll_import.yml | 4 ++++ test/testdata/lll_multi_import.go | 14 ++++++++++++++ test/testdata/lll_single_import.go | 10 ++++++++++ 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 test/testdata/configs/lll_import.yml create mode 100644 test/testdata/lll_multi_import.go create mode 100644 test/testdata/lll_single_import.go diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index de2c56825fa8..e68c6fc222e7 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -86,28 +86,25 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r lineNumber := 1 scanner := bufio.NewScanner(f) - importsEnded := false multiImportEnabled := false for scanner.Scan() { line := scanner.Text() line = strings.ReplaceAll(line, "\t", tabSpaces) - // Skips imports - if !importsEnded { - if strings.HasPrefix(line, "import") { - if strings.HasSuffix(line, "(") { - multiImportEnabled = true - } - - lineNumber++ - continue + if strings.HasPrefix(line, "import") { + if strings.HasSuffix(line, "(") { + multiImportEnabled = true } - if multiImportEnabled && line != ")" { - lineNumber++ - continue + lineNumber++ + continue + } + if multiImportEnabled { + if line == ")" { + multiImportEnabled = false } - importsEnded = true + lineNumber++ + continue } lineLen := utf8.RuneCountInString(line) diff --git a/test/testdata/configs/lll_import.yml b/test/testdata/configs/lll_import.yml new file mode 100644 index 000000000000..e281e3d24513 --- /dev/null +++ b/test/testdata/configs/lll_import.yml @@ -0,0 +1,4 @@ +linters-settings: + lll: + tab-width: 4 + line-length: 60 diff --git a/test/testdata/lll_multi_import.go b/test/testdata/lll_multi_import.go new file mode 100644 index 000000000000..4c2ab5ab477e --- /dev/null +++ b/test/testdata/lll_multi_import.go @@ -0,0 +1,14 @@ +//golangcitest:args -Elll +//golangcitest:config_path testdata/configs/lll_import.yml +//golangcitest:expected_exitcode 0 +package testdata + +import ( + anotherVeryLongImportAliasNameForTest "github.com/golangci/golangci-lint/internal/golinters" + veryLongImportAliasNameForTest "github.com/golangci/golangci-lint/internal/golinters" +) + +func LllMultiImport() { + _ = veryLongImportAliasNameForTest.NewLLL(nil) + _ = anotherVeryLongImportAliasNameForTest.NewLLL(nil) +} diff --git a/test/testdata/lll_single_import.go b/test/testdata/lll_single_import.go new file mode 100644 index 000000000000..ed86985aa568 --- /dev/null +++ b/test/testdata/lll_single_import.go @@ -0,0 +1,10 @@ +//golangcitest:args -Elll +//golangcitest:config_path testdata/configs/lll_import.yml +//golangcitest:expected_exitcode 0 +package testdata + +import veryLongImportAliasNameForTest "github.com/golangci/golangci-lint/internal/golinters" + +func LllSingleImport() { + _ = veryLongImportAliasNameForTest.NewLLL(nil) +} From 11929d555ca47f3b955917b636260063ee497d76 Mon Sep 17 00:00:00 2001 From: Ivan Bezrodnov Date: Tue, 11 Oct 2022 17:07:40 +0600 Subject: [PATCH 4/5] fixes after review --- pkg/golinters/lll.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index e68c6fc222e7..e816e2c18dbf 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -85,19 +85,19 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r defer f.Close() lineNumber := 1 - scanner := bufio.NewScanner(f) multiImportEnabled := false + + scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() line = strings.ReplaceAll(line, "\t", tabSpaces) - if strings.HasPrefix(line, "import") { - if strings.HasSuffix(line, "(") { - multiImportEnabled = true - } + if strings.HasPrefix(line, "import") { + multiImportEnabled = strings.HasSuffix(line, "(") lineNumber++ continue } + if multiImportEnabled { if line == ")" { multiImportEnabled = false From cf36c08a9bd6572f11470c47f35eedf7932f78f5 Mon Sep 17 00:00:00 2001 From: Ivan Bezrodnov Date: Wed, 12 Oct 2022 10:02:32 +0600 Subject: [PATCH 5/5] simplify lineNumber counter --- pkg/golinters/lll.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index e816e2c18dbf..551ff98a2c87 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -84,17 +84,18 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r } defer f.Close() - lineNumber := 1 + lineNumber := 0 multiImportEnabled := false scanner := bufio.NewScanner(f) for scanner.Scan() { + lineNumber++ + line := scanner.Text() line = strings.ReplaceAll(line, "\t", tabSpaces) if strings.HasPrefix(line, "import") { multiImportEnabled = strings.HasSuffix(line, "(") - lineNumber++ continue } @@ -103,7 +104,6 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r multiImportEnabled = false } - lineNumber++ continue } @@ -118,7 +118,6 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r FromLinter: lllName, }) } - lineNumber++ } if err := scanner.Err(); err != nil {