Skip to content

Commit a773515

Browse files
devversionjelbourn
authored andcommitted
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
1 parent dbe2c33 commit a773515

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

src/lib/core/observe-content/observe-content.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import {Component} from '@angular/core';
22
import {async, TestBed} from '@angular/core/testing';
33
import {ObserveContentModule} from './observe-content';
44

5-
/**
6-
* TODO(elad): `ProxyZone` doesn't seem to capture the events raised by
7-
* `MutationObserver` and needs to be investigated
8-
*/
5+
// TODO(elad): `ProxyZone` doesn't seem to capture the events raised by
6+
// `MutationObserver` and needs to be investigated
97

108
describe('Observe content', () => {
119
beforeEach(async(() => {

src/lib/select/select.ts

-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
654654
/**
655655
* Sets the `multiple` property on each option. The promise is necessary
656656
* in order to avoid Angular errors when modifying the property after init.
657-
* TODO: there should be a better way of doing this.
658657
*/
659658
private _setOptionMultiple() {
660659
if (this.multiple) {

src/lib/tabs/tab-body.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,10 @@ export class MdTabBody implements OnInit, AfterViewChecked, AfterContentChecked
153153
* computed style (with Angular > 2.3.0). This can alternatively be determined by checking the
154154
* transform: canBeAnimated = getComputedStyle(element) !== '', however document.contains should
155155
* be faster since it doesn't cause a reflow.
156-
*
157-
* TODO: This can safely be removed after we stop supporting Angular < 2.4.2. The fix landed via
158-
* https://github.com/angular/angular/commit/21030e9a1cf30e8101399d8535ed72d847a23ba6
159156
*/
160157
ngAfterContentChecked() {
158+
// TODO: This can safely be removed after we stop supporting Angular < 2.4.2. The fix landed via
159+
// https://github.com/angular/angular/commit/21030e9a1cf30e8101399d8535ed72d847a23ba6
161160
if (!this._canBeAnimated) {
162161
this._canBeAnimated = document.body.contains(this._elementRef.nativeElement);
163162

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const ts = require('typescript');
2+
const utils = require('tsutils');
3+
const Lint = require('tslint');
4+
5+
const ERROR_MESSAGE =
6+
'A TODO may only appear in inline (//) style comments. ' +
7+
'This is meant to prevent a TODO from being accidentally included in any public API docs.';
8+
9+
/**
10+
* Rule that walks through all comments inside of the library and adds failures when it
11+
* detects TODO's inside of multi-line comments. TODOs need to be placed inside of single-line
12+
* comments.
13+
*/
14+
class Rule extends Lint.Rules.AbstractRule {
15+
16+
apply(sourceFile) {
17+
return this.applyWithWalker(new NoExposedTodoWalker(sourceFile, this.getOptions()));
18+
}
19+
}
20+
21+
class NoExposedTodoWalker extends Lint.RuleWalker {
22+
23+
visitSourceFile(sourceFile) {
24+
utils.forEachComment(sourceFile, (fullText, commentRange) => {
25+
let isTodoComment = fullText.substring(commentRange.pos, commentRange.end).includes('TODO');
26+
27+
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia && isTodoComment) {
28+
this.addFailureAt(commentRange.pos, commentRange.end - commentRange.pos, ERROR_MESSAGE);
29+
}
30+
});
31+
}
32+
}
33+
34+
exports.Rule = Rule;

tslint.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"rulesDirectory": ["node_modules/tslint-no-unused-var"],
2+
"rulesDirectory": [
3+
"./node_modules/tslint-no-unused-var/",
4+
"./tools/tslint-rules/"
5+
],
36
"rules": {
47
"max-line-length": [true, 100],
58
// Disable this flag because of SHA tslint#48b0c597f9257712c7d1f04b55ed0aa60e333f6a
@@ -26,6 +29,7 @@
2629
"no-unused-expression": true,
2730
"no-unused-var": [true, {"ignore-pattern": "^(_.*)$"}],
2831
"no-var-keyword": true,
32+
"no-exposed-todo": true,
2933
"no-debugger": true,
3034
"one-line": [
3135
true,

0 commit comments

Comments
 (0)