Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring runComment in mod.go #194

Merged
merged 9 commits into from
Oct 25, 2022
73 changes: 49 additions & 24 deletions internal/mcheck/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,74 @@ 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://`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would treat that as code comment (i.e. just indent the line)

//
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
func runComment(pass *analysis.Pass) (interface{}, error) {
fileLoop:
for _, file := range pass.Files {
isFirst := true
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
}

Expand Down
2 changes: 2 additions & 0 deletions services/dkg/pedersen/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)},
Expand Down
1 change: 1 addition & 0 deletions services/dkg/pedersen/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func TestPedersen_Setup(t *testing.T) {
},
context: serdecontext,
formFac: formFac,
status: &dkg.Status{},
}

// Wrong formID
Expand Down