Skip to content

Commit

Permalink
Fix: Remove all tokens inside comments from tokens array (fixes eslin…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Dec 24, 2017
1 parent e94ede3 commit a582b78
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
28 changes: 27 additions & 1 deletion lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Empty file added package-lock.json.1799798656
Empty file.
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
* @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

0 comments on commit a582b78

Please sign in to comment.