Skip to content

Commit a08aa14

Browse files
committed
Address PR feedback
1 parent 58a99f3 commit a08aa14

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/compiler/checker.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,12 @@ module ts {
22962296
return getTypeFromArrayTypeNode(<ArrayTypeNode>node);
22972297
case SyntaxKind.TypeLiteral:
22982298
return getTypeFromTypeLiteralNode(<TypeLiteralNode>node);
2299+
// This function assumes that an identifier or qualified name is a type expression
2300+
// Callers should first ensure this by calling isTypeNode
2301+
case SyntaxKind.Identifier:
2302+
case SyntaxKind.QualifiedName:
2303+
var symbol = getSymbolInfo(node);
2304+
return getDeclaredTypeOfSymbol(symbol);
22992305
default:
23002306
return unknownType;
23012307
}
@@ -6544,13 +6550,15 @@ module ts {
65446550
return mapToArray(symbols);
65456551
}
65466552

6547-
// True if the given identifier, string literal, or number literal is the name of a declaration node
6553+
// True if the given identifier is the name of a type declaration node (class, interface, enum, type parameter, etc)
65486554
function isTypeDeclarationName(name: Node): boolean {
6549-
return name.kind == SyntaxKind.Identifier && isTypeDeclaration(name.parent);
6555+
return name.kind == SyntaxKind.Identifier &&
6556+
isTypeDeclaration(name.parent) &&
6557+
(<Declaration>name.parent).name === name;
65506558
}
65516559

65526560
// True if the given identifier, string literal, or number literal is the name of a declaration node
6553-
function isDeclarationName(name: Node): boolean {
6561+
function isDeclarationOrFunctionExpressionOrCatchVariableName(name: Node): boolean {
65546562
if (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.StringLiteral && name.kind !== SyntaxKind.NumericLiteral) {
65556563
return false;
65566564
}
@@ -6607,7 +6615,6 @@ module ts {
66076615
}
66086616

66096617
function isExpression(node: Node): boolean {
6610-
// Omitted expression?
66116618
switch (node.kind) {
66126619
case SyntaxKind.ThisKeyword:
66136620
case SyntaxKind.SuperKeyword:
@@ -6629,6 +6636,7 @@ module ts {
66296636
case SyntaxKind.PostfixOperator:
66306637
case SyntaxKind.BinaryExpression:
66316638
case SyntaxKind.ConditionalExpression:
6639+
case SyntaxKind.OmittedExpression:
66326640
return true;
66336641
case SyntaxKind.QualifiedName:
66346642
while (node.parent.kind === SyntaxKind.QualifiedName) node = node.parent;
@@ -6641,14 +6649,6 @@ module ts {
66416649
case SyntaxKind.NumericLiteral:
66426650
case SyntaxKind.StringLiteral:
66436651
var parent = node.parent;
6644-
if (parent.kind === SyntaxKind.TypeAssertion) {
6645-
return node === (<TypeAssertion>parent).operand;
6646-
}
6647-
6648-
if (isExpression(parent)) {
6649-
return true;
6650-
}
6651-
66526652
switch (parent.kind) {
66536653
case SyntaxKind.VariableDeclaration:
66546654
case SyntaxKind.Parameter:
@@ -6667,10 +6667,18 @@ module ts {
66676667
case SyntaxKind.SwitchStatement:
66686668
return (<ExpressionStatement>parent).expression === node;
66696669
case SyntaxKind.ForStatement:
6670-
return (<ForStatement>parent).initializer === node || (<ForStatement>parent).condition === node ||
6670+
return (<ForStatement>parent).initializer === node ||
6671+
(<ForStatement>parent).condition === node ||
66716672
(<ForStatement>parent).iterator === node;
66726673
case SyntaxKind.ForInStatement:
6673-
return (<ForInStatement>parent).variable === node || (<ForInStatement>parent).expression === node;
6674+
return (<ForInStatement>parent).variable === node ||
6675+
(<ForInStatement>parent).expression === node;
6676+
case SyntaxKind.TypeAssertion:
6677+
return node === (<TypeAssertion>parent).operand;
6678+
default:
6679+
if (isExpression(parent)) {
6680+
return true;
6681+
}
66746682
}
66756683
}
66766684
return false;
@@ -6690,6 +6698,7 @@ module ts {
66906698
case SyntaxKind.VoidKeyword:
66916699
return node.parent.kind !== SyntaxKind.PrefixOperator;
66926700
case SyntaxKind.StringLiteral:
6701+
// Specialized signatures can have string literals as their parameters' type names
66936702
return node.parent.kind === SyntaxKind.Parameter;
66946703
// Identifiers and qualified names may be type nodes, depending on their context. Climb
66956704
// above them to find the lowest container
@@ -6700,6 +6709,7 @@ module ts {
67006709
}
67016710
// Fall through
67026711
case SyntaxKind.QualifiedName:
6712+
// At this point, node is either a qualified name or an identifier
67036713
var parent = node.parent;
67046714
if (parent.kind === SyntaxKind.TypeQuery) {
67056715
return false;
@@ -6710,7 +6720,7 @@ module ts {
67106720
//
67116721
// Calling isTypeNode would consider the qualified name A.B a type node. Only C or
67126722
// A.B.C is a type node.
6713-
if (node.kind >= SyntaxKind.FirstTypeNode && node.kind <= SyntaxKind.LastTypeNode) {
6723+
if (parent.kind >= SyntaxKind.FirstTypeNode && parent.kind <= SyntaxKind.LastTypeNode) {
67146724
return true;
67156725
}
67166726
switch (parent.kind) {
@@ -6764,7 +6774,7 @@ module ts {
67646774
}
67656775

67666776
function getSymbolOfIdentifier(identifier: Identifier) {
6767-
if (isDeclarationName(identifier)) {
6777+
if (isDeclarationOrFunctionExpressionOrCatchVariableName(identifier)) {
67686778
return getSymbolOfNode(identifier.parent);
67696779
}
67706780

@@ -6852,10 +6862,6 @@ module ts {
68526862
return getTypeOfExpression(<Expression>node);
68536863
}
68546864
if (isTypeNode(node)) {
6855-
if (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName) {
6856-
var symbol = getSymbolInfo(node);
6857-
return getDeclaredTypeOfSymbol(symbol);
6858-
}
68596865
return getTypeFromTypeNode(<TypeNode>node);
68606866
}
68616867

@@ -6876,7 +6882,7 @@ module ts {
68766882
return getTypeOfSymbol(symbol);
68776883
}
68786884

6879-
if (isDeclarationName(node)) {
6885+
if (isDeclarationOrFunctionExpressionOrCatchVariableName(node)) {
68806886
var symbol = getSymbolInfo(node);
68816887
return getTypeOfSymbol(symbol);
68826888
}

src/harness/typeWriter.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,18 @@ class TypeWriterWalker {
7676
private log(node: ts.Node, type: ts.Type): void {
7777
var actualPos = ts.skipTrivia(this.currentSourceFile.text, node.pos);
7878
var lineAndCharacter = this.currentSourceFile.getLineAndCharacterFromPosition(actualPos);
79-
var name = ts.getSourceTextOfNodeFromSourceText(this.currentSourceFile.text, node);
80-
var isUnkownType = (<ts.IntrinsicType>type).intrinsicName === "unknown";
79+
var sourceText = ts.getSourceTextOfNodeFromSourceText(this.currentSourceFile.text, node);
80+
var isUnknownType = (<ts.IntrinsicType>type).intrinsicName === "unknown";
8181

82+
// If we got an unknown type, we temporarily want to fall back to just pretending the name
83+
// (source text) of the node is the type. This is to align with the old typeWriter to make
84+
// baseline comparisons easier. In the long term, we will want to just call typeToString
8285
this.results.push({
8386
line: lineAndCharacter.line - 1,
8487
column: lineAndCharacter.character,
8588
syntaxKind: ts.SyntaxKind[node.kind],
86-
identifierName: name,
87-
type: isUnkownType ? name : this.checker.typeToString(type)
89+
identifierName: sourceText,
90+
type: isUnknownType ? sourceText : this.checker.typeToString(type)
8891
});
8992
}
9093

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ module ts {
16931693

16941694
// Right of dot member completion list
16951695
if (isRightOfDot) {
1696-
var type: Type = typeInfoResolver.getApparentType(typeInfoResolver.getTypeOfNode(mappedNode));
1696+
var type: ApparentType = typeInfoResolver.getApparentType(typeInfoResolver.getTypeOfNode(mappedNode));
16971697
if (!type) {
16981698
return undefined;
16991699
}

0 commit comments

Comments
 (0)