-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTypescriptParser.ts
More file actions
246 lines (234 loc) · 8.49 KB
/
TypescriptParser.ts
File metadata and controls
246 lines (234 loc) · 8.49 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { readFileSync } from 'fs';
import { parse } from 'path';
import {
ClassDeclaration,
createSourceFile,
EnumDeclaration,
ExportAssignment,
ExportDeclaration,
FunctionDeclaration,
Identifier,
ImportDeclaration,
ImportEqualsDeclaration,
InterfaceDeclaration,
ModuleDeclaration,
Node,
ScriptKind,
ScriptTarget,
SourceFile,
SyntaxKind,
TypeAliasDeclaration,
VariableStatement,
} from 'typescript';
import { parseClass } from './node-parser/class-parser';
import { parseEnum } from './node-parser/enum-parser';
import { parseExport } from './node-parser/export-parser';
import { parseFunction } from './node-parser/function-parser';
import { parseIdentifier } from './node-parser/identifier-parser';
import { parseImport } from './node-parser/import-parser';
import { parseInterface } from './node-parser/interface-parser';
import { parseModule } from './node-parser/module-parser';
import { traverseAst } from './node-parser/traverse-ast';
import { parseTypeAlias } from './node-parser/type-alias-parser';
import { parseVariable } from './node-parser/variable-parser';
import { File } from './resources/File';
import { Resource } from './resources/Resource';
/**
* Magic.happens('here');
* This class is the parser of the whole extension. It uses the typescript compiler to parse a file or given
* source code into the token stream and therefore into the AST of the source. Afterwards an array of
* resources is generated and returned.
*
* @export
* @class TypescriptParser
*/
export class TypescriptParser {
/**
* Parses the given source into an anonymous File resource.
* Mainly used to parse source code of a document.
*
* @param {string} source
* @param {ScriptKind} [scriptKind=ScriptKind.TS]
* @returns {Promise<File>}
*
* @memberof TsResourceParser
*/
public async parseSource(source: string, scriptKind: ScriptKind = ScriptKind.TS): Promise<File> {
return await this.parseTypescript(
createSourceFile(
'inline.tsx',
source,
ScriptTarget.ES2015,
true,
scriptKind),
'/');
}
/**
* Parses a single file into a parsed file.
*
* @param {string} filePath
* @param {string} rootPath
* @returns {Promise<File>}
*
* @memberof TsResourceParser
*/
public async parseFile(filePath: string, rootPath: string): Promise<File> {
const parse = await this.parseFiles([filePath], rootPath);
return parse[0];
}
/**
* Parses a single file into a parsed file synchronously.
*
* @param {string} filePath
* @param {string} rootPath
* @returns {File}
*
* @memberof TsResourceParser
*/
public parseFileSync(filePath: string, rootPath: string): File {
const parse = this.parseFilesSync([filePath], rootPath);
return parse[0];
}
/**
* Parses multiple files into parsed files.
*
* @param {string[]} filePathes
* @param {string} rootPath
* @returns {Promise<File[]>}
*
* @memberof TsResourceParser
*/
public async parseFiles(
filePathes: string[],
rootPath: string): Promise<File[]> {
return this.parseFilesSync(filePathes, rootPath);
}
/**
* Parses multiple files into parsed files Synchronously.
*
* @param {string[]} filePathes
* @param {string} rootPath
* @returns {File[]}
*
* @memberof TsResourceParser
*/
public parseFilesSync(
filePathes: string[],
rootPath: string): File[] {
return filePathes
.map((o) => {
let scriptKind: ScriptKind = ScriptKind.Unknown;
const parsed = parse(o);
switch (parsed.ext.toLowerCase()) {
case 'js':
scriptKind = ScriptKind.JS;
break;
case 'jsx':
scriptKind = ScriptKind.JSX;
break;
case 'ts':
scriptKind = ScriptKind.TS;
break;
case 'tsx':
scriptKind = ScriptKind.TSX;
break;
}
return createSourceFile(
o,
readFileSync(o).toString(),
ScriptTarget.ES2015,
true,
scriptKind,
);
})
.map(o => this.parseTypescript(o, rootPath));
}
/**
* Parses the typescript source into the file instance. Calls .parse afterwards to
* get the declarations and other information about the source.
*
* @private
* @param {SourceFile} source
* @param {string} rootPath
* @returns {TsFile}
*
* @memberof TsResourceParser
*/
private parseTypescript(source: SourceFile, rootPath: string): File {
const file = new File(source.fileName, rootPath, source.getStart(), source.getEnd());
const syntaxList = source.getChildAt(0);
this.parse(file, syntaxList);
return file;
}
/**
* Recursive function that runs through the AST of a source and parses the nodes.
* Creates the class / function / etc declarations and instanciates a new module / namespace
* resource if needed.
*
* @private
* @param {Resource} resource
* @param {Node} node
*
* @memberof TsResourceParser
*/
private parse(resource: Resource, root: Node): void {
const modules = [{ moduleRoot: root, moduleResource: resource }];
for (let iter = modules.shift(); iter !== undefined; iter = modules.shift()) {
const { moduleRoot, moduleResource } = iter;
traverseAst(
moduleRoot as any,
(node: any) => {
switch (node.kind) {
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
parseImport(moduleResource, <ImportDeclaration | ImportEqualsDeclaration>node);
break;
case SyntaxKind.ExportDeclaration:
case SyntaxKind.ExportAssignment:
parseExport(moduleResource, <ExportAssignment | ExportDeclaration>node);
break;
case SyntaxKind.EnumDeclaration:
parseEnum(moduleResource, <EnumDeclaration>node);
break;
case SyntaxKind.TypeAliasDeclaration:
parseTypeAlias(moduleResource, <TypeAliasDeclaration>node);
break;
case SyntaxKind.FunctionDeclaration:
parseFunction(moduleResource, <FunctionDeclaration>node);
break;
case SyntaxKind.VariableStatement:
parseVariable(moduleResource, <VariableStatement>node);
break;
case SyntaxKind.InterfaceDeclaration:
parseInterface(moduleResource, <InterfaceDeclaration>node);
break;
case SyntaxKind.ClassDeclaration:
parseClass(moduleResource, <ClassDeclaration>node);
break;
case SyntaxKind.Identifier:
parseIdentifier(moduleResource, <Identifier>node);
break;
case SyntaxKind.ModuleDeclaration:
modules.push({
moduleRoot: node,
moduleResource: parseModule(moduleResource, <ModuleDeclaration>node),
});
break;
default:
break;
}
},
(node: any) => {
switch (node.kind) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.FunctionDeclaration:
return true;
default:
return false;
}
},
);
}
}
}