You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Even in the presence of incorrect position information, the go/printer should always
format //-style comments with a newline at the end. Otherwise, illegal Go programs can
be the result. The following is a test case.
package main
import (
"bytes"
"fmt"
"go/parser"
"go/printer"
"go/token"
)
const src =
`// comment 1
// comment 2
// comment 3
package main
`
func newlines(s string) (n int) {
for _, b := range s {
if b == '\n' {
n++
}
}
return
}
func main() {
fset := token.NewFileSet()
ast1, err1 := parser.ParseFile(fset, "", src, parser.ParseComments)
if err1 != nil {
panic(err1)
}
var buf bytes.Buffer
// Comments are not properly terminated when using a new (empty)
// file set. Without the assignment below, the code works.
fset = token.NewFileSet()
printer.Fprint(&buf, fset, ast1)
nsrc := newlines(src)
nbuf := newlines(buf.String())
if nsrc != nbuf {
fmt.Printf("got %d, expected %d\n", nbuf, nsrc)
}
}
The text was updated successfully, but these errors were encountered:
fwiw the test case from earlier today had two problems.
in addition to not printing the final \n on the comment,
it was deciding to insert a comment after the fmt in fmt.Printf,
but that's not a valid place to insert a \n in go.
the closest valid place would have been after the dot.
The text was updated successfully, but these errors were encountered: