Skip to content

Use patience diff algorithm #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/dlclark/regexp2 v1.11.5
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8
github.com/google/go-cmp v0.7.0
github.com/pkg/diff v0.0.0-20241224192749-4e6772a4315c
github.com/peter-evans/patience v0.3.0
golang.org/x/sync v0.14.0
golang.org/x/sys v0.33.0
gotest.tools/v3 v3.5.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matryer/moq v0.5.3 h1:4femQCFmBUwFPYs8VfM5ID7AI67/DTEDRBbTtSWy7GU=
github.com/matryer/moq v0.5.3/go.mod h1:8288Qkw7gMZhUP3cIN86GG7g5p9jRuZH8biXLW4RXvQ=
github.com/pkg/diff v0.0.0-20241224192749-4e6772a4315c h1:8TRxBMS/YsupXoOiGKHr9ZOXo+5DezGWPgBAhBHEHto=
github.com/pkg/diff v0.0.0-20241224192749-4e6772a4315c/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/peter-evans/patience v0.3.0 h1:rX0JdJeepqdQl1Sk9c9uvorjYYzL2TfgLX1adqYm9cA=
github.com/peter-evans/patience v0.3.0/go.mod h1:Kmxu5sY1NmBLFSStvXjX1wS9mIv7wMcP/ubucyMOAu0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
Expand Down
19 changes: 13 additions & 6 deletions internal/testutil/baseline/baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/repo"
"github.com/microsoft/typescript-go/internal/stringutil"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/pkg/diff"
"github.com/peter-evans/patience"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -102,21 +103,27 @@ func readFileOrNoContent(fileName string) string {
return string(content)
}

Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Add a Go doc comment for diffText, briefly explaining the parameters and the returned unified diff string to improve maintainability.

Suggested change
// diffText generates a unified diff string between the expected and actual text.
// Parameters:
// - oldName: The name of the source file or content being compared.
// - newName: The name of the destination file or content being compared.
// - expected: The expected content as a string.
// - actual: The actual content as a string.
// Returns:
// A unified diff string highlighting the differences between the expected and actual content.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why would anyone add docs like this ever again if they can just ask copilot to guess at the docs like this? 🤔

...it's also just self-evident, in this case.

func diffText(oldName string, newName string, expected string, actual string) string {
lines := patience.Diff(stringutil.SplitLines(expected), stringutil.SplitLines(actual))
return patience.UnifiedDiffTextWithOptions(lines, patience.UnifiedDiffOptions{
Precontext: 3,
Postcontext: 3,
SrcHeader: oldName,
DstHeader: newName,
})
}

func getBaselineDiff(t *testing.T, actual string, expected string, fileName string, fixupOld func(string) string) string {
if fixupOld != nil {
expected = fixupOld(expected)
}
if actual == expected {
return NoContent
}
var b strings.Builder
if err := diff.Text("old."+fileName, "new."+fileName, expected, actual, &b); err != nil {
return fmt.Sprintf("failed to diff the actual and expected content: %v\n", err)
}
s := diffText("old."+fileName, "new."+fileName, expected, actual)

// Remove line numbers from unified diff headers; this avoids adding/deleting
// lines in our baselines from causing knock-on header changes later in the diff.
s := b.String()

aCurLine := 1
bCurLine := 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
+++ new.2dArrays.js
@@= skipped -17, +17 lines =@@
}

//// [2dArrays.js]
-var Cell = /** @class */ (function () {
- function Cell() {
Expand Down Expand Up @@ -32,4 +32,4 @@
- return Board;
-}());
+ }
+}
+}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
+++ new.2dArrays.symbols
@@= skipped -8, +8 lines =@@
>Ship : Symbol(Ship, Decl(2dArrays.ts, 1, 1))

isSunk: boolean;
->isSunk : Symbol(Ship.isSunk, Decl(2dArrays.ts, 3, 12))
+>isSunk : Symbol(isSunk, Decl(2dArrays.ts, 3, 12))
}

class Board {
>Board : Symbol(Board, Decl(2dArrays.ts, 5, 1))

ships: Ship[];
->ships : Symbol(Board.ships, Decl(2dArrays.ts, 7, 13))
+>ships : Symbol(ships, Decl(2dArrays.ts, 7, 13))
>Ship : Symbol(Ship, Decl(2dArrays.ts, 1, 1))

cells: Cell[];
->cells : Symbol(Board.cells, Decl(2dArrays.ts, 8, 18))
+>cells : Symbol(cells, Decl(2dArrays.ts, 8, 18))
>Cell : Symbol(Cell, Decl(2dArrays.ts, 0, 0))

private allShipsSunk() {
->allShipsSunk : Symbol(Board.allShipsSunk, Decl(2dArrays.ts, 9, 18))
+>allShipsSunk : Symbol(allShipsSunk, Decl(2dArrays.ts, 9, 18))

return this.ships.every(function (val) { return val.isSunk; });
->this.ships.every : Symbol(Array.every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
->this.ships : Symbol(Board.ships, Decl(2dArrays.ts, 7, 13))
Expand All @@ -33,13 +33,15 @@
>this : Symbol(Board, Decl(2dArrays.ts, 5, 1))
->ships : Symbol(Board.ships, Decl(2dArrays.ts, 7, 13))
->every : Symbol(Array.every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
->val : Symbol(val, Decl(2dArrays.ts, 12, 42))
->val.isSunk : Symbol(Ship.isSunk, Decl(2dArrays.ts, 3, 12))
->val : Symbol(val, Decl(2dArrays.ts, 12, 42))
->isSunk : Symbol(Ship.isSunk, Decl(2dArrays.ts, 3, 12))
+>ships : Symbol(ships, Decl(2dArrays.ts, 7, 13))
+>every : Symbol(every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>val : Symbol(val, Decl(2dArrays.ts, 12, 42))
->val.isSunk : Symbol(Ship.isSunk, Decl(2dArrays.ts, 3, 12))
+>val : Symbol(val, Decl(2dArrays.ts, 12, 42))
+>val.isSunk : Symbol(isSunk, Decl(2dArrays.ts, 3, 12))
>val : Symbol(val, Decl(2dArrays.ts, 12, 42))
->isSunk : Symbol(Ship.isSunk, Decl(2dArrays.ts, 3, 12))
+>val : Symbol(val, Decl(2dArrays.ts, 12, 42))
+>isSunk : Symbol(isSunk, Decl(2dArrays.ts, 3, 12))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APILibCheck.errors.txt
+++ new.APILibCheck.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+index.ts(1,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+index.ts(2,29): error TS2307: Cannot find module 'typescript-internal' or its corresponding type declarations.
+index.ts(3,34): error TS2307: Cannot find module 'tsserverlibrary' or its corresponding type declarations.
Expand Down Expand Up @@ -46,4 +45,4 @@
+ "name": "tsserverlibrary-internal",
+ "types": "/.ts/tsserverlibrary.internal.d.ts"
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_Watch.errors.txt
+++ new.APISample_Watch.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_Watch.ts(11,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+APISample_Watch.ts(14,27): error TS7006: Parameter 'path' implicitly has an 'any' type.
+APISample_Watch.ts(45,61): error TS7006: Parameter 'options' implicitly has an 'any' type.
Expand Down Expand Up @@ -104,4 +103,4 @@
+ "name": "typescript",
+ "types": "/.ts/typescript.d.ts"
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_Watch.js
+++ new.APISample_Watch.js
@@= skipped -84, +84 lines =@@

//// [APISample_Watch.js]
"use strict";
-/*
Expand Down Expand Up @@ -47,4 +47,4 @@
+ host.afterProgramCreate = program => {
console.log("** We finished making the program! **");
origPostProgramCreate(program);
};
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_WatchWithDefaults.errors.txt
+++ new.APISample_WatchWithDefaults.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_WatchWithDefaults.ts(8,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+APISample_WatchWithDefaults.ts(32,27): error TS7006: Parameter 'rootNames' implicitly has an 'any' type.
+APISample_WatchWithDefaults.ts(32,38): error TS7006: Parameter 'options' implicitly has an 'any' type.
Expand Down Expand Up @@ -77,4 +76,4 @@
+ "name": "typescript",
+ "types": "/.ts/typescript.d.ts"
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_WatchWithDefaults.js
+++ new.APISample_WatchWithDefaults.js
@@= skipped -57, +57 lines =@@

//// [APISample_WatchWithDefaults.js]
"use strict";
-/*
Expand Down Expand Up @@ -38,4 +38,4 @@
+ host.afterProgramCreate = program => {
console.log("** We finished making the program! **");
origPostProgramCreate(program);
};
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_WatchWithOwnWatchHost.errors.txt
+++ new.APISample_WatchWithOwnWatchHost.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_WatchWithOwnWatchHost.ts(8,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+APISample_WatchWithOwnWatchHost.ts(22,32): error TS7006: Parameter 'options' implicitly has an 'any' type.
+APISample_WatchWithOwnWatchHost.ts(39,27): error TS7006: Parameter 'rootNames' implicitly has an 'any' type.
Expand Down Expand Up @@ -87,4 +86,4 @@
+ "name": "typescript",
+ "types": "/.ts/typescript.d.ts"
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_WatchWithOwnWatchHost.js
+++ new.APISample_WatchWithOwnWatchHost.js
@@= skipped -64, +64 lines =@@

//// [APISample_WatchWithOwnWatchHost.js]
"use strict";
-/*
Expand Down Expand Up @@ -49,4 +49,4 @@
+ host.afterProgramCreate = program => {
console.log("** We finished making the program! **");
origPostProgramCreate(program);
};
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_compile.errors.txt
+++ new.APISample_compile.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_compile.ts(11,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+APISample_compile.ts(19,28): error TS7006: Parameter 'diagnostic' implicitly has an 'any' type.
+
Expand Down Expand Up @@ -55,4 +54,4 @@
+ "name": "typescript",
+ "types": "/.ts/typescript.d.ts"
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_compile.js
+++ new.APISample_compile.js
@@= skipped -47, +47 lines =@@

//// [APISample_compile.js]
"use strict";
-/*
Expand Down Expand Up @@ -34,4 +34,4 @@
+ console.log(`Process exiting with code '${exitCode}'.`);
process.exit(exitCode);
}
compile(process.argv.slice(2), {
compile(process.argv.slice(2), {
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_jsdoc.errors.txt
+++ new.APISample_jsdoc.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_jsdoc.ts(10,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+APISample_jsdoc.ts(26,47): error TS7006: Parameter 'comment' implicitly has an 'any' type.
+APISample_jsdoc.ts(31,20): error TS7006: Parameter 'doc' implicitly has an 'any' type.
Expand Down Expand Up @@ -133,4 +132,4 @@
+ tags.push(ts.getJSDocTemplateTag(node));
+ return tags;
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_jsdoc.js
+++ new.APISample_jsdoc.js
@@= skipped -122, +122 lines =@@

//// [APISample_jsdoc.js]
"use strict";
-/*
Expand Down Expand Up @@ -104,4 +104,4 @@
+ const type = ts.getJSDocTypeTag(node);
if (type) {
tags.push(type);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_linter.errors.txt
+++ new.APISample_linter.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_linter.ts(11,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+
+
Expand Down Expand Up @@ -78,4 +77,4 @@
+ // delint it
+ delint(sourceFile);
+ });
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_linter.js
+++ new.APISample_linter.js
@@= skipped -73, +73 lines =@@

//// [APISample_linter.js]
"use strict";
-/*
Expand Down Expand Up @@ -53,4 +53,4 @@
+ let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2015, /*setParentNodes */ true);
// delint it
delint(sourceFile);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
--- old.APISample_parseConfig.errors.txt
+++ new.APISample_parseConfig.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-<no content>
@@= skipped --1, +1 lines =@@
+APISample_parseConfig.ts(11,21): error TS2307: Cannot find module 'typescript' or its corresponding type declarations.
+
+
Expand Down Expand Up @@ -50,4 +49,4 @@
+ "name": "typescript",
+ "types": "/.ts/typescript.d.ts"
+ }
+
+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- old.APISample_parseConfig.js
+++ new.APISample_parseConfig.js
@@= skipped -45, +45 lines =@@

//// [APISample_parseConfig.js]
"use strict";
-/*
Expand Down Expand Up @@ -37,4 +37,4 @@
+ for (const err of settings.errors) {
printError(err);
}
return undefined;
return undefined;
Loading
Loading