From e17b9543e7dcde6e81ad24b65f9e761ca8206a99 Mon Sep 17 00:00:00 2001 From: Fabrice Rabaute Date: Fri, 20 Jul 2018 12:36:18 -0700 Subject: [PATCH] Add tab-width option to lll linter --- .golangci.example.yml | 5 ++++- README.md | 5 ++++- pkg/commands/run.go | 4 ++++ pkg/config/config.go | 2 ++ pkg/golinters/lll.go | 7 ++++--- test/testdata/lll.go | 4 ++-- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index 0dfede2f..5f7d5d88 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -107,8 +107,11 @@ linters-settings: # Setting locale to US will correct the British spelling of 'colour' to 'color'. locale: US lll: - # max line length, lines longer will be reported. Default is 120. '\t' is counted as 1 character. + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option line-length: 120 + # tab width in spaces. Default to 1. + tab-width: 1 unused: # treat code as a program (not a library) and report unused exported identifiers; default is false. # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: diff --git a/README.md b/README.md index 34519ca8..a82d08d9 100644 --- a/README.md +++ b/README.md @@ -520,8 +520,11 @@ linters-settings: # Setting locale to US will correct the British spelling of 'colour' to 'color'. locale: US lll: - # max line length, lines longer will be reported. Default is 120. '\t' is counted as 1 character. + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option line-length: 120 + # tab width in spaces. Default to 1. + tab-width: 1 unused: # treat code as a program (not a library) and report unused exported identifiers; default is false. # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 3071c82c..211776dc 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -124,6 +124,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) { "Depguard: check list against standard lib") hideFlag("depguard.include-go-root") + fs.IntVar(&lsc.Lll.TabWidth, "lll.tab-width", 1, + "Lll: tab width in spaces") + hideFlag("lll.tab-width") + // Linters config lc := &cfg.Linters fs.StringSliceVarP(&lc.Enable, "enable", "E", nil, wh("Enable specific linter")) diff --git a/pkg/config/config.go b/pkg/config/config.go index 07a29033..df0eb383 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -168,6 +168,7 @@ type LintersSettings struct { type LllSettings struct { LineLength int `mapstructure:"line-length"` + TabWidth int `mapstructure:"tab-width"` } type UnparamSettings struct { @@ -188,6 +189,7 @@ type PreallocSettings struct { var defaultLintersSettings = LintersSettings{ Lll: LllSettings{ LineLength: 120, + TabWidth: 1, }, Unparam: UnparamSettings{ Algo: "cha", diff --git a/pkg/golinters/lll.go b/pkg/golinters/lll.go index a1239576..80ebd5c9 100644 --- a/pkg/golinters/lll.go +++ b/pkg/golinters/lll.go @@ -23,7 +23,7 @@ func (Lll) Desc() string { return "Reports long lines" } -func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issue, error) { +func (lint Lll) getIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]result.Issue, error) { var res []result.Issue f, err := os.Open(filename) @@ -36,7 +36,7 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issu scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() - line = strings.Replace(line, "\t", " ", -1) + line = strings.Replace(line, "\t", tabSpaces, -1) lineLen := utf8.RuneCountInString(line) if lineLen > maxLineLen { res = append(res, result.Issue{ @@ -61,8 +61,9 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issu func (lint Lll) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { var res []result.Issue + spaces := strings.Repeat(" ", lintCtx.Settings().Lll.TabWidth) for _, f := range lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests) { - issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength) + issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength, spaces) if err != nil { return nil, err } diff --git a/test/testdata/lll.go b/test/testdata/lll.go index 63f27ada..ae12c3b2 100644 --- a/test/testdata/lll.go +++ b/test/testdata/lll.go @@ -1,6 +1,6 @@ -// args: -Elll +// args: -Elll --lll.tab-width 4 package testdata func Lll() { - // In my experience, long lines are the lines with comments, not the code. So this is a long comment // ERROR "line is 135 characters" + // In my experience, long lines are the lines with comments, not the code. So this is a long comment // ERROR "line is 138 characters" }