Skip to content

Commit

Permalink
reduce parseFile complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
cuonglm committed Jul 22, 2019
1 parent e7d8f4c commit e49fb69
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions parse.go
Expand Up @@ -15,13 +15,10 @@ func parseFile(fset *token.FileSet, path, template string) (*ast.File, error) {
nil,
parser.ParseComments|parser.AllErrors,
)

if err != nil {
return nil, err
}

commentTemplate := commentBase + template

// Inject first comment to prevent nil comment map
if len(af.Comments) == 0 {
af.Comments = []*ast.CommentGroup{{List: []*ast.Comment{{Slash: -1, Text: "// gocmt"}}}}
Expand All @@ -30,6 +27,11 @@ func parseFile(fset *token.FileSet, path, template string) (*ast.File, error) {
af.Comments = af.Comments[1:]
}()
}

return buildComments(af, commentBase+template)
}

func buildComments(af *ast.File, commentTemplate string) (*ast.File, error) {
cmap := ast.NewCommentMap(fset, af, af.Comments)

for _, d := range af.Decls {
Expand Down

0 comments on commit e49fb69

Please sign in to comment.