Skip to content

Commit f4c0045

Browse files
committed
Print types for arbitrary expressions in typeWriter
1 parent 537d878 commit f4c0045

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/harness/typeWriter.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,43 @@ class TypeWriterWalker {
2222
}
2323

2424
private visitNode(node: ts.Node): void {
25-
if (node.kind === ts.SyntaxKind.Identifier) {
26-
var identifier = <ts.Identifier>node;
27-
if (!this.isLabel(identifier)) {
28-
var type = this.getTypeOfIdentifier(identifier);
29-
this.log(node, type);
30-
}
31-
}
32-
else if (node.kind === ts.SyntaxKind.ThisKeyword) {
33-
this.log(node, undefined);
34-
}
35-
else {
36-
ts.forEachChild(node, child => this.visitNode(child));
25+
switch (node.kind) {
26+
// Should always log expressions that are not tokens
27+
// Also, always log the "this" keyword
28+
// TODO: Ideally we should log all expressions, but to compare to the
29+
// old typeWriter baselines, suppress tokens
30+
case ts.SyntaxKind.ThisKeyword:
31+
case ts.SyntaxKind.RegularExpressionLiteral:
32+
case ts.SyntaxKind.ArrayLiteral:
33+
case ts.SyntaxKind.ObjectLiteral:
34+
case ts.SyntaxKind.PropertyAccess:
35+
case ts.SyntaxKind.IndexedAccess:
36+
case ts.SyntaxKind.CallExpression:
37+
case ts.SyntaxKind.NewExpression:
38+
case ts.SyntaxKind.TypeAssertion:
39+
case ts.SyntaxKind.ParenExpression:
40+
case ts.SyntaxKind.FunctionExpression:
41+
case ts.SyntaxKind.ArrowFunction:
42+
case ts.SyntaxKind.PrefixOperator:
43+
case ts.SyntaxKind.PostfixOperator:
44+
case ts.SyntaxKind.BinaryExpression:
45+
case ts.SyntaxKind.ConditionalExpression:
46+
this.log(node, this.getTypeOfNode(node));
47+
break;
48+
49+
// Should not change expression status (maybe expressions)
50+
// TODO: Again, ideally should log number and string literals too,
51+
// but to be consistent with the old typeWriter, just log identifiers
52+
case ts.SyntaxKind.Identifier:
53+
var identifier = <ts.Identifier>node;
54+
if (!this.isLabel(identifier)) {
55+
var type = this.getTypeOfNode(identifier);
56+
this.log(node, type);
57+
}
58+
break;
3759
}
60+
61+
ts.forEachChild(node, child => this.visitNode(child));
3862
}
3963

4064
private isLabel(identifier: ts.Identifier): boolean {
@@ -63,10 +87,8 @@ class TypeWriterWalker {
6387
});
6488
}
6589

66-
private getTypeOfIdentifier(node: ts.Identifier): ts.Type {
67-
var identifierSymbol = this.checker.getSymbolInfo(node);
68-
ts.Debug.assert(identifierSymbol, "symbol doesn't exist");
69-
var type = this.checker.getTypeOfSymbol(identifierSymbol);
90+
private getTypeOfNode(node: ts.Node): ts.Type {
91+
var type = this.checker.getTypeOfNode(node, /*apparentType*/ false);
7092
ts.Debug.assert(type, "type doesn't exist");
7193
return type;
7294
}

0 commit comments

Comments
 (0)