Skip to content

Commit 61994a7

Browse files
committed
Show the type information in places where there is no symbol associated
1 parent 0dbd303 commit 61994a7

14 files changed

+56
-30
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,9 @@ module ts {
10711071

10721072
function writeType(type: Type, flags: TypeFormatFlags) {
10731073
if (type.flags & TypeFlags.Intrinsic) {
1074-
writer.writeKind((<IntrinsicType>type).intrinsicName, SymbolDisplayPartKind.keyword);
1074+
// Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving
1075+
writer.writeKind(!(flags & TypeFormatFlags.WriteOwnNameForAnyLike) &&
1076+
(type.flags & TypeFlags.Any) ? "any" : (<IntrinsicType>type).intrinsicName, SymbolDisplayPartKind.keyword);
10751077
}
10761078
else if (type.flags & TypeFlags.Reference) {
10771079
writeTypeReference(<TypeReference>type);
@@ -6953,19 +6955,6 @@ module ts {
69536955
return findChildAtPosition(sourceFile);
69546956
}
69556957

6956-
function isInsideWithStatementBody(node: Node): boolean {
6957-
if (node) {
6958-
while (node.parent) {
6959-
if (node.parent.kind === SyntaxKind.WithStatement && (<WithStatement>node.parent).statement === node) {
6960-
return true;
6961-
}
6962-
node = node.parent;
6963-
}
6964-
}
6965-
6966-
return false;
6967-
}
6968-
69696958
function getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]{
69706959
var symbols: SymbolTable = {};
69716960
var memberFlags: NodeFlags = 0;

src/compiler/parser.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ module ts {
166166
}
167167
}
168168

169+
export function isInsideWithStatementBody(node: Node): boolean {
170+
if (node) {
171+
while (node.parent) {
172+
if (node.parent.kind === SyntaxKind.WithStatement && (<WithStatement>node.parent).statement === node) {
173+
return true;
174+
}
175+
node = node.parent;
176+
}
177+
}
178+
179+
return false;
180+
}
181+
169182
export var fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)('|")(.+?)\2.*?\/>/
170183

171184
// Invokes a callback for each child of the given node. The 'cbNode' callback is invoked for all child nodes

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ module ts {
683683
UseTypeOfFunction = 0x00000002, // Write typeof instead of function type literal
684684
NoTruncation = 0x00000004, // Don't truncate typeToString result
685685
WriteArrowStyleSignature= 0x00000008, // Write arrow style signature
686+
WriteOwnNameForAnyLike = 0x00000010, // Write symbol's own name instead of 'any' for any like types (eg. unknown, __resolving__ etc)
686687
}
687688

688689
export enum SymbolFormatFlags {

src/harness/typeWriter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class TypeWriterWalker {
8686
column: lineAndCharacter.character,
8787
syntaxKind: ts.SyntaxKind[node.kind],
8888
sourceText: sourceText,
89-
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation)
89+
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.WriteOwnNameForAnyLike)
9090
});
9191
}
9292

src/services/services.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,29 @@ module ts {
29452945

29462946
var symbol = typeInfoResolver.getSymbolInfo(node);
29472947
if (!symbol) {
2948+
2949+
// Try getting just type at this position and show
2950+
switch (node.kind) {
2951+
case SyntaxKind.Identifier:
2952+
case SyntaxKind.PropertyAccess:
2953+
case SyntaxKind.QualifiedName:
2954+
case SyntaxKind.ThisKeyword:
2955+
case SyntaxKind.SuperKeyword:
2956+
// For the identifiers/this/usper etc get the type at position if not inside if statement
2957+
if (!isInsideWithStatementBody(node)) {
2958+
var type = typeInfoResolver.getTypeOfNode(node);
2959+
if (type) {
2960+
return {
2961+
kind: ScriptElementKind.unknown,
2962+
kindModifiers: ScriptElementKindModifier.none,
2963+
textSpan: new TypeScript.TextSpan(node.getStart(), node.getWidth()),
2964+
displayParts: typeToDisplayParts(typeInfoResolver, type, getContainerNode(node), TypeFormatFlags.NoTruncation),
2965+
documentation: type.symbol ? type.symbol.getDocumentationComment() : undefined
2966+
};
2967+
}
2968+
}
2969+
}
2970+
29482971
return undefined;
29492972
}
29502973

tests/baselines/reference/constructorOverloads1.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/constructorOverloads1.ts(7,5): error TS2392: Multiple constructor implementations are not allowed.
22
tests/cases/compiler/constructorOverloads1.ts(16,18): error TS2345: Argument of type 'Foo' is not assignable to parameter of type 'number'.
3-
tests/cases/compiler/constructorOverloads1.ts(17,18): error TS2345: Argument of type 'unknown[]' is not assignable to parameter of type 'number'.
3+
tests/cases/compiler/constructorOverloads1.ts(17,18): error TS2345: Argument of type 'any[]' is not assignable to parameter of type 'number'.
44

55

66
==== tests/cases/compiler/constructorOverloads1.ts (3 errors) ====
@@ -28,7 +28,7 @@ tests/cases/compiler/constructorOverloads1.ts(17,18): error TS2345: Argument of
2828
!!! error TS2345: Argument of type 'Foo' is not assignable to parameter of type 'number'.
2929
var f4 = new Foo([f1,f2,f3]);
3030
~~~~~~~~~~
31-
!!! error TS2345: Argument of type 'unknown[]' is not assignable to parameter of type 'number'.
31+
!!! error TS2345: Argument of type 'any[]' is not assignable to parameter of type 'number'.
3232

3333
f1.bar1();
3434
f1.bar2();

tests/baselines/reference/genericConstraint2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/compiler/genericConstraint2.ts(5,18): error TS2313: Constraint of a type parameter cannot reference any type parameter from the same type parameter list.
22
tests/cases/compiler/genericConstraint2.ts(11,7): error TS2421: Class 'ComparableString' incorrectly implements interface 'Comparable<string>':
33
Property 'comparer' is missing in type 'ComparableString'.
4-
tests/cases/compiler/genericConstraint2.ts(21,17): error TS2343: Type 'ComparableString' does not satisfy the constraint 'Comparable<unknown>':
4+
tests/cases/compiler/genericConstraint2.ts(21,17): error TS2343: Type 'ComparableString' does not satisfy the constraint 'Comparable<any>':
55
Property 'comparer' is missing in type 'ComparableString'.
66

77

@@ -33,5 +33,5 @@ tests/cases/compiler/genericConstraint2.ts(21,17): error TS2343: Type 'Comparabl
3333
var b = new ComparableString("b");
3434
var c = compare<ComparableString>(a, b);
3535
~~~~~~~~~~~~~~~~
36-
!!! error TS2343: Type 'ComparableString' does not satisfy the constraint 'Comparable<unknown>':
36+
!!! error TS2343: Type 'ComparableString' does not satisfy the constraint 'Comparable<any>':
3737
!!! error TS2343: Property 'comparer' is missing in type 'ComparableString'.

tests/baselines/reference/lambdaArgCrash.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/lambdaArgCrash.ts(27,25): error TS2304: Cannot find name 'ItemSet'.
2-
tests/cases/compiler/lambdaArgCrash.ts(29,14): error TS2345: Argument of type '(items: unknown) => void' is not assignable to parameter of type '() => any'.
2+
tests/cases/compiler/lambdaArgCrash.ts(29,14): error TS2345: Argument of type '(items: any) => void' is not assignable to parameter of type '() => any'.
33

44

55
==== tests/cases/compiler/lambdaArgCrash.ts (2 errors) ====
@@ -35,7 +35,7 @@ tests/cases/compiler/lambdaArgCrash.ts(29,14): error TS2345: Argument of type '(
3535

3636
super.add(listener);
3737
~~~~~~~~
38-
!!! error TS2345: Argument of type '(items: unknown) => void' is not assignable to parameter of type '() => any'.
38+
!!! error TS2345: Argument of type '(items: any) => void' is not assignable to parameter of type '() => any'.
3939

4040
}
4141

tests/baselines/reference/maxConstraints.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/maxConstraints.ts(5,6): error TS2313: Constraint of a type parameter cannot reference any type parameter from the same type parameter list.
2-
tests/cases/compiler/maxConstraints.ts(8,22): error TS2345: Argument of type 'number' is not assignable to parameter of type 'Comparable<unknown>'.
2+
tests/cases/compiler/maxConstraints.ts(8,22): error TS2345: Argument of type 'number' is not assignable to parameter of type 'Comparable<any>'.
33

44

55
==== tests/cases/compiler/maxConstraints.ts (2 errors) ====
@@ -14,4 +14,4 @@ tests/cases/compiler/maxConstraints.ts(8,22): error TS2345: Argument of type 'nu
1414
var max2: Comparer = (x, y) => { return (x.compareTo(y) > 0) ? x : y };
1515
var maxResult = max2(1, 2);
1616
~
17-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'Comparable<unknown>'.
17+
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'Comparable<any>'.

tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo
1010
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(55,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'.
1111
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'.
1212
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(73,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'.
13-
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
13+
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
1414
Index signatures are incompatible:
1515
Type '{}' is not assignable to type 'string'.
1616
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(88,9): error TS2304: Cannot find name 'Myn'.
@@ -116,7 +116,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo
116116
// error
117117
var b: { [x: number]: string; } = {
118118
~
119-
!!! error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
119+
!!! error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
120120
!!! error TS2322: Index signatures are incompatible:
121121
!!! error TS2322: Type '{}' is not assignable to type 'string'.
122122
a: '',

0 commit comments

Comments
 (0)