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

Commit 25207e0

Browse files
authored
Fix: Optimize convertTokens, treat JsxText as token (fixes #70) (#158)
1 parent 76c33f8 commit 25207e0

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

lib/ast-converter.js

+34-10
Original file line numberDiff line numberDiff line change
@@ -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
*/
423443
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);
432457
}
433-
token = ts.findNextToken(token, ast);
434458
}
435-
459+
walk(ast);
436460
return result;
437461
}
438462

tests/lib/ecma-features.js

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var filesWithOutsandingTSIssues = [
2828
"jsx/embedded-tags", // https://github.com/Microsoft/TypeScript/issues/7410
2929
"jsx/namespaced-attribute-and-value-inserted", // https://github.com/Microsoft/TypeScript/issues/7411
3030
"jsx/namespaced-name-and-attribute", // https://github.com/Microsoft/TypeScript/issues/7411
31-
"jsx/test-content", // https://github.com/Microsoft/TypeScript/issues/7471
3231
"jsx/multiple-blank-spaces"
3332
];
3433

0 commit comments

Comments
 (0)