Skip to content

Commit

Permalink
Make everything private, add missing flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Mar 15, 2023
1 parent cee7b81 commit 245aa92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
18 changes: 13 additions & 5 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -62,20 +70,20 @@ 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"),
})
}

pass.Report(analysis.Diagnostic{
Pos: pos,
Category: "whitespace",
Message: fix.Reason,
Message: fix.reason,
SuggestedFixes: []analysis.SuggestedFix{
{
TextEdits: textEdits,
Expand Down
30 changes: 15 additions & 15 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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),
)
}

0 comments on commit 245aa92

Please sign in to comment.