Skip to content

Commit

Permalink
all: remove last usages of ast.Node.{Comments,AddComment}
Browse files Browse the repository at this point in the history
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 <[email protected]>
Change-Id: Ib1ab7bd84da50cd7ee07e54c338273b91c5df0c1
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194709
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
NoamTD authored and mvdan committed May 14, 2024
1 parent b3006ad commit 4ae7530
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cue/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
Expand All @@ -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])
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cue/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions encoding/protobuf/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/astinternal/debugstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 4ae7530

Please sign in to comment.