build: detect todos in public comments (#4059)

* build: detect todos in public comments

* Creates a naive rule that checks all multi-line comments and reports failures once it detects TODOs inside of it.

* This means that TODOs always need to placed inside of single-line comments.

Fixes #4026
This commit is contained in:
Paul Gschwendtner
2017-04-14 13:06:38 -07:00
committed by Jeremy Elbourn
parent dbe2c33afc
commit a773515c56
5 changed files with 43 additions and 9 deletions
+34
View File
@@ -0,0 +1,34 @@
const ts = require('typescript');
const utils = require('tsutils');
const Lint = require('tslint');
const ERROR_MESSAGE =
'A TODO may only appear in inline (//) style comments. ' +
'This is meant to prevent a TODO from being accidentally included in any public API docs.';
/**
* Rule that walks through all comments inside of the library and adds failures when it
* detects TODO's inside of multi-line comments. TODOs need to be placed inside of single-line
* comments.
*/
class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile) {
return this.applyWithWalker(new NoExposedTodoWalker(sourceFile, this.getOptions()));
}
}
class NoExposedTodoWalker extends Lint.RuleWalker {
visitSourceFile(sourceFile) {
utils.forEachComment(sourceFile, (fullText, commentRange) => {
let isTodoComment = fullText.substring(commentRange.pos, commentRange.end).includes('TODO');
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia && isTodoComment) {
this.addFailureAt(commentRange.pos, commentRange.end - commentRange.pos, ERROR_MESSAGE);
}
});
}
}
exports.Rule = Rule;