Skip to content

Commit bffe7ad

Browse files
committed
go/parser: Add TestBothLineAndLeadComment
I was wondering whether is is expected that both p.lineComment and p.leadComment might be populated at the same time. i.e. whether parser.go:275 can be changed from: if p.lineFor(p.pos) != endline || p.tok == token.SEMICOLON || p.tok == token.EOF to: if (p.tok != token.COMMENT && p.lineFor(p.pos) != endline) || p.tok == token.SEMICOLON || p.tok == token.EOF It turns out that we cannot do so. So while i am here, add a test case for that, since nothing else failed with that change. Change-Id: I6a6a6964f760237c068098db8a7b4b7aaf26c651 Reviewed-on: https://go-review.googlesource.com/c/go/+/703915 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 02a888e commit bffe7ad

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/go/parser/parser_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,53 @@ func test() {
896896
t.Fatalf("unexpected f.Comments got:\n%v\nwant:\n%v", got.String(), want.String())
897897
}
898898
}
899+
900+
// TestBothLineAndLeadComment makes sure that we populate the
901+
// p.lineComment field even though there is a comment after the
902+
// line comment.
903+
func TestBothLineAndLeadComment(t *testing.T) {
904+
const src = `package test
905+
906+
var _ int; /* line comment */
907+
// Doc comment
908+
func _() {}
909+
910+
var _ int; /* line comment */
911+
// Some comment
912+
913+
func _() {}
914+
`
915+
916+
fset := token.NewFileSet()
917+
f, _ := ParseFile(fset, "", src, ParseComments|SkipObjectResolution)
918+
919+
lineComment := f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.ValueSpec).Comment
920+
docComment := f.Decls[1].(*ast.FuncDecl).Doc
921+
922+
if lineComment == nil {
923+
t.Fatal("missing line comment")
924+
}
925+
if docComment == nil {
926+
t.Fatal("missing doc comment")
927+
}
928+
929+
if lineComment.List[0].Text != "/* line comment */" {
930+
t.Errorf(`unexpected line comment got = %q; want "/* line comment */"`, lineComment.List[0].Text)
931+
}
932+
if docComment.List[0].Text != "// Doc comment" {
933+
t.Errorf(`unexpected line comment got = %q; want "// Doc comment"`, docComment.List[0].Text)
934+
}
935+
936+
lineComment2 := f.Decls[2].(*ast.GenDecl).Specs[0].(*ast.ValueSpec).Comment
937+
if lineComment2 == nil {
938+
t.Fatal("missing line comment")
939+
}
940+
if lineComment.List[0].Text != "/* line comment */" {
941+
t.Errorf(`unexpected line comment got = %q; want "/* line comment */"`, lineComment.List[0].Text)
942+
}
943+
944+
docComment2 := f.Decls[3].(*ast.FuncDecl).Doc
945+
if docComment2 != nil {
946+
t.Errorf("unexpected doc comment %v", docComment2)
947+
}
948+
}

0 commit comments

Comments
 (0)