Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit d613bff

Browse files
committed
Chore: Optimize convertTokens() as per TS Team feedback
1 parent 0b343ab commit d613bff

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lib/ast-converter.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ 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+
return node.kind >= ts.SyntaxKind.FirstToken && node.kind <= ts.SyntaxKind.LastToken;
265+
}
266+
258267
/**
259268
* Returns true if a given TSNode is a JSX token
260269
* @param {TSNode} node TSNode to be checked
@@ -421,18 +430,22 @@ function convertToken(token, ast) {
421430
* @returns {ESTreeToken[]} the converted ESTreeTokens
422431
*/
423432
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);
433+
var result = [];
434+
/**
435+
* @param {TSNode} node the TSNode
436+
* @returns {undefined}
437+
*/
438+
function walk(node) {
439+
if (isToken(node) && node.kind !== ts.SyntaxKind.EndOfFileToken) {
440+
var converted = convertToken(node, ast);
441+
if (converted) {
442+
result.push(converted);
443+
}
444+
} else {
445+
node.getChildren().forEach(walk);
432446
}
433-
token = ts.findNextToken(token, ast);
434447
}
435-
448+
walk(ast);
436449
return result;
437450
}
438451

0 commit comments

Comments
 (0)