this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

all: use cue/token.Pos.IsValid rather than comparing with NoPos

Using the IsValid method is clearer, and a nice side benefit
is that it's a little shorter and sometimes avoids needing an import.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I193c00c8712353486b4a0d88afda00fcec1bfab4
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1227394
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+25 -29
+2 -2
cue/ast/ast.go
··· 937 937 } 938 938 939 939 func (s *ImportSpec) End() token.Pos { 940 - if s.EndPos != token.NoPos { 940 + if s.EndPos.IsValid() { 941 941 return s.EndPos 942 942 } 943 943 return s.Path.End() ··· 1145 1145 1146 1146 func (p *Package) Pos() token.Pos { return getPos(p) } 1147 1147 func (p *Package) pos() *token.Pos { 1148 - if p.PackagePos != token.NoPos { 1148 + if p.PackagePos.IsValid() { 1149 1149 return &p.PackagePos 1150 1150 } 1151 1151 if p.Name != nil {
+4 -4
cue/errors/errors.go
··· 162 162 func comparePosWithNoPosFirst(a, b token.Pos) int { 163 163 if a == b { 164 164 return 0 165 - } else if a == token.NoPos { 165 + } else if !a.IsValid() { 166 166 return -1 167 - } else if b == token.NoPos { 167 + } else if !b.IsValid() { 168 168 return +1 169 169 } 170 170 return token.Pos.Compare(a, b) ··· 255 255 } 256 256 257 257 func (e *wrapped) Position() token.Pos { 258 - if p := e.main.Position(); p != token.NoPos { 258 + if p := e.main.Position(); p.IsValid() { 259 259 return p 260 260 } 261 261 if wrap, ok := e.wrap.(Error); ok { ··· 422 422 func approximateEqual(a, b Error) bool { 423 423 aPos := a.Position() 424 424 bPos := b.Position() 425 - if aPos == token.NoPos || bPos == token.NoPos { 425 + if !aPos.IsValid() || !bPos.IsValid() { 426 426 return a.Error() == b.Error() 427 427 } 428 428 return aPos.Compare(bPos) == 0 && slices.Compare(a.Path(), b.Path()) == 0
+3 -4
cue/token/position.go
··· 141 141 func (p Pos) Compare(p2 Pos) int { 142 142 if p == p2 { 143 143 return 0 144 - } else if p == NoPos { 144 + } else if !p.IsValid() { 145 145 return +1 146 - } else if p2 == NoPos { 146 + } else if !p2.IsValid() { 147 147 return -1 148 148 } 149 149 // Avoid calling [Pos.Position] as it also unpacks line and column info; ··· 229 229 // meaning either a printable file position to obtain via [Pos.Position], 230 230 // and/or a relative position to obtain via [Pos.RelPos]. 231 231 func (p Pos) IsValid() bool { 232 - // TODO(mvdan): replace manual NoPos checks with IsValid 233 232 return p != NoPos 234 233 } 235 234 ··· 583 582 // //line comments; otherwise those comments are ignored. 584 583 // p must be a Pos value in f or NoPos. 585 584 func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) { 586 - if p != NoPos { 585 + if p.IsValid() { 587 586 pos = f.position(p, adjusted) 588 587 } 589 588 return
+2 -2
cue/types.go
··· 1032 1032 } 1033 1033 1034 1034 if src := v.Source(); src != nil { 1035 - if pos := src.Pos(); pos != token.NoPos { 1035 + if pos := src.Pos(); pos.IsValid() { 1036 1036 return pos 1037 1037 } 1038 1038 } ··· 1041 1041 for c := range v.v.LeafConjuncts() { 1042 1042 x := c.Elem() 1043 1043 pp := adt.Pos(x) 1044 - if pp == token.NoPos { 1044 + if !pp.IsValid() { 1045 1045 continue 1046 1046 } 1047 1047 p = pp
+1 -1
internal/astinternal/debug.go
··· 397 397 398 398 case *ast.ImportDecl: 399 399 out := "import " 400 - if v.Lparen != token.NoPos { 400 + if v.Lparen.IsValid() { 401 401 out += "( " 402 402 out += DebugStr(v.Specs) 403 403 out += " )"
+1 -1
internal/core/adt/context.go
··· 859 859 if b, ok := result.(*Bottom); ok { 860 860 if c.src != nil && 861 861 b.Code == CycleError && 862 - b.Err.Position() == token.NoPos && 862 + !b.Err.Position().IsValid() && 863 863 len(b.Err.InputPositions()) == 0 { 864 864 bb := *b 865 865 bb.Err = errors.Wrapf(b.Err, c.src.Pos(), "")
+2 -2
internal/core/adt/errors.go
··· 382 382 } 383 383 384 384 func appendNodePositions(a []token.Pos, n Node) []token.Pos { 385 - if p := Pos(n); p != token.NoPos { 385 + if p := Pos(n); p.IsValid() { 386 386 a = append(a, p) 387 387 } 388 388 if v, ok := n.(*Vertex); ok { ··· 426 426 // level packages. This will allow the debug packages to be used 427 427 // more widely. 428 428 b, _ := cueformat.Node(x) 429 - if p := x.Pos(); p != token.NoPos { 429 + if p := x.Pos(); p.IsValid() { 430 430 a = append(a, p) 431 431 } 432 432 args[i] = string(b)
+3 -5
internal/core/export/extract.go
··· 18 18 "slices" 19 19 20 20 "cuelang.org/go/cue/ast" 21 - "cuelang.org/go/cue/token" 22 21 "cuelang.org/go/internal" 23 22 "cuelang.org/go/internal/core/adt" 24 23 ) ··· 112 111 // nestedField returns the child field of a field shorthand. 113 112 func nestedField(f *ast.Field) *ast.Field { 114 113 s, _ := f.Value.(*ast.StructLit) 115 - if s == nil || 116 - len(s.Elts) != 1 || 117 - s.Lbrace != token.NoPos || 118 - s.Rbrace != token.NoPos { 114 + if s == nil || len(s.Elts) != 1 || 115 + s.Lbrace.IsValid() || 116 + s.Rbrace.IsValid() { 119 117 return nil 120 118 } 121 119
+2 -2
internal/core/toposort/vertex.go
··· 286 286 refs.Ref, refs.Ref, refs.Ref.Source().Pos()) 287 287 } 288 288 maps.Insert(structMetaMap(refs.Ref), maps.All(sMetas)) 289 - if pos := refs.Ref.Source().Pos(); pos != token.NoPos { 289 + if pos := refs.Ref.Source().Pos(); pos.IsValid() { 290 290 for sMeta := range nodeToStructMetas[refs.Ref] { 291 291 sMeta.pos = pos 292 292 } ··· 485 485 // same field within the same structLit 486 486 debug(" skipping 1\n") 487 487 488 - } else if exists && !sMeta.isExplicit && sMeta.pos != token.NoPos && 488 + } else if exists && !sMeta.isExplicit && sMeta.pos.IsValid() && 489 489 node.structMeta != nil && 490 490 node.structMeta.pos.Filename() == filename { 491 491 // same field within the same file during implicit unification
+4 -4
internal/lsp/definitions/definitions.go
··· 1213 1213 // positions. Existing definitions for those offsets are overwritten 1214 1214 // without warning. 1215 1215 func (n *astNode) addDefinition(start, end token.Pos, targets []*navigableBindings) { 1216 - if len(targets) == 0 || start == token.NoPos || end == token.NoPos { 1216 + if len(targets) == 0 || !start.IsValid() || !end.IsValid() { 1217 1217 return 1218 1218 } 1219 1219 ··· 1243 1243 // the existing path element so that the LSP server can send 1244 1244 // completions to the client that correctly edit the existing path. 1245 1245 func (n *astNode) addEmbedCompletions(start, end token.Pos, node *astNode, targets []*navigableBindings, startPos token.Pos) { 1246 - if (len(targets) == 0 && node == nil) || start == token.NoPos || end == token.NoPos { 1246 + if (len(targets) == 0 && node == nil) || !start.IsValid() || !end.IsValid() { 1247 1247 return 1248 1248 } 1249 1249 ··· 1271 1271 // ensure that when the LSP server suggests completions to the client, 1272 1272 // those completions correctly edit the existing field. 1273 1273 func (n *astNode) addFieldCompletions(start, end token.Pos, targets []*navigableBindings, colonPos token.Pos) { 1274 - if len(targets) == 0 || start == token.NoPos || end == token.NoPos { 1274 + if len(targets) == 0 || !start.IsValid() || !end.IsValid() { 1275 1275 return 1276 1276 } 1277 1277 ··· 1549 1549 case *fieldDeclExpr: 1550 1550 parent := n.parent 1551 1551 key := parent.key 1552 - if node.colonPos != token.NoPos { 1552 + if node.colonPos.IsValid() { 1553 1553 navs := expandNavigableViaAncestralPath(parent.navigable.parent) 1554 1554 n.addFieldCompletions(key.Pos(), key.End(), navs, node.colonPos.Add(1)) 1555 1555 }
+1 -2
internal/lsp/fscache/fs_cache.go
··· 15 15 "cuelang.org/go/cue/ast" 16 16 "cuelang.org/go/cue/build" 17 17 "cuelang.org/go/cue/parser" 18 - "cuelang.org/go/cue/token" 19 18 "cuelang.org/go/internal" 20 19 "cuelang.org/go/internal/filetypes" 21 20 "cuelang.org/go/internal/golangorgx/gopls/protocol" ··· 164 163 // package declaration themselves. 165 164 func IsPhantomPackage(pkgDecl *ast.Package) bool { 166 165 name := pkgDecl.Name 167 - return name != nil && name.Pos() == token.NoPos && len(name.Name) == phantomPackageNameLength && name.Name[0] == '_' 166 + return name != nil && !name.Pos().IsValid() && len(name.Name) == phantomPackageNameLength && name.Name[0] == '_' 168 167 } 169 168 170 169 // RemovePhantomPackageDecl removes any phantom package declaration