diff --git a/go.mod b/go.mod index c3f3e81e..a9fbb0c9 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,4 @@ module github.com/evanw/esbuild go 1.13 -require ( - github.com/kylelemons/godebug v1.1.0 - golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 -) +require golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 diff --git a/go.sum b/go.sum index 9be20ecc..2feba761 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ -github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= -github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 h1:5B6i6EAiSYyejWfvc5Rc9BbI3rzIsrrXfAQBWnYfn+w= golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/bundler/bundler_test.go b/internal/bundler/bundler_test.go index 1387dc98..1dd7607d 100644 --- a/internal/bundler/bundler_test.go +++ b/internal/bundler/bundler_test.go @@ -21,7 +21,7 @@ import ( "github.com/evanw/esbuild/internal/fs" "github.com/evanw/esbuild/internal/logger" "github.com/evanw/esbuild/internal/resolver" - "github.com/kylelemons/godebug/diff" + "github.com/evanw/esbuild/internal/test" ) func es(version int) compat.JSFeature { @@ -36,7 +36,7 @@ func assertEqual(t *testing.T, a interface{}, b interface{}) { stringA := fmt.Sprintf("%v", a) stringB := fmt.Sprintf("%v", b) if strings.Contains(stringA, "\n") { - t.Fatal(diff.Diff(stringB, stringA)) + t.Fatal(test.Diff(stringB, stringA)) } else { t.Fatalf("%s != %s", a, b) } diff --git a/internal/test/diff.go b/internal/test/diff.go new file mode 100644 index 00000000..17d28d37 --- /dev/null +++ b/internal/test/diff.go @@ -0,0 +1,66 @@ +package test + +import ( + "strings" +) + +func Diff(old string, new string) string { + return strings.Join(diffRec(nil, strings.Split(old, "\n"), strings.Split(new, "\n")), "\n") +} + +// This is a simple recursive line-by-line diff implementation +func diffRec(result []string, old []string, new []string) []string { + o, n, common := lcSubstr(old, new) + + if common == 0 { + // Everything changed + for _, line := range old { + result = append(result, "-"+line) + } + for _, line := range new { + result = append(result, "+"+line) + } + } else { + // Something in the middle stayed the same + result = diffRec(result, old[:o], new[:n]) + for _, line := range old[o : o+common] { + result = append(result, " "+line) + } + result = diffRec(result, old[o+common:], new[n+common:]) + } + + return result +} + +// From: https://en.wikipedia.org/wiki/Longest_common_substring_problem +func lcSubstr(S []string, T []string) (int, int, int) { + r := len(S) + n := len(T) + Lprev := make([]int, n) + Lnext := make([]int, n) + z := 0 + retI := 0 + retJ := 0 + + for i := 0; i < r; i++ { + for j := 0; j < n; j++ { + if S[i] == T[j] { + if j == 0 { + Lnext[j] = 1 + } else { + Lnext[j] = Lprev[j-1] + 1 + } + if Lnext[j] > z { + z = Lnext[j] + retI = i + 1 + retJ = j + 1 + } + } else { + Lnext[j] = 0 + } + } + Lprev, Lnext = Lnext, Lprev + } + + return retI - z, retJ - z, z +}