UNPKG

3.22 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || function (d, b) {
2 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3 function __() { this.constructor = d; }
4 __.prototype = b.prototype;
5 d.prototype = new __();
6};
7var ErrorTolerantWalker = require('./utils/ErrorTolerantWalker');
8var SyntaxKind = require('./utils/SyntaxKind');
9var Rule = (function (_super) {
10 __extends(Rule, _super);
11 function Rule() {
12 _super.apply(this, arguments);
13 }
14 Rule.prototype.apply = function (sourceFile) {
15 return this.applyWithWalker(new ChaiVagueErrorsRuleWalker(sourceFile, this.getOptions()));
16 };
17 Rule.FAILURE_STRING = 'Found chai call with vague failure message. Please add an explicit failure message';
18 return Rule;
19})(Lint.Rules.AbstractRule);
20exports.Rule = Rule;
21var ChaiVagueErrorsRuleWalker = (function (_super) {
22 __extends(ChaiVagueErrorsRuleWalker, _super);
23 function ChaiVagueErrorsRuleWalker() {
24 _super.apply(this, arguments);
25 }
26 ChaiVagueErrorsRuleWalker.prototype.visitPropertyAccessExpression = function (node) {
27 if (this.isExpectInvocation(node)) {
28 if (/ok|true|false|undefined|null/.test(node.name.getText())) {
29 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
30 }
31 }
32 _super.prototype.visitPropertyAccessExpression.call(this, node);
33 };
34 ChaiVagueErrorsRuleWalker.prototype.visitCallExpression = function (node) {
35 if (this.isExpectInvocation(node)) {
36 if (node.expression.kind === SyntaxKind.current().PropertyAccessExpression) {
37 var propExpression = node.expression;
38 if (/equal|equals|eql/.test(propExpression.name.getText())) {
39 if (node.arguments.length === 1) {
40 if (/true|false|null|undefined/.test(node.arguments[0].getText())) {
41 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
42 }
43 }
44 }
45 }
46 }
47 _super.prototype.visitCallExpression.call(this, node);
48 };
49 ChaiVagueErrorsRuleWalker.prototype.isExpectInvocation = function (node) {
50 var callExpression = ChaiVagueErrorsRuleWalker.getLeftMostCallExpression(node);
51 if (callExpression == null) {
52 return false;
53 }
54 return /.*\.?expect/.test(callExpression.expression.getText());
55 };
56 ChaiVagueErrorsRuleWalker.getLeftMostCallExpression = function (node) {
57 var leftSide = node.expression;
58 while (leftSide != null) {
59 if (leftSide.kind === SyntaxKind.current().CallExpression) {
60 return leftSide;
61 }
62 else if (leftSide.kind === (SyntaxKind.current().PropertyAccessExpression)) {
63 leftSide = leftSide.expression;
64 }
65 else {
66 return null;
67 }
68 }
69 return null;
70 };
71 return ChaiVagueErrorsRuleWalker;
72})(ErrorTolerantWalker);
73//# sourceMappingURL=chaiVagueErrorsRule.js.map
\No newline at end of file