lll: skip imports (#3288)

This commit is contained in:
Ivan 2022-10-15 11:57:28 +03:00 committed by GitHub
parent 2823ec6215
commit 8a1cf904f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 2 deletions

View File

@ -84,11 +84,29 @@ 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, "(")
continue
}
if multiImportEnabled {
if line == ")" {
multiImportEnabled = false
}
continue
}
lineLen := utf8.RuneCountInString(line)
if lineLen > maxLineLen {
res = append(res, result.Issue{
@ -100,7 +118,6 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r
FromLinter: lllName,
})
}
lineNumber++
}
if err := scanner.Err(); err != nil {

4
test/testdata/configs/lll_import.yml vendored Normal file
View File

@ -0,0 +1,4 @@
linters-settings:
lll:
tab-width: 4
line-length: 60

14
test/testdata/lll_multi_import.go vendored Normal file
View File

@ -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)
}

10
test/testdata/lll_single_import.go vendored Normal file
View File

@ -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)
}