@@ -255,6 +255,26 @@ function fixExports(node, result, ast) {
255255 return result ;
256256}
257257
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 doesn'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+
258278/**
259279 * Returns true if a given TSNode is a JSX token
260280 * @param {TSNode } node TSNode to be checked
@@ -421,18 +441,22 @@ function convertToken(token, ast) {
421441 * @returns {ESTreeToken[] } the converted ESTreeTokens
422442 */
423443function 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 ) ;
432457 }
433- token = ts . findNextToken ( token , ast ) ;
434458 }
435-
459+ walk ( ast ) ;
436460 return result ;
437461}
438462
0 commit comments