github.com/julianthome/gore@v0.0.0-20231109011145-b3a6bbe6fe55/node.go (about)

     1  package gore
     2  
     3  import (
     4  	"go/ast"
     5  	"go/token"
     6  	"reflect"
     7  )
     8  
     9  // normalizeNodePos resets all position information of node and its descendants.
    10  func normalizeNodePos(node ast.Node) {
    11  	ast.Inspect(node, func(node ast.Node) bool {
    12  		if node == nil {
    13  			return true
    14  		}
    15  
    16  		if node.Pos() == token.NoPos && node.End() == token.NoPos {
    17  			return true
    18  		}
    19  
    20  		pv := reflect.ValueOf(node)
    21  		if pv.Kind() != reflect.Ptr {
    22  			return true
    23  		}
    24  
    25  		v := pv.Elem()
    26  		if v.Kind() != reflect.Struct {
    27  			return true
    28  		}
    29  
    30  		for i := 0; i < v.NumField(); i++ {
    31  			f := v.Field(i)
    32  			ft := f.Type()
    33  			if f.CanSet() && ft.PkgPath() == "go/token" && ft.Name() == "Pos" && f.Int() != 0 {
    34  				f.SetInt(1)
    35  			}
    36  		}
    37  
    38  		return true
    39  	})
    40  }