UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var ChaiUtils_1 = require("./utils/ChaiUtils");
17var BASE_ERROR = 'Found chai call with vague failure message. ';
18var FAILURE_STRING = BASE_ERROR + 'Please add an explicit failure message';
19var FAILURE_STRING_COMPARE_TRUE = BASE_ERROR + 'Move the strict equality comparison from the expect ' +
20 'call into the assertion value';
21var FAILURE_STRING_COMPARE_FALSE = BASE_ERROR + 'Move the strict inequality comparison from the expect ' +
22 'call into the assertion value. ';
23var Rule = (function (_super) {
24 __extends(Rule, _super);
25 function Rule() {
26 return _super !== null && _super.apply(this, arguments) || this;
27 }
28 Rule.prototype.apply = function (sourceFile) {
29 return this.applyWithWalker(new ChaiVagueErrorsRuleWalker(sourceFile, this.getOptions()));
30 };
31 Rule.metadata = {
32 ruleName: 'chai-vague-errors',
33 type: 'maintainability',
34 description: 'Avoid Chai assertions that result in vague errors',
35 options: null,
36 optionsDescription: '',
37 typescriptOnly: true,
38 issueClass: 'Non-SDL',
39 issueType: 'Warning',
40 severity: 'Important',
41 level: 'Opportunity for Excellence',
42 group: 'Clarity',
43 commonWeaknessEnumeration: '398, 710'
44 };
45 return Rule;
46}(Lint.Rules.AbstractRule));
47exports.Rule = Rule;
48var ChaiVagueErrorsRuleWalker = (function (_super) {
49 __extends(ChaiVagueErrorsRuleWalker, _super);
50 function ChaiVagueErrorsRuleWalker() {
51 return _super !== null && _super.apply(this, arguments) || this;
52 }
53 ChaiVagueErrorsRuleWalker.prototype.visitPropertyAccessExpression = function (node) {
54 if (ChaiUtils_1.ChaiUtils.isExpectInvocation(node)) {
55 if (/ok|true|false|undefined|null/.test(node.name.getText())) {
56 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
57 }
58 }
59 _super.prototype.visitPropertyAccessExpression.call(this, node);
60 };
61 ChaiVagueErrorsRuleWalker.prototype.visitCallExpression = function (node) {
62 if (ChaiUtils_1.ChaiUtils.isExpectInvocation(node)) {
63 if (node.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
64 if (ChaiUtils_1.ChaiUtils.isEqualsInvocation(node.expression)) {
65 if (node.arguments.length === 1) {
66 if (/true|false|null|undefined/.test(node.arguments[0].getText())) {
67 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
68 }
69 }
70 }
71 }
72 var actualValue = ChaiUtils_1.ChaiUtils.getFirstExpectCallParameter(node);
73 if (actualValue.kind === ts.SyntaxKind.BinaryExpression) {
74 var expectedValue = ChaiUtils_1.ChaiUtils.getFirstExpectationParameter(node);
75 var binaryExpression = actualValue;
76 var operator = binaryExpression.operatorToken.getText();
77 var expectingBooleanKeyword = expectedValue.kind === ts.SyntaxKind.TrueKeyword
78 || expectedValue.kind === ts.SyntaxKind.FalseKeyword;
79 if (operator === '===' && expectingBooleanKeyword) {
80 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING_COMPARE_TRUE);
81 }
82 else if (operator === '!==' && expectingBooleanKeyword) {
83 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING_COMPARE_FALSE);
84 }
85 }
86 }
87 _super.prototype.visitCallExpression.call(this, node);
88 };
89 return ChaiVagueErrorsRuleWalker;
90}(ErrorTolerantWalker_1.ErrorTolerantWalker));
91//# sourceMappingURL=chaiVagueErrorsRule.js.map
\No newline at end of file