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

Fix: Remove all tokens inside comments from tokens array (fixes #422) #423

Merged
merged 2 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ module.exports = {
convertTokens,
getNodeContainer,
isWithinTypeAnnotation,
isTypeKeyword
isTypeKeyword,
isComment,
isJSDocComment
};
/* eslint-enable no-use-before-define */

Expand Down Expand Up @@ -244,6 +246,24 @@ function isComma(token) {
return token.kind === SyntaxKind.CommaToken;
}

/**
* Returns true if the given TSNode is a comment
* @param {TSNode} node the TypeScript node
* @returns {boolean} is commment
*/
function isComment(node) {
return node.kind === SyntaxKind.SingleLineCommentTrivia || node.kind === SyntaxKind.MultiLineCommentTrivia;
}

/**
* Returns true if the given TSNode is a JSDoc comment
* @param {TSNode} node the TypeScript node
* @returns {boolean} is JSDoc comment
*/
function isJSDocComment(node) {
return node.kind === SyntaxKind.JSDocComment;
}

/**
* Returns the binary expression type of the given TSToken
* @param {TSToken} operator the operator token
Expand All @@ -256,7 +276,6 @@ function getBinaryExpressionType(operator) {
return "LogicalExpression";
}
return "BinaryExpression";

}

/**
Expand Down Expand Up @@ -693,6 +712,12 @@ function convertTokens(ast) {
* @returns {undefined}
*/
function walk(node) {
// TypeScript generates tokens for types in JSDoc blocks. Comment tokens
// and their children should not be walked or added to the resulting tokens list.
if (isComment(node) || isJSDocComment(node)) {
return;
}

if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
const converted = convertToken(node, ast);

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/comments/jsdoc-comment.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* This is a function.
* @param {String} bar some string
* @returns {String} returns bar
*/
function foo(bar) {
return bar;
}
11 changes: 10 additions & 1 deletion tests/integration/external-fixtures/jsdoc-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ foo;
/**
* a
*/
foo;
foo;

/**
* This is a function.
* @param {String} bar some string
* @returns {String} returns bar
*/
function foo(bar) {
return bar;
}
2 changes: 1 addition & 1 deletion tests/integration/typescript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
{
Expand Down
Loading