quickfix

package module
v0.0.0-...-39bb724 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 9 Imported by: 13

README

goquickfix

CI Status Go Report Card MIT License release pkg.go.dev

The goquickfix command quickly fixes Go source that is well typed but Go refuses to compile e.g. declared and not used: x.

Installation

go install github.com/motemen/go-quickfix/cmd/goquickfix@latest

Usage

goquickfix [-w] [-revert] <path>...

Flags:
  -revert: try to revert possible quickfixes introduced by goquickfix
  -w: write result to (source) file instead of stdout
Description

While coding, you may write a Go program that is completely well typed but go build (or run or test) refuses to build, like this:

package main

import (
	"fmt"
	"log"
)

func main() {
	nums := []int{3, 1, 4, 1, 5}
	for i, n := range nums {
		fmt.Println(n)
	}
}

The Go compiler will complain:

main.go:5:2: "log" imported and not used
main.go:10:6: declared and not used: i

Do we have to bother to comment out the import line or remove the unused identifier one by one for the Go compiler? Of course no, goquickfix does the work instead of you.

Run

goquickfix -w main.go

and you will get the source rewritten so that Go compiles it well without changing the semantics:

package main

import (
	"fmt"
	_ "log"
)

func main() {
	nums := []int{3, 1, 4, 1, 5}
	for i, n := range nums {
		fmt.Println(n)
		_ = i
	}
}

Now, you can go run or go test your code successfully.

Documentation

Overview

Package quickfix provides functions for fixing Go ASTs that are well typed but "go build" refuses to build.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuickFix

func QuickFix(fset *token.FileSet, files []*ast.File) error

QuickFix a file set.

func RevertQuickFix

func RevertQuickFix(fset *token.FileSet, files []*ast.File) error

RevertQuickFix a file set.

Types

type Config

type Config struct {
	Fset     *token.FileSet
	Files    []*ast.File
	Dir      string
	TypeInfo *types.Info
	MaxTries int
}

Config for quickfix.

func (Config) QuickFix

func (c Config) QuickFix() (err error)

QuickFix rewrites AST files of same package so that they pass go build. For example:

declared and not used: v            -> append `_ = v`
"p" imported and not used           -> rewrite to `import _ "p"`
no new variables on left side of := -> rewrite `:=` to `=`

TODO implement hardMode, which removes errorneous code rather than adding

func (Config) QuickFixOnce

func (c Config) QuickFixOnce() (bool, error)

QuickFixOnce apply the fixes once.

func (Config) RevertQuickFix

func (c Config) RevertQuickFix() (err error)

RevertQuickFix reverts possible quickfixes introduced by QuickFix. This may result to non-buildable source, and cannot reproduce the original code before prior QuickFix. For example:

`_ = v`        -> removed
`import _ "p"` -> rewritten to `import "p"`

type ErrCouldNotLocate

type ErrCouldNotLocate struct {
	Err  types.Error
	Fset *token.FileSet
}

ErrCouldNotLocate represents a file not found error.

func (ErrCouldNotLocate) Error

func (e ErrCouldNotLocate) Error() string

type ErrorList

type ErrorList []error

ErrorList represents a collection of errors.

func (ErrorList) Error

func (errs ErrorList) Error() string

Directories

Path Synopsis
cmd
goquickfix command
The goquickfix command quick fixes Go source that is well typed but go refuses to compile e.g.
The goquickfix command quick fixes Go source that is well typed but go refuses to compile e.g.

Jump to

Keyboard shortcuts

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