@@ -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 node is a valid ESTree class member
120
+ * @param {object } node AST node
121
+ * @returns {boolean } is valid 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 token is a comma
129
+ * @param {object } token the syntax 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 operator is the assignment operator
138
+ * @param {object } operator the operator
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 operator is a logical operator
147
+ * @param {object } operator the operator
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 operator
156
+ * @param {object } operator the operator
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 node or token,
195
+ * for the given AST
196
+ * @param {object } nodeOrToken the node or token
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 node
220
+ * @param {object } node the node
221
+ * @param {object } result result
222
+ * @param {[type] } ast the AST
223
+ * @returns {object } the node 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 token
275
+ * @param {object } token the token
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 token, for a given AST
331
+ * @param {object } token the token
332
+ * @param {object } ast the AST object
333
+ * @returns {object } the converted token
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 {array } the converted tokens
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 node
390
+ * @param {object } node the node
391
+ * @param {object } parent the parent node
392
+ * @returns {object } the converted node
393
+ */
323
394
function convert ( node , parent ) {
324
395
325
396
// exit early for null and undefined
@@ -333,12 +404,21 @@ module.exports = function(ast, extra) {
333
404
loc : getLoc ( node , ast )
334
405
} ;
335
406
407
+ /**
408
+ * Copies the result object
409
+ * @returns {void }
410
+ */
336
411
function simplyCopy ( ) {
337
412
assign ( result , {
338
413
type : SyntaxKind [ node . kind ]
339
414
} ) ;
340
415
}
341
416
417
+ /**
418
+ * Converts child node
419
+ * @param {object } child the child node
420
+ * @returns {object } the converted child node
421
+ */
342
422
function convertChild ( child ) {
343
423
return convert ( child , node ) ;
344
424
}
@@ -348,7 +428,7 @@ module.exports = function(ast, extra) {
348
428
assign ( result , {
349
429
type : "Program" ,
350
430
body : [ ] ,
351
- sourceType : node . externalModuleIndicator ? "module" : "script"
431
+ sourceType : node . externalModuleIndicator ? "module" : "script"
352
432
} ) ;
353
433
354
434
// filter out unknown nodes for now
@@ -531,10 +611,18 @@ module.exports = function(ast, extra) {
531
611
532
612
case SyntaxKind . VariableStatement :
533
613
614
+ var varStatementKind ;
615
+
616
+ if ( node . declarationList . flags ) {
617
+ varStatementKind = ( node . declarationList . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
618
+ } else {
619
+ varStatementKind = "var" ;
620
+ }
621
+
534
622
assign ( result , {
535
623
type : "VariableDeclaration" ,
536
624
declarations : node . declarationList . declarations . map ( convertChild ) ,
537
- kind : ( node . declarationList . flags ? ( node . declarationList . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
625
+ kind : varStatementKind
538
626
} ) ;
539
627
540
628
// check for exports
@@ -543,10 +631,19 @@ module.exports = function(ast, extra) {
543
631
544
632
// mostly for for-of, for-in
545
633
case SyntaxKind . VariableDeclarationList :
634
+
635
+ var varDeclarationListKind ;
636
+
637
+ if ( node . flags ) {
638
+ varDeclarationListKind = ( node . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
639
+ } else {
640
+ varDeclarationListKind = "var" ;
641
+ }
642
+
546
643
assign ( result , {
547
644
type : "VariableDeclaration" ,
548
645
declarations : node . declarations . map ( convertChild ) ,
549
- kind : ( node . flags ? ( node . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
646
+ kind : varDeclarationListKind
550
647
} ) ;
551
648
break ;
552
649
@@ -719,7 +816,7 @@ module.exports = function(ast, extra) {
719
816
// TypeScript uses this even for static methods named "constructor"
720
817
case SyntaxKind . Constructor :
721
818
722
- var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
819
+ var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
723
820
firstConstructorToken = constructorIsStatic ? ts . findNextToken ( node . getFirstToken ( ) , ast ) : node . getFirstToken ( ) ,
724
821
constructorOffset = 11 ,
725
822
constructorStartOffset = constructorOffset + firstConstructorToken . getStart ( ) - node . getFirstToken ( ) . getStart ( ) ,
@@ -807,13 +904,6 @@ module.exports = function(ast, extra) {
807
904
} ) ;
808
905
break ;
809
906
810
- case SyntaxKind . SpreadElementExpression :
811
- assign ( result , {
812
- type : "SpreadElement" ,
813
- argument : convertChild ( node . expression )
814
- } ) ;
815
- break ;
816
-
817
907
case SyntaxKind . ArrayBindingPattern :
818
908
assign ( result , {
819
909
type : "ArrayPattern" ,
@@ -997,7 +1087,7 @@ module.exports = function(ast, extra) {
997
1087
range : [ openBrace . getStart ( ) , result . range [ 1 ] ] ,
998
1088
loc : getLocFor ( openBrace . getStart ( ) , node . end , ast )
999
1089
} ,
1000
- superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null ) ,
1090
+ superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null )
1001
1091
} ) ;
1002
1092
1003
1093
var filteredMembers = node . members . filter ( isESTreeClassMember ) ;
0 commit comments