Skip to content

Commit

Permalink
Add package name invariance (#3)
Browse files Browse the repository at this point in the history
Ensure that if the two input packages have different names, this
does not affect their equivalence.
  • Loading branch information
kevinmbeaulieu authored Apr 19, 2021
1 parent 13ac8ac commit 76934f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions eq-go/comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
// Each method returns an int representing the result of the comparison check and a tree node
// providing more info about the differences if the two inputs were not equivalent.

// TODO: (kevinb) Refactor to avoid using global variables for these.
var equivalentPackageNameA string
var equivalentPackageNameB string

type node struct {
msg string
leftPos token.Pos
Expand Down Expand Up @@ -133,6 +137,11 @@ func compareBools(a bool, b bool) (int, *node) {
}

func compareStrings(a string, b string) (int, *node) {
if equivalentPackageNameA != "" && equivalentPackageNameB != "" {
a = strings.ReplaceAll(a, equivalentPackageNameB, equivalentPackageNameA)
b = strings.ReplaceAll(b, equivalentPackageNameB, equivalentPackageNameA)
}

if a < b {
return -1, newNode(fmt.Sprintf("strings did not match: %s < %s", a, b), nil, nil, nil)
} else if a > b {
Expand Down
3 changes: 3 additions & 0 deletions eq-go/eq-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func PackagesEquivalent(a *ast.Package, fsetA *token.FileSet, b *ast.Package, fs
panic(fmt.Errorf("missing package"))
}

equivalentPackageNameA = a.Name
equivalentPackageNameB = b.Name

mergeMode := ast.FilterUnassociatedComments | ast.FilterImportDuplicates
mergedFileA := ast.MergePackageFiles(a, mergeMode)
mergedFileB := ast.MergePackageFiles(b, mergeMode)
Expand Down

0 comments on commit 76934f8

Please sign in to comment.