astequal: github.com/go-toolsmith/astequal Index | Examples | Files

package astequal

import "github.com/go-toolsmith/astequal"

Package astequal provides AST (deep) equallity check operations.

Code:

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

Package Files

astequal.go

func Decl Uses

func Decl(x, y ast.Decl) bool

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 Uses

func Expr(x, y ast.Expr) bool

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.

func Node Uses

func Node(x, y ast.Node) bool

Node reports whether two AST nodes are structurally (deep) equal.

Nil arguments are permitted: true is returned if x and y are both nils.

See also: Expr, Stmt, Decl functions.

func Stmt Uses

func Stmt(x, y ast.Stmt) bool

Stmt reports whether two AST statements are structurally (deep) equal.

Nil arguments are permitted: true is returned if x and y are both nils. ast.BadStmt comparison always yields false.

Package astequal imports 2 packages (graph) and is imported by 2 packages. Updated 2018-06-16. Refresh now. Tools for package owners.