Skip to content

dev: change lll impl to use bytes instead of strings #5190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions pkg/golinters/lll/lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package lll

import (
"bufio"
"bytes"
"errors"
"fmt"
"go/token"
"os"
"strings"
"sync"
"unicode/utf8"

Expand All @@ -21,7 +21,7 @@ import (

const linterName = "lll"

const goCommentDirectivePrefix = "//go:"
var goCommentDirectivePrefix = []byte("//go:")

func New(settings *config.LllSettings) *goanalysis.Linter {
var mu sync.Mutex
Expand Down Expand Up @@ -61,7 +61,7 @@ func New(settings *config.LllSettings) *goanalysis.Linter {
func runLll(pass *analysis.Pass, settings *config.LllSettings) ([]goanalysis.Issue, error) {
fileNames := internal.GetFileNames(pass)

spaces := strings.Repeat(" ", settings.TabWidth)
spaces := bytes.Repeat([]byte{' '}, settings.TabWidth)

var issues []goanalysis.Issue
for _, f := range fileNames {
Expand All @@ -78,7 +78,7 @@ func runLll(pass *analysis.Pass, settings *config.LllSettings) ([]goanalysis.Iss
return issues, nil
}

func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]result.Issue, error) {
func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces []byte) ([]result.Issue, error) {
var res []result.Issue

f, err := os.Open(filename)
Expand All @@ -87,34 +87,33 @@ func getLLLIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]r
}
defer f.Close()

lineNumber := 0
lineNumber := 1
multiImportEnabled := false

scanner := bufio.NewScanner(f)
for scanner.Scan() {
lineNumber++
for ; scanner.Scan(); lineNumber++ {
line := scanner.Bytes()

line := scanner.Text()
line = strings.ReplaceAll(line, "\t", tabSpaces)

if strings.HasPrefix(line, goCommentDirectivePrefix) {
if bytes.HasPrefix(line, goCommentDirectivePrefix) {
continue
}

if strings.HasPrefix(line, "import") {
multiImportEnabled = strings.HasSuffix(line, "(")
if bytes.HasPrefix(line, []byte("import")) {
multiImportEnabled = bytes.HasSuffix(line, []byte{'('})
continue
}

if multiImportEnabled {
if line == ")" {
if bytes.Equal(line, []byte{')'}) {
multiImportEnabled = false
}

continue
}

lineLen := utf8.RuneCountInString(line)
line = bytes.ReplaceAll(line, []byte{'\t'}, tabSpaces)

lineLen := utf8.RuneCount(line)
if lineLen > maxLineLen {
res = append(res, result.Issue{
Pos: token.Position{
Expand Down
Loading