diff --git a/tsc/internal/fourslash/fourslash.go b/tsc/internal/fourslash/fourslash.go index b2ffb1c33606d..a70264613d833 100644 --- a/tsc/internal/fourslash/fourslash.go +++ b/tsc/internal/fourslash/fourslash.go @@ -879,7 +879,7 @@ func (f *FourslashTest) goToPosition(t *testing.T, position lsproto.Position) { func (f *FourslashTest) GoToEachMarker(t *testing.T, markerNames []string, action func(marker *Marker, index int)) { var markers []*Marker - if len(markers) == 0 { + if len(markerNames) == 0 { markers = f.Markers() } else { markers = make([]*Marker, 0, len(markerNames)) @@ -4896,7 +4896,10 @@ func (f *FourslashTest) VerifyRenameSucceeded(t *testing.T, preferences *lsutil. Position: f.currentCaretPosition, NewName: "RENAME_SUCCEEDED_TEST", }) - if renameResult.WorkspaceEdit == nil || renameResult.WorkspaceEdit.Changes == nil || len(*renameResult.WorkspaceEdit.Changes) == 0 { + edit := renameResult.WorkspaceEdit + if edit == nil || + ((edit.Changes == nil || len(*edit.Changes) == 0) && + (edit.DocumentChanges == nil || len(*edit.DocumentChanges) == 0)) { t.Fatal(prefix + "prepareRename succeeded but textDocument/rename returned no changes") } } @@ -5229,7 +5232,10 @@ func (f *FourslashTest) VerifyRenameFailed(t *testing.T, preferences *lsutil.Use if renameMsg != nil && renameMsg.AsResponse().Error != nil { return } - if renameResult.WorkspaceEdit != nil && renameResult.WorkspaceEdit.Changes != nil && len(*renameResult.WorkspaceEdit.Changes) > 0 { + edit := renameResult.WorkspaceEdit + if edit != nil && + ((edit.Changes != nil && len(*edit.Changes) > 0) || + (edit.DocumentChanges != nil && len(*edit.DocumentChanges) > 0)) { t.Fatalf("%sprepareRename returned null but textDocument/rename returned changes", prefix) } } diff --git a/tsc/internal/fourslash/fourslash_test.go b/tsc/internal/fourslash/fourslash_test.go new file mode 100644 index 0000000000000..e5dbd025ebf8d --- /dev/null +++ b/tsc/internal/fourslash/fourslash_test.go @@ -0,0 +1,49 @@ +package fourslash + +import ( + "slices" + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" +) + +func newMarkerTest(t *testing.T) *FourslashTest { + data := ParseTestData(t, `const /*first*/x = /*second*/1; /*third*/`, "markers.ts") + return &FourslashTest{ + testData: &data, + activeFilename: data.Markers[0].FileName(), + } +} + +func TestGoToEachMarker(t *testing.T) { + t.Parallel() + for _, tt := range []struct { + name string + selected []string + want []string + }{ + {name: "all", want: []string{"first", "second", "third"}}, + {name: "empty", selected: []string{}, want: []string{"first", "second", "third"}}, + {name: "subset", selected: []string{"second"}, want: []string{"second"}}, + {name: "ordered duplicates", selected: []string{"third", "first", "third"}, want: []string{"third", "first", "third"}}, + } { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + f := newMarkerTest(t) + var visited []string + f.GoToEachMarker(t, tt.selected, func(marker *Marker, index int) { + if index != len(visited) { + t.Errorf("callback index = %d, want %d", index, len(visited)) + } + if f.currentCaretPosition != marker.LSPos() || f.lastKnownMarkerName != marker.GetName() || f.selectionEnd != nil { + t.Error("callback did not observe navigation to its marker") + } + visited = append(visited, *marker.Name) + f.selectionEnd = new(lsproto.Position{}) + }) + if !slices.Equal(visited, tt.want) { + t.Fatalf("visited %v, want %v", visited, tt.want) + } + }) + } +} diff --git a/tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_js_test.go b/tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_js_test.go rename to tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_ts_js_test.go b/tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_ts_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_ts_js_test.go rename to tsc/internal/fourslash/tests/autoImportPackageJsonImportsPattern_ts_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/autoImportPackageJsonImports_js_test.go b/tsc/internal/fourslash/tests/autoImportPackageJsonImports_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/autoImportPackageJsonImports_js_test.go rename to tsc/internal/fourslash/tests/autoImportPackageJsonImports_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/docCommentTemplateFunctionWithParameters_js_test.go b/tsc/internal/fourslash/tests/docCommentTemplateFunctionWithParameters_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/docCommentTemplateFunctionWithParameters_js_test.go rename to tsc/internal/fourslash/tests/docCommentTemplateFunctionWithParameters_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_class_js_test.go b/tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_class_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_class_js_test.go rename to tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_class_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_function_js_test.go b/tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_function_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_function_js_test.go rename to tsc/internal/fourslash/tests/findAllRefsJsDocTemplateTag_function_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/findAllRefsJsDocTypeDef_js_test.go b/tsc/internal/fourslash/tests/findAllRefsJsDocTypeDef_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/findAllRefsJsDocTypeDef_js_test.go rename to tsc/internal/fourslash/tests/findAllRefsJsDocTypeDef_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/findAllRefs_importType_js_test.go b/tsc/internal/fourslash/tests/findAllRefs_importType_jsFile_test.go similarity index 84% rename from tsc/internal/fourslash/tests/findAllRefs_importType_js_test.go rename to tsc/internal/fourslash/tests/findAllRefs_importType_jsFile_test.go index 08d2b655e1b52..16d75c16a85a6 100644 --- a/tsc/internal/fourslash/tests/findAllRefs_importType_js_test.go +++ b/tsc/internal/fourslash/tests/findAllRefs_importType_jsFile_test.go @@ -13,12 +13,13 @@ func TestFindAllRefs_importType_js(t *testing.T) { const content = `// @allowJs: true // @checkJs: true // @Filename: /a.js -/**/module.exports = class C {}; -module.exports.D = class D {}; +class C {} +C.D = class D {}; +/**/module.exports = C; // @Filename: /b.js /** @type {import("./a")} */ const x = 0; -/** @type {import("./a").D} */ +/** @type {InstanceType} */ const y = 0;` f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) defer done() diff --git a/tsc/internal/fourslash/tests/importAllowRenameOfImportPath_test.go b/tsc/internal/fourslash/tests/importAllowRenameOfImportPath_test.go index 6465cfa65b4b5..5e0c4e80e087a 100644 --- a/tsc/internal/fourslash/tests/importAllowRenameOfImportPath_test.go +++ b/tsc/internal/fourslash/tests/importAllowRenameOfImportPath_test.go @@ -17,9 +17,9 @@ export const x = 0; // @Filename: /dir/index.ts export const x = 0; // @Filename: /b.ts -import * as a from "./[|a|]"; -import * as dir from "./[|dir|]"; -import * as dir2 from "./dir/[|index|]"; +import * as a from "./[|/*a*/a|]"; +import * as dir from "./[|/*dir*/dir|]"; +import * as dir2 from "./dir/[|/*index*/index|]"; // @Filename: /c.js const a = require("./[|a|]"); ` diff --git a/tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_js_test.go b/tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_js_test.go rename to tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_ts_js_test.go b/tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_ts_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_ts_js_test.go rename to tsc/internal/fourslash/tests/importCompletionsPackageJsonImportsPattern_ts_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/importCompletionsPackageJsonImports_js_test.go b/tsc/internal/fourslash/tests/importCompletionsPackageJsonImports_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/importCompletionsPackageJsonImports_js_test.go rename to tsc/internal/fourslash/tests/importCompletionsPackageJsonImports_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/importNameCodeFix_all_js_test.go b/tsc/internal/fourslash/tests/importNameCodeFix_all_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/importNameCodeFix_all_js_test.go rename to tsc/internal/fourslash/tests/importNameCodeFix_all_jsFile_test.go diff --git a/tsc/internal/fourslash/tests/importStatementCompletions_js_test.go b/tsc/internal/fourslash/tests/importStatementCompletions_jsFile_test.go similarity index 100% rename from tsc/internal/fourslash/tests/importStatementCompletions_js_test.go rename to tsc/internal/fourslash/tests/importStatementCompletions_jsFile_test.go diff --git a/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTemplateTag_class_js.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTemplateTag_class_js.baseline.jsonc new file mode 100644 index 0000000000000..4067d0f90d156 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTemplateTag_class_js.baseline.jsonc @@ -0,0 +1,19 @@ +// === findAllReferences === +// === /a.js === +// /** @template /*FIND ALL REFS*/T */ +// class C { +// constructor() { +// /** @type {T} */ +// --- (line: 5) skipped --- + + + +// === findAllReferences === +// === /a.js === +// /** @template T */ +// class C { +// constructor() { +// /** @type {/*FIND ALL REFS*/[|T|]} */ +// this.x = null; +// } +// } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTemplateTag_function_js.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTemplateTag_function_js.baseline.jsonc new file mode 100644 index 0000000000000..1e85c2d93fca0 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTemplateTag_function_js.baseline.jsonc @@ -0,0 +1,17 @@ +// === findAllReferences === +// === /a.js === +// /** +// * @template /*FIND ALL REFS*/T +// * @return {T} +// */ +// function f() {} + + + +// === findAllReferences === +// === /a.js === +// /** +// * @template T +// * @return {/*FIND ALL REFS*/[|T|]} +// */ +// function f() {} \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTypeDef_js.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTypeDef_js.baseline.jsonc new file mode 100644 index 0000000000000..477f7f8c79270 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefsJsDocTypeDef_js.baseline.jsonc @@ -0,0 +1,55 @@ +// === findAllReferences === +// === /a.js === +// /** /*FIND ALL REFS*/@typedef {number} T */ +// +// /** +// * @return {T} +// --- (line: 5) skipped --- + + + +// === findAllReferences === +// === /a.js === +// /** @typedef {number} /*FIND ALL REFS*/[|T|] */ +// +// /** +// * @return {[|T|]} +// */ +// function f(obj) { return 0; } +// +// /** +// * @return {[|T|]} +// */ +// function f2(obj) { return 0; } + + + +// === findAllReferences === +// === /a.js === +// /** @typedef {number} [|T|] */ +// +// /** +// * @return {/*FIND ALL REFS*/[|T|]} +// */ +// function f(obj) { return 0; } +// +// /** +// * @return {[|T|]} +// */ +// function f2(obj) { return 0; } + + + +// === findAllReferences === +// === /a.js === +// /** @typedef {number} [|T|] */ +// +// /** +// * @return {[|T|]} +// */ +// function f(obj) { return 0; } +// +// /** +// * @return {/*FIND ALL REFS*/[|T|]} +// */ +// function f2(obj) { return 0; } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefs_importType_js.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefs_importType_js.baseline.jsonc new file mode 100644 index 0000000000000..2aa830a1f8517 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/findAllReferences/findAllRefs_importType_js.baseline.jsonc @@ -0,0 +1,11 @@ +// === findAllReferences === +// === /a.js === +// class C {} +// C.D = class D {}; +// /*FIND ALL REFS*/[|module|].exports = C; + +// === /b.js === +// /** @type {import("[|./a|]")} */ +// const x = 0; +// /** @type {InstanceType} */ +// const y = 0; \ No newline at end of file