@@ -12,13 +12,15 @@ import {
12
12
InterfaceDeclaration ,
13
13
ModuleDeclaration ,
14
14
Node ,
15
+ ScriptKind ,
15
16
ScriptTarget ,
16
17
SourceFile ,
17
18
SyntaxKind ,
18
19
TypeAliasDeclaration ,
19
20
VariableStatement ,
20
21
} from 'typescript' ;
21
22
23
+ import { parse } from 'path' ;
22
24
import { parseClass } from './node-parser/class-parser' ;
23
25
import { parseEnum } from './node-parser/enum-parser' ;
24
26
import { parseExport } from './node-parser/export-parser' ;
@@ -37,31 +39,39 @@ import { Resource } from './resources/Resource';
37
39
* This class is the parser of the whole extension. It uses the typescript compiler to parse a file or given
38
40
* source code into the token stream and therefore into the AST of the source. Afterwards an array of
39
41
* resources is generated and returned.
40
- *
42
+ *
41
43
* @export
42
44
* @class TypescriptParser
43
45
*/
44
46
export class TypescriptParser {
45
47
/**
46
48
* Parses the given source into an anonymous File resource.
47
49
* Mainly used to parse source code of a document.
48
- *
50
+ *
49
51
* @param {string } source
52
+ * @param {ScriptKind } [scriptKind=ScriptKind.TS]
50
53
* @returns {Promise<File> }
51
- *
54
+ *
52
55
* @memberof TsResourceParser
53
56
*/
54
- public async parseSource ( source : string ) : Promise < File > {
55
- return await this . parseTypescript ( createSourceFile ( 'inline.tsx' , source , ScriptTarget . ES2015 , true ) , '/' ) ;
57
+ public async parseSource ( source : string , scriptKind : ScriptKind = ScriptKind . TS ) : Promise < File > {
58
+ return await this . parseTypescript (
59
+ createSourceFile (
60
+ 'inline.tsx' ,
61
+ source ,
62
+ ScriptTarget . ES2015 ,
63
+ true ,
64
+ scriptKind ) ,
65
+ '/' ) ;
56
66
}
57
67
58
68
/**
59
69
* Parses a single file into a parsed file.
60
- *
70
+ *
61
71
* @param {string } filePath
62
72
* @param {string } rootPath
63
73
* @returns {Promise<File> }
64
- *
74
+ *
65
75
* @memberof TsResourceParser
66
76
*/
67
77
public async parseFile ( filePath : string , rootPath : string ) : Promise < File > {
@@ -71,28 +81,52 @@ export class TypescriptParser {
71
81
72
82
/**
73
83
* Parses multiple files into parsed files.
74
- *
84
+ *
75
85
* @param {string[] } filePathes
76
86
* @param {string } rootPath
77
87
* @returns {Promise<File[]> }
78
- *
88
+ *
79
89
* @memberof TsResourceParser
80
90
*/
81
- public async parseFiles ( filePathes : string [ ] , rootPath : string ) : Promise < File [ ] > {
91
+ public async parseFiles (
92
+ filePathes : string [ ] ,
93
+ rootPath : string ) : Promise < File [ ] > {
82
94
return filePathes
83
- . map ( o => createSourceFile ( o , readFileSync ( o ) . toString ( ) , ScriptTarget . ES2015 , true ) )
95
+ . map ( ( o ) => {
96
+ let scriptKind : ScriptKind = ScriptKind . Unknown ;
97
+ const parsed = parse ( o ) ;
98
+ switch ( parsed . ext . toLowerCase ( ) ) {
99
+ case 'js' :
100
+ scriptKind = ScriptKind . JS ;
101
+ break ;
102
+ case 'jsx' :
103
+ scriptKind = ScriptKind . JSX ;
104
+ break ;
105
+ case 'ts' :
106
+ scriptKind = ScriptKind . TS ;
107
+ break ;
108
+ case 'tsx' :
109
+ scriptKind = ScriptKind . TSX ;
110
+ break ;
111
+ }
112
+ return createSourceFile ( o ,
113
+ readFileSync ( o ) . toString ( ) ,
114
+ ScriptTarget . ES2015 ,
115
+ true ,
116
+ scriptKind ) ;
117
+ } )
84
118
. map ( o => this . parseTypescript ( o , rootPath ) ) ;
85
119
}
86
120
87
121
/**
88
122
* Parses the typescript source into the file instance. Calls .parse afterwards to
89
123
* get the declarations and other information about the source.
90
- *
124
+ *
91
125
* @private
92
126
* @param {SourceFile } source
93
127
* @param {string } rootPath
94
128
* @returns {TsFile }
95
- *
129
+ *
96
130
* @memberof TsResourceParser
97
131
*/
98
132
private parseTypescript ( source : SourceFile , rootPath : string ) : File {
@@ -108,11 +142,11 @@ export class TypescriptParser {
108
142
* Recursive function that runs through the AST of a source and parses the nodes.
109
143
* Creates the class / function / etc declarations and instanciates a new module / namespace
110
144
* resource if needed.
111
- *
145
+ *
112
146
* @private
113
147
* @param {Resource } resource
114
148
* @param {Node } node
115
- *
149
+ *
116
150
* @memberof TsResourceParser
117
151
*/
118
152
private parse ( resource : Resource , node : Node ) : void {
0 commit comments