Giter VIP home page Giter VIP logo

unimport's Introduction

unimport

unimport is a Go static analysis tool to find unnecessary import aliases.

Installation

go get -u github.com/alexkohler/unimport

Usage

Similar to other Go static anaylsis tools (such as golint, go vet) , unimport can be invoked with one or more filenames, directories, or packages named by its import path. Unimport also supports the ... wildcard.

unimport files/directories/packages

Currently, no flag are supported. A -w flag may be added in the future to automatically remove aliases where possible (Similar to gofmt's -w flag).

Purpose

As noted in Go's Code Review comments:

Avoid renaming imports except to avoid a name collision; good package names should not require renaming. In the event of collision, prefer to rename the most local or project-specific import.

This tool will check if any import aliases are truly needed (by ensuring there is a name collision that would exist without the import alias). This tool will ignore import paths containing dashes and dots, as these are generally useful aliases while importing a specific revision. For example, in gometalinter, there are some imports like kingpin "gopkg.in/alecthomas/kingpin.v3-unstable". This is a reasonable import alias and will not be flagged.

Example

Running unimports on the Go source:

$ unimport $GOROOT/src/...
cmd/go/pkg.go:18 unnecessary import alias pathpkg
go/build/build.go:19 unnecessary import alias pathpkg
go/internal/gcimporter/gcimporter.go:23 unnecessary import alias exact
os/pipe_test.go:14 unnecessary import alias osexec
os/os_windows_test.go:10 unnecessary import alias osexec

Below are some of the arguably unneeded import aliases it found:

// go/internal/gcimporter/gcimporter.go
import (                                                                                       
    "bufio"                                                                                    
    "errors"                                                                                   
    "fmt"                                                                                      
    "go/build"                                                                                 
    "go/token"                                                                                 
    "io"                                                                                       
    "io/ioutil"                                                                                
    "os"                                                                                       
    "path/filepath"                                                                            
    "sort"                                                                                     
    "strconv"                                                                                  
    "strings"                                                                                  
    "text/scanner"                                                                             
                                                                                               
    exact "go/constant"                                                                        
    "go/types"                                                                                 
)


// os/pipe_test.go.go
import (                                                                                       
    "fmt"                                                                                      
    "internal/testenv"                                                                         
    "os"                                                                                       
    osexec "os/exec"                                                                           
    "os/signal"                                                                                
    "syscall"                                                                                  
    "testing"                                                                                  
)

TODO

  • Unit tests
  • Flagging of packages that contain an uppercase letter or underscore
  • -w flag to write changes to file where/if possible
  • Globbing support (e.g. unimport *.go)

Contributing

Pull requests welcome!

  • nakedret - Finds naked returns.
  • prealloc - Finds slice declarations that could potentially be preallocated.

unimport's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

isgasho aolenevme

unimport's Issues

False positive.

In your README, one of the examples is:

go/build/build.go:19 unnecessary import alias pathpkg

That appears to be a false positive. That import rename of path package is neccessary. Without it, the Context.Import method would have a name conflict:

func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) {
	p := &Package{
		ImportPath: path,
	}
	if path == "" {
		return p, fmt.Errorf("import %q: invalid import path", path)
	}
...
	setPkga := func() {
		switch ctxt.Compiler {
		case "gccgo":
			dir, elem := pathpkg.Split(p.ImportPath) // if this were path.Split, that wouldn't compile because path is a string here
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.