Coder Social home page Coder Social logo

quasilyte / go-namecheck Goto Github PK

View Code? Open in Web Editor NEW
37.0 6.0 0.0 16 KB

Source code analyzer that helps you to maintain variable/field naming conventions inside your project.

License: MIT License

Go 100.00%
go golang linter source-code-analysis consistency consistency-checking conventions manifest

go-namecheck's Introduction

go-namecheck

Source code analyzer that helps you to maintain variable/field naming conventions inside your project.

Quick start / Installation

To install go-namecheck binary under your $(go env GOPATH)/bin:

go get -v github.com/quasilyte/go-namecheck

If $GOPATH/bin is under your system $PATH, go-namecheck command should be available after that.
This should print the help message:

go-namecheck --help

In big teams, same things end up being called differently eventually. Sometimes you bring inconsistencies on your own. Suppose it's considered idiomatic to call string parameter s if you can't figure a more descriptive name, but sometimes you see str names used by other programmers from your team. This is where go-namecheck can help.

For a better illustration, suppose we also want to catch regexp variables that use re prefix and propose RE suffix instead, so var reFoo *regexp.Regexp becomes var fooRE *regexp.Regexp.

{
  "string": {"param": {"str": "s"}},
  "regexp\\.Regexp": {
    "local+global": {"^re[A-Z]\\w*$": "use RE suffix instead of re prefix"}
  }
}

Rules above implement checks we described.

First key describes regular expression that matches a type. For that key there is an object for scopes. Scope can be one of:

  • param - function input params
  • receiver - method receiver
  • global - any global constant or variable
  • local - any local constant or variable
  • field - struct field

You can combine several scopes like param+receiver+local, etc.

Inside a scope there is an JSON object that maps "from" => "to" pair. In the simplest form, it's a simple literal matching that suggests to replace one name with another, like in str=>s rule. Key can also be a regular expression, in this case, the "to" part does not describe exact substitution, but rather describes how to make name idiomatic (what to change).

You start by creating your rules file (or borrowing someone else set). Then you can run go-namecheck like this:

go-namecheck foo.go bar.go mypkg

You can also use std, ./... and other conventional targets that are normally understood by Go tools.

go-namecheck's People

Contributors

aleksi avatar quasilyte avatar

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-namecheck's Issues

add rule scoping

There should be at least these scopes:

  • param - function input params
  • receiver - method receiver
  • global - any global constant or variable
  • local - any local constant or variable
  • field - struct field

By default, param+global+local+field scope is used (only receiver is disabled).

Print scope that matched the name

This can increase warning messages clarity.

./testdata/main_test.go:5:20: test testing.T: use t for testing.T params
./testdata/main_test.go:6:20: tst testing.T: use t for testing.T params
./testdata/main_test.go:8:24: bench testing.B: use b for testing.B params

=> could omit explanation related to "params"

./testdata/main_test.go:5:20: param test testing.T: rename to t
./testdata/main_test.go:6:20: param tst testing.T: rename to t
./testdata/main_test.go:8:24: param bench testing.B: rename to t

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.