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

Commit a91306c

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

File tree

4 files changed

+389
-21
lines changed

4 files changed

+389
-21
lines changed

lib/node-utils.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ module.exports = {
185185
convertTokens,
186186
getNodeContainer,
187187
isWithinTypeAnnotation,
188-
isTypeKeyword
188+
isTypeKeyword,
189+
isComment,
190+
isJSDocNode,
191+
isJSDocCommentContainingNode,
192+
isJSDocTag
189193
};
190194
/* eslint-enable no-use-before-define */
191195

@@ -245,30 +249,40 @@ function isComma(token) {
245249
}
246250

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

256260
/**
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+
* Returns true if the given TSNode is a JSDoc node
262+
* @param {TSNode} node the TypeScript node
263+
* @returns {boolean} is JSDoc node
261264
*/
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);
265+
function isJSDocNode(node) {
266+
return node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode;
267+
}
268+
269+
/**
270+
* Returns true if the given TSNode is a JSDoc tag
271+
* @param {TSNode} node the TypeScript node
272+
* @returns {boolean} isJSDoc tag
273+
*/
274+
function isJSDocTag(node) {
275+
return node.kind >= SyntaxKind.FirstJSDocTagNode && node.kind <= SyntaxKind.LastJSDocTagNode;
270276
}
271277

278+
/**
279+
* Returns true if the given TSNode contains a JSDoc comment
280+
* @param {TSNode} node the TypeScript node
281+
* @returns {boolean} contains JSDoc comment
282+
*/
283+
function isJSDocCommentContainingNode(node) {
284+
return node.kind === SyntaxKind.JSDocComment || isJSDocTag(node);
285+
}
272286

273287
/**
274288
* Returns the binary expression type of the given TSToken
@@ -282,7 +296,6 @@ function getBinaryExpressionType(operator) {
282296
return "LogicalExpression";
283297
}
284298
return "BinaryExpression";
285-
286299
}
287300

288301
/**
@@ -719,7 +732,13 @@ function convertTokens(ast) {
719732
* @returns {undefined}
720733
*/
721734
function walk(node) {
722-
if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) {
735+
// TypeScript generates tokens for types in JSDoc blocks. Comment tokens
736+
// and their children should not be walked or added to the resulting tokens list.
737+
if (isComment(node) || isJSDocCommentContainingNode(node)) {
738+
return;
739+
}
740+
741+
if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
723742
const converted = convertToken(node, ast);
724743

725744
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)