From 7f39fe4327704a6c729967396c8e3d03926b4795 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Sun, 24 Dec 2017 00:38:56 -0500 Subject: [PATCH] Fix: Remove all tokens inside comments from tokens array (fixes #422) --- lib/node-utils.js | 28 ++++++++++++++++++- .../external-fixtures/jsdoc-indent.js | 11 +++++++- tests/integration/typescript.spec.js | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/node-utils.js b/lib/node-utils.js index 928ff14..3ed6e4d 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -244,6 +244,32 @@ function isComma(token) { return token.kind === SyntaxKind.CommaToken; } +/** + * Returns true if the given TSToken is a comment + * @param {TSToken} token the TypeScript token + * @returns {boolean} is commment + */ +function isComment(token) { + return (token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia) || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral); +} + +/** + * Returns true if the given TSToken is inside a comment + * Non-comment type tokens are generated for types in JSDoc Blocks + * @param {TSToken} token the TypeScript token + * @returns {boolean} is inside a commment + */ +function isInsideComment(token) { + if (token.kind === SyntaxKind.SourceFile) { + return false; + } + if (isComment(token)) { + return true; + } + return isInsideComment(token.parent); +} + + /** * Returns the binary expression type of the given TSToken * @param {TSToken} operator the operator token @@ -693,7 +719,7 @@ function convertTokens(ast) { * @returns {undefined} */ function walk(node) { - if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) { + if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) { const converted = convertToken(node, ast); if (converted) { diff --git a/tests/integration/external-fixtures/jsdoc-indent.js b/tests/integration/external-fixtures/jsdoc-indent.js index 25641d5..0854f1b 100644 --- a/tests/integration/external-fixtures/jsdoc-indent.js +++ b/tests/integration/external-fixtures/jsdoc-indent.js @@ -9,4 +9,13 @@ foo; /** * a */ -foo; \ No newline at end of file +foo; + +/** + * This is a function. + * @param {String} bar + * @returns {String} returns bar + */ +function foo(bar) { + return bar; +} diff --git a/tests/integration/typescript.spec.js b/tests/integration/typescript.spec.js index 6a83a13..74ddff0 100644 --- a/tests/integration/typescript.spec.js +++ b/tests/integration/typescript.spec.js @@ -80,7 +80,7 @@ describe("TypeScript", () => { ); }); - it("should not produce any lint errors on valid JSDoc indentation (#344)", () => { + it("should not produce any lint errors on valid JSDoc indentation (#344 && #422)", () => { verifyAndAssertMessages( loadExternalFixture("jsdoc-indent"), {