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

Commit 572ec04

Browse files
author
Kai Cataldo
committed
Refactor to avoid crawling up tree on every converted token
1 parent 5ca72f0 commit 572ec04

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

lib/node-utils.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,6 @@ function isComment(token) {
253253
return (token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia) || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral);
254254
}
255255

256-
/**
257-
* Returns true if the given TSToken is inside a comment
258-
* Non-comment type tokens are generated for types in JSDoc Blocks
259-
* @param {TSToken} token the TypeScript token
260-
* @returns {boolean} is inside a commment
261-
*/
262-
function isInsideComment(token) {
263-
if (token.kind === SyntaxKind.SourceFile) {
264-
return false;
265-
}
266-
if (isComment(token)) {
267-
return true;
268-
}
269-
return isInsideComment(token.parent);
270-
}
271-
272-
273256
/**
274257
* Returns the binary expression type of the given TSToken
275258
* @param {TSToken} operator the operator token
@@ -716,17 +699,24 @@ function convertTokens(ast) {
716699
const result = [];
717700
/**
718701
* @param {TSNode} node the TSNode
702+
* @param {Object} meta additional metadata
719703
* @returns {undefined}
720704
*/
721-
function walk(node) {
722-
if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) {
705+
function walk(node, meta) {
706+
const isInsideComment = meta && meta.isInsideComment;
707+
708+
if (isToken(node) && !isInsideComment && node.kind !== SyntaxKind.EndOfFileToken) {
723709
const converted = convertToken(node, ast);
724710

725711
if (converted) {
726712
result.push(converted);
727713
}
728714
} else {
729-
node.getChildren().forEach(walk);
715+
// Non-comment type tokens are generated for types in JSDoc blocks.
716+
// These should not be added to the token list for ESTree.
717+
node.getChildren().forEach(childNode => walk(childNode, {
718+
isInsideComment: isInsideComment || isComment(node)
719+
}));
730720
}
731721
}
732722
walk(ast);

0 commit comments

Comments
 (0)