-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eac868c
Refactoring runComment in mod.go :reduced its cognitive complexity an…
mamine2207 cee1b05
Merge remote-tracking branch 'origin/main' into amine
mamine2207 c441271
fix dkg/pedersen tests
mamine2207 ae79b29
Merge branch 'main' into amine
mamine2207 1fd0a6d
fixed bug
mamine2207 1fe41d8
Comments corrected
mamine2207 2234396
Merge branch 'main' into amine
mamine2207 997498d
comment shortened
mamine2207 35c6820
Merge branch 'amine' of https://github.com/dedis/d-voting into amine
mamine2207 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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://` | ||||||
// | ||||||
// 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// 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 | ||||||
} | ||||||
|
||||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)