UNPKG

4.75 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 var expectInvocation = ChaiUtils_1.ChaiUtils.getExpectInvocation(node);
57 if (!expectInvocation || expectInvocation.arguments.length !== 2) {
58 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
59 }
60 }
61 }
62 _super.prototype.visitPropertyAccessExpression.call(this, node);
63 };
64 ChaiVagueErrorsRuleWalker.prototype.visitCallExpression = function (node) {
65 if (ChaiUtils_1.ChaiUtils.isExpectInvocation(node)) {
66 if (node.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
67 if (ChaiUtils_1.ChaiUtils.isEqualsInvocation(node.expression)) {
68 if (node.arguments.length === 1) {
69 if (/true|false|null|undefined/.test(node.arguments[0].getText())) {
70 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
71 }
72 }
73 }
74 }
75 var actualValue = ChaiUtils_1.ChaiUtils.getFirstExpectCallParameter(node);
76 if (actualValue.kind === ts.SyntaxKind.BinaryExpression) {
77 var expectedValue = ChaiUtils_1.ChaiUtils.getFirstExpectationParameter(node);
78 var binaryExpression = actualValue;
79 var operator = binaryExpression.operatorToken.getText();
80 var expectingBooleanKeyword = expectedValue.kind === ts.SyntaxKind.TrueKeyword
81 || expectedValue.kind === ts.SyntaxKind.FalseKeyword;
82 if (operator === '===' && expectingBooleanKeyword) {
83 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING_COMPARE_TRUE);
84 }
85 else if (operator === '!==' && expectingBooleanKeyword) {
86 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING_COMPARE_FALSE);
87 }
88 }
89 }
90 _super.prototype.visitCallExpression.call(this, node);
91 };
92 return ChaiVagueErrorsRuleWalker;
93}(ErrorTolerantWalker_1.ErrorTolerantWalker));
94//# sourceMappingURL=chaiVagueErrorsRule.js.map
\No newline at end of file