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

Commit 5ca72f0

Browse files
author
Kai Cataldo
committed
Fix: Remove all tokens inside comments from tokens array (fixes #422)
1 parent e94ede3 commit 5ca72f0

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

lib/node-utils.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,32 @@ function isComma(token) {
244244
return token.kind === SyntaxKind.CommaToken;
245245
}
246246

247+
/**
248+
* Returns true if the given TSToken is a comment
249+
* @param {TSToken} token the TypeScript token
250+
* @returns {boolean} is commment
251+
*/
252+
function isComment(token) {
253+
return (token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia) || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral);
254+
}
255+
256+
/**
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+
*/
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);
270+
}
271+
272+
247273
/**
248274
* Returns the binary expression type of the given TSToken
249275
* @param {TSToken} operator the operator token
@@ -693,7 +719,7 @@ function convertTokens(ast) {
693719
* @returns {undefined}
694720
*/
695721
function walk(node) {
696-
if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
722+
if (isToken(node) && !isInsideComment(node) && node.kind !== SyntaxKind.EndOfFileToken) {
697723
const converted = convertToken(node, ast);
698724

699725
if (converted) {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ foo;
99
/**
1010
* a
1111
*/
12-
foo;
12+
foo;
13+
14+
/**
15+
* This is a function.
16+
* @param {String} bar
17+
* @returns {String} returns bar
18+
*/
19+
function foo(bar) {
20+
return bar;
21+
}

tests/integration/typescript.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe("TypeScript", () => {
8080
);
8181
});
8282

83-
it("should not produce any lint errors on valid JSDoc indentation (#344)", () => {
83+
it("should not produce any lint errors on valid JSDoc indentation (#344 & #422)", () => {
8484
verifyAndAssertMessages(
8585
loadExternalFixture("jsdoc-indent"),
8686
{

0 commit comments

Comments
 (0)