this repo has no description
0
fork

Configure Feed

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

cue/ast: add walkNilable and applyNilable helpers

To avoid the repetition of explicit nil checks.
Overall we don't save that many lines of code, but what matters
is that saving repetition reduces the chances for human mistakes,
like using different fields for the nil check and the recursing call.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ic1aacaf86bc3a7f1d93948e1c1709334c1f2fadf
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1227720
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+47 -55
+26 -28
cue/ast/astutil/apply.go
··· 317 317 return c.decls 318 318 } 319 319 320 - func apply[N ast.Node](v applyVisitor, parent Cursor, nodePtr *N) { 320 + type nilableNode interface { 321 + ast.Node 322 + comparable // pointer nodes, which can be compared to nil 323 + } 324 + 325 + func applyIfNotNil[N nilableNode](v applyVisitor, parent Cursor, nodePtr *N) { 326 + var zero N // nil 327 + if *nodePtr != zero { 328 + apply(v, parent, nodePtr) 329 + } 330 + } 331 + 332 + func apply[N nilableNode](v applyVisitor, parent Cursor, nodePtr *N) { 321 333 node := *nodePtr 334 + var zero N // nil 335 + if node == zero { 336 + panic("unexpected nil node; malformed syntax tree?") 337 + } 322 338 c := newCursor(parent, node, nodePtr) 323 339 applyCursor(v, c) 324 340 if c.modified && parent != nil { ··· 381 397 case *ast.Field: 382 398 beforeValue = n.Value 383 399 apply(v, c, &n.Label) 384 - if n.Alias != nil { 385 - apply(v, c, &n.Alias) 386 - } 387 - if n.Value != nil { 388 - apply(v, c, &n.Value) 389 - } 400 + applyIfNotNil(v, c, &n.Alias) 401 + applyIfNotNil(v, c, &n.Value) 390 402 applyList(v, c, n.Attrs) 391 403 392 404 case *ast.StructLit: ··· 403 415 applyList(v, c, n.Elts) 404 416 405 417 case *ast.Ellipsis: 406 - if n.Type != nil { 407 - apply(v, c, &n.Type) 408 - } 418 + applyIfNotNil(v, c, &n.Type) 409 419 410 420 case *ast.ParenExpr: 411 421 apply(v, c, &n.X) ··· 420 430 421 431 case *ast.SliceExpr: 422 432 apply(v, c, &n.X) 423 - if n.Low != nil { 424 - apply(v, c, &n.Low) 425 - } 426 - if n.High != nil { 427 - apply(v, c, &n.High) 428 - } 433 + applyIfNotNil(v, c, &n.Low) 434 + applyIfNotNil(v, c, &n.High) 429 435 430 436 case *ast.CallExpr: 431 437 apply(v, c, &n.Fun) ··· 443 449 444 450 // Declarations 445 451 case *ast.ImportSpec: 446 - if n.Name != nil { 447 - apply(v, c, &n.Name) 448 - } 452 + applyIfNotNil(v, c, &n.Name) 449 453 apply(v, c, &n.Path) 450 454 451 455 case *ast.BadDecl: ··· 466 470 apply(v, c, &n.Expr) 467 471 468 472 case *ast.PostfixAlias: 469 - if n.Label != nil { 470 - apply(v, c, &n.Label) 471 - } 472 - if n.Field != nil { 473 - apply(v, c, &n.Field) 474 - } 473 + applyIfNotNil(v, c, &n.Label) 474 + applyIfNotNil(v, c, &n.Field) 475 475 476 476 case *ast.Comprehension: 477 477 applyList(v, c, n.Clauses) ··· 485 485 apply(v, c, &n.Name) 486 486 487 487 case *ast.ForClause: 488 - if n.Key != nil { 489 - apply(v, c, &n.Key) 490 - } 488 + applyIfNotNil(v, c, &n.Key) 491 489 apply(v, c, &n.Value) 492 490 apply(v, c, &n.Source) 493 491
+21 -27
cue/ast/walk.go
··· 18 18 "fmt" 19 19 ) 20 20 21 + type nilableNode interface { 22 + Node 23 + comparable // pointer nodes, which can be compared to nil 24 + } 25 + 26 + func walkIfNotNil[N nilableNode](node N, before func(Node) bool, after func(Node)) { 27 + var zero N // nil 28 + if node != zero { 29 + Walk(node, before, after) 30 + } 31 + } 32 + 21 33 func walkList[N Node](list []N, before func(Node) bool, after func(Node)) { 22 34 for _, node := range list { 23 35 Walk(node, before, after) ··· 53 65 54 66 case *Field: 55 67 Walk(n.Label, before, after) 56 - if n.Alias != nil { 57 - Walk(n.Alias, before, after) 58 - } 59 - if n.Value != nil { 60 - Walk(n.Value, before, after) 61 - } 68 + walkIfNotNil(n.Alias, before, after) 69 + walkIfNotNil(n.Value, before, after) 62 70 walkList(n.Attrs, before, after) 63 71 64 72 case *Func: ··· 79 87 walkList(n.Elts, before, after) 80 88 81 89 case *Ellipsis: 82 - if n.Type != nil { 83 - Walk(n.Type, before, after) 84 - } 90 + walkIfNotNil(n.Type, before, after) 85 91 86 92 case *ParenExpr: 87 93 Walk(n.X, before, after) ··· 96 102 97 103 case *SliceExpr: 98 104 Walk(n.X, before, after) 99 - if n.Low != nil { 100 - Walk(n.Low, before, after) 101 - } 102 - if n.High != nil { 103 - Walk(n.High, before, after) 104 - } 105 + walkIfNotNil(n.Low, before, after) 106 + walkIfNotNil(n.High, before, after) 105 107 106 108 case *CallExpr: 107 109 Walk(n.Fun, before, after) ··· 119 121 120 122 // Declarations 121 123 case *ImportSpec: 122 - if n.Name != nil { 123 - Walk(n.Name, before, after) 124 - } 124 + walkIfNotNil(n.Name, before, after) 125 125 Walk(n.Path, before, after) 126 126 127 127 case *BadDecl: ··· 142 142 Walk(n.Expr, before, after) 143 143 144 144 case *PostfixAlias: 145 - if n.Label != nil { 146 - Walk(n.Label, before, after) 147 - } 148 - if n.Field != nil { 149 - Walk(n.Field, before, after) 150 - } 145 + walkIfNotNil(n.Label, before, after) 146 + walkIfNotNil(n.Field, before, after) 151 147 152 148 case *Comprehension: 153 149 walkList(n.Clauses, before, after) ··· 161 157 Walk(n.Name, before, after) 162 158 163 159 case *ForClause: 164 - if n.Key != nil { 165 - Walk(n.Key, before, after) 166 - } 160 + walkIfNotNil(n.Key, before, after) 167 161 Walk(n.Value, before, after) 168 162 Walk(n.Source, before, after) 169 163