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 AstUtils_1 = require("./utils/AstUtils");
|
20 | var ChaiUtils_1 = require("./utils/ChaiUtils");
|
21 | var FAILURE_STRING = 'Found chai call with indexOf that can be converted to .contain assertion';
|
22 | var Rule = (function (_super) {
|
23 | __extends(Rule, _super);
|
24 | function Rule() {
|
25 | return _super !== null && _super.apply(this, arguments) || this;
|
26 | }
|
27 | Rule.prototype.apply = function (sourceFile) {
|
28 | return this.applyWithFunction(sourceFile, walk);
|
29 | };
|
30 | Rule.metadata = {
|
31 | ruleName: 'chai-prefer-contains-to-index-of',
|
32 | type: 'maintainability',
|
33 | description: 'Avoid Chai assertions that invoke indexOf and compare for a -1 result.',
|
34 | options: null,
|
35 | optionsDescription: '',
|
36 | typescriptOnly: true,
|
37 | issueClass: 'Non-SDL',
|
38 | issueType: 'Warning',
|
39 | severity: 'Important',
|
40 | level: 'Opportunity for Excellence',
|
41 | group: 'Clarity',
|
42 | commonWeaknessEnumeration: '398, 710'
|
43 | };
|
44 | return Rule;
|
45 | }(Lint.Rules.AbstractRule));
|
46 | exports.Rule = Rule;
|
47 | function walk(ctx) {
|
48 | function cb(node) {
|
49 | if (tsutils.isCallExpression(node)) {
|
50 | if (ChaiUtils_1.ChaiUtils.isExpectInvocation(node)) {
|
51 | if (isFirstArgumentIndexOfResult(node)) {
|
52 | if (tsutils.isPropertyAccessExpression(node.expression)) {
|
53 | if (ChaiUtils_1.ChaiUtils.isEqualsInvocation(node.expression)) {
|
54 | if (isFirstArgumentNegative1(node)) {
|
55 | ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
|
56 | }
|
57 | }
|
58 | }
|
59 | }
|
60 | }
|
61 | }
|
62 | return ts.forEachChild(node, cb);
|
63 | }
|
64 | function isFirstArgumentNegative1(node) {
|
65 | if (node.arguments && node.arguments.length > 0) {
|
66 | var firstArgument = node.arguments[0];
|
67 | return firstArgument.getText() === '-1';
|
68 | }
|
69 | return false;
|
70 | }
|
71 | function isFirstArgumentIndexOfResult(node) {
|
72 | var expectCall = ChaiUtils_1.ChaiUtils.getLeftMostCallExpression(node);
|
73 | if (expectCall && expectCall.arguments && expectCall.arguments.length > 0) {
|
74 | var firstArgument = expectCall.arguments[0];
|
75 | if (tsutils.isCallExpression(firstArgument)) {
|
76 | return AstUtils_1.AstUtils.getFunctionName(firstArgument) === 'indexOf';
|
77 | }
|
78 | }
|
79 | return false;
|
80 | }
|
81 | return ts.forEachChild(ctx.sourceFile, cb);
|
82 | }
|
83 |
|
\ | No newline at end of file |