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