@@ -37,8 +37,8 @@ var ts = require("typescript"),
37
37
// Private
38
38
//------------------------------------------------------------------------------
39
39
40
- var SyntaxKind = ts . SyntaxKind ,
41
- TokenClass = ts . TokenClass ;
40
+ var SyntaxKind = ts . SyntaxKind ;
41
+ // var TokenClass = ts.TokenClass;
42
42
43
43
var ASSIGNMENT_OPERATORS = [
44
44
SyntaxKind . EqualsToken ,
@@ -115,22 +115,47 @@ TOKEN_TO_TEXT[SyntaxKind.CaretEqualsToken] = "^=";
115
115
TOKEN_TO_TEXT [ SyntaxKind . AtToken ] = "@" ;
116
116
TOKEN_TO_TEXT [ SyntaxKind . InKeyword ] = "in" ;
117
117
118
+ /**
119
+ * Returns true if the given TSNode is a valid ESTree class member
120
+ * @param {TSNode } node TypeScript AST node
121
+ * @returns {boolean } is valid ESTree class member
122
+ */
118
123
function isESTreeClassMember ( node ) {
119
124
return node . kind !== SyntaxKind . PropertyDeclaration && node . kind !== SyntaxKind . SemicolonClassElement ;
120
125
}
121
126
127
+ /**
128
+ * Returns true if the given TSToken is a comma
129
+ * @param {TSToken } token the TypeScript token
130
+ * @returns {boolean } is comma
131
+ */
122
132
function isComma ( token ) {
123
133
return token . kind === SyntaxKind . CommaToken ;
124
134
}
125
135
136
+ /**
137
+ * Returns true if the given TSToken is the assignment operator
138
+ * @param {TSToken } operator the operator token
139
+ * @returns {boolean } is assignment
140
+ */
126
141
function isAssignmentOperator ( operator ) {
127
142
return ASSIGNMENT_OPERATORS . indexOf ( operator . kind ) > - 1 ;
128
143
}
129
144
145
+ /**
146
+ * Returns true if the given TSToken is a logical operator
147
+ * @param {TSToken } operator the operator token
148
+ * @returns {boolean } is a logical operator
149
+ */
130
150
function isLogicalOperator ( operator ) {
131
151
return LOGICAL_OPERATORS . indexOf ( operator . kind ) > - 1 ;
132
152
}
133
153
154
+ /**
155
+ * Returns the binary expression type of the given TSToken
156
+ * @param {TSToken } operator the operator token
157
+ * @returns {string } the binary expression type
158
+ */
134
159
function getBinaryExpressionType ( operator ) {
135
160
if ( isAssignmentOperator ( operator ) ) {
136
161
return "AssignmentExpression" ;
@@ -141,6 +166,14 @@ function getBinaryExpressionType(operator) {
141
166
}
142
167
}
143
168
169
+ /**
170
+ * Returns line and column data for the given start and end positions,
171
+ * for the given AST
172
+ * @param {Object } start start data
173
+ * @param {Object } end end data
174
+ * @param {Object } ast the AST object
175
+ * @returns {Object } the loc data
176
+ */
144
177
function getLocFor ( start , end , ast ) {
145
178
var startLoc = ast . getLineAndCharacterOfPosition ( start ) ,
146
179
endLoc = ast . getLineAndCharacterOfPosition ( end ) ;
@@ -157,6 +190,13 @@ function getLocFor(start, end, ast) {
157
190
} ;
158
191
}
159
192
193
+ /**
194
+ * Returns line and column data for the given ESTreeNode or ESTreeToken,
195
+ * for the given AST
196
+ * @param {ESTreeToken|ESTreeNode } nodeOrToken the ESTreeNode or ESTreeToken
197
+ * @param {Object } ast the AST object
198
+ * @returns {Object } the loc data
199
+ */
160
200
function getLoc ( nodeOrToken , ast ) {
161
201
return getLocFor ( nodeOrToken . getStart ( ) , nodeOrToken . end , ast ) ;
162
202
// var start = nodeOrToken.getStart(),
@@ -175,6 +215,13 @@ function getLoc(nodeOrToken, ast) {
175
215
// };
176
216
}
177
217
218
+ /**
219
+ * Fixes the exports of the given TSNode
220
+ * @param {TSNode } node the TSNode
221
+ * @param {Object } result result
222
+ * @param {Object } ast the AST
223
+ * @returns {TSNode } the TSNode with fixed exports
224
+ */
178
225
function fixExports ( node , result , ast ) {
179
226
// check for exports
180
227
if ( node . modifiers && node . modifiers [ 0 ] . kind === SyntaxKind . ExportKeyword ) {
@@ -206,6 +253,11 @@ function fixExports(node, result, ast) {
206
253
return result ;
207
254
}
208
255
256
+ /**
257
+ * Extends and formats a given error object
258
+ * @param {Object } error the error object
259
+ * @returns {Object } converted error object
260
+ */
209
261
function convertError ( error ) {
210
262
211
263
var loc = error . file . getLineAndCharacterOfPosition ( error . start ) ;
@@ -218,9 +270,13 @@ function convertError(error) {
218
270
} ;
219
271
}
220
272
273
+ /**
274
+ * Returns the type of a given ESTreeToken
275
+ * @param {ESTreeToken } token the ESTreeToken
276
+ * @returns {string } the token type
277
+ */
221
278
function getTokenType ( token ) {
222
279
223
-
224
280
// Need two checks for keywords since some are also identifiers
225
281
if ( token . originalKeywordKind ) {
226
282
@@ -233,7 +289,7 @@ function getTokenType(token) {
233
289
return "Identifier" ;
234
290
235
291
default :
236
- return "Keyword"
292
+ return "Keyword" ;
237
293
}
238
294
}
239
295
@@ -264,19 +320,23 @@ function getTokenType(token) {
264
320
case SyntaxKind . GetKeyword :
265
321
case SyntaxKind . SetKeyword :
266
322
// falls through
323
+ default :
267
324
}
268
325
269
-
270
-
271
-
272
326
return "Identifier" ;
273
327
}
274
328
329
+ /**
330
+ * Extends and formats a given ESTreeToken, for a given AST
331
+ * @param {ESTreeToken } token the ESTreeToken
332
+ * @param {Object } ast the AST object
333
+ * @returns {ESTreeToken } the converted ESTreeToken
334
+ */
275
335
function convertToken ( token , ast ) {
276
336
277
337
var start = token . getStart ( ) ,
278
338
value = ast . text . slice ( start , token . end ) ,
279
- newToken = {
339
+ newToken = {
280
340
type : getTokenType ( token ) ,
281
341
value : value ,
282
342
range : [ start , token . end ] ,
@@ -293,6 +353,11 @@ function convertToken(token, ast) {
293
353
return newToken ;
294
354
}
295
355
356
+ /**
357
+ * Converts all tokens for the given AST
358
+ * @param {Object } ast the AST object
359
+ * @returns {ESTreeToken[] } the converted ESTreeTokens
360
+ */
296
361
function convertTokens ( ast ) {
297
362
var token = ast . getFirstToken ( ) ,
298
363
converted ,
@@ -320,6 +385,12 @@ module.exports = function(ast, extra) {
320
385
throw convertError ( ast . parseDiagnostics [ 0 ] ) ;
321
386
}
322
387
388
+ /**
389
+ * Converts a TypeScript node into an ESTree node
390
+ * @param {TSNode } node the TSNode
391
+ * @param {TSNode } parent the parent TSNode
392
+ * @returns {ESTreeNode } the converted ESTreeNode
393
+ */
323
394
function convert ( node , parent ) {
324
395
325
396
// exit early for null and undefined
@@ -333,12 +404,22 @@ module.exports = function(ast, extra) {
333
404
loc : getLoc ( node , ast )
334
405
} ;
335
406
407
+ /**
408
+ * Copies the result object into an ESTree node with just a type property.
409
+ * This is used only for leaf nodes that have no other properties.
410
+ * @returns {void }
411
+ */
336
412
function simplyCopy ( ) {
337
413
assign ( result , {
338
414
type : SyntaxKind [ node . kind ]
339
415
} ) ;
340
416
}
341
417
418
+ /**
419
+ * Converts a TypeScript node into an ESTree node.
420
+ * @param {TSNode } child the child TSNode
421
+ * @returns {ESTreeNode } the converted ESTree node
422
+ */
342
423
function convertChild ( child ) {
343
424
return convert ( child , node ) ;
344
425
}
@@ -348,7 +429,7 @@ module.exports = function(ast, extra) {
348
429
assign ( result , {
349
430
type : "Program" ,
350
431
body : [ ] ,
351
- sourceType : node . externalModuleIndicator ? "module" : "script"
432
+ sourceType : node . externalModuleIndicator ? "module" : "script"
352
433
} ) ;
353
434
354
435
// filter out unknown nodes for now
@@ -531,10 +612,18 @@ module.exports = function(ast, extra) {
531
612
532
613
case SyntaxKind . VariableStatement :
533
614
615
+ var varStatementKind ;
616
+
617
+ if ( node . declarationList . flags ) {
618
+ varStatementKind = ( node . declarationList . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
619
+ } else {
620
+ varStatementKind = "var" ;
621
+ }
622
+
534
623
assign ( result , {
535
624
type : "VariableDeclaration" ,
536
625
declarations : node . declarationList . declarations . map ( convertChild ) ,
537
- kind : ( node . declarationList . flags ? ( node . declarationList . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
626
+ kind : varStatementKind
538
627
} ) ;
539
628
540
629
// check for exports
@@ -543,10 +632,19 @@ module.exports = function(ast, extra) {
543
632
544
633
// mostly for for-of, for-in
545
634
case SyntaxKind . VariableDeclarationList :
635
+
636
+ var varDeclarationListKind ;
637
+
638
+ if ( node . flags ) {
639
+ varDeclarationListKind = ( node . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
640
+ } else {
641
+ varDeclarationListKind = "var" ;
642
+ }
643
+
546
644
assign ( result , {
547
645
type : "VariableDeclaration" ,
548
646
declarations : node . declarations . map ( convertChild ) ,
549
- kind : ( node . flags ? ( node . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
647
+ kind : varDeclarationListKind
550
648
} ) ;
551
649
break ;
552
650
@@ -719,7 +817,7 @@ module.exports = function(ast, extra) {
719
817
// TypeScript uses this even for static methods named "constructor"
720
818
case SyntaxKind . Constructor :
721
819
722
- var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
820
+ var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
723
821
firstConstructorToken = constructorIsStatic ? ts . findNextToken ( node . getFirstToken ( ) , ast ) : node . getFirstToken ( ) ,
724
822
constructorOffset = 11 ,
725
823
constructorStartOffset = constructorOffset + firstConstructorToken . getStart ( ) - node . getFirstToken ( ) . getStart ( ) ,
@@ -807,13 +905,6 @@ module.exports = function(ast, extra) {
807
905
} ) ;
808
906
break ;
809
907
810
- case SyntaxKind . SpreadElementExpression :
811
- assign ( result , {
812
- type : "SpreadElement" ,
813
- argument : convertChild ( node . expression )
814
- } ) ;
815
- break ;
816
-
817
908
case SyntaxKind . ArrayBindingPattern :
818
909
assign ( result , {
819
910
type : "ArrayPattern" ,
@@ -997,7 +1088,7 @@ module.exports = function(ast, extra) {
997
1088
range : [ openBrace . getStart ( ) , result . range [ 1 ] ] ,
998
1089
loc : getLocFor ( openBrace . getStart ( ) , node . end , ast )
999
1090
} ,
1000
- superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null ) ,
1091
+ superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null )
1001
1092
} ) ;
1002
1093
1003
1094
var filteredMembers = node . members . filter ( isESTreeClassMember ) ;
0 commit comments