Skip to content

Commit 84b0b16

Browse files
authored
Add Diff func (#20)
1 parent 8dece9b commit 84b0b16

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

diff.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package astequal
2+
3+
import (
4+
"bytes"
5+
"go/ast"
6+
"go/format"
7+
"go/token"
8+
9+
"github.com/google/go-cmp/cmp"
10+
)
11+
12+
func Diff(x, y ast.Node) string {
13+
var buf bytes.Buffer
14+
format.Node(&buf, token.NewFileSet(), x)
15+
s1 := buf.String()
16+
17+
buf.Reset()
18+
format.Node(&buf, token.NewFileSet(), y)
19+
s2 := buf.String()
20+
21+
// TODO(cristaloleg): replace with a more lightweight diff impl.
22+
return cmp.Diff(s1, s2)
23+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ go 1.16
44

55
require (
66
github.com/go-toolsmith/strparse v1.1.0
7+
github.com/google/go-cmp v0.6.0
78
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/go-toolsmith/astequal v1.0.3/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/
22
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
33
github.com/go-toolsmith/strparse v1.1.0 h1:GAioeZUK9TGxnLS+qfdqNbA4z0SSm5zVNtCQiyP2Bvw=
44
github.com/go-toolsmith/strparse v1.1.0/go.mod h1:7ksGy58fsaQkGQlY8WVoBFNyEPMGuJin1rfoPS4lBSQ=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
57
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
68
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9 h1:6WHiuFL9FNjg8RljAaT7FNUuKDbvMqS1i5cr2OE2sLQ=
79
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=

0 commit comments

Comments
 (0)