Skip to content

Commit

Permalink
Compiles, lints and passes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Feb 13, 2023
1 parent 56c7e0e commit a3c022a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
9 changes: 7 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

// Analyzer is the wsl analyzer.
// nolint // gochecknoglobals - Not needed
//
//nolint:gochecknoglobals // Analyzers tend to be global.
var Analyzer = &analysis.Analyzer{
Name: "wsl",
Doc: "add or remove empty lines",
Expand All @@ -17,6 +18,7 @@ var Analyzer = &analysis.Analyzer{
RunDespiteErrors: true,
}

//nolint:gochecknoglobals // Config needs to be overwritten with flags.
var config = Configuration{
StrictAppend: true,
AllowAssignAndCallCuddle: true,
Expand All @@ -39,7 +41,7 @@ func flags() flag.FlagSet {

func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
processor := NewProcessorWithConfig(file, pass.Fset, config)
processor := NewProcessorWithConfig(file, pass.Fset, &config)
processor.ParseAST()

for _, v := range processor.Result {
Expand All @@ -50,6 +52,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
textEdits []analysis.TextEdit
)

//nolint:exhaustive // Not while TODO
switch v.Type {
case WhitespaceShouldAddBefore:
pos = v.Node.Pos()
Expand All @@ -60,6 +63,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
end = v.Node.End()
newText = []byte("\n")
default:
//nolint:gocritic // We need TODOs while iterating...
// TODO
continue
}
Expand Down Expand Up @@ -90,5 +94,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
}

//nolint:nilnil // A pass don't need to return anything.
return nil, nil
}
47 changes: 27 additions & 20 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (e ErrorType) String() string {
return ""
}

// Error reason strings
// Error reason strings.
const (
reasonMustCuddleErrCheck = "if statements that check an error must be cuddled with the statement that assigned the error"
reasonOnlyCuddleIfWithAssign = "if statements should only be cuddled with assignments"
Expand Down Expand Up @@ -108,7 +108,7 @@ type Configuration struct {
// x = AnotherAssign()
AllowAssignAndCallCuddle bool

// AllowAssignAndCallCuddle allows assignments to be cuddled with anything.
// AllowAssignAndAnythingCuddle allows assignments to be cuddled with anything.
// Example supported with this set to true:
// if x == 1 {
// x = 0
Expand All @@ -134,10 +134,6 @@ type Configuration struct {
// number, the case *must* end white a newline.
ForceCaseTrailingWhitespaceLimit int

// If the number of lines in a case block is equal to or lager than this
// number, the case *must* end white a newline.
CaseForceTrailingWhitespaceLimit int

// AllowTrailingComment will allow blocks to end with comments.
AllowTrailingComment bool

Expand Down Expand Up @@ -235,15 +231,15 @@ type Result struct {
// Processor is the type that keeps track of the file and fileset and holds the
// results from parsing the AST.
type Processor struct {
config Configuration
config *Configuration
file *ast.File
fileSet *token.FileSet
Result []Result
Warnings []string
}

// NewProcessorWithConfig will create a Processor with the passed configuration.
func NewProcessorWithConfig(file *ast.File, fileSet *token.FileSet, cfg Configuration) *Processor {
func NewProcessorWithConfig(file *ast.File, fileSet *token.FileSet, cfg *Configuration) *Processor {
return &Processor{
config: cfg,
file: file,
Expand All @@ -255,7 +251,7 @@ func NewProcessorWithConfig(file *ast.File, fileSet *token.FileSet, cfg Configur
func NewProcessor(file *ast.File, fileSet *token.FileSet) *Processor {
return NewProcessorWithConfig(
file, fileSet,
Configuration{
&Configuration{
StrictAppend: true,
AllowAssignAndCallCuddle: true,
AllowMultiLineAssignCuddle: true,
Expand Down Expand Up @@ -419,9 +415,19 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
// it was and use *that* statement's position
if p.config.ForceExclusiveShortDeclarations && cuddledWithLastStmt {
if p.isShortDecl(stmt) && !p.isShortDecl(previousStatement) {
p.addError(stmt.Pos(), reasonShortDeclNotExclusive)
p.addError(
stmt,
reasonShortDeclNotExclusive,
WhitespaceShouldAddAfter,
false,
)
} else if p.isShortDecl(previousStatement) && !p.isShortDecl(stmt) {
p.addError(previousStatement.Pos(), reasonShortDeclNotExclusive)
p.addError(
previousStatement,
reasonShortDeclNotExclusive,
WhitespaceShouldAddBefore,
false,
)
}
}

Expand Down Expand Up @@ -488,7 +494,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
continue
}

if atLeastOneInListsMatch(assignedOnLineAbove, assignedFirstInBlock) {
if atLeastOneInListsMatch(assignedOnLineAbove, calledOrAssignedFirstInBlock) {
p.addWhitespaceBeforeErrorNoFix(t, reasonOnlyOneCuddle)
continue
}
Expand Down Expand Up @@ -565,6 +571,10 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
case *ast.ExprStmt:
switch previousStatement.(type) {
case *ast.DeclStmt, *ast.ReturnStmt:
if p.config.AllowAssignAndCallCuddle && p.config.AllowCuddleDeclaration {
continue
}

p.addWhitespaceBeforeError(t, reasonExpressionCuddledWithDeclOrRet)
case *ast.IfStmt, *ast.RangeStmt, *ast.SwitchStmt:
p.addWhitespaceBeforeError(t, reasonExpressionCuddledWithBlock)
Expand Down Expand Up @@ -594,7 +604,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
}

if !atLeastOneInListsMatch(rightAndLeftHandSide, assignedOnLineAbove) {
if !atLeastOneInListsMatch(assignedOnLineAbove, assignedFirstInBlock) {
if !atLeastOneInListsMatch(assignedOnLineAbove, calledOrAssignedFirstInBlock) {
p.addWhitespaceBeforeError(t, reasonRangeCuddledWithoutUse)
}
}
Expand Down Expand Up @@ -638,7 +648,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
}

// Allow use to cuddled defer func literals with usages on line
// abouve. Example:
// above. Example:
// b := getB()
// defer func() {
// makesSenseToUse(b)
Expand Down Expand Up @@ -675,7 +685,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
// comments regarding variable usages on the line before or as the
// first line in the block for details.
if !atLeastOneInListsMatch(rightAndLeftHandSide, assignedOnLineAbove) {
if !atLeastOneInListsMatch(assignedOnLineAbove, assignedFirstInBlock) {
if !atLeastOneInListsMatch(assignedOnLineAbove, calledOrAssignedFirstInBlock) {
p.addWhitespaceBeforeError(t, reasonForCuddledAssignWithoutUse)
}
}
Expand Down Expand Up @@ -734,7 +744,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
if !atLeastOneInListsMatch(rightHandSide, assignedOnLineAbove) {
// Allow type assertion on variables used in the first case
// immediately.
if !atLeastOneInListsMatch(assignedOnLineAbove, assignedFirstInBlock) {
if !atLeastOneInListsMatch(assignedOnLineAbove, calledOrAssignedFirstInBlock) {
p.addWhitespaceBeforeError(t, reasonTypeSwitchCuddledWithoutUse)
}
}
Expand Down Expand Up @@ -1136,10 +1146,7 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
// And now if the first statement is passed the number of allowed lines,
// then we had extra WS, possibly before the first comment group.
if p.nodeStart(firstStatement) > blockStartLine+allowedLinesBeforeFirstStatement {
p.addError(
blockStartPos,
reasonBlockStartsWithWS,
)
p.addError(stmt, reasonBlockStartsWithWS, WhitespaceShouldRemoveBeginning, false)
}

// If the blockEndLine is not 0 we're a regular block (not case).
Expand Down
4 changes: 2 additions & 2 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ func TestWithConfig(t *testing.T) {
p := NewProcessor(file, fileSet)

if tc.customConfig != nil {
p = NewProcessorWithConfig(file, fileSet, *tc.customConfig)
p = NewProcessorWithConfig(file, fileSet, tc.customConfig)
}

p.ParseAST()
Expand Down Expand Up @@ -2044,7 +2044,7 @@ func TestTODO(t *testing.T) {
p := NewProcessor(file, fileSet)

if tc.customConfig != nil {
p = NewProcessorWithConfig(file, fileSet, *tc.customConfig)
p = NewProcessorWithConfig(file, fileSet, tc.customConfig)
}

p.ParseAST()
Expand Down

0 comments on commit a3c022a

Please sign in to comment.