Skip to content

Commit

Permalink
Merge 6082080 into 4c96186
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Sep 26, 2023
2 parents 4c96186 + 6082080 commit f6d4389
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
- [**Guide**](https://google.github.io/styleguide/go/guide)
- [mixedcaps](analyzer/guide/mixedcaps) ... based on https://google.github.io/styleguide/go/guide#mixed-caps
- [**Decisions**](https://google.github.io/styleguide/go/decisions)
- [funcfmt](analyzer/decisions/funcfmt) ... based on https://google.github.io/styleguide/go/decisions#function-formatting
- [getters](analyzer/decisions/getters) ... based on https://google.github.io/styleguide/go/decisions#getters
- [nilslices](analyzer/decisions/nilslices) ... based on https://google.github.io/styleguide/go/decisions#nil-slices
- [pkgnames](analyzer/decisions/pkgnames) ... based on https://google.github.io/styleguide/go/decisions#package-names
Expand Down Expand Up @@ -143,6 +144,14 @@ analyzers-settings:
include-generated: false # include generated codes (default: false)
```

#### funcfmt

```yaml
analyzers-settings:
funcfmt:
include-generated: false # include generated codes (default: false)
```

#### getters

```yaml
Expand Down
118 changes: 118 additions & 0 deletions analyzer/decisions/funcfmt/funcfmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package funcfmt

import (
"fmt"
"go/ast"

"github.com/gostaticanalysis/comment/passes/commentmap"
"github.com/k1LoW/gostyle/config"
"github.com/k1LoW/gostyle/reporter"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const (
name = "funcfmt"
doc = "Analyzer based on https://google.github.io/styleguide/go/decisions#function-formatting"
msgs = "The signature of a function or method declaration should remain on a single line to avoid indentation confusion. (ref: https://google.github.io/styleguide/go/decisions#function-formatting)"
msgc = "Function and method calls should not be separated based solely on line length. (ref: https://google.github.io/styleguide/go/decisions#function-formatting)"
)

var (
disable bool
includeGenerated bool
)

// Analyzer based on https://google.github.io/styleguide/go/decisions#function-formatting
var Analyzer = &analysis.Analyzer{
Name: name,
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
commentmap.Analyzer,
},
}

// AnalyzerWithConfig based on https://google.github.io/styleguide/go/decisions#function-formatting
var AnalyzerWithConfig = &analysis.Analyzer{
Name: name,
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
config.Loader,
inspect.Analyzer,
commentmap.Analyzer,
},
}

func run(pass *analysis.Pass) (any, error) {
c, err := config.Load(pass)
if err != nil {
return nil, err
}
if c != nil {
disable = c.IsDisabled(name)
includeGenerated = c.AnalyzersSettings.Funcfmt.IncludeGenerated
}

if disable {
return nil, nil
}
i, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, fmt.Errorf("unexpected result type from inspect: %T", pass.ResultOf[inspect.Analyzer])
}

nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.CallExpr)(nil),
}

var opts []reporter.Option
if includeGenerated {
opts = append(opts, reporter.IncludeGenerated())
}
r, err := reporter.New(name, pass, opts...)
if err != nil {
return nil, err
}

i.Preorder(nodeFilter, func(n ast.Node) {
switch nn := n.(type) {
case *ast.FuncDecl:
if len(nn.Type.Params.List) == 0 {
return
}
for _, f := range nn.Type.Params.List {
if len(f.Names) == 0 {
continue
}
for _, id := range f.Names {
if pass.Fset.Position(id.Pos()).Line != pass.Fset.Position(nn.Pos()).Line {
r.Append(nn.Pos(), msgs)
return
}
}
}
case *ast.CallExpr:
if len(nn.Args) == 0 {
return
}
for _, arg := range nn.Args {
if pass.Fset.Position(arg.Pos()).Line != pass.Fset.Position(nn.Pos()).Line {
r.Append(nn.Pos(), msgc)
return
}
}
}
})
r.Report()
return nil, nil
}

func init() {
Analyzer.Flags.BoolVar(&disable, "disable", false, "disable "+name+" analyzer")
Analyzer.Flags.BoolVar(&includeGenerated, "include-generated", false, "include generated codes")
}
14 changes: 14 additions & 0 deletions analyzer/decisions/funcfmt/funcfmt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package funcfmt

import (
"testing"

"github.com/gostaticanalysis/testutil"
"golang.org/x/tools/go/analysis/analysistest"
)

// TestAnalyzer is a test for Analyzer.
func TestAnalyzer(t *testing.T) {
td := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, td, Analyzer, "a")
}
13 changes: 13 additions & 0 deletions analyzer/decisions/funcfmt/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package a

func b( // want "gostyle.funcfmt"
a, b, c, d int,
e, f,
g string) error {
return nil
}

func c() error {
return b(1, 2, 3, 4, // want "gostyle.funcfmt"
"", "", "")
}
4 changes: 4 additions & 0 deletions analyzer/decisions/funcfmt/testdata/src/a/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module a

go 1.21

5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type AnalyzersSettings struct {
Contexts Contexts `yaml:"contexts"`
Dontpanic Dontpanic `yaml:"dontpanic"`
Errorstrings Errorstrings `yaml:"errorstrings"`
Funcfmt Funcfmt `yaml:"funcfmt"`
Getters Getters `yaml:"getters"`
Handlerrors Handlerrors `yaml:"handlerrors"`
Ifacenames Ifacenames `yaml:"ifacenames"`
Expand Down Expand Up @@ -62,6 +63,10 @@ type Errorstrings struct {
IncludeGenerated bool `yaml:"include-generated"`
}

type Funcfmt struct {
IncludeGenerated bool `yaml:"include-generated"`
}

type Getters struct {
Exclude []string `yaml:"exclude"`
IncludeGenerated bool `yaml:"include-generated"`
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/k1LoW/gostyle/analyzer/code_review_comments/dontpanic"
"github.com/k1LoW/gostyle/analyzer/code_review_comments/errorstrings"
"github.com/k1LoW/gostyle/analyzer/code_review_comments/handlerrors"
"github.com/k1LoW/gostyle/analyzer/decisions/funcfmt"
"github.com/k1LoW/gostyle/analyzer/decisions/getters"
"github.com/k1LoW/gostyle/analyzer/decisions/nilslices"
"github.com/k1LoW/gostyle/analyzer/decisions/pkgnames"
Expand Down Expand Up @@ -52,11 +53,12 @@ func main() {
}
}

unitchecker.Main(
unitchecker.Main( //nostyle:funcfmt
config.Loader,
contexts.AnalyzerWithConfig,
dontpanic.AnalyzerWithConfig,
errorstrings.AnalyzerWithConfig,
funcfmt.AnalyzerWithConfig,
getters.AnalyzerWithConfig,
handlerrors.AnalyzerWithConfig,
ifacenames.AnalyzerWithConfig,
Expand Down

0 comments on commit f6d4389

Please sign in to comment.