Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Wassim CHEGHAM committed Apr 11, 2018
1 parent 6ef5b2c commit 8e62b8a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/stats/comments.ts
@@ -0,0 +1,16 @@
import { SourceFile, SyntaxKind } from 'ts-simple-ast';
export default function(sourcesFiles: SourceFile[]) {
const comments = [];
sourcesFiles.map(sourceFile => {
// console.log(
// sourceFile.getDescendantsOfKind(SyntaxKind.JSDocComment).map(n => n.getText()),
// sourceFile.getDescendantsOfKind(SyntaxKind.MultiLineCommentTrivia).map(n => n.getText()),
// sourceFile.getDescendantsOfKind(SyntaxKind.SingleLineCommentTrivia).map(n => n.getText()),
// )
});

return {
keys: ['Comments'],
values: [comments.length]
};
}

1 comment on commit 8e62b8a

@dsherret
Copy link

@dsherret dsherret commented on 8e62b8a Apr 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manekinekko the sad part about comments is that they aren't part of the AST (except for JSDocComments) and because of that, they can't be found via .getDescendantsOfKind(SyntaxKind.MultiLineCommentTrivia) or .getDescendantsOfKind(SyntaxKind.SingleLineCommentTrivia) :(

Read about trivia in the compiler API here: https://github.com/Microsoft/TypeScript/wiki/Architectural-Overview#trivia

Also, comments in ts-simple-ast: https://dsherret.github.io/ts-simple-ast/details/comment-ranges

To be honest, I'm not super familiar with comments in TypeScript's AST so I'm not sure what the best way to do this would be... I don't know if it's possible to get all the comments with getTrailingCommentRanges and getLeadingCommentRanges. For example, is it possible to get this comment? I'm not sure:

function func() {
  // comment with no surrounding nodes
}

I'm wondering now if I could somehow incorporate them better into ts-simple-ast using the scanner with skipTrivia set to false then somehow making the comment ranges work with getDescendantsOfKind. I have no idea.

Please sign in to comment.