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
180 lines (163 loc) · 6.8 KB
/
typeWriter.ts
File metadata and controls
180 lines (163 loc) · 6.8 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/** TODO: Rewrite entirely **/
class TypeWriterWalker {
constructor(public filename: string, public compiler: any) {
}
public run() { }
public results: any[];
}
/*
class TypeWriterWalker extends TypeScript.SyntaxWalker {
private document: TypeScript.Document;
private syntaxTree: TypeScript.SyntaxTree;
private text: TypeScript.ISimpleText;
private currentPosition = 0;
public results: {
line: number;
column: number;
syntaxKind: string;
identifierName: string;
type: string;
}[] = [];
constructor(public filename: string, public compiler: TypeScript.TypeScriptCompiler) {
super();
this.document = compiler.getDocument(filename);
this.syntaxTree = this.document.syntaxTree();
this.text = this.syntaxTree.text;
}
public run() {
TypeScript.visitNodeOrToken(this, this.syntaxTree.sourceUnit());
}
private isName(token: TypeScript.ISyntaxToken) {
var parent = token.parent;
switch (parent.kind()) {
case TypeScript.SyntaxKind.ContinueStatement:
return (<TypeScript.ContinueStatementSyntax>parent).identifier === token;
case TypeScript.SyntaxKind.BreakStatement:
return (<TypeScript.BreakStatementSyntax>parent).identifier === token;
case TypeScript.SyntaxKind.LabeledStatement:
return (<TypeScript.LabeledStatementSyntax>parent).identifier === token;
}
return false;
}
public visitToken(token: TypeScript.ISyntaxToken) {
if (token.kind() === TypeScript.SyntaxKind.IdentifierName) {
if (!this.isName(token)) {
this.log(token);
}
} else if (token.kind() === TypeScript.SyntaxKind.ThisKeyword) {
this.log(token);
}
return super.visitToken(token);
}
public visitNode(node: TypeScript.ISyntaxNode) {
return super.visitNode(node);
}
private getAstForElement(element: TypeScript.ISyntaxElement) {
if (!TypeScript.isShared(element)) {
return element;
}
}
private getEnclosingScopeSymbol(ast: TypeScript.ISyntaxElement): TypeScript.PullSymbol {
var enclosingScopeAST = TypeScript.DeclarationEmitter.getEnclosingContainer(ast);
if (enclosingScopeAST) {
var typeInfo = this.compiler.pullGetSymbolInformationFromAST(enclosingScopeAST, this.document);
return typeInfo ? typeInfo.symbol : null;
}
return null;
}
private getTypeOfElement(element: TypeScript.ISyntaxElement) {
var ast = this.getAstForElement(element);
if (ast) {
var typeInfo = this.compiler.pullGetSymbolInformationFromAST(ast, this.document);
if (typeInfo.symbol && typeInfo.symbol.type) {
var enclosingScope = this.getEnclosingScopeSymbol(ast);
return typeInfo.symbol.type.toString(enclosingScope);
}
}
return "<unknown>";
}
public visitPrefixUnaryExpression(node: TypeScript.PrefixUnaryExpressionSyntax) {
this.log(node);
return super.visitPrefixUnaryExpression(node);
}
public visitArrayLiteralExpression(node: TypeScript.ArrayLiteralExpressionSyntax) {
this.log(node);
return super.visitArrayLiteralExpression(node);
}
public visitOmittedExpression(node: TypeScript.OmittedExpressionSyntax) {
this.log(node);
return super.visitOmittedExpression(node);
}
public visitParenthesizedExpression(node: TypeScript.ParenthesizedExpressionSyntax) {
this.log(node);
return super.visitParenthesizedExpression(node);
}
public visitSimpleArrowFunctionExpression(node: TypeScript.SimpleArrowFunctionExpressionSyntax) {
this.log(node);
return super.visitSimpleArrowFunctionExpression(node);
}
public visitParenthesizedArrowFunctionExpression(node: TypeScript.ParenthesizedArrowFunctionExpressionSyntax) {
this.log(node);
return super.visitParenthesizedArrowFunctionExpression(node);
}
public visitObjectCreationExpression(node: TypeScript.ObjectCreationExpressionSyntax) {
this.log(node);
return super.visitObjectCreationExpression(node);
}
public visitCastExpression(node: TypeScript.CastExpressionSyntax) {
this.log(node);
return super.visitCastExpression(node);
}
public visitObjectLiteralExpression(node: TypeScript.ObjectLiteralExpressionSyntax) {
this.log(node);
return super.visitObjectLiteralExpression(node);
}
public visitFunctionExpression(node: TypeScript.FunctionExpressionSyntax) {
this.log(node);
return super.visitFunctionExpression(node);
}
public visitTypeOfExpression(node: TypeScript.TypeOfExpressionSyntax) {
this.log(node);
return super.visitTypeOfExpression(node);
}
public visitDeleteExpression(node: TypeScript.DeleteExpressionSyntax) {
this.log(node);
return super.visitDeleteExpression(node);
}
public visitVoidExpression(node: TypeScript.VoidExpressionSyntax) {
this.log(node);
return super.visitVoidExpression(node);
}
public visitMemberAccessExpression(node: TypeScript.MemberAccessExpressionSyntax) {
this.log(node);
return super.visitMemberAccessExpression(node);
}
public visitPostfixUnaryExpression(node: TypeScript.PostfixUnaryExpressionSyntax) {
this.log(node);
return super.visitPostfixUnaryExpression(node);
}
public visitElementAccessExpression(node: TypeScript.ElementAccessExpressionSyntax) {
this.log(node);
return super.visitElementAccessExpression(node);
}
public visitInvocationExpression(node: TypeScript.InvocationExpressionSyntax) {
this.log(node);
return super.visitInvocationExpression(node);
}
public visitBinaryExpression(node: TypeScript.BinaryExpressionSyntax) {
this.log(node);
return super.visitBinaryExpression(node);
}
public visitConditionalExpression(node: TypeScript.ConditionalExpressionSyntax) {
this.log(node);
return super.visitConditionalExpression(node);
}
public log(node: TypeScript.ISyntaxNodeOrToken) {
var _fullStart = TypeScript.fullStart(node);
if (_fullStart >= 0) {
var pos = this.document.lineMap().getLineAndCharacterFromPosition(_fullStart);
this.results.push({ line: pos.line(), column: pos.character(), syntaxKind: TypeScript.SyntaxKind[node.kind()], identifierName: TypeScript.fullText(node, this.text).trim(), type: this.getTypeOfElement(node) });
}
}
}
*/