Skip to content

Commit

Permalink
1. renamed linter
Browse files Browse the repository at this point in the history
2. stringsFlag replaced with globsFlag
3. onlyPackageGlobs renamed to packageGlobsOnly
  • Loading branch information
maranqz committed Dec 15, 2023
1 parent 626a8af commit 8c9d73e
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- v*

env:
GO_VERSION: "1.21"
GO_VERSION: "1.20"

jobs:

Expand Down Expand Up @@ -36,6 +36,6 @@ jobs:
with:
version: latest
args: release --clean
workdir: cmd/go-factory-lint/
workdir: cmd/go-gofactory-lint/
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linters-settings:
- v
- ok
gci:
local-prefixes: github.com/maranqz/go-factory-lint
local-prefixes: github.com/maranqz/gofactory

linters:
enable-all: true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ test.coverage:
$(GO_TEST) -covermode=atomic -coverprofile=$(COVERAGE_FILE)

install:
go install ./cmd/go-factory-lint
go install ./cmd/gofactory
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Factory linter

[![CI](https://github.com/maranqz/go-factory-lint/actions/workflows/ci.yml/badge.svg)](https://github.com/maranqz/go-factory-lint/actions/workflows/ci.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/maranqz/go-factory-lint)](https://goreportcard.com/report/github.com/maranqz/go-factory-lint?dummy=unused)
[![CI](https://github.com/maranqz/gofactory/actions/workflows/ci.yml/badge.svg)](https://github.com/maranqz/gofactory/actions/workflows/ci.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/maranqz/gofactory)](https://goreportcard.com/report/github.com/maranqz/gofactory?dummy=unused)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
[![Coverage Status](https://coveralls.io/repos/github/maranqz/go-factory-lint/badge.svg?branch=main)](https://coveralls.io/github/maranqz/go-factory-lint?branch=main)
[![Coverage Status](https://coveralls.io/repos/github/maranqz/gofactory/badge.svg?branch=main)](https://coveralls.io/github/maranqz/gofactory?branch=main)

The linter checks that the Structures are created by the Factory, and not directly.

Expand All @@ -14,14 +14,14 @@ The checking helps to provide invariants without exclusion and helps avoid creat

### Installation

go install github.com/maranqz/go-factory-lint/cmd/go-factory-lint@latest
go install github.com/maranqz/gofactory/cmd/gofactory@latest

### Options

- `--packageGlobs` – list of glob packages, which can create structures without factories inside the glob package.
By default, all structures from another package should be created by factories, [tests](testdata/src/factory/packageGlobs).
- `--onlyPackageGlobs` – use a factory to initiate a structure for glob packages only,
[tests](testdata/src/factory/onlyPackageGlobs). Doesn't make sense without `--packageGlobs`.
- `--packageGlobsOnly` – use a factory to initiate a structure for glob packages only,
[tests](testdata/src/factory/packageGlobsOnly). Doesn't make sense without `--packageGlobs`.

## Example

Expand All @@ -40,7 +40,7 @@ import (
)

func main() {
// Use factory for bad.User
// Use gofactory for bad.User
u := &bad.User{
ID: -1,
}
Expand Down Expand Up @@ -130,6 +130,7 @@ Linter doesn't catch some cases.
2. Local initialization, [example](testdata/src/factory/unimplemented/local/).
3. Named return. If you want to block that case, you can use [nonamedreturns](https://github.com/firefart/nonamedreturns) linter, [example](testdata/src/factory/unimplemented/named_return.go).
4. var declaration, `var initilized nested.Struct` gives structure without factory, [example](testdata/src/factory/unimplemented/var.go).
To block that case, you can use [gopublicfield](github.com/maranqz/gopublicfield) to prevent fill of structure fields.

## TODO

Expand All @@ -138,7 +139,7 @@ Linter doesn't catch some cases.
1. Catch nested struct in the same package, [example](testdata/src/factory/unimplemented/local/nested_struct.go).
```go
return Struct{
Other: OtherStruct{}, // want `Use factory for nested.Struct`
Other: OtherStruct{}, // want `Use gofactory for nested.Struct`
}
```
2. Resolve false negative issue with `var declaration`.
Expand Down
4 changes: 2 additions & 2 deletions cmd/go-factory-lint/main.go → cmd/gofactory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"golang.org/x/tools/go/analysis/singlechecker"

"github.com/maranqz/go-factory-lint/v2"
"github.com/maranqz/gofactory"
)

func main() {
singlechecker.Main(factory.NewAnalyzer())
singlechecker.Main(gofactory.NewAnalyzer())
}
31 changes: 6 additions & 25 deletions factory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package factory
package gofactory

import (
"fmt"
"go/ast"
"go/types"
"log"
Expand All @@ -11,7 +10,7 @@ import (
)

type config struct {
pkgGlobs stringsFlag
pkgGlobs globsFlag
onlyPkgGlobs bool
}

Expand All @@ -33,7 +32,7 @@ func NewAnalyzer() *analysis.Analyzer {

analyzer.Flags.Var(&cfg.pkgGlobs, "packageGlobs", packageGlobsDesc)

analyzer.Flags.BoolVar(&cfg.onlyPkgGlobs, "onlyPackageGlobs", false, onlyPkgGlobsDesc)
analyzer.Flags.BoolVar(&cfg.onlyPkgGlobs, "packageGlobsOnly", false, onlyPkgGlobsDesc)

analyzer.Run = run(&cfg)

Expand All @@ -43,17 +42,14 @@ func NewAnalyzer() *analysis.Analyzer {
func run(cfg *config) func(pass *analysis.Pass) (interface{}, error) {
return func(pass *analysis.Pass) (interface{}, error) {
var blockedStrategy blockedStrategy = newAnotherPkg()
if len(cfg.pkgGlobs) > 0 {

pkgGlobs := cfg.pkgGlobs.Value()
if len(pkgGlobs) > 0 {
defaultStrategy := blockedStrategy
if cfg.onlyPkgGlobs {
defaultStrategy = newNilPkg()
}

pkgGlobs, err := compileGlobs(cfg.pkgGlobs.Value())
if err != nil {
return nil, err
}

blockedStrategy = newBlockedPkgs(
pkgGlobs,
defaultStrategy,
Expand Down Expand Up @@ -229,18 +225,3 @@ func containsMatchGlob(globs []glob.Glob, el string) bool {

return false
}

func compileGlobs(globs []string) ([]glob.Glob, error) {
compiledGlobs := make([]glob.Glob, len(globs))

for idx, globString := range globs {
compiled, err := glob.Compile(globString)
if err != nil {
return nil, fmt.Errorf("unable to compile glob %q: %w", globString, err)
}

compiledGlobs[idx] = compiled
}

return compiledGlobs, nil
}
35 changes: 35 additions & 0 deletions globs_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gofactory

import (
"fmt"
"strings"

"github.com/gobwas/glob"
)

type globsFlag struct {
globsString []string
globs []glob.Glob
}

func (g globsFlag) String() string {
return strings.Join(g.globsString, ", ")
}

func (g *globsFlag) Set(globString string) error {
globString = strings.TrimSpace(globString)

compiled, err := glob.Compile(globString)
if err != nil {
return fmt.Errorf("unable to compile globs %s: %w", globString, err)
}

g.globsString = append(g.globsString, globString)
g.globs = append(g.globs, compiled)

return nil
}

func (g globsFlag) Value() []glob.Glob {
return g.globs
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/maranqz/go-factory-lint/v2
module github.com/maranqz/gofactory

go 1.20

Expand Down
14 changes: 7 additions & 7 deletions lint_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package factory_test
package gofactory_test

import (
"path/filepath"
Expand All @@ -7,7 +7,7 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"

"github.com/maranqz/go-factory-lint/v2"
"github.com/maranqz/gofactory"
)

func TestLinterSuite(t *testing.T) {
Expand All @@ -28,14 +28,14 @@ func TestLinterSuite(t *testing.T) {
return a.Flags.Set("packageGlobs", "factory/packageGlobs/blocked/**")
},
},
"onlyPackageGlobs": {
pkgs: []string{"onlyPackageGlobs/main/..."},
"packageGlobsOnly": {
pkgs: []string{"packageGlobsOnly/main/..."},
prepare: func(t *testing.T, a *analysis.Analyzer) error {
if err := a.Flags.Set("packageGlobs", "factory/onlyPackageGlobs/blocked/**"); err != nil {
if err := a.Flags.Set("packageGlobs", "factory/packageGlobsOnly/blocked/**"); err != nil {
return err
}

return a.Flags.Set("onlyPackageGlobs", "true")
return a.Flags.Set("packageGlobsOnly", "true")
},
},
}
Expand All @@ -51,7 +51,7 @@ func TestLinterSuite(t *testing.T) {
dirs = append(dirs, filepath.Join(testdata, "src", "factory", pkg))
}

analyzer := factory.NewAnalyzer()
analyzer := gofactory.NewAnalyzer()

if tt.prepare != nil {
if err := tt.prepare(t, analyzer); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion strategy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package factory
package gofactory

import (
"go/types"
Expand Down
25 changes: 0 additions & 25 deletions strings_flag.go

This file was deleted.

2 changes: 1 addition & 1 deletion testdata/src/factory/packageGlobs/nested/nested.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nested

import (
"factory/onlyPackageGlobs/blocked"
"factory/packageGlobsOnly/blocked"
)

type Struct struct{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package blocked

import (
"factory/onlyPackageGlobs/blocked/blocked_nested"
"factory/packageGlobsOnly/blocked/blocked_nested"
)

type Struct struct{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"factory/onlyPackageGlobs/blocked"
"factory/onlyPackageGlobs/blocked/blocked_nested"
"factory/onlyPackageGlobs/nested"
"factory/packageGlobsOnly/blocked"
"factory/packageGlobsOnly/blocked/blocked_nested"
"factory/packageGlobsOnly/nested"
)

type Struct struct{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nested

import (
"factory/onlyPackageGlobs/blocked"
"factory/packageGlobsOnly/blocked"
)

type Struct struct{}
Expand Down

0 comments on commit 8c9d73e

Please sign in to comment.