diff --git a/cue/ast/astutil/sanitize_test.go b/cue/ast/astutil/sanitize_test.go index b6fe9b144..cb54a29b9 100644 --- a/cue/ast/astutil/sanitize_test.go +++ b/cue/ast/astutil/sanitize_test.go @@ -34,7 +34,7 @@ func TestSanitize(t *testing.T) { desc: "Take existing import and rename it", file: func() *ast.File { spec := ast.NewImport(nil, "list") - spec.AddComment(internal.NewComment(true, "will be renamed")) + ast.AddComment(spec, internal.NewComment(true, "will be renamed")) return &ast.File{Decls: []ast.Decl{ &ast.ImportDecl{Specs: []*ast.ImportSpec{spec}}, &ast.EmbedDecl{ diff --git a/cue/format/import.go b/cue/format/import.go index 873de2c7f..7fef6a97e 100644 --- a/cue/format/import.go +++ b/cue/format/import.go @@ -61,7 +61,7 @@ func setRelativePos(s *ast.ImportSpec, r token.RelPos) { } func hasDoc(s *ast.ImportSpec) bool { - for _, doc := range s.Comments() { + for _, doc := range ast.Comments(s) { if doc.Doc { return true } @@ -86,7 +86,7 @@ func importName(s *ast.ImportSpec) string { } func importComment(s *ast.ImportSpec) string { - for _, c := range s.Comments() { + for _, c := range ast.Comments(s) { if c.Line { return c.Text() } @@ -99,7 +99,7 @@ func collapse(prev, next *ast.ImportSpec) bool { if importPath(next) != importPath(prev) || importName(next) != importName(prev) { return false } - for _, c := range prev.Comments() { + for _, c := range ast.Comments(prev) { if !c.Doc { return false } diff --git a/cue/format/node.go b/cue/format/node.go index 91503f74e..24783a08d 100644 --- a/cue/format/node.go +++ b/cue/format/node.go @@ -423,7 +423,7 @@ func (f *formatter) decl(decl ast.Decl) { break } switch { - case len(n.Specs) == 1 && len(n.Specs[0].Comments()) == 0: + case len(n.Specs) == 1 && len(ast.Comments(n.Specs[0])) == 0: if !n.Lparen.IsValid() { f.print(blank) f.walkSpecList(n.Specs) @@ -715,7 +715,7 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) { case len(x.Elts) == 0: // collapse curly braces if the body is empty. ffAlt := blank | nooverride - for _, c := range x.Comments() { + for _, c := range ast.Comments(x) { if c.Position == 1 { ffAlt = ff break @@ -745,7 +745,7 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) { if len(x.Elts) == 0 { // collapse square brackets if the body is empty. collapseWs := blank | nooverride - for _, c := range x.Comments() { + for _, c := range ast.Comments(x) { if c.Position == 1 { collapseWs = ws break diff --git a/cue/types.go b/cue/types.go index c7db3e503..3d9dd4033 100644 --- a/cue/types.go +++ b/cue/types.go @@ -965,14 +965,14 @@ outer: for _, c := range ast.Comments(e.Expr) { ast.AddComment(f, c) } - ast.SetComments(e.Expr, f.Comments()) + ast.SetComments(e.Expr, ast.Comments(f)) return e.Expr } } st := &ast.StructLit{ Elts: f.Decls, } - ast.SetComments(st, f.Comments()) + ast.SetComments(st, ast.Comments(f)) return st } diff --git a/encoding/jsonschema/constraints_object.go b/encoding/jsonschema/constraints_object.go index 764aeda0a..02240d109 100644 --- a/encoding/jsonschema/constraints_object.go +++ b/encoding/jsonschema/constraints_object.go @@ -392,10 +392,10 @@ func constraintProperties(key string, n cue.Value, s *state) { f.Value = ast.NewString(s.k8sAPIVersion) hasAPIVersion = true } - if len(obj.Elts) > 0 && len(f.Comments()) > 0 { + if len(obj.Elts) > 0 && len(ast.Comments(f)) > 0 { // TODO: change formatter such that either a NewSection on the // field or doc comment will cause a new section. - ast.SetRelPos(f.Comments()[0], token.NewSection) + ast.SetRelPos(ast.Comments(f)[0], token.NewSection) } if state.deprecated { switch expr.(type) { diff --git a/encoding/openapi/decode.go b/encoding/openapi/decode.go index 5a6fea05e..71a37ee1b 100644 --- a/encoding/openapi/decode.go +++ b/encoding/openapi/decode.go @@ -73,7 +73,7 @@ func Extract(data cue.InstanceOrValue, c *Config) (*ast.File, error) { if c.PkgName != "" { p := &ast.Package{Name: ast.NewIdent(c.PkgName)} - p.AddComment(cg) + ast.AddComment(p, cg) add(p) } else if cg != nil { add(cg) diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go index 56c2a1a3f..10850ad8d 100644 --- a/encoding/protobuf/parse.go +++ b/encoding/protobuf/parse.go @@ -706,7 +706,7 @@ func (p *protoConverter) oneOf(x *proto.Oneof) { s := ast.NewStruct() ast.SetRelPos(s, token.Newline) embed := &ast.EmbedDecl{Expr: s} - embed.AddComment(comment(x.Comment, true)) + ast.AddComment(embed, comment(x.Comment, true)) p.addDecl(embed) diff --git a/internal/core/compile/compile.go b/internal/core/compile/compile.go index 491ac37d0..9fdf1a514 100644 --- a/internal/core/compile/compile.go +++ b/internal/core/compile/compile.go @@ -810,7 +810,7 @@ func (c *compiler) decl(d ast.Decl) adt.Decl { for _, c := range ast.Comments(x.Expr) { ast.AddComment(x, c) } - ast.SetComments(x.Expr, x.Comments()) + ast.SetComments(x.Expr, ast.Comments(x)) return c.expr(x.Expr) case ast.Expr: diff --git a/internal/core/export/export.go b/internal/core/export/export.go index b87e10f2e..7afd071c3 100644 --- a/internal/core/export/export.go +++ b/internal/core/export/export.go @@ -224,12 +224,12 @@ func (e *exporter) toFile(v *adt.Vertex, x ast.Expr) *ast.File { if pkgName != "" { pkg.Name = ast.NewIdent(pkgName) fout.Decls = append(fout.Decls, pkg) - ast.SetComments(pkg, mergeDocs(pkg.Comments())) + ast.SetComments(pkg, mergeDocs(ast.Comments(pkg))) } else { - for _, c := range fout.Comments() { + for _, c := range ast.Comments(fout) { ast.AddComment(pkg, c) } - ast.SetComments(fout, mergeDocs(pkg.Comments())) + ast.SetComments(fout, mergeDocs(ast.Comments(pkg))) } } diff --git a/internal/core/export/extract.go b/internal/core/export/extract.go index ce7e3ddb9..26f3d1b25 100644 --- a/internal/core/export/extract.go +++ b/internal/core/export/extract.go @@ -49,7 +49,7 @@ func extractDocs(v *adt.Vertex) (docs []*ast.CommentGroup) { continue } fields = append(fields, f) - for _, cg := range f.Comments() { + for _, cg := range ast.Comments(f) { if !containsDoc(docs, cg) && cg.Doc { docs = append(docs, cg) } @@ -76,7 +76,7 @@ func extractDocs(v *adt.Vertex) (docs []*ast.CommentGroup) { for _, child := range fields { if nested == child { newFields = append(newFields, f) - for _, cg := range f.Comments() { + for _, cg := range ast.Comments(f) { if !containsDoc(docs, cg) && cg.Doc { docs = append(docs, cg) } diff --git a/internal/core/export/value.go b/internal/core/export/value.go index b1695a087..d9315bf6a 100644 --- a/internal/core/export/value.go +++ b/internal/core/export/value.go @@ -238,7 +238,7 @@ func (e *exporter) bottom(n *adt.Bottom) *ast.BottomLit { if x := n.Err; x != nil { msg := x.Error() comment := &ast.Comment{Text: "// " + msg} - err.AddComment(&ast.CommentGroup{ + ast.AddComment(err, &ast.CommentGroup{ Line: true, Position: 2, List: []*ast.Comment{comment}, diff --git a/internal/internal.go b/internal/internal.go index b65a4cc0f..298c023b4 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -181,10 +181,10 @@ func FileComments(f *ast.File) (docs, rest []*ast.CommentGroup) { hasPkg := false if pkg, _ := Package(f); pkg != nil { hasPkg = true - docs = pkg.Comments() + docs = ast.Comments(pkg) } - for _, c := range f.Comments() { + for _, c := range ast.Comments(f) { if c.Doc { docs = append(docs, c) } else {