Skip to content

Commit 6183abf

Browse files
authored
Port 'go to type definition' tests (#1883)
1 parent fd15f29 commit 6183abf

File tree

43 files changed

+1266
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1266
-11
lines changed

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,14 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
186186
return [parseBaselineSignatureHelp(callExpression.arguments)];
187187
case "baselineGoToDefinition":
188188
case "baselineGetDefinitionAtPosition":
189-
// Both of these take the same arguments, but differ in that...
189+
case "baselineGoToType":
190+
// Both `baselineGoToDefinition` and `baselineGetDefinitionAtPosition` take the same
191+
// arguments, but differ in that...
190192
// - `verify.baselineGoToDefinition(...)` called getDefinitionAndBoundSpan
191193
// - `verify.baselineGetDefinitionAtPosition(...)` called getDefinitionAtPosition
192-
// LSP doesn't have two separate commands though. It's unclear how we would model bound spans though.
193-
return parseBaselineGoToDefinitionArgs(callExpression.arguments);
194+
// LSP doesn't have two separate commands though.
195+
// It's unclear how we would model bound spans though.
196+
return parseBaselineGoToDefinitionArgs(func.text, callExpression.arguments);
194197
case "baselineRename":
195198
case "baselineRenameAtRangesWithText":
196199
// `verify.baselineRename...(...)`
@@ -817,7 +820,20 @@ function parseBaselineDocumentHighlightsArgs(args: readonly ts.Expression[]): [V
817820
}];
818821
}
819822

820-
function parseBaselineGoToDefinitionArgs(args: readonly ts.Expression[]): [VerifyBaselineGoToDefinitionCmd] | undefined {
823+
function parseBaselineGoToDefinitionArgs(
824+
funcName: "baselineGoToDefinition" | "baselineGoToType" | "baselineGetDefinitionAtPosition",
825+
args: readonly ts.Expression[],
826+
): [VerifyBaselineGoToDefinitionCmd] | undefined {
827+
let kind: "verifyBaselineGoToDefinition" | "verifyBaselineGoToType";
828+
switch (funcName) {
829+
case "baselineGoToDefinition":
830+
case "baselineGetDefinitionAtPosition":
831+
kind = "verifyBaselineGoToDefinition";
832+
break;
833+
case "baselineGoToType":
834+
kind = "verifyBaselineGoToType";
835+
break;
836+
}
821837
const newArgs = [];
822838
for (const arg of args) {
823839
let strArg;
@@ -829,19 +845,19 @@ function parseBaselineGoToDefinitionArgs(args: readonly ts.Expression[]): [Verif
829845
}
830846
else if (arg.getText() === "...test.ranges()") {
831847
return [{
832-
kind: "verifyBaselineGoToDefinition",
848+
kind,
833849
markers: [],
834850
ranges: true,
835851
}];
836852
}
837853
else {
838-
console.error(`Unrecognized argument in verify.baselineGoToDefinition: ${arg.getText()}`);
854+
console.error(`Unrecognized argument in verify.${funcName}: ${arg.getText()}`);
839855
return undefined;
840856
}
841857
}
842858

843859
return [{
844-
kind: "verifyBaselineGoToDefinition",
860+
kind,
845861
markers: newArgs,
846862
}];
847863
}
@@ -1293,7 +1309,7 @@ interface VerifyBaselineFindAllReferencesCmd {
12931309
}
12941310

12951311
interface VerifyBaselineGoToDefinitionCmd {
1296-
kind: "verifyBaselineGoToDefinition";
1312+
kind: "verifyBaselineGoToDefinition" | "verifyBaselineGoToType";
12971313
markers: string[];
12981314
ranges?: boolean;
12991315
}
@@ -1393,11 +1409,20 @@ function generateBaselineDocumentHighlights({ args, preferences }: VerifyBaselin
13931409
return `f.VerifyBaselineDocumentHighlights(t, ${preferences}, ${args.join(", ")})`;
13941410
}
13951411

1396-
function generateBaselineGoToDefinition({ markers, ranges }: VerifyBaselineGoToDefinitionCmd): string {
1412+
function generateBaselineGoToDefinition({ markers, ranges, kind }: VerifyBaselineGoToDefinitionCmd): string {
1413+
let goFunc;
1414+
switch (kind) {
1415+
case "verifyBaselineGoToDefinition":
1416+
goFunc = "VerifyBaselineGoToDefinition";
1417+
break;
1418+
case "verifyBaselineGoToType":
1419+
goFunc = "VerifyBaselineGoToTypeDefinition";
1420+
break;
1421+
}
13971422
if (ranges || markers.length === 0) {
1398-
return `f.VerifyBaselineGoToDefinition(t)`;
1423+
return `f.${goFunc}(t)`;
13991424
}
1400-
return `f.VerifyBaselineGoToDefinition(t, ${markers.join(", ")})`;
1425+
return `f.${goFunc}(t, ${markers.join(", ")})`;
14011426
}
14021427

14031428
function generateGoToCommand({ funcName, args }: GoToCmd): string {
@@ -1436,6 +1461,7 @@ function generateCmd(cmd: Cmd): string {
14361461
case "verifyBaselineDocumentHighlights":
14371462
return generateBaselineDocumentHighlights(cmd);
14381463
case "verifyBaselineGoToDefinition":
1464+
case "verifyBaselineGoToType":
14391465
return generateBaselineGoToDefinition(cmd);
14401466
case "verifyBaselineQuickInfo":
14411467
// Quick Info -> Hover

internal/fourslash/_scripts/failingTests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ TestJsDocPropertyDescription6
245245
TestJsDocPropertyDescription7
246246
TestJsDocPropertyDescription8
247247
TestJsDocPropertyDescription9
248+
TestJsDocServices
248249
TestJsDocTagsWithHyphen
249250
TestJsQuickInfoGenerallyAcceptableSize
250251
TestJsRequireQuickInfo

internal/fourslash/fourslash.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,55 @@ func (f *FourslashTest) VerifyBaselineGoToDefinition(
852852
}
853853
}
854854

855+
func (f *FourslashTest) VerifyBaselineGoToTypeDefinition(
856+
t *testing.T,
857+
markers ...string,
858+
) {
859+
referenceLocations := f.lookupMarkersOrGetRanges(t, markers)
860+
861+
for _, markerOrRange := range referenceLocations {
862+
// worker in `baselineEachMarkerOrRange`
863+
f.GoToMarkerOrRange(t, markerOrRange)
864+
865+
params := &lsproto.TypeDefinitionParams{
866+
TextDocument: lsproto.TextDocumentIdentifier{
867+
Uri: ls.FileNameToDocumentURI(f.activeFilename),
868+
},
869+
Position: f.currentCaretPosition,
870+
}
871+
872+
resMsg, result, resultOk := sendRequest(t, f, lsproto.TextDocumentTypeDefinitionInfo, params)
873+
if resMsg == nil {
874+
if f.lastKnownMarkerName == nil {
875+
t.Fatalf("Nil response received for type definition request at pos %v", f.currentCaretPosition)
876+
} else {
877+
t.Fatalf("Nil response received for type definition request at marker '%s'", *f.lastKnownMarkerName)
878+
}
879+
}
880+
if !resultOk {
881+
if f.lastKnownMarkerName == nil {
882+
t.Fatalf("Unexpected type definition response type at pos %v: %T", f.currentCaretPosition, resMsg.AsResponse().Result)
883+
} else {
884+
t.Fatalf("Unexpected type definition response type at marker '%s': %T", *f.lastKnownMarkerName, resMsg.AsResponse().Result)
885+
}
886+
}
887+
888+
var resultAsLocations []lsproto.Location
889+
if result.Locations != nil {
890+
resultAsLocations = *result.Locations
891+
} else if result.Location != nil {
892+
resultAsLocations = []lsproto.Location{*result.Location}
893+
} else if result.DefinitionLinks != nil {
894+
t.Fatalf("Unexpected type definition response type at marker '%s': %T", *f.lastKnownMarkerName, result.DefinitionLinks)
895+
}
896+
897+
f.addResultToBaseline(t, "goToType", f.getBaselineForLocationsWithFileContents(resultAsLocations, baselineFourslashLocationsOptions{
898+
marker: markerOrRange,
899+
markerName: "/*GOTO TYPE*/",
900+
}))
901+
}
902+
}
903+
855904
func (f *FourslashTest) VerifyBaselineHover(t *testing.T) {
856905
markersAndItems := core.MapFiltered(f.Markers(), func(marker *Marker) (markerAndItem[*lsproto.Hover], bool) {
857906
if marker.Name == nil {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinition2(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `// @Filename: goToTypeDefinition2_Definition.ts
15+
interface /*definition*/I1 {
16+
p;
17+
}
18+
type propertyType = I1;
19+
interface I2 {
20+
property: propertyType;
21+
}
22+
// @Filename: goToTypeDefinition2_Consumption.ts
23+
var i2: I2;
24+
i2.prop/*reference*/erty;`
25+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
26+
f.VerifyBaselineGoToTypeDefinition(t, "reference")
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinition3(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `type /*definition*/T = string;
15+
const x: /*reference*/T;`
16+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
17+
f.VerifyBaselineGoToTypeDefinition(t, "reference")
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinition4(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `// @Filename: foo.ts
15+
export type /*def0*/T = string;
16+
export const /*def1*/T = "";
17+
// @Filename: bar.ts
18+
import { T } from "./foo";
19+
let x: [|/*reference*/T|];`
20+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
21+
f.VerifyBaselineGoToTypeDefinition(t, "reference")
22+
f.VerifyBaselineGoToDefinition(t, "reference")
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinition5(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `// @Filename: foo.ts
15+
let Foo: /*definition*/unresolved;
16+
type Foo = { x: string };
17+
/*reference*/Foo;`
18+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
19+
f.VerifyBaselineGoToTypeDefinition(t, "reference")
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinitionAliases(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `// @Filename: goToTypeDefinitioAliases_module1.ts
15+
interface /*definition*/I {
16+
p;
17+
}
18+
export {I as I2};
19+
// @Filename: goToTypeDefinitioAliases_module2.ts
20+
import {I2 as I3} from "./goToTypeDefinitioAliases_module1";
21+
var v1: I3;
22+
export {v1 as v2};
23+
// @Filename: goToTypeDefinitioAliases_module3.ts
24+
import {/*reference1*/v2 as v3} from "./goToTypeDefinitioAliases_module2";
25+
/*reference2*/v3;`
26+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
27+
f.VerifyBaselineGoToTypeDefinition(t, "reference1", "reference2")
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinitionEnumMembers(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `enum E {
15+
value1,
16+
/*definition*/value2
17+
}
18+
var x = E.value2;
19+
20+
/*reference*/x;`
21+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
22+
f.VerifyBaselineGoToTypeDefinition(t, "reference")
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestGoToTypeDefinitionImportMeta(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `// @module: esnext
15+
// @Filename: foo.ts
16+
/// <reference no-default-lib="true"/>
17+
/// <reference path='./bar.d.ts' />
18+
import.me/*reference*/ta;
19+
//@Filename: bar.d.ts
20+
interface /*definition*/ImportMeta {
21+
}`
22+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
23+
f.VerifyBaselineGoToTypeDefinition(t, "reference")
24+
}

0 commit comments

Comments
 (0)