From 4ae7530de148cfc79b78e2438a00656e4f0d32b1 Mon Sep 17 00:00:00 2001 From: Noam Dolovich Date: Tue, 14 May 2024 16:00:26 +0300 Subject: [PATCH] all: remove last usages of ast.Node.{Comments,AddComment} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL removes the last remaining usages of the deprecated ast.Node.Comments and ast.Node.AddComment methods in the cue codebase. Updates #2480. Signed-off-by: Noam Dolovich Change-Id: Ib1ab7bd84da50cd7ee07e54c338273b91c5df0c1 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194709 Reviewed-by: Daniel Martí Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo --- cmd/cue/cmd/get_go.go | 4 ++-- cue/format/format.go | 2 +- cue/format/node.go | 8 ++++---- cue/load/loader_test.go | 14 ++++++++++---- cue/parser/parser.go | 6 +++--- encoding/protobuf/util.go | 4 ++-- internal/astinternal/debugstr.go | 2 +- 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go index bd788b15ab8..fdc1d13ddf2 100644 --- a/cmd/cue/cmd/get_go.go +++ b/cmd/cue/cmd/get_go.go @@ -802,7 +802,7 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) { b.Value = val } if b.Value != val { - cv.AddComment(internal.NewComment(false, val)) + cueast.AddComment(cv, internal.NewComment(false, val)) } } @@ -859,7 +859,7 @@ func (e *extractor) altType(typ types.Type) cueast.Expr { func addDoc(g *ast.CommentGroup, x cueast.Node) bool { doc := makeDoc(g, true) if doc != nil { - x.AddComment(doc) + cueast.AddComment(x, doc) return true } return false diff --git a/cue/format/format.go b/cue/format/format.go index d4ae93203aa..a4008285c38 100644 --- a/cue/format/format.go +++ b/cue/format/format.go @@ -282,7 +282,7 @@ func (f *formatter) before(node ast.Node) bool { if ok && len(s.Elts) <= 1 && f.current.nodeSep != blank && f.onOneLine(node) { f.current.nodeSep = blank } - f.current.cg = node.Comments() + f.current.cg = ast.Comments(node) f.visitComments(f.current.pos) return true } diff --git a/cue/format/node.go b/cue/format/node.go index 7b675f06ad3..03115df2640 100644 --- a/cue/format/node.go +++ b/cue/format/node.go @@ -87,16 +87,16 @@ func nestDepth(f *ast.Field) int { // TODO: be more accurate and move to astutil func hasDocComments(d ast.Decl) bool { - if len(d.Comments()) > 0 { + if len(ast.Comments(d)) > 0 { return true } switch x := d.(type) { case *ast.Field: - return len(x.Label.Comments()) > 0 + return len(ast.Comments(x.Label)) > 0 case *ast.Alias: - return len(x.Ident.Comments()) > 0 + return len(ast.Comments(x.Ident)) > 0 case *ast.LetClause: - return len(x.Ident.Comments()) > 0 + return len(ast.Comments(x.Ident)) > 0 } return false } diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go index ae7cfd8ca2b..410a77769fc 100644 --- a/cue/load/loader_test.go +++ b/cue/load/loader_test.go @@ -27,6 +27,7 @@ import ( "github.com/go-quicktest/qt" "cuelang.org/go/cue" + "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/errors" "cuelang.org/go/cue/format" "cuelang.org/go/internal/cueexperiment" @@ -424,9 +425,14 @@ func TestOverlays(t *testing.T) { } return r } - for i, inst := range cue.Build(Instances([]string{"./dir/..."}, c)) { - if inst.Err != nil { - t.Error(inst.Err) + ctx := cuecontext.New() + insts, err := ctx.BuildInstances(Instances([]string{"./dir/..."}, c)) + if err != nil { + t.Fatal(err) + } + for i, inst := range insts { + if err := inst.Err(); err != nil { + t.Error(err) continue } b, err := format.Node(inst.Value().Syntax(cue.Final())) @@ -435,7 +441,7 @@ func TestOverlays(t *testing.T) { continue } if got := string(bytes.Map(rmSpace, b)); got != want[i] { - t.Errorf("%s: got %s; want %s", inst.Dir, got, want[i]) + t.Errorf("%s: got %s; want %s", inst.BuildInstance().Dir, got, want[i]) } } } diff --git a/cue/parser/parser.go b/cue/parser/parser.go index b52ce21afca..27bcf1a98f1 100644 --- a/cue/parser/parser.go +++ b/cue/parser/parser.go @@ -114,7 +114,7 @@ func (p *parser) openComments() *commentState { groups = append(groups, cg) } } - groups = append(groups, c.lastChild.Comments()...) + groups = append(groups, ast.Comments(c.lastChild)...) for _, cg := range c.groups { if cg.Position != 0 { cg.Position = c.lastPos @@ -165,7 +165,7 @@ func (p *parser) closeList() { if c.lastChild != nil { for _, cg := range c.groups { cg.Position = c.lastPos - c.lastChild.AddComment(cg) + ast.AddComment(c.lastChild, cg) } c.groups = nil } @@ -206,7 +206,7 @@ func (c *commentState) closeNode(p *parser, n ast.Node) ast.Node { for _, cg := range c.groups { if n != nil { if cg != nil { - n.AddComment(cg) + ast.AddComment(n, cg) } } } diff --git a/encoding/protobuf/util.go b/encoding/protobuf/util.go index def05fb1812..13afa50326c 100644 --- a/encoding/protobuf/util.go +++ b/encoding/protobuf/util.go @@ -49,8 +49,8 @@ func addComments(f ast.Node, i int, doc, inline *proto.Comment) bool { if cg != nil && len(cg.List) > 0 && i > 0 { cg.List[0].Slash = newSection } - f.AddComment(cg) - f.AddComment(comment(inline, false)) + ast.AddComment(f, cg) + ast.AddComment(f, comment(inline, false)) return doc != nil } diff --git a/internal/astinternal/debugstr.go b/internal/astinternal/debugstr.go index 81cba96659f..83091af5c14 100644 --- a/internal/astinternal/debugstr.go +++ b/internal/astinternal/debugstr.go @@ -27,7 +27,7 @@ import ( func DebugStr(x interface{}) (out string) { if n, ok := x.(ast.Node); ok { comments := "" - for _, g := range n.Comments() { + for _, g := range ast.Comments(n) { comments += DebugStr(g) } if comments != "" {