Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: fzipp/gocyclo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.3.1
Choose a base ref
...
head repository: fzipp/gocyclo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.4.0
Choose a head ref
  • 12 commits
  • 10 files changed
  • 1 contributor

Commits on Oct 20, 2020

  1. Copy the full SHA
    6455a03 View commit details

Commits on Oct 23, 2020

  1. Add travis-ci configuration

    fzipp committed Oct 23, 2020
    Copy the full SHA
    f765863 View commit details
  2. README: add build status badge

    fzipp committed Oct 23, 2020
    Copy the full SHA
    1ae4744 View commit details

Commits on Oct 24, 2020

  1. Copy the full SHA
    316085f View commit details

Commits on Apr 8, 2021

  1. README: use go install

    fzipp committed Apr 8, 2021
    Copy the full SHA
    ffe36aa View commit details

Commits on Jul 17, 2021

  1. use filepath.WalkDir instead of filepath.Walk

    requires Go 1.16 or later
    fzipp committed Jul 17, 2021
    Copy the full SHA
    2f7fd03 View commit details
  2. format the code

    fzipp committed Jul 17, 2021
    Copy the full SHA
    7537a3d View commit details
  3. Copy the full SHA
    af52b96 View commit details

Commits on Sep 19, 2021

  1. Copy the full SHA
    e117b7f View commit details

Commits on Oct 30, 2021

  1. Copy the full SHA
    44804ba View commit details

Commits on Oct 31, 2021

  1. Copy the full SHA
    a4d5b57 View commit details

Commits on Dec 19, 2021

  1. update CHANGELOG for v0.4.0

    fzipp committed Dec 19, 2021
    Copy the full SHA
    9daf4d9 View commit details
Showing with 85 additions and 22 deletions.
  1. +15 −0 .github/workflows/build.yml
  2. +8 −1 CHANGELOG.md
  3. +2 −1 README.md
  4. +5 −16 analyze.go
  5. +2 −2 analyze_test.go
  6. +1 −1 cmd/gocyclo/main.go
  7. +1 −1 go.mod
  8. +26 −0 recv.go
  9. +24 −0 recv_pre118.go
  10. +1 −0 testdata/loops.go
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: build

on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.16'
- name: Run tests
run: go test -cover ./...
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.1]
## [0.4.0] - 2021-12-19
### Added
- Support method receivers with type parameters introduced in Go 1.18

### Changed
- Use more efficient filepath.WalkDir instead of filepath.Walk

## [0.3.1] - 2020-10-20
### Added
- Test coverage

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gocyclo

[![PkgGoDev](https://pkg.go.dev/badge/github.com/fzipp/gocyclo)](https://pkg.go.dev/github.com/fzipp/gocyclo)
![Build Status](https://github.com/fzipp/gocyclo/workflows/build/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/fzipp/gocyclo)](https://goreportcard.com/report/github.com/fzipp/gocyclo)

Gocyclo calculates
@@ -31,7 +32,7 @@ to smaller functions.
To install the `gocyclo` command, run

```
$ go get github.com/fzipp/gocyclo/cmd/gocyclo
$ go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
```

and put the resulting binary in one of your PATH directories if
21 changes: 5 additions & 16 deletions analyze.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"log"
"os"
"path/filepath"
@@ -39,17 +40,17 @@ func Analyze(paths []string, ignore *regexp.Regexp) Stats {
}

func analyzeDir(dirname string, ignore *regexp.Regexp, stats Stats) Stats {
filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error {
if err == nil && isGoFile(info) {
filepath.WalkDir(dirname, func(path string, entry fs.DirEntry, err error) error {
if err == nil && isGoFile(entry) {
stats = analyzeFile(path, ignore, stats)
}
return err
})
return stats
}

func isGoFile(f os.FileInfo) bool {
return !f.IsDir() && strings.HasSuffix(f.Name(), ".go")
func isGoFile(entry fs.DirEntry) bool {
return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go")
}

func analyzeFile(path string, ignore *regexp.Regexp, stats Stats) Stats {
@@ -137,15 +138,3 @@ func funcName(fn *ast.FuncDecl) string {
}
return fn.Name.Name
}

// recvString returns a string representation of recv of the
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
}
return "BADRECV"
}
4 changes: 2 additions & 2 deletions analyze_test.go
Original file line number Diff line number Diff line change
@@ -23,9 +23,9 @@ func TestAnalyze(t *testing.T) {
},
{
[]string{"testdata/loops.go"},
`4 testdata l4 testdata/loops.go:19:1
`4 testdata l4 testdata/loops.go:20:1
3 testdata l3 testdata/loops.go:8:1
2 testdata l2range testdata/loops.go:14:1
2 testdata l2range testdata/loops.go:15:1
2 testdata l2 testdata/loops.go:3:1`,
},
{
2 changes: 1 addition & 1 deletion cmd/gocyclo/main.go
Original file line number Diff line number Diff line change
@@ -116,6 +116,6 @@ func printTotal(s gocyclo.Stats, short bool) {
}

func usage() {
_, _ = fmt.Fprintf(os.Stderr, usageDoc)
_, _ = fmt.Fprint(os.Stderr, usageDoc)
os.Exit(2)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/fzipp/gocyclo

go 1.15
go 1.18
26 changes: 26 additions & 0 deletions recv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.18
// +build go1.18

package gocyclo

import "go/ast"

// recvString returns a string representation of recv of the
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
case *ast.IndexExpr:
return recvString(t.X)
case *ast.IndexListExpr:
return recvString(t.X)
}
return "BADRECV"
}
24 changes: 24 additions & 0 deletions recv_pre118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !go1.18
// +build !go1.18

package gocyclo

import "go/ast"

// recvString returns a string representation of recv of the
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
case *ast.IndexExpr:
return recvString(t.X)
}
return "BADRECV"
}
1 change: 1 addition & 0 deletions testdata/loops.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ func l3() {
}
}
}

func l2range() {
for range []struct{}{} {
}