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

Commit aa41652

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

File tree

4 files changed

+370
-22
lines changed

4 files changed

+370
-22
lines changed

lib/node-utils.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ module.exports = {
185185
convertTokens,
186186
getNodeContainer,
187187
isWithinTypeAnnotation,
188-
isTypeKeyword
188+
isTypeKeyword,
189+
isComment,
190+
isJSDocComment
189191
};
190192
/* eslint-enable no-use-before-define */
191193

@@ -245,31 +247,23 @@ function isComma(token) {
245247
}
246248

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

256258
/**
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
259+
* Returns true if the given TSNode is a JSDoc comment
260+
* @param {TSNode} node the TypeScript node
261+
* @returns {boolean} contains JSDoc comment
261262
*/
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);
263+
function isJSDocComment(node) {
264+
return node.kind === SyntaxKind.JSDocComment;
270265
}
271266

272-
273267
/**
274268
* Returns the binary expression type of the given TSToken
275269
* @param {TSToken} operator the operator token
@@ -282,7 +276,6 @@ function getBinaryExpressionType(operator) {
282276
return "LogicalExpression";
283277
}
284278
return "BinaryExpression";
285-
286279
}
287280

288281
/**
@@ -719,7 +712,13 @@ function convertTokens(ast) {
719712
* @returns {undefined}
720713
*/
721714
function walk(node) {
722-
if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) {
715+
// TypeScript generates tokens for types in JSDoc blocks. Comment tokens
716+
// and their children should not be walked or added to the resulting tokens list.
717+
if (isComment(node) || isJSDocComment(node)) {
718+
return;
719+
}
720+
721+
if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
723722
const converted = convertToken(node, ast);
724723

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