Skip to content

Commit 20b4b7f

Browse files
committed
Merge pull request microsoft#3611 from tinganho/goToDefinitionTypePredicate
Go to definition for type predicate
2 parents 4645a5b + cb9f80f commit 20b4b7f

6 files changed

+44
-0
lines changed

src/compiler/checker.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12334,6 +12334,10 @@ namespace ts {
1233412334
return resolveEntityName(<EntityName>entityName, meaning);
1233512335
}
1233612336

12337+
if (entityName.parent.kind === SyntaxKind.TypePredicate) {
12338+
return resolveEntityName(<Identifier>entityName, /*meaning*/ SymbolFlags.FunctionScopedVariable);
12339+
}
12340+
1233712341
// Do we want to return undefined here?
1233812342
return undefined;
1233912343
}

tests/baselines/reference/typeGuardFunction.symbols

+11
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ class C extends A {
2525
declare function isA(p1: any): p1 is A;
2626
>isA : Symbol(isA, Decl(typeGuardFunction.ts, 11, 1))
2727
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 13, 21))
28+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 13, 21))
2829
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
2930

3031
declare function isB(p1: any): p1 is B;
3132
>isB : Symbol(isB, Decl(typeGuardFunction.ts, 13, 39))
3233
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 14, 21))
34+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 14, 21))
3335
>B : Symbol(B, Decl(typeGuardFunction.ts, 3, 1))
3436

3537
declare function isC(p1: any): p1 is C;
3638
>isC : Symbol(isC, Decl(typeGuardFunction.ts, 14, 39))
3739
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 15, 21))
40+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 15, 21))
3841
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
3942

4043
declare function retC(): C;
@@ -98,6 +101,7 @@ interface I1 {
98101
(p1: A): p1 is C;
99102
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 41, 5))
100103
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
104+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 41, 5))
101105
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
102106
}
103107

@@ -107,6 +111,7 @@ declare function isC_multipleParams(p1, p2): p1 is C;
107111
>isC_multipleParams : Symbol(isC_multipleParams, Decl(typeGuardFunction.ts, 42, 1))
108112
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 46, 36))
109113
>p2 : Symbol(p2, Decl(typeGuardFunction.ts, 46, 39))
114+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 46, 36))
110115
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
111116

112117
if (isC_multipleParams(a, 0)) {
@@ -127,6 +132,7 @@ var obj: {
127132
>func1 : Symbol(func1, Decl(typeGuardFunction.ts, 52, 10))
128133
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 53, 10))
129134
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
135+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 53, 10))
130136
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
131137
}
132138
class D {
@@ -136,6 +142,7 @@ class D {
136142
>method1 : Symbol(method1, Decl(typeGuardFunction.ts, 55, 9))
137143
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 56, 12))
138144
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
145+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 56, 12))
139146
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
140147

141148
return true;
@@ -147,6 +154,7 @@ let f1 = (p1: A): p1 is C => false;
147154
>f1 : Symbol(f1, Decl(typeGuardFunction.ts, 62, 3))
148155
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 62, 10))
149156
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
157+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 62, 10))
150158
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
151159

152160
// Function type
@@ -155,13 +163,15 @@ declare function f2(p1: (p1: A) => p1 is C);
155163
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 65, 20))
156164
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 65, 25))
157165
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
166+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 65, 25))
158167
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
159168

160169
// Function expressions
161170
f2(function(p1: A): p1 is C {
162171
>f2 : Symbol(f2, Decl(typeGuardFunction.ts, 62, 35))
163172
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 68, 12))
164173
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
174+
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 68, 12))
165175
>C : Symbol(C, Decl(typeGuardFunction.ts, 7, 1))
166176

167177
return true;
@@ -182,6 +192,7 @@ declare function acceptingTypeGuardFunction(p1: (item) => item is A);
182192
>acceptingTypeGuardFunction : Symbol(acceptingTypeGuardFunction, Decl(typeGuardFunction.ts, 74, 25))
183193
>p1 : Symbol(p1, Decl(typeGuardFunction.ts, 77, 44))
184194
>item : Symbol(item, Decl(typeGuardFunction.ts, 77, 49))
195+
>item : Symbol(item, Decl(typeGuardFunction.ts, 77, 49))
185196
>A : Symbol(A, Decl(typeGuardFunction.ts, 0, 0))
186197

187198
acceptingTypeGuardFunction(isA);

tests/baselines/reference/typeGuardFunctionGenerics.symbols

+7
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class C extends A {
2525
declare function isB(p1): p1 is B;
2626
>isB : Symbol(isB, Decl(typeGuardFunctionGenerics.ts, 11, 1))
2727
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 13, 21))
28+
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 13, 21))
2829
>B : Symbol(B, Decl(typeGuardFunctionGenerics.ts, 3, 1))
2930

3031
declare function isC(p1): p1 is C;
3132
>isC : Symbol(isC, Decl(typeGuardFunctionGenerics.ts, 13, 34))
3233
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 14, 21))
34+
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 14, 21))
3335
>C : Symbol(C, Decl(typeGuardFunctionGenerics.ts, 7, 1))
3436

3537
declare function retC(x): C;
@@ -52,13 +54,15 @@ declare function funB<T>(p1: (p1) => T, p2: any): p2 is T;
5254
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 18, 30))
5355
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 18, 22))
5456
>p2 : Symbol(p2, Decl(typeGuardFunctionGenerics.ts, 18, 39))
57+
>p2 : Symbol(p2, Decl(typeGuardFunctionGenerics.ts, 18, 39))
5558
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 18, 22))
5659

5760
declare function funC<T>(p1: (p1) => p1 is T): T;
5861
>funC : Symbol(funC, Decl(typeGuardFunctionGenerics.ts, 18, 58))
5962
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 19, 22))
6063
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 19, 25))
6164
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 19, 30))
65+
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 19, 30))
6266
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 19, 22))
6367
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 19, 22))
6468

@@ -67,8 +71,10 @@ declare function funD<T>(p1: (p1) => p1 is T, p2: any): p2 is T;
6771
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 20, 22))
6872
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 20, 25))
6973
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 20, 30))
74+
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 20, 30))
7075
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 20, 22))
7176
>p2 : Symbol(p2, Decl(typeGuardFunctionGenerics.ts, 20, 45))
77+
>p2 : Symbol(p2, Decl(typeGuardFunctionGenerics.ts, 20, 45))
7278
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 20, 22))
7379

7480
declare function funE<T, U>(p1: (p1) => p1 is T, p2: U): T;
@@ -77,6 +83,7 @@ declare function funE<T, U>(p1: (p1) => p1 is T, p2: U): T;
7783
>U : Symbol(U, Decl(typeGuardFunctionGenerics.ts, 21, 24))
7884
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 21, 28))
7985
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 21, 33))
86+
>p1 : Symbol(p1, Decl(typeGuardFunctionGenerics.ts, 21, 33))
8087
>T : Symbol(T, Decl(typeGuardFunctionGenerics.ts, 21, 22))
8188
>p2 : Symbol(p2, Decl(typeGuardFunctionGenerics.ts, 21, 48))
8289
>U : Symbol(U, Decl(typeGuardFunctionGenerics.ts, 21, 24))

tests/baselines/reference/typeGuardOfFormIsType.symbols

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var strOrNum: string | number;
3131
function isC1(x: any): x is C1 {
3232
>isC1 : Symbol(isC1, Decl(typeGuardOfFormIsType.ts, 12, 30))
3333
>x : Symbol(x, Decl(typeGuardOfFormIsType.ts, 14, 14))
34+
>x : Symbol(x, Decl(typeGuardOfFormIsType.ts, 14, 14))
3435
>C1 : Symbol(C1, Decl(typeGuardOfFormIsType.ts, 0, 0))
3536

3637
return true;
@@ -39,6 +40,7 @@ function isC1(x: any): x is C1 {
3940
function isC2(x: any): x is C2 {
4041
>isC2 : Symbol(isC2, Decl(typeGuardOfFormIsType.ts, 16, 1))
4142
>x : Symbol(x, Decl(typeGuardOfFormIsType.ts, 18, 14))
43+
>x : Symbol(x, Decl(typeGuardOfFormIsType.ts, 18, 14))
4244
>C2 : Symbol(C2, Decl(typeGuardOfFormIsType.ts, 3, 1))
4345

4446
return true;
@@ -47,6 +49,7 @@ function isC2(x: any): x is C2 {
4749
function isD1(x: any): x is D1 {
4850
>isD1 : Symbol(isD1, Decl(typeGuardOfFormIsType.ts, 20, 1))
4951
>x : Symbol(x, Decl(typeGuardOfFormIsType.ts, 22, 14))
52+
>x : Symbol(x, Decl(typeGuardOfFormIsType.ts, 22, 14))
5053
>D1 : Symbol(D1, Decl(typeGuardOfFormIsType.ts, 6, 1))
5154

5255
return true;

tests/baselines/reference/typeGuardOfFormIsTypeOnInterfaces.symbols

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var strOrNum: string | number;
5050
function isC1(x: any): x is C1 {
5151
>isC1 : Symbol(isC1, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 17, 30))
5252
>x : Symbol(x, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 20, 14))
53+
>x : Symbol(x, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 20, 14))
5354
>C1 : Symbol(C1, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 0, 0))
5455

5556
return true;
@@ -58,6 +59,7 @@ function isC1(x: any): x is C1 {
5859
function isC2(x: any): x is C2 {
5960
>isC2 : Symbol(isC2, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 22, 1))
6061
>x : Symbol(x, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 24, 14))
62+
>x : Symbol(x, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 24, 14))
6163
>C2 : Symbol(C2, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 5, 1))
6264

6365
return true;
@@ -66,6 +68,7 @@ function isC2(x: any): x is C2 {
6668
function isD1(x: any): x is D1 {
6769
>isD1 : Symbol(isD1, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 26, 1))
6870
>x : Symbol(x, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 28, 14))
71+
>x : Symbol(x, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 28, 14))
6972
>D1 : Symbol(D1, Decl(typeGuardOfFormIsTypeOnInterfaces.ts, 10, 1))
7073

7174
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
//// /*classDeclaration*/class A {}
4+
//// function f(/*parameterDeclaration*/parameter: any): /*parameterName*/parameter is /*typeReference*/A {
5+
//// return typeof parameter === "string";
6+
//// }
7+
8+
goTo.marker('parameterName');
9+
10+
goTo.definition();
11+
verify.caretAtMarker('parameterDeclaration');
12+
13+
goTo.marker('typeReference');
14+
15+
goTo.definition();
16+
verify.caretAtMarker('classDeclaration');

0 commit comments

Comments
 (0)