diff --git a/internal/mcheck/mod.go b/internal/mcheck/mod.go index 51de2a0f9..c781587ff 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)) + } +} + +// 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: for _, file := range pass.Files { @@ -55,41 +98,20 @@ fileLoop: for _, cg := range file.Comments { for i := 0; i < len(cg.List); i++ { c := cg.List[i] - 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 } diff --git a/services/dkg/pedersen/mod_test.go b/services/dkg/pedersen/mod_test.go index 70825133f..e90d47280 100644 --- a/services/dkg/pedersen/mod_test.go +++ b/services/dkg/pedersen/mod_test.go @@ -339,6 +339,7 @@ func TestPedersen_Setup(t *testing.T) { context: serdecontext, formFac: formFac, status: &dkg.Status{}, + } // Wrong formID