Code Monkey home page Code Monkey logo

delivery-club-rules's Introduction

Project in archive now. For actual rules look into: https://github.com/peakle/go-rules

delivery-club-rules

Delivery club go rules linter

Tests Go Report Card Go Reference

How to use:

Full installation example: https://github.com/peakle/dc-rules-example

  1. Install rules:
    go get -v github.com/delivery-club/delivery-club-rules
  2. Create rules.go file in your project like in example
  3. Add linter to your pipeline:
    1. Like another one check in golangci-lint (will work for golangci-lint >v1.46.0):

      linters:
        enable:
          - gocritic
      linters-settings:
        gocritic:
          enabled-checks:
            - ruleguard
          settings:
            ruleguard:
              rules: "YourDir/rules.go"
    2. Like explicit check WITHOUT golangci-lint:

      1. install binary by curl or go install:
      curl -sSfL https://raw.githubusercontent.com/delivery-club/delivery-club-rules/master/releaser/install.sh | sh -s -- -d -b $(go env GOPATH)/bin latest

      OR

      go install -v github.com/delivery-club/delivery-club-rules/cmd/dcRules@latest
      1. Run lint:
      dcRules ./...
    3. Like file watcher in Goland IDE (will work for golangci-lint >v1.46.0):

      1. add golangci-lint as File Watcher in IDE (Preferences -> Tools -> File Watchers -> Add)
      2. set Arguments field where .golangci.yml file will be like example above:
      run $FileDir$ --config=$ProjectFileDir$/.golangci.yml
      

How to update to new rules version:

  1. update rules version in your go.mod file
  2. download new rules version:
    go get github.com/delivery-club/delivery-club-rules@newVersion
  3. if you using golangci-lint update cache:
    golangci-lint cache clean

How to add new checks:

  1. Ruleguard tour for newbees: https://go-ruleguard.github.io/by-example
  2. Fork repo && open PR :D

delivery-club-rules's People

Contributors

peakle 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

Watchers

 avatar

delivery-club-rules's Issues

[dc-rules] add tests for binary mode usage

Is your feature request related to a problem? Please describe.
after implementing #13 we can use linter as explict binary without on any other files, but we dont have test for this usage (flags, etc)

[dc-rules] support linter usage with module vendoring

Is your feature request related to a problem? Please describe.
module vendoring doesn't download pkg directory, so rules can't properly compile

Describe the solution you'd like
we can create explict repository for interfaces like in pkg/ dir and import them in rules

[dc-rules] add rule for sql structs

Is your feature request related to a problem? Please describe.
add rule which will check sql structs with pointers to basic types (string, int, bool, etc) and will recommend change it to sql.Null* type. So we can decrease load on heap and increase runtime speed

POC:

type RowHeap struct {
	Str *string `db:"str"`
}

type RowStack struct {
	Str sql.NullString `db:"str"`
}

BenchmarkResults https://github.com/peakle/meetup/blob/master/go-optimizations/bench2_test.go:

RowHeap:
BenchmarkRowHeap-16           59960768                19.82 ns/op           16 B/op          1 allocs/op
BenchmarkRowHeap-16           61210947                21.10 ns/op           16 B/op          1 allocs/op
BenchmarkRowHeap-16           49640668                28.02 ns/op           16 B/op          1 allocs/op

RowStack:
BenchmarkRowStack-16           1000000000               0.7385 ns/op          0 B/op          0 allocs/op
BenchmarkRowStack-16           1000000000               0.7114 ns/op          0 B/op          0 allocs/op
BenchmarkRowStack-16           1000000000               0.7178 ns/op          0 B/op          0 allocs/op

[dc-rules] add test cases

Is your feature request related to a problem? Please describe.
add test cases for expressions like param.Method().Method(arg).Method() for deferInLopp, uncheckedTypeAssert

[dc-rules] change Text.Matches to Contains where it possible

Is your feature request related to a problem? Please describe.
Text.Matches method use regexp so it slower in execution than Contains method which based on go ast

Describe the solution you'd like
replace Text.Matches calls to Contains method

[dc-rules] add check defer call cancelFunc after context.WithCancel

Is your feature request related to a problem? Please describe.
to prevent goroutines leak check for cancelFunc defer call (from context.WithCancel)

Describe the solution you'd like
add new rule for cancelFunc call after creation

Code example(s) or test data

ctx, cancel := context.WithCancel(context.Background())
defer cancel() <- expect warning if line doesn't exist

[dc-rules] add rule for one line return after assign var

Is your feature request related to a problem? Please describe.
add rule for one line return after assign var

Describe the solution you'd like
such cases can be simplified for better code understanding

code example:

before:

func() string {
    /.../
    foo := "123123"
    return foo
}

after:

func() string {
    /.../
    return "123123"
}

[dc-rules] create support matrix

Is your feature request related to a problem? Please describe.
create support matrix for releases of ruleguard/go-critic/golangci-lint wth releases of our rules from v0.0.5, afaik rules loaded dynamicly so dependencies in this repository will not be equal to dependencies in binaries (golangci-lint/gocritic/ruleguard)

Describe the solution you'd like
add support matrix for dc-rules releases by one of two last releases of binaries (golangci-lint/gocritic/ruleguard)
if no one of two last releases of binaries not support yet dc-rules, write not supported yet
if both releases of binaries support rules version write earliest

[dc-rules] extend regexpCompileInLoop rule

Is your feature request related to a problem? Please describe.
now we find only = and := and not match var $_ = and var $_ $_ = assiments

Describe the solution you'd like
add another assigment patterns to rule

Code example(s) or test data
var r = regexp.MustCompile("\w")
var r *regexp.Regexp = regexp.MustCompile("\w")

[dc-rules] fix suggest blocks

Is your feature request related to a problem? Please describe.
after implementing quickfix logic from go-critic/go-critic#1179, golangci-lint can use our suggest blocks to fix code, but there are some bugs in our suggest blocks, so we need to fix that

see simplifyErrorReturn rule (maybe smth else), it have $*_ in suggest block which can't be properly moved to code base, it need to add some name like $*args or smth else, need to test

Describe the solution you'd like
our linter fix code fine when execute golangci-lint with --fix option

Code example(s) or test
in testdata dir

[dc-rules] skip experimental tag by default?

Is your feature request related to a problem? Please describe.
with experimental tag we will be able to create some rules, which may be false-positive in some cases or useful for some specific situations and will be skipped by default run

Solution: need to check feature on runtime and binary modes

[dc-rules] extend type assert check

Is your feature request related to a problem? Please describe.
extend type assert rule

Describe the solution you'd like
now not supported statements like:

var f interface{}

ch <- f.(string)

map[string]string{
 f.(string): "123",
 "123": f.(string),
 }

which also can panic

[dc-rules] add new rule for err checking in one line

Is your feature request related to a problem? Please describe.
a common problem on code reviews with simplify error checking in one line if

Describe the solution you'd like
new rule for simplify error check that under 120 LOC

[dc-rules] add mechanism for build binary

Is your feature request related to a problem? Please describe.
Create explict binary with rules. We now rely on golangci-lint/gocritic dependencies, so sometimes we have to wait for new release to get updated dsl/ruleguard dependencies. That block our releases. Also add support matrix with golangci-lint/gocritic realeases complitable with our releases.

[dc-rules] bug: cannot start binary on non go 1.17 projects

Is your feature request related to a problem? Please describe.
on start linter in binary mode for non 1.17 projects like https://github.com/peakle/eapteka-hackathon errors occurred:
deliveryClubRules: used Run() with an empty rule set; forgot to call Load() first?
deliveryClubRules: used Run() with an empty rule set; forgot to call Load() first?
deliveryClubRules: on load ir rules: rulesdata.go:282: can't load github.com/delivery-club/delivery-club-rules/pkg

Describe the solution you'd like
find and fix bugs

Code example(s) or test data
https://github.com/peakle/eapteka-hackathon

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.