From eac868ce67f3f7cb49188a4e35ef29a3f5ce7e52 Mon Sep 17 00:00:00 2001 From: mamine2207 Date: Tue, 18 Oct 2022 16:23:41 +0200 Subject: [PATCH 1/5] Refactoring runComment in mod.go :reduced its cognitive complexity and added comments --- internal/mcheck/mod.go | 73 ++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/internal/mcheck/mod.go b/internal/mcheck/mod.go index 51de2a0f9..6184f5118 100644 --- a/internal/mcheck/mod.go +++ b/internal/mcheck/mod.go @@ -47,7 +47,50 @@ func main() { ) } -// run parses all the comments in ast.File +// iterateOverLines iterates over the lines of a comment and +// checks if the line is too long or contains a prefix +// that should be ignored +func iterateOverLines(pass *analysis.Pass, lines []string, c *ast.Comment) { + for j := 0; j < len(lines); j++ { + line := lines[j] + if checkPrefixes(line) { + continue + } + ifTooLong(line, pass, c) + if strings.HasPrefix(line, NoLint) { + // Skip next comment for block comment. + j++ + } + } +} + +// checkPrefixes checks if the line starts with any of the following +// prefixes: +// +// - `//go:generate` +// - `// http://` +// - `// https://` +// +// If it does, it returns `true`. Otherwise, it returns `false` +func checkPrefixes(line string) bool { + return strings.HasPrefix(line, "//go:generate") || + strings.HasPrefix(line, "// http://") || + strings.HasPrefix(line, "// https://") +} + +// ifTooLong reports a comment if it's too long +func ifTooLong(line string, pass *analysis.Pass, c *ast.Comment) { + if len(line) > MaxLen { + pass.Reportf( // `c` is a comment. + c.Pos(), "Comment too long: %s (%d)", + line, len(line)) + } +} + +// It loops over all the files in the package, and for each file it loops +// over all the comments in the file, and for each comment it loops over +// all the lines in the comment, and for each line it checks +// if the line is too long func runComment(pass *analysis.Pass) (interface{}, error) { fileLoop: for _, file := range pass.Files { @@ -55,41 +98,23 @@ fileLoop: for _, cg := range file.Comments { for i := 0; i < len(cg.List); i++ { c := cg.List[i] - + // Checking if the first comment starts with "// Code generated" + //and if it does, it continues the + // fileLoop and changes the file if isFirst && strings.HasPrefix(c.Text, "// Code generated") { continue fileLoop } // in case of /* */ comment there might be multiple lines lines := strings.Split(c.Text, "\n") - for j := 0; j < len(lines); j++ { - line := lines[j] - - if strings.HasPrefix(line, "//go:generate") { - continue - } - if strings.HasPrefix(line, "// http://") || strings.HasPrefix(line, "// https://") { - continue - } - if len(line) > MaxLen { - pass.Reportf(c.Pos(), "Comment too long: %s (%d)", - line, len(line)) - } - if strings.HasPrefix(line, NoLint) { - // Skip next comment for block comment. - j++ - } - } - + iterateOverLines(pass, lines, c) isFirst = false - if strings.HasPrefix(c.Text, NoLint) { - // Skip next comment for one-line comment. + // Skip next comment for block comment. i++ } } } } - return nil, nil } From c44127146794cc68e42fb594c2ef5064eeb40c7e Mon Sep 17 00:00:00 2001 From: mamine2207 Date: Mon, 24 Oct 2022 11:40:47 +0200 Subject: [PATCH 2/5] fix dkg/pedersen tests --- services/dkg/pedersen/handler_test.go | 2 ++ services/dkg/pedersen/mod_test.go | 1 + 2 files changed, 3 insertions(+) diff --git a/services/dkg/pedersen/handler_test.go b/services/dkg/pedersen/handler_test.go index a60d789d0..7747c60d0 100644 --- a/services/dkg/pedersen/handler_test.go +++ b/services/dkg/pedersen/handler_test.go @@ -13,6 +13,7 @@ import ( "go.dedis.ch/dela/serde/json" "github.com/dedis/d-voting/internal/testing/fake" + "github.com/dedis/d-voting/services/dkg" "github.com/dedis/d-voting/services/dkg/pedersen/types" "github.com/stretchr/testify/require" "go.dedis.ch/dela/mino" @@ -107,6 +108,7 @@ func TestHandler_Start(t *testing.T) { h := Handler{ startRes: &state{}, privKey: privKey, + status: &dkg.Status{}, } start := types.NewStart( []mino.Address{fake.NewAddress(0)}, diff --git a/services/dkg/pedersen/mod_test.go b/services/dkg/pedersen/mod_test.go index cf4b70a4d..4a78007ec 100644 --- a/services/dkg/pedersen/mod_test.go +++ b/services/dkg/pedersen/mod_test.go @@ -338,6 +338,7 @@ func TestPedersen_Setup(t *testing.T) { }, context: serdecontext, electionFac: electionFac, + status: &dkg.Status{}, } // Wrong electionID From 1fd0a6d5887e432700d2976679c875aef428c09d Mon Sep 17 00:00:00 2001 From: mamine2207 Date: Mon, 24 Oct 2022 15:32:29 +0200 Subject: [PATCH 3/5] fixed bug --- services/dkg/pedersen/mod_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/services/dkg/pedersen/mod_test.go b/services/dkg/pedersen/mod_test.go index 1798d7b53..620225ef5 100644 --- a/services/dkg/pedersen/mod_test.go +++ b/services/dkg/pedersen/mod_test.go @@ -336,10 +336,9 @@ func TestPedersen_Setup(t *testing.T) { handler: &Handler{ startRes: &state{}, }, - context: serdecontext, - electionFac: electionFac, - status: &dkg.Status{}, - + context: serdecontext, + formFac: formFac, + status: &dkg.Status{}, } // Wrong formID From 1fe41d80f559bcc912effbcd0e2e8f7dce152374 Mon Sep 17 00:00:00 2001 From: mamine2207 Date: Mon, 24 Oct 2022 17:51:37 +0200 Subject: [PATCH 4/5] Comments corrected --- internal/mcheck/mod.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/internal/mcheck/mod.go b/internal/mcheck/mod.go index 6184f5118..d1e44924b 100644 --- a/internal/mcheck/mod.go +++ b/internal/mcheck/mod.go @@ -67,9 +67,9 @@ func iterateOverLines(pass *analysis.Pass, lines []string, c *ast.Comment) { // checkPrefixes checks if the line starts with any of the following // prefixes: // -// - `//go:generate` -// - `// http://` -// - `// https://` +// `go:generate` +// `http://` +// `https://` // // If it does, it returns `true`. Otherwise, it returns `false` func checkPrefixes(line string) bool { @@ -87,7 +87,7 @@ func ifTooLong(line string, pass *analysis.Pass, c *ast.Comment) { } } -// It loops over all the files in the package, and for each file it loops +// runComment loops over all the files in the package, and for each file it loops // over all the comments in the file, and for each comment it loops over // all the lines in the comment, and for each line it checks // if the line is too long @@ -98,9 +98,6 @@ fileLoop: for _, cg := range file.Comments { for i := 0; i < len(cg.List); i++ { c := cg.List[i] - // Checking if the first comment starts with "// Code generated" - //and if it does, it continues the - // fileLoop and changes the file if isFirst && strings.HasPrefix(c.Text, "// Code generated") { continue fileLoop } From 997498d078ba5e83fb48e1c5ff9fae78554a1f19 Mon Sep 17 00:00:00 2001 From: mamine2207 Date: Mon, 24 Oct 2022 17:58:41 +0200 Subject: [PATCH 5/5] comment shortened --- internal/mcheck/mod.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/mcheck/mod.go b/internal/mcheck/mod.go index d1e44924b..c781587ff 100644 --- a/internal/mcheck/mod.go +++ b/internal/mcheck/mod.go @@ -87,9 +87,9 @@ func ifTooLong(line string, pass *analysis.Pass, c *ast.Comment) { } } -// runComment loops over all the files in the package, and for each file it loops -// over all the comments in the file, and for each comment it loops over -// all the lines in the comment, and for each line it checks +// runComment loops over all the files in the package, and for each file it +// loops over all the comments in the file, and for each comment it loops +// over all the lines in the comment, and for each line it checks // if the line is too long func runComment(pass *analysis.Pass) (interface{}, error) { fileLoop: