forked from x-motemen/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
41 lines (33 loc) · 708 Bytes
/
node.go
File metadata and controls
41 lines (33 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"reflect"
"go/ast"
"go/token"
)
// normalizeNodePos resets all position information of node and its descendants.
func normalizeNodePos(node ast.Node) {
ast.Inspect(node, func(node ast.Node) bool {
if node == nil {
return true
}
if node.Pos() == token.NoPos && node.End() == token.NoPos {
return true
}
pv := reflect.ValueOf(node)
if pv.Kind() != reflect.Ptr {
return true
}
v := pv.Elem()
if v.Kind() != reflect.Struct {
return true
}
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ft := f.Type()
if f.CanSet() && ft.PkgPath() == "go/token" && ft.Name() == "Pos" && f.Int() != 0 {
f.SetInt(1)
}
}
return true
})
}