Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions build/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ var keywordToken = map[string]int{
// order walks the expression adding it and its subexpressions to the
// preorder and postorder lists.
func (in *input) order(v Expr) {
if v != nil {
if len(in.lineComments) > 0 && v != nil {
in.pre = append(in.pre, v)
}
switch v := v.(type) {
Expand Down Expand Up @@ -832,17 +832,29 @@ func (in *input) order(v Expr) {
in.order(s)
}
}
if v != nil {
if len(in.suffixComments) > 0 && v != nil {
in.post = append(in.post, v)
}
}

// assignComments attaches comments to nearby syntax.
func (in *input) assignComments() {
// Generate preorder and postorder lists.
// Line comments are attached using the preorder list, suffix comments using
// the postorder list (order() builds only the list(s) for the comment kinds
// present). If the file has neither kind, skip the whole-tree walk entirely.
hasLine := len(in.lineComments) > 0
hasSuffix := len(in.suffixComments) > 0
if !hasLine && !hasSuffix {
return
}

in.order(in.file)
in.assignSuffixComments()
in.assignLineComments()
if hasSuffix {
in.assignSuffixComments()
}
if hasLine {
in.assignLineComments()
}
}

func (in *input) assignSuffixComments() {
Expand Down