forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefocusRule.ts
55 lines (46 loc) · 1.66 KB
/
defocusRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/*
* Taken from https://github.com/Sergiioo/tslint-defocus
* Copyright (c) 2016 Sergio Annecchiarico
* MIT - https://github.com/Sergiioo/tslint-defocus/blob/master/LICENSE
*/
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'defocus',
description: "Bans the use of `fdescribe` and 'fit' Jasmine functions.",
rationale: 'It is all too easy to mistakenly commit a focussed Jasmine test suite or spec.',
options: null,
optionsDescription: 'Not configurable.',
type: 'functionality',
typescriptOnly: false,
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.CallExpression) {
const expression = (node as ts.CallExpression).expression;
const functionName = expression.getText();
bannedFunctions.forEach((banned) => {
if (banned === functionName) {
ctx.addFailureAtNode(expression, failureMessage(functionName));
}
});
}
return ts.forEachChild(node, cb);
});
}
const bannedFunctions: ReadonlyArray<string> = ['fdescribe', 'fit'];
const failureMessage = (functionName: string) => {
return `Calls to '${functionName}' are not allowed.`;
};