-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
"", "", "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module a | ||
|
||
go 1.21 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters