forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeWriter.ts
More file actions
98 lines (88 loc) · 3.99 KB
/
Copy pathtypeWriter.ts
File metadata and controls
98 lines (88 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
interface TypeWriterResult {
line: number;
column: number;
syntaxKind: number;
sourceText: string;
type: string;
}
class TypeWriterWalker {
results: TypeWriterResult[];
currentSourceFile: ts.SourceFile;
constructor(public checker: ts.TypeChecker) {
}
public getTypes(fileName: string): TypeWriterResult[] {
var sourceFile = this.checker.getProgram().getSourceFile(fileName);
this.currentSourceFile = sourceFile;
this.results = [];
this.visitNode(sourceFile);
return this.results;
}
private visitNode(node: ts.Node): void {
switch (node.kind) {
// Should always log expressions that are not tokens
// Also, always log the "this" keyword
// TODO: Ideally we should log all expressions, but to compare to the
// old typeWriter baselines, suppress tokens
case ts.SyntaxKind.ThisKeyword:
case ts.SyntaxKind.SuperKeyword:
case ts.SyntaxKind.ArrayLiteral:
case ts.SyntaxKind.ObjectLiteral:
case ts.SyntaxKind.PropertyAccess:
case ts.SyntaxKind.IndexedAccess:
case ts.SyntaxKind.CallExpression:
case ts.SyntaxKind.NewExpression:
case ts.SyntaxKind.TypeAssertion:
case ts.SyntaxKind.ParenExpression:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.PrefixOperator:
case ts.SyntaxKind.PostfixOperator:
case ts.SyntaxKind.BinaryExpression:
case ts.SyntaxKind.ConditionalExpression:
this.log(node, this.getTypeOfNode(node));
break;
// Should not change expression status (maybe expressions)
// TODO: Again, ideally should log number and string literals too,
// but to be consistent with the old typeWriter, just log identifiers
case ts.SyntaxKind.Identifier:
var identifier = <ts.Identifier>node;
if (!this.isLabel(identifier)) {
var type = this.getTypeOfNode(identifier);
this.log(node, type);
}
break;
}
ts.forEachChild(node, child => this.visitNode(child));
}
private isLabel(identifier: ts.Identifier): boolean {
var parent = identifier.parent;
switch (parent.kind) {
case ts.SyntaxKind.ContinueStatement:
case ts.SyntaxKind.BreakStatement:
return (<ts.BreakOrContinueStatement>parent).label === identifier;
case ts.SyntaxKind.LabeledStatement:
return (<ts.LabeledStatement>parent).label === identifier;
}
return false;
}
private log(node: ts.Node, type: ts.Type): void {
var actualPos = ts.skipTrivia(this.currentSourceFile.text, node.pos);
var lineAndCharacter = this.currentSourceFile.getLineAndCharacterFromPosition(actualPos);
var sourceText = ts.getTextOfNodeFromSourceText(this.currentSourceFile.text, node);
// If we got an unknown type, we temporarily want to fall back to just pretending the name
// (source text) of the node is the type. This is to align with the old typeWriter to make
// baseline comparisons easier. In the long term, we will want to just call typeToString
this.results.push({
line: lineAndCharacter.line - 1,
column: lineAndCharacter.character,
syntaxKind: node.kind,
sourceText: sourceText,
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.WriteOwnNameForAnyLike)
});
}
private getTypeOfNode(node: ts.Node): ts.Type {
var type = this.checker.getTypeOfNode(node);
ts.Debug.assert(type !== undefined, "type doesn't exist");
return type;
}
}