aliasimport

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 6 Imported by: 0

README

aliasimport

pkg.go.dev Test reviewdog Releases codecov

aliasimport defines some rules about import statement alias.

Define rules via YAML

You can define two type rules as aliases and noaliases, looks like the following code.

---
aliases:
  grpc_retry: github.com/grpc-ecosystem/go-grpc-middleware/retry
  pkgerr: "github.com/pkg/errors"  # quoted
noaliases:
  - gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer
  • aliases
    • The list of definitions about alias naming
    • Only one alias can be defined for each package.
  • noaliases
    • The list of package names that don't need any aliases
    • These packages should be imported nakedly.
package main

import (
	"context" // no rules

	"github.com/goccy/go-yaml" // no rules

	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" // valid alias

	"github.com/pkg/errors"           // no alias, invalid
	pkgerr "github.com/pkg/errors"    // valid alias
	pkgerrors "github.com/pkg/errors" // invalid alias

	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"          // no aliases
	ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" // use alias, invalid
)

func main() {
	var (
		_ context.Context
		_ yaml.BytesMarshaler
		_ tracer.Span
		_ ddtracer.Span
		_ grpc_retry.BackoffFunc
	)
	_ = errors.New("error")
	_ = pkgerr.New("error")
	_ = pkgerrors.New("error")
}
go vet -vettool=`which aliasimport` -aliasimport.rule=$(pwd)/rules.yml main.go
./main.go:10:2: the package "github.com/pkg/errors" should be imported with the alias name pkgerr
./main.go:12:2: the alias name of "github.com/pkg/errors" should be pkgerr, not pkgerrors
./main.go:15:2: the package "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" shouldn't be imported with any aliases, but with ddtracer

False Positives

Analyzers ignore nodes which are annotated by staticcheck's style comments as belows. A ignore comment includes analyzer names and reason of ignoring checking. If you specify aliasimport as analyzer name, all analyzers ignore corresponding code.

import (
	//lint:ignore aliasimport reason
	"github.com/pkg/errors"

	pkgerrors "github.com/pkg/errors" //lint:ignore aliasimport reason
)

Commiters

License

Licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "aliasimport",
	Doc:  doc,
	Run:  run,
}

Analyzer is ...

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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