Skip to content

Commit

Permalink
small refactoring, to split big file a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
nunnatsa committed Feb 13, 2024
1 parent 0033f3d commit 1cb550c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 42 deletions.
50 changes: 50 additions & 0 deletions analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ginkgolinter

import (
"flag"
"fmt"

"golang.org/x/tools/go/analysis"

"github.com/nunnatsa/ginkgolinter/linter"
"github.com/nunnatsa/ginkgolinter/types"
"github.com/nunnatsa/ginkgolinter/version"
)

// NewAnalyzer returns an Analyzer - the package interface with nogo
func NewAnalyzer() *analysis.Analyzer {
config := &types.Config{
SuppressLen: false,
SuppressNil: false,
SuppressErr: false,
SuppressCompare: false,
ForbidFocus: false,
AllowHaveLen0: false,
ForceExpectTo: false,
}

theLinter := linter.NewGinkgoLinter(config)
a := &analysis.Analyzer{
Name: "ginkgolinter",
Doc: fmt.Sprintf(doc, version.Version()),
Run: theLinter.Run,
}

var ignored bool
a.Flags.Init("ginkgolinter", flag.ExitOnError)
a.Flags.Var(&config.SuppressLen, "suppress-len-assertion", "Suppress warning for wrong length assertions")
a.Flags.Var(&config.SuppressNil, "suppress-nil-assertion", "Suppress warning for wrong nil assertions")
a.Flags.Var(&config.SuppressErr, "suppress-err-assertion", "Suppress warning for wrong error assertions")
a.Flags.Var(&config.SuppressCompare, "suppress-compare-assertion", "Suppress warning for wrong comparison assertions")
a.Flags.Var(&config.SuppressAsync, "suppress-async-assertion", "Suppress warning for function call in async assertion, like Eventually")
a.Flags.Var(&config.SuppressTypeCompare, "suppress-type-compare-assertion", "Suppress warning for comparing values from different types, like int32 and uint32")
a.Flags.Var(&config.AllowHaveLen0, "allow-havelen-0", "Do not warn for HaveLen(0); default = false")
a.Flags.Var(&config.ForceExpectTo, "force-expect-to", "force using `Expect` with `To`, `ToNot` or `NotTo`. reject using `Expect` with `Should` or `ShouldNot`; default = false (not forced)")
a.Flags.BoolVar(&ignored, "suppress-focus-container", true, "Suppress warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt. Deprecated and ignored: use --forbid-focus-container instead")
a.Flags.Var(&config.ForbidFocus, "forbid-focus-container", "trigger a warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt; default = false.")

return a
}

// Analyzer is the interface to go_vet
var Analyzer = NewAnalyzer()
File renamed without changes.
49 changes: 7 additions & 42 deletions ginkgo_linter.go → linter/ginkgo_linter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ginkgolinter
package linter

import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/constant"
Expand All @@ -19,7 +18,6 @@ import (
"github.com/nunnatsa/ginkgolinter/interfaces"
"github.com/nunnatsa/ginkgolinter/reverseassertion"
"github.com/nunnatsa/ginkgolinter/types"
"github.com/nunnatsa/ginkgolinter/version"
)

// The ginkgolinter enforces standards of using ginkgo and gomega.
Expand Down Expand Up @@ -80,51 +78,18 @@ const ( // gomega actuals
consistentlyWithOffset = "ConsistentlyWithOffset"
)

// Analyzer is the interface to go_vet
var Analyzer = NewAnalyzer()

type ginkgoLinter struct {
type GinkgoLinter struct {
config *types.Config
}

// NewAnalyzer returns an Analyzer - the package interface with nogo
func NewAnalyzer() *analysis.Analyzer {
linter := ginkgoLinter{
config: &types.Config{
SuppressLen: false,
SuppressNil: false,
SuppressErr: false,
SuppressCompare: false,
ForbidFocus: false,
AllowHaveLen0: false,
ForceExpectTo: false,
},
}

a := &analysis.Analyzer{
Name: "ginkgolinter",
Doc: fmt.Sprintf(doc, version.Version()),
Run: linter.run,
func NewGinkgoLinter(config *types.Config) *GinkgoLinter {
return &GinkgoLinter{
config: config,
}

var ignored bool
a.Flags.Init("ginkgolinter", flag.ExitOnError)
a.Flags.Var(&linter.config.SuppressLen, "suppress-len-assertion", "Suppress warning for wrong length assertions")
a.Flags.Var(&linter.config.SuppressNil, "suppress-nil-assertion", "Suppress warning for wrong nil assertions")
a.Flags.Var(&linter.config.SuppressErr, "suppress-err-assertion", "Suppress warning for wrong error assertions")
a.Flags.Var(&linter.config.SuppressCompare, "suppress-compare-assertion", "Suppress warning for wrong comparison assertions")
a.Flags.Var(&linter.config.SuppressAsync, "suppress-async-assertion", "Suppress warning for function call in async assertion, like Eventually")
a.Flags.Var(&linter.config.SuppressTypeCompare, "suppress-type-compare-assertion", "Suppress warning for comparing values from different types, like int32 and uint32")
a.Flags.Var(&linter.config.AllowHaveLen0, "allow-havelen-0", "Do not warn for HaveLen(0); default = false")
a.Flags.Var(&linter.config.ForceExpectTo, "force-expect-to", "force using `Expect` with `To`, `ToNot` or `NotTo`. reject using `Expect` with `Should` or `ShouldNot`; default = false (not forced)")
a.Flags.BoolVar(&ignored, "suppress-focus-container", true, "Suppress warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt. Deprecated and ignored: use --forbid-focus-container instead")
a.Flags.Var(&linter.config.ForbidFocus, "forbid-focus-container", "trigger a warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt; default = false.")

return a
}

// main assertion function
func (l *ginkgoLinter) run(pass *analysis.Pass) (interface{}, error) {
// Run is the main assertion function
func (l *GinkgoLinter) Run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
fileConfig := l.config.Clone()

Expand Down

0 comments on commit 1cb550c

Please sign in to comment.