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

Commit 2a8fd38

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

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

lib/node-utils.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -250,26 +250,9 @@ function isComma(token) {
250250
* @returns {boolean} is commment
251251
*/
252252
function isComment(token) {
253-
return (token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia) || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral);
253+
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
@@ -719,7 +702,13 @@ function convertTokens(ast) {
719702
* @returns {undefined}
720703
*/
721704
function walk(node) {
722-
if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) {
705+
// TypeScript generates tokens for types in JSDoc blocks. Comment tokens
706+
// and their children should not be walked or added to the resulting tokens list.
707+
if (isComment(node)) {
708+
return;
709+
}
710+
711+
if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
723712
const converted = convertToken(node, ast);
724713

725714
if (converted) {

0 commit comments

Comments
 (0)