From 245aa9245e1d3b85fddfdfa844b159f2c6640b79 Mon Sep 17 00:00:00 2001 From: Simon Sawert Date: Wed, 15 Mar 2023 21:33:49 +0100 Subject: [PATCH] Make everything private, add missing flags --- analyzer.go | 18 +++++++++++++----- wsl.go | 30 +++++++++++++++--------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/analyzer.go b/analyzer.go index cbff48f..3af5e3e 100644 --- a/analyzer.go +++ b/analyzer.go @@ -49,10 +49,18 @@ func flags() flag.FlagSet { flags.BoolVar(&config.StrictAppend, "strict-append", true, "Strict rules for append") flags.IntVar(&config.ForceCaseTrailingWhitespaceLimit, "force-case-trailing-whitespace", 0, "Force newlines for case blocks > this number.") + flags.String("allow-cuddle-with-calls", "Lock,RLock", "Comma separated list of idents that can have cuddles after") + flags.String("allow-cuddle-with-rhs", "Lock,RLock", "Comma separated list of idents that can have cuddles before") + flags.String("error-variable-names", "err", "Comma separated list of error variable names") + return *flags } func run(pass *analysis.Pass) (interface{}, error) { + config.AllowCuddleWithCalls = strings.Split(pass.Analyzer.Flags.Lookup("allow-cuddle-with-calls").Value.String(), ",") + config.AllowCuddleWithRHS = strings.Split(pass.Analyzer.Flags.Lookup("allow-cuddle-with-rhs").Value.String(), ",") + config.ErrorVariableNames = strings.Split(pass.Analyzer.Flags.Lookup("error-variable-names").Value.String(), ",") + for _, file := range pass.Files { filename := pass.Fset.Position(file.Pos()).Filename if !strings.HasSuffix(filename, ".go") { @@ -62,12 +70,12 @@ func run(pass *analysis.Pass) (interface{}, error) { processor := newProcessorWithConfig(file, pass.Fset, &config) processor.parseAST() - for pos, fix := range processor.Result { + for pos, fix := range processor.result { textEdits := []analysis.TextEdit{} - for _, f := range fix.FixRanges { + for _, f := range fix.fixRanges { textEdits = append(textEdits, analysis.TextEdit{ - Pos: f.FixRangeStart, - End: f.FixRangeEnd, + Pos: f.fixRangeStart, + End: f.fixRangeEnd, NewText: []byte("\n"), }) } @@ -75,7 +83,7 @@ func run(pass *analysis.Pass) (interface{}, error) { pass.Report(analysis.Diagnostic{ Pos: pos, Category: "whitespace", - Message: fix.Reason, + Message: fix.reason, SuggestedFixes: []analysis.SuggestedFix{ { TextEdits: textEdits, diff --git a/wsl.go b/wsl.go index bd0c341..636e35b 100644 --- a/wsl.go +++ b/wsl.go @@ -178,14 +178,14 @@ type Configuration struct { // fix is a range to fixup. type fix struct { - FixRangeStart token.Pos - FixRangeEnd token.Pos + fixRangeStart token.Pos + fixRangeEnd token.Pos } // result represents the result of one error. type result struct { - FixRanges []fix - Reason string + fixRanges []fix + reason string } // processor is the type that keeps track of the file and fileset and holds the @@ -194,8 +194,8 @@ type processor struct { config *Configuration file *ast.File fileSet *token.FileSet - Result map[token.Pos]result - Warnings []string + result map[token.Pos]result + warnings []string } // newProcessorWithConfig will create a Processor with the passed configuration. @@ -204,7 +204,7 @@ func newProcessorWithConfig(file *ast.File, fileSet *token.FileSet, cfg *Configu config: cfg, file: file, fileSet: fileSet, - Result: make(map[token.Pos]result), + result: make(map[token.Pos]result), } } @@ -1326,26 +1326,26 @@ func (p *processor) addWhitespaceBeforeError(node ast.Node, reason string) { } func (p *processor) addErrorRange(reportAt, start, end token.Pos, reason string) { - report, ok := p.Result[reportAt] + report, ok := p.result[reportAt] if !ok { report = result{ - Reason: reason, - FixRanges: []fix{}, + reason: reason, + fixRanges: []fix{}, } } - report.FixRanges = append(report.FixRanges, fix{ - FixRangeStart: start, - FixRangeEnd: end, + report.fixRanges = append(report.fixRanges, fix{ + fixRangeStart: start, + fixRangeEnd: end, }) - p.Result[reportAt] = report + p.result[reportAt] = report } func (p *processor) addWarning(w string, pos token.Pos, t interface{}) { position := p.fileSet.Position(pos) - p.Warnings = append(p.Warnings, + p.warnings = append(p.warnings, fmt.Sprintf("%s:%d: %s (%T)", position.Filename, position.Line, w, t), ) }