Remove all comments from Go source code.
SYNOPSIS
Install the command.
go install github.com/shogo82148/go-comment-eraser@latest
go-comment-eraser $PATH_TO_SOURCE_DIRECTORY
Examples
go-comment-eraser github.com/shogo82148/go-comment-eraser
diff --git a/main.go b/main.go
index 5087d08..913debc 100644
--- a/main.go
+++ b/main.go
@@ -65,22 +65,18 @@ func eraseComment(src string) error {
return nil
}
-// parseFile parses the Go source code file and returns the Go source
-// that is modified to erase all comments.
func parseFile(src string) ([]byte, error) {
- // Parse the Go source code file
+
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, src, nil, parser.ParseComments)
if err != nil {
return nil, err
}
- // If the AST is generated, just copy the file
if ast.IsGenerated(node) {
return nil, errSkipErase
}
- // Erase all comments from the AST
ast.Inspect(node, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.CommentGroup:
@@ -109,7 +105,6 @@ func parseFile(src string) ([]byte, error) {
}
node.Comments = list
- // Print the modified Go source code
var buf bytes.Buffer
if err := format.Node(&buf, fset, node); err != nil {
return nil, err