Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.ast: add CommentGroup.Raw function #66868

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/go/ast/ast.go
Expand Up @@ -8,6 +8,7 @@ package ast

import (
"go/token"
"runtime"
"strings"
)

Expand Down Expand Up @@ -88,6 +89,27 @@ func stripTrailingWhitespace(s string) string {
return s[0:i]
}

// Raw returns the raw (original) text of the comment.
// It includes the comment markers (//, /*, and */).
// It adds a newline at the end of the comment.
func (g *CommentGroup) Raw() string {
if g == nil {
return ""
}
var (
b strings.Builder
ls = "\n"
)
if runtime.GOOS == "windows" {
ls = "\r\n"
}

for _, c := range g.List {
b.WriteString(c.Text + ls)
}
return b.String()
}

// Text returns the text of the comment.
// Comment markers (//, /*, and */), the first space of a line comment, and
// leading and trailing empty lines are removed.
Expand Down
55 changes: 53 additions & 2 deletions src/go/ast/ast_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

var comments = []struct {
var commentsText = []struct {
list []string
text string
}{
Expand Down Expand Up @@ -39,7 +39,7 @@ var comments = []struct {
}

func TestCommentText(t *testing.T) {
for i, c := range comments {
for i, c := range commentsText {
list := make([]*Comment, len(c.list))
for i, s := range c.list {
list[i] = &Comment{Text: s}
Expand All @@ -52,6 +52,57 @@ func TestCommentText(t *testing.T) {
}
}

var commentsRaw = []struct {
list []string
raw string
}{
{[]string{"//"}, "//\n"},
{[]string{"// "}, "// \n"},
{[]string{"//", "//", "// "}, "//\n//\n// \n"},
{[]string{"// foo "}, "// foo \n"},
{[]string{"//", "//", "// foo"}, "//\n//\n// foo\n"},
{[]string{"// foo bar "}, "// foo bar \n"},
{[]string{"// foo", "// bar"}, "// foo\n// bar\n"},
{[]string{"// foo", "//", "//", "//", "// bar"},
"// foo\n//\n//\n//\n// bar\n"},
{[]string{"// foo", "/* bar */"}, "// foo\n/* bar */\n"},
{[]string{"//", "//", "//", "// foo", "//", "//", "//"},
"//\n//\n//\n// foo\n//\n//\n//\n"},

{[]string{"/**/"}, "/**/\n"},
{[]string{"/* */"}, "/* */\n"},
{[]string{"/**/", "/**/", "/* */"},
"/**/\n/**/\n/* */\n"},
{[]string{"/* Foo */"}, "/* Foo */\n"},
{[]string{"/* Foo Bar */"}, "/* Foo Bar */\n"},
{[]string{"/* Foo*/", "/* Bar*/"}, "/* Foo*/\n/* Bar*/\n"},
{[]string{"/* Foo*/", "/**/", "/**/", "/**/", "// Bar"},
"/* Foo*/\n/**/\n/**/\n/**/\n// Bar\n"},
{[]string{"/* Foo*/", "/*\n*/", "//", "/*\n*/", "// Bar"},
"/* Foo*/\n/*\n*/\n//\n/*\n*/\n// Bar\n"},
{[]string{"/* Foo*/", "// Bar"}, "/* Foo*/\n// Bar\n"},
{[]string{"/* Foo\n Bar*/"}, "/* Foo\n Bar*/\n"},

{[]string{"// foo", "//go:noinline", "// bar", "//:baz"},
"// foo\n//go:noinline\n// bar\n//:baz\n"},
{[]string{"// foo", "//lint123:ignore", "// bar"},
"// foo\n//lint123:ignore\n// bar\n"},
}

func TestCommentRaw(t *testing.T) {
for i, c := range commentsRaw {
list := make([]*Comment, len(c.list))
for i, s := range c.list {
list[i] = &Comment{Text: s}
}

text := (&CommentGroup{list}).Raw()
if text != c.raw {
t.Errorf("case %d: got %q; expected %q", i, text, c.raw)
}
}
}

var isDirectiveTests = []struct {
in string
ok bool
Expand Down