-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathTypescriptParser.ts
160 lines (152 loc) · 5.61 KB
/
TypescriptParser.ts
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
import { readFileSync } from 'fs';
import {
ClassDeclaration,
createSourceFile,
EnumDeclaration,
ExportAssignment,
ExportDeclaration,
FunctionDeclaration,
Identifier,
ImportDeclaration,
ImportEqualsDeclaration,
InterfaceDeclaration,
ModuleDeclaration,
Node,
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 { 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
* @returns {Promise<File>}
*
* @memberof TsResourceParser
*/
public async parseSource(source: string): Promise<File> {
return await this.parseTypescript(createSourceFile('inline.ts', source, ScriptTarget.ES2015, true), '/');
}
/**
* 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 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 filePathes
.map(o => createSourceFile(o, readFileSync(o).toString(), ScriptTarget.ES2015, true))
.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, node: Node): void {
for (const child of node.getChildren()) {
switch (child.kind) {
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
parseImport(resource, <ImportDeclaration | ImportEqualsDeclaration>child);
break;
case SyntaxKind.ExportDeclaration:
case SyntaxKind.ExportAssignment:
parseExport(resource, <ExportAssignment | ExportDeclaration>child);
break;
case SyntaxKind.EnumDeclaration:
parseEnum(resource, <EnumDeclaration>child);
break;
case SyntaxKind.TypeAliasDeclaration:
parseTypeAlias(resource, <TypeAliasDeclaration>child);
break;
case SyntaxKind.FunctionDeclaration:
parseFunction(resource, <FunctionDeclaration>child);
continue;
case SyntaxKind.VariableStatement:
parseVariable(resource, <VariableStatement>child);
break;
case SyntaxKind.InterfaceDeclaration:
parseInterface(resource, <InterfaceDeclaration>child);
break;
case SyntaxKind.ClassDeclaration:
parseClass(resource, <ClassDeclaration>child);
continue;
case SyntaxKind.Identifier:
parseIdentifier(resource, <Identifier>child);
break;
case SyntaxKind.ModuleDeclaration:
const newResource = parseModule(resource, <ModuleDeclaration>child);
this.parse(newResource, child);
continue;
default:
break;
}
this.parse(resource, child);
}
}
}