Skip to content

Commit a032f13

Browse files
committed
ci: add tslint rule to disable tslint:disable pragmas
They will be only allowed in spec files.
1 parent a398690 commit a032f13

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

rules/noGlobalTslintDisableRule.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import * as Lint from 'tslint';
9+
import * as ts from 'typescript';
10+
11+
12+
export class Rule extends Lint.Rules.AbstractRule {
13+
public static metadata: Lint.IRuleMetadata = {
14+
ruleName: 'no-global-tslint-disable',
15+
type: 'style',
16+
description: `Ensure global tslint disable are only used for unit tests.`,
17+
rationale: `Some projects want to disallow tslint disable and only use per-line ones.`,
18+
options: null,
19+
optionsDescription: `Not configurable.`,
20+
typescriptOnly: false,
21+
};
22+
23+
public static FAILURE_STRING = 'tslint:disable is not allowed in this context.';
24+
25+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
26+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
27+
}
28+
}
29+
30+
31+
class Walker extends Lint.RuleWalker {
32+
private _findComments(node: ts.Node): ts.CommentRange[] {
33+
return ([] as ts.CommentRange[]).concat(
34+
ts.getLeadingCommentRanges(node.getFullText(), 0) || [],
35+
ts.getTrailingCommentRanges(node.getFullText(), 0) || [],
36+
node.getChildren().reduce((acc, n) => {
37+
return acc.concat(this._findComments(n));
38+
}, [] as ts.CommentRange[]),
39+
);
40+
}
41+
42+
walk(sourceFile: ts.SourceFile) {
43+
super.walk(sourceFile);
44+
45+
// Ignore spec files.
46+
if (sourceFile.fileName.match(/_spec.ts$/)) {
47+
return;
48+
}
49+
50+
// Find all comment nodes.
51+
const ranges = this._findComments(sourceFile);
52+
ranges.forEach(range => {
53+
const text = sourceFile.getFullText().substring(range.pos, range.end);
54+
let i = text.indexOf('tslint:disable:');
55+
56+
while (i != -1) {
57+
this.addFailureAt(range.pos + i + 1, range.pos + i + 15, Rule.FAILURE_STRING);
58+
i = text.indexOf('tslint:disable:', i + 1);
59+
}
60+
});
61+
}
62+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"rules": {
66
"import-groups": true,
77
"non-null-operator": true,
8+
"no-global-tslint-disable": true,
89

910
"import-blacklist": [
1011
true,

0 commit comments

Comments
 (0)