Skip to content

Commit

Permalink
Merge pull request #13 from ldez/feat/remove-golancgi-lint-mode
Browse files Browse the repository at this point in the history
feat: remove golangci-lint mode
  • Loading branch information
ghostiam authored Dec 22, 2024
2 parents 213f5ee + 20c6b3b commit d065bf3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 61 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@ jobs:
contents: write
strategy:
matrix:
go: [ '1.23' ]
go: [ stable ]

steps:
- name: Check out
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- run: git fetch --force --tags
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: true
- name: release
uses: goreleaser/goreleaser-action@v4
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9 changes: 4 additions & 5 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.22', '1.23' ]
go: [ stable, oldstable ]

steps:
- name: Check out
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: true
- name: Test
run: make test
run: make test
3 changes: 2 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2
before:
hooks:
- go mod tidy
Expand All @@ -21,4 +22,4 @@ changelog:
exclude:
- '^docs:'
- '^test:'
- '^ci:'
- '^ci:'
55 changes: 6 additions & 49 deletions protogetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ import (
"golang.org/x/tools/go/ast/inspector"
)

type Mode int

const (
StandaloneMode Mode = iota
GolangciLintMode
)

const msgFormat = "avoid direct access to proto field %s, use %s instead"

func NewAnalyzer(cfg *Config) *analysis.Analyzer {
Expand All @@ -35,7 +28,7 @@ func NewAnalyzer(cfg *Config) *analysis.Analyzer {
Doc: "Reports direct reads from proto message fields when getters should be used",
Flags: flags(cfg),
Run: func(pass *analysis.Pass) (any, error) {
_, err := Run(pass, cfg)
err := Run(pass, cfg)
return nil, err
},
}
Expand All @@ -62,14 +55,13 @@ func flags(opts *Config) flag.FlagSet {
}

type Config struct {
Mode Mode // Zero value is StandaloneMode.
SkipGeneratedBy []string
SkipFiles []string
SkipAnyGenerated bool
ReplaceFirstArgInAppend bool
}

func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {
func Run(pass *analysis.Pass, cfg *Config) error {
skipGeneratedBy := make([]string, 0, len(cfg.SkipGeneratedBy)+3)
// Always skip files generated by protoc-gen-go, protoc-gen-go-grpc and protoc-gen-grpc-gateway.
skipGeneratedBy = append(skipGeneratedBy, "protoc-gen-go", "protoc-gen-go-grpc", "protoc-gen-grpc-gateway")
Expand All @@ -90,7 +82,7 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {

compile, err := glob.Compile(s)
if err != nil {
return nil, fmt.Errorf("invalid glob pattern: %w", err)
return fmt.Errorf("invalid glob pattern: %w", err)
}

skipFilesGlobPatterns = append(skipFilesGlobPatterns, compile)
Expand Down Expand Up @@ -124,24 +116,16 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {

ins := inspector.New(files)

var issues []Issue

filter := NewPosFilter()
ins.Preorder(nodeTypes, func(node ast.Node) {
report := analyse(pass, filter, node, cfg)
if report == nil {
return
}

switch cfg.Mode {
case StandaloneMode:
pass.Report(report.ToDiagReport())
case GolangciLintMode:
issues = append(issues, report.ToIssue(pass.Fset))
}
pass.Report(report.ToDiagReport())
})

return issues, nil
return nil
}

func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node, cfg *Config) *Report {
Expand Down Expand Up @@ -185,19 +169,6 @@ func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node, cfg *Config) *R
}
}

// Issue is used to integrate with golangci-lint's inline auto fix.
type Issue struct {
Pos token.Position
Message string
InlineFix InlineFix
}

type InlineFix struct {
StartCol int // zero-based
Length int
NewString string
}

type Report struct {
node ast.Node
result *Result
Expand Down Expand Up @@ -225,27 +196,13 @@ func (r *Report) ToDiagReport() analysis.Diagnostic {
}
}

func (r *Report) ToIssue(fset *token.FileSet) Issue {
msg := fmt.Sprintf(msgFormat, r.result.From, r.result.To)
return Issue{
Pos: fset.Position(r.node.Pos()),
Message: msg,
InlineFix: InlineFix{
StartCol: fset.Position(r.node.Pos()).Column - 1,
Length: len(r.result.From),
NewString: r.result.To,
},
}
}

func skipGeneratedFile(f *ast.File, prefixes []string, skipAny bool) bool {
if len(f.Comments) == 0 {
return false
}
firstComment := f.Comments[0].Text()

// https://golang.org/s/generatedcode
if skipAny && strings.HasPrefix(firstComment, "Code generated") {
if skipAny && ast.IsGenerated(f) {
return true
}

Expand Down

0 comments on commit d065bf3

Please sign in to comment.