Skip to content

Commit

Permalink
lll: skip imports (#3288)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibez92 committed Oct 15, 2022
1 parent 2823ec6 commit 8a1cf90
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/golinters/lll.go
Expand Up @@ -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{
Expand All @@ -100,7 +118,6 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r
FromLinter: lllName,
})
}
lineNumber++
}

if err := scanner.Err(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/configs/lll_import.yml
@@ -0,0 +1,4 @@
linters-settings:
lll:
tab-width: 4
line-length: 60
14 changes: 14 additions & 0 deletions 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)
}
10 changes: 10 additions & 0 deletions 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)
}

0 comments on commit 8a1cf90

Please sign in to comment.