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

Commit adc0b1b

Browse files
kaicataldoJamesHenry
authored andcommitted
Breaking: Remove all tokens inside comments from tokens array (fixes #422) (#423)
1 parent e1c6b33 commit adc0b1b

File tree

5 files changed

+387
-4
lines changed

5 files changed

+387
-4
lines changed

lib/node-utils.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ module.exports = {
187187
convertTokens,
188188
getNodeContainer,
189189
isWithinTypeAnnotation,
190-
isTypeKeyword
190+
isTypeKeyword,
191+
isComment,
192+
isJSDocComment
191193
};
192194
/* eslint-enable no-use-before-define */
193195

@@ -246,6 +248,24 @@ function isComma(token) {
246248
return token.kind === SyntaxKind.CommaToken;
247249
}
248250

251+
/**
252+
* Returns true if the given TSNode is a comment
253+
* @param {TSNode} node the TypeScript node
254+
* @returns {boolean} is commment
255+
*/
256+
function isComment(node) {
257+
return node.kind === SyntaxKind.SingleLineCommentTrivia || node.kind === SyntaxKind.MultiLineCommentTrivia;
258+
}
259+
260+
/**
261+
* Returns true if the given TSNode is a JSDoc comment
262+
* @param {TSNode} node the TypeScript node
263+
* @returns {boolean} is JSDoc comment
264+
*/
265+
function isJSDocComment(node) {
266+
return node.kind === SyntaxKind.JSDocComment;
267+
}
268+
249269
/**
250270
* Returns the binary expression type of the given TSToken
251271
* @param {TSToken} operator the operator token
@@ -258,7 +278,6 @@ function getBinaryExpressionType(operator) {
258278
return "LogicalExpression";
259279
}
260280
return "BinaryExpression";
261-
262281
}
263282

264283
/**
@@ -695,6 +714,12 @@ function convertTokens(ast) {
695714
* @returns {undefined}
696715
*/
697716
function walk(node) {
717+
// TypeScript generates tokens for types in JSDoc blocks. Comment tokens
718+
// and their children should not be walked or added to the resulting tokens list.
719+
if (isComment(node) || isJSDocComment(node)) {
720+
return;
721+
}
722+
698723
if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
699724
const converted = convertToken(node, ast);
700725

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: 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 some string
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)