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

Commit af4d3d7

Browse files
author
Kai Cataldo
committed
Refactor to avoid crawling up tree on every converted token
1 parent 57fff67 commit af4d3d7

File tree

4 files changed

+375
-19
lines changed

4 files changed

+375
-19
lines changed

lib/node-utils.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,30 +245,31 @@ function isComma(token) {
245245
}
246246

247247
/**
248-
* Returns true if the given TSToken is a comment
249-
* @param {TSToken} token the TypeScript token
250-
* @returns {boolean} is commment
248+
* Returns true if the given TSNode is a comment
249+
* @param {TSNode} node the TypeScript node
250+
* @returns {boolean} is commment
251251
*/
252-
function isComment(token) {
253-
return (token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia) || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral);
252+
function isComment(node) {
253+
return node.kind === SyntaxKind.SingleLineCommentTrivia || node.kind === SyntaxKind.MultiLineCommentTrivia;
254254
}
255255

256256
/**
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
257+
* Returns true if the given TSNode is a JSDoc tag
258+
* @param {TSNode} node the TypeScript node
259+
* @returns {boolean} isJSDoc tag
261260
*/
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);
261+
function isJSDocTag(node) {
262+
return node.kind >= SyntaxKind.FirstJSDocTagNode && node.kind <= SyntaxKind.LastJSDocTagNode;
270263
}
271264

265+
/**
266+
* Returns true if the given TSNode contains a JSDoc comment
267+
* @param {TSNode} node the TypeScript node
268+
* @returns {boolean} contains JSDoc comment
269+
*/
270+
function isJSDocCommentContainingNode(node) {
271+
return node.kind === SyntaxKind.JSDocComment || isJSDocTag(node);
272+
}
272273

273274
/**
274275
* Returns the binary expression type of the given TSToken
@@ -719,7 +720,13 @@ function convertTokens(ast) {
719720
* @returns {undefined}
720721
*/
721722
function walk(node) {
722-
if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) {
723+
// TypeScript generates tokens for types in JSDoc blocks. Comment tokens
724+
// and their children should not be walked or added to the resulting tokens list.
725+
if (isComment(node) || isJSDocCommentContainingNode(node)) {
726+
return;
727+
}
728+
729+
if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
723730
const converted = convertToken(node, ast);
724731

725732
if (converted) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This is a function.
3+
* @param {String} bar some string
4+
* @returns {String} returns bar
5+
*/
6+
function foo(bar) {
7+
return bar;
8+
}

tests/integration/external-fixtures/jsdoc-indent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ foo;
1313

1414
/**
1515
* This is a function.
16-
* @param {String} bar
16+
* @param {String} bar some string
1717
* @returns {String} returns bar
1818
*/
1919
function foo(bar) {

0 commit comments

Comments
 (0)