wsl

package module
v3.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2023 License: MIT Imports: 7 Imported by: 8

README

WSL - Whitespace Linter

forthebadge forthebadge

GitHub Actions Coverage Status

WSL is a linter that enforces a very non scientific vision of how to make code more readable by enforcing empty lines at the right places.

I think too much code out there is to cuddly and a bit too warm for it's own good, making it harder for other people to read and understand. The linter will warn about newlines in and around blocks, in the beginning of files and other places in the code.

I know this linter is aggressive and a lot of projects I've tested it on have failed miserably. For this linter to be useful at all I want to be open to new ideas, configurations and discussions! Also note that some of the warnings might be bugs or unintentional false positives so I would love an issue to fix, discuss, change or make something configurable!

Installation

By go get (local installation)

You can do that by using:

go get -u github.com/bombsimon/wsl/v3/cmd/...
By golangci-lint (CI automation)

wsl is already integrated with golangci-lint. Please refer to the instructions there.

Usage

How to use depends on how you install wsl.

With local binary

The general command format for wsl is:

$ wsl [flags] <file1> [files...]
$ wsl [flags] </path/to/package/...>

# Examples

$ wsl ./main.go
$ wsl --no-test ./main.go
$ wsl --allow-cuddle-declarations ./main.go
$ wsl --no-test --allow-cuddle-declaration ./main.go
$ wsl --no-test --allow-trailing-comment ./myProject/...

The "..." wildcard is not used like other go commands but instead can only be to a relative or absolute path.

By default, the linter will run on ./... which means all go files in the current path and all subsequent paths, including test files. To disable linting test files, use -n or --no-test.

By golangci-lint (CI automation)

The recommended command is:

golangci-lint run --disable-all --enable wsl

For more information, please refer to golangci-lint's documentation.

Issues and configuration

The linter suppers a few ways to configure it to satisfy more than one kind of code style. These settings could be set either with flags or with YAML configuration if used via golangci-lint.

The supported configuration can be found in the documentation.

Below are the available checklist for any hit from wsl. If you do not see any, feel free to raise an issue.

Note: this linter doesn't take in consideration the issues that will be fixed with go fmt -s so ensure that the code is properly formatted before use.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	// StrictAppend will do strict checking when assigning from append (x =
	// append(x, y)). If this is set to true the append call must append either
	// a variable assigned, called or used on the line above. Example on not
	// allowed when this is true:
	//
	//  x := []string{}
	//  y := "not going in X"
	//  x = append(x, "not y") // This is not allowed with StrictAppend
	//  z := "going in X"
	//
	//  x = append(x, z) // This is allowed with StrictAppend
	//
	//  m := transform(z)
	//  x = append(x, z) // So is this because Z is used above.
	StrictAppend bool

	// AllowAssignAndCallCuddle allows assignments to be cuddled with variables
	// used in calls on line above and calls to be cuddled with assignments of
	// variables used in call on line above.
	// Example supported with this set to true:
	//
	//  x.Call()
	//  x = Assign()
	//  x.AnotherCall()
	//  x = AnotherAssign()
	AllowAssignAndCallCuddle bool

	// AllowAssignAndCallCuddle allows assignments to be cuddled with anything.
	// Example supported with this set to true:
	//  if x == 1 {
	//  	x = 0
	//  }
	//  z := x + 2
	// 	fmt.Println("x")
	//  y := "x"
	AllowAssignAndAnythingCuddle bool

	// AllowMultiLineAssignCuddle allows cuddling to assignments even if they
	// span over multiple lines. This defaults to true which allows the
	// following example:
	//
	//  err := function(
	//  	"multiple", "lines",
	//  )
	//  if err != nil {
	//  	// ...
	//  }
	AllowMultiLineAssignCuddle bool

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

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

	// AllowSeparatedLeadingComment will allow multiple comments in the
	// beginning of a block separated with newline. Example:
	//  func () {
	//		// Comment one
	//
	//		// Comment two
	// 		fmt.Println("x")
	//  }
	AllowSeparatedLeadingComment bool

	// AllowCuddleDeclaration will allow multiple var/declaration statements to
	// be cuddled. This defaults to false but setting it to true will enable the
	// following example:
	//  var foo bool
	//  var err error
	AllowCuddleDeclaration bool

	// AllowCuddleWithCalls is a list of call idents that everything can be
	// cuddled with. Defaults to calls looking like locks to support a flow like
	// this:
	//
	//  mu.Lock()
	//  allow := thisAssignment
	AllowCuddleWithCalls []string

	// AllowCuddleWithRHS is a list of right hand side variables that is allowed
	// to be cuddled with anything. Defaults to assignments or calls looking
	// like unlocks to support a flow like this:
	//
	//  allow := thisAssignment()
	//  mu.Unlock()
	AllowCuddleWithRHS []string

	// ForceCuddleErrCheckAndAssign will cause an error when an If statement that
	// checks an error variable doesn't cuddle with the assignment of that variable.
	// This defaults to false but setting it to true will cause the following
	// to generate an error:
	//
	// err := ProduceError()
	//
	// if err != nil {
	//     return err
	// }
	ForceCuddleErrCheckAndAssign bool

	// When ForceCuddleErrCheckAndAssign is enabled this is a list of names
	// used for error variables to check for in the conditional.
	// Defaults to just "err"
	ErrorVariableNames []string

	// ForceExclusiveShortDeclarations will cause an error if a short declaration
	// (:=) cuddles with anything other than another short declaration. For example
	//
	// a := 2
	// b := 3
	//
	// is allowed, but
	//
	// a := 2
	// b = 3
	//
	// is not allowed. This logic overrides ForceCuddleErrCheckAndAssign among others.
	ForceExclusiveShortDeclarations bool
}

func DefaultConfig

func DefaultConfig() Configuration

DefaultConfig returns default configuration.

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

func NewProcessor

func NewProcessor() *Processor

NewProcessor will create a Processor.

func NewProcessorWithConfig

func NewProcessorWithConfig(cfg Configuration) *Processor

NewProcessor will create a Processor.

func (*Processor) ProcessFiles

func (p *Processor) ProcessFiles(filenames []string) ([]Result, []string)

ProcessFiles takes a string slice with file names (full paths) and lints them.

type Result

type Result struct {
	FileName   string
	LineNumber int
	Position   token.Position
	Reason     string
}

Result represents the result of one error.

func (*Result) String

func (r *Result) String() string

String returns the filename, line number and reason of a Result.

Directories

Path Synopsis
cmd
wsl

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL