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