@@ -255,6 +255,26 @@ function fixExports(node, result, ast) {
255
255
return result ;
256
256
}
257
257
258
+ /**
259
+ * Returns true if a given TSNode is a token
260
+ * @param {TSNode } node the TSNode
261
+ * @returns {boolean } is a token
262
+ */
263
+ function isToken ( node ) {
264
+ /**
265
+ * Note: We treat JsxText like a token (TypeScript don't classify it as one),
266
+ * to prevent traversing into it and creating unintended addiontal tokens from
267
+ * its contents.
268
+ *
269
+ * It looks like this has been addressed in the master branch of TypeScript, so we can
270
+ * look to remove this extra check as part of the 2.2.x support
271
+ */
272
+ if ( node . kind === ts . SyntaxKind . JsxText ) {
273
+ return true ;
274
+ }
275
+ return node . kind >= ts . SyntaxKind . FirstToken && node . kind <= ts . SyntaxKind . LastToken ;
276
+ }
277
+
258
278
/**
259
279
* Returns true if a given TSNode is a JSX token
260
280
* @param {TSNode } node TSNode to be checked
@@ -421,18 +441,22 @@ function convertToken(token, ast) {
421
441
* @returns {ESTreeToken[] } the converted ESTreeTokens
422
442
*/
423
443
function convertTokens ( ast ) {
424
- var token = ast . getFirstToken ( ) ,
425
- converted ,
426
- result = [ ] ;
427
-
428
- while ( token ) {
429
- converted = convertToken ( token , ast ) ;
430
- if ( converted ) {
431
- result . push ( converted ) ;
444
+ var result = [ ] ;
445
+ /**
446
+ * @param {TSNode } node the TSNode
447
+ * @returns {undefined }
448
+ */
449
+ function walk ( node ) {
450
+ if ( isToken ( node ) && node . kind !== ts . SyntaxKind . EndOfFileToken ) {
451
+ var converted = convertToken ( node , ast ) ;
452
+ if ( converted ) {
453
+ result . push ( converted ) ;
454
+ }
455
+ } else {
456
+ node . getChildren ( ) . forEach ( walk ) ;
432
457
}
433
- token = ts . findNextToken ( token , ast ) ;
434
458
}
435
-
459
+ walk ( ast ) ;
436
460
return result ;
437
461
}
438
462
0 commit comments