-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
347fd1a
commit 010b800
Showing
3 changed files
with
58 additions
and
2 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Package todo contains the necessary logic for the todo linter. | ||
package todo | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
// doc defines the help text for the todo linter. | ||
const doc = `Ensures that each TODO comment defined in the codebase conforms to one of the | ||
following formats: TODO(<gh-user>)[<jira-ticket>]: <summary> or TODO[<jira-ticket>]: <summary>` | ||
|
||
// Analyzer exports the todo analyzer (linter). | ||
var Analyzer = analysis.Analyzer{ | ||
Name: "todo", | ||
Doc: doc, | ||
Run: todo, | ||
} | ||
|
||
// Regular expression variable block for the todo linter. | ||
var ( | ||
// reTodo is the regular expression that matches the required TODO format by this | ||
// linter. | ||
// | ||
// For examples, see: https://regex101.com/r/vsbdEm/1 | ||
reTodo = regexp.MustCompile(`^TODO(\((?P<ghUser>[\w-]+)\))?\[(?P<jiraTicket>[A-Z]+-\d+)\]: .+$`) | ||
|
||
// These subexpression indexes are placeholders just incase they're ever needed to | ||
// be used for whatever reason in the future. | ||
_ = reTodo.SubexpIndex("ghUser") | ||
_ = reTodo.SubexpIndex("jiraTicket") | ||
) | ||
|
||
// todo is the function that gets passed to the Analyzer which runs the actual | ||
// analysis for the todo linter on a set of files. | ||
func todo(pass *analysis.Pass) (interface{}, error) { | ||
for _, file := range pass.Files { | ||
for _, commentGroup := range file.Comments { | ||
for _, comment := range commentGroup.List { | ||
text := strings.TrimSpace(strings.TrimPrefix(comment.Text, "//")) | ||
|
||
if strings.HasPrefix(text, "TODO") { | ||
if !reTodo.MatchString(text) { | ||
pass.Reportf(comment.Pos(), "TODO comment must match one of the required formats: TODO(<gh-user>)[<jira-ticket>]: <summary> or TODO[<jira-ticket>]: <summary>") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil, nil | ||
} |