Documentation
¶
Overview ¶
Package astequal provides AST (deep) equallity check operations.
Example ¶
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"reflect"
"github.com/go-toolsmith/astequal"
)
func main() {
const code = `
package foo
func main() {
x := []int{1, 2, 3}
x := []int{1, 2, 3}
}`
fset := token.NewFileSet()
pkg, err := parser.ParseFile(fset, "string", code, 0)
if err != nil {
log.Fatalf("parse error: %+v", err)
}
fn := pkg.Decls[0].(*ast.FuncDecl)
x := fn.Body.List[0]
y := fn.Body.List[1]
// Reflect DeepEqual will fail due to different Pos values.
// astequal only checks whether two nodes describe AST.
fmt.Println(reflect.DeepEqual(x, y)) // => false
fmt.Println(astequal.Node(x, y)) // => true
fmt.Println(astequal.Stmt(x, y)) // => true
}
Output: false true true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decl ¶
Decl reports whether two AST declarations are structurally (deep) equal.
Nil arguments are permitted: true is returned if x and y are both nils. ast.BadDecl comparison always yields false.
func Expr ¶
Expr reports whether two AST expressions are structurally (deep) equal.
Nil arguments are permitted: true is returned if x and y are both nils. ast.BadExpr comparison always yields false.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.