-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
dbe2c33
commit a773515
Showing
5 changed files
with
43 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters