From a02bc540fa5502e0ba6846267ea9f912934495aa Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:08:38 -0700 Subject: [PATCH 1/3] Rename package for nicer imports --- main.go | 2 +- pkg/{analyzer/analyzer.go => commentmimic/commentmimic.go} | 2 +- .../analyzer_test.go => commentmimic/commentmimic_test.go} | 6 +++--- pkg/{analyzer => commentmimic}/testdata/comment_types.go | 0 .../testdata/commenttype_string.go | 0 pkg/{analyzer => commentmimic}/testdata/out_of_scope.go | 0 pkg/{analyzer => commentmimic}/testdata/templates.go | 0 7 files changed, 5 insertions(+), 5 deletions(-) rename pkg/{analyzer/analyzer.go => commentmimic/commentmimic.go} (99%) rename pkg/{analyzer/analyzer_test.go => commentmimic/commentmimic_test.go} (99%) rename pkg/{analyzer => commentmimic}/testdata/comment_types.go (100%) rename pkg/{analyzer => commentmimic}/testdata/commenttype_string.go (100%) rename pkg/{analyzer => commentmimic}/testdata/out_of_scope.go (100%) rename pkg/{analyzer => commentmimic}/testdata/templates.go (100%) diff --git a/main.go b/main.go index 45b8710..1cb522d 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "golang.org/x/tools/go/analysis/singlechecker" - "github.com/ashmrtn/commentmimic/pkg/analyzer" + "github.com/ashmrtn/commentmimic/pkg/commentmimic" ) func main() { diff --git a/pkg/analyzer/analyzer.go b/pkg/commentmimic/commentmimic.go similarity index 99% rename from pkg/analyzer/analyzer.go rename to pkg/commentmimic/commentmimic.go index aab2cdc..7987648 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/commentmimic/commentmimic.go @@ -1,4 +1,4 @@ -package analyzer +package commentmimic import ( "flag" diff --git a/pkg/analyzer/analyzer_test.go b/pkg/commentmimic/commentmimic_test.go similarity index 99% rename from pkg/analyzer/analyzer_test.go rename to pkg/commentmimic/commentmimic_test.go index af3c077..ada3fbe 100644 --- a/pkg/analyzer/analyzer_test.go +++ b/pkg/commentmimic/commentmimic_test.go @@ -1,4 +1,4 @@ -package analyzer_test +package commentmimic_test import ( "bytes" @@ -13,8 +13,8 @@ import ( "github.com/stretchr/testify/suite" "golang.org/x/tools/go/analysis/analysistest" - "github.com/ashmrtn/commentmimic/pkg/analyzer" - "github.com/ashmrtn/commentmimic/pkg/analyzer/testdata" + "github.com/ashmrtn/commentmimic/pkg/commentmimic" + "github.com/ashmrtn/commentmimic/pkg/commentmimic/testdata" ) const ( diff --git a/pkg/analyzer/testdata/comment_types.go b/pkg/commentmimic/testdata/comment_types.go similarity index 100% rename from pkg/analyzer/testdata/comment_types.go rename to pkg/commentmimic/testdata/comment_types.go diff --git a/pkg/analyzer/testdata/commenttype_string.go b/pkg/commentmimic/testdata/commenttype_string.go similarity index 100% rename from pkg/analyzer/testdata/commenttype_string.go rename to pkg/commentmimic/testdata/commenttype_string.go diff --git a/pkg/analyzer/testdata/out_of_scope.go b/pkg/commentmimic/testdata/out_of_scope.go similarity index 100% rename from pkg/analyzer/testdata/out_of_scope.go rename to pkg/commentmimic/testdata/out_of_scope.go diff --git a/pkg/analyzer/testdata/templates.go b/pkg/commentmimic/testdata/templates.go similarity index 100% rename from pkg/analyzer/testdata/templates.go rename to pkg/commentmimic/testdata/templates.go From a6fc8eedf1ac700ea5c61499100931417c419ad3 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:08:56 -0700 Subject: [PATCH 2/3] Stop stutter in constructor --- main.go | 2 +- pkg/commentmimic/commentmimic.go | 2 +- pkg/commentmimic/commentmimic_test.go | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 1cb522d..67539c5 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,6 @@ import ( ) func main() { - a := analyzer.NewCommentMimic() + a := commentmimic.New() singlechecker.Main(a) } diff --git a/pkg/commentmimic/commentmimic.go b/pkg/commentmimic/commentmimic.go index 7987648..879c61f 100644 --- a/pkg/commentmimic/commentmimic.go +++ b/pkg/commentmimic/commentmimic.go @@ -369,7 +369,7 @@ type mimic struct { noTestComments bool } -func NewCommentMimic() *analysis.Analyzer { +func New() *analysis.Analyzer { m := mimic{} fs := flag.NewFlagSet("CommentMimicFlags", flag.ExitOnError) diff --git a/pkg/commentmimic/commentmimic_test.go b/pkg/commentmimic/commentmimic_test.go index ada3fbe..1b74f85 100644 --- a/pkg/commentmimic/commentmimic_test.go +++ b/pkg/commentmimic/commentmimic_test.go @@ -597,7 +597,7 @@ func executeMimicWithFlagsOnFiles( ) { t.Helper() - mimic := analyzer.NewCommentMimic() + mimic := commentmimic.New() for flag, value := range flags { require.NoError(t, mimic.Flags.Set(flag, strconv.FormatBool(value))) @@ -665,7 +665,7 @@ func (s *CommentMimicSuite) TestDoesNotErrorOnOutOfScope() { defer cleanup() - mimic := analyzer.NewCommentMimic() + mimic := commentmimic.New() for flag, value := range flags { require.NoError(s.T(), mimic.Flags.Set(flag, strconv.FormatBool(value))) @@ -688,7 +688,7 @@ func (s *CommentMimicSuite) TestHandlesExtraWhitespace() { defer cleanup() - mimic := analyzer.NewCommentMimic() + mimic := commentmimic.New() analysistest.Run(t, dir, mimic, "a") } @@ -710,7 +710,7 @@ func (s *CommentMimicSuite) TestMachineCommentsMismatch() { defer cleanup() - mimic := analyzer.NewCommentMimic() + mimic := commentmimic.New() for flag, value := range flags { require.NoError(t, mimic.Flags.Set(flag, strconv.FormatBool(value))) @@ -736,7 +736,7 @@ func (s *CommentMimicSuite) TestMachineCommentsOnExported() { defer cleanup() - mimic := analyzer.NewCommentMimic() + mimic := commentmimic.New() for flag, value := range flags { require.NoError(t, mimic.Flags.Set(flag, strconv.FormatBool(value))) @@ -764,7 +764,7 @@ func (s *CommentMimicSuite) TestSkipTestComments() { defer cleanup() - mimic := analyzer.NewCommentMimic() + mimic := commentmimic.New() for flag, value := range flags { require.NoError(t, mimic.Flags.Set(flag, strconv.FormatBool(value))) From 4b7a9bc7267b7743e44fee39b94ff6538583fc01 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:42:11 -0700 Subject: [PATCH 3/3] Create exports for flag names --- pkg/commentmimic/commentmimic.go | 18 ++- pkg/commentmimic/commentmimic_test.go | 192 +++++++++++++------------- 2 files changed, 109 insertions(+), 101 deletions(-) diff --git a/pkg/commentmimic/commentmimic.go b/pkg/commentmimic/commentmimic.go index 879c61f..13738fb 100644 --- a/pkg/commentmimic/commentmimic.go +++ b/pkg/commentmimic/commentmimic.go @@ -361,6 +361,14 @@ func (m mimic) run(pass *analysis.Pass) (any, error) { return nil, nil } +const ( + CommentExportedFuncsFlag = "comment-exported" + CommentAllExportedFuncsFlag = "comment-all-exported" + CommentInterfacesFlag = "comment-interfaces" + NoTestCommentsFlag = "no-test-comments" + CommentStructsFlag = "comment-structs" +) + type mimic struct { commentExportedFuncs bool commentAllExportedFuncs bool @@ -375,35 +383,35 @@ func New() *analysis.Analyzer { fs := flag.NewFlagSet("CommentMimicFlags", flag.ExitOnError) fs.BoolVar( &m.commentExportedFuncs, - "comment-exported", + CommentExportedFuncsFlag, false, "require comments on exported functions if their receiver is also exported", ) fs.BoolVar( &m.commentAllExportedFuncs, - "comment-all-exported", + CommentAllExportedFuncsFlag, false, "require comments on all exported functions", ) fs.BoolVar( &m.commentInterfaces, - "comment-interfaces", + CommentInterfacesFlag, false, "require comments on all exported interfaces", ) fs.BoolVar( &m.noTestComments, - "no-test-comments", + NoTestCommentsFlag, true, "don't require comments on tests, benchmarks, examples, and fuzz tests", ) fs.BoolVar( &m.commentStructs, - "comment-structs", + CommentStructsFlag, false, "require comments on all exported structs", ) diff --git a/pkg/commentmimic/commentmimic_test.go b/pkg/commentmimic/commentmimic_test.go index 1b74f85..45989a2 100644 --- a/pkg/commentmimic/commentmimic_test.go +++ b/pkg/commentmimic/commentmimic_test.go @@ -31,100 +31,100 @@ var ( ) flagProduct = []map[string]bool{ { - "comment-exported": false, - "comment-all-exported": false, - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": false, - "comment-all-exported": false, - "comment-interfaces": false, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": false, - "comment-all-exported": false, - "comment-interfaces": true, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": false, - "comment-all-exported": false, - "comment-interfaces": true, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": false, - "comment-all-exported": true, - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": false, - "comment-all-exported": true, - "comment-interfaces": false, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": false, - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": false, - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": true, - "comment-all-exported": false, - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": true, - "comment-all-exported": false, - "comment-interfaces": false, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": true, - "comment-all-exported": false, - "comment-interfaces": true, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": true, - "comment-all-exported": false, - "comment-interfaces": true, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": true, - "comment-all-exported": true, - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": true, - "comment-all-exported": true, - "comment-interfaces": false, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: true, }, { - "comment-exported": true, - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": true, - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, }, } ) @@ -695,10 +695,10 @@ func (s *CommentMimicSuite) TestHandlesExtraWhitespace() { func (s *CommentMimicSuite) TestMachineCommentsMismatch() { t := s.T() flags := map[string]bool{ - "comment-exported": true, - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, } fileMap := map[string]string{ @@ -722,9 +722,9 @@ func (s *CommentMimicSuite) TestMachineCommentsMismatch() { func (s *CommentMimicSuite) TestMachineCommentsOnExported() { t := s.T() flags := map[string]bool{ - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, } fileMap := map[string]string{ @@ -748,11 +748,11 @@ func (s *CommentMimicSuite) TestMachineCommentsOnExported() { func (s *CommentMimicSuite) TestSkipTestComments() { t := s.T() flags := map[string]bool{ - "comment-exported": true, - "comment-all-exported": true, - "comment-interfaces": true, - "comment-structs": true, - "no-test-comments": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: true, + commentmimic.NoTestCommentsFlag: true, } fileMap := map[string]string{ @@ -1049,12 +1049,12 @@ func (s *CommentMimicSuite) TestCommentAccessibleExportedFuncs() { var ( flags = map[string]bool{ - "comment-exported": true, - "comment-all-exported": false, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: false, // Turn off as this will be testing some exported interfaces that we don't // want to comment. - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, } funcCases = []templateData{ @@ -1169,20 +1169,20 @@ func (s *CommentMimicSuite) TestCommentAllExportedFuncs() { var ( flagSets = []map[string]bool{ { - "comment-exported": false, - "comment-all-exported": true, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: true, // Turn off as this will be testing some exported interfaces that we // don't want to comment. - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, }, { - "comment-exported": true, - "comment-all-exported": true, + commentmimic.CommentExportedFuncsFlag: true, + commentmimic.CommentAllExportedFuncsFlag: true, // Turn off as this will be testing some exported interfaces that we // don't want to comment. - "comment-interfaces": false, - "comment-structs": false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: false, }, } @@ -1304,10 +1304,10 @@ func (s *CommentMimicSuite) TestCommentExportedEmptyInterfaces() { var ( flags = map[string]bool{ - "comment-exported": false, - "comment-all-exported": false, - "comment-interfaces": true, - "comment-structs": false, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: true, + commentmimic.CommentStructsFlag: false, } ifaceCases = []templateData{ @@ -1411,10 +1411,10 @@ func (s *CommentMimicSuite) TestCommentExportedStructs() { var ( flags = map[string]bool{ - "comment-exported": false, - "comment-all-exported": false, - "comment-interfaces": false, - "comment-structs": true, + commentmimic.CommentExportedFuncsFlag: false, + commentmimic.CommentAllExportedFuncsFlag: false, + commentmimic.CommentInterfacesFlag: false, + commentmimic.CommentStructsFlag: true, } baseStructCases = []templateData{