UNPKG

4.8 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 Rule = (function (_super) {
17 __extends(Rule, _super);
18 function Rule() {
19 return _super !== null && _super.apply(this, arguments) || this;
20 }
21 Rule.prototype.apply = function (sourceFile) {
22 if (Rule.isWarningShown === false) {
23 console.warn('Warning: valid-typeof rule is deprecated. Replace your usage with the TSLint typeof-compare rule.');
24 Rule.isWarningShown = true;
25 }
26 return this.applyWithWalker(new ValidTypeofRuleWalker(sourceFile, this.getOptions()));
27 };
28 Rule.metadata = {
29 ruleName: 'valid-typeof',
30 type: 'maintainability',
31 description: 'Ensures that the results of typeof are compared against a valid string.',
32 options: null,
33 optionsDescription: '',
34 typescriptOnly: true,
35 issueClass: 'Non-SDL',
36 issueType: 'Error',
37 severity: 'Critical',
38 level: 'Opportunity for Excellence',
39 group: 'Deprecated'
40 };
41 Rule.FAILURE_STRING = 'Invalid comparison in typeof. Did you mean ';
42 Rule.VALID_TERMS = ['undefined', 'object', 'boolean', 'number', 'string', 'function', 'symbol'];
43 Rule.isWarningShown = false;
44 return Rule;
45}(Lint.Rules.AbstractRule));
46exports.Rule = Rule;
47var ValidTypeofRuleWalker = (function (_super) {
48 __extends(ValidTypeofRuleWalker, _super);
49 function ValidTypeofRuleWalker() {
50 return _super !== null && _super.apply(this, arguments) || this;
51 }
52 ValidTypeofRuleWalker.prototype.visitBinaryExpression = function (node) {
53 if (node.left.kind === ts.SyntaxKind.TypeOfExpression && node.right.kind === ts.SyntaxKind.StringLiteral) {
54 this.validateTypeOf(node.right);
55 }
56 else if (node.right.kind === ts.SyntaxKind.TypeOfExpression && node.left.kind === ts.SyntaxKind.StringLiteral) {
57 this.validateTypeOf(node.left);
58 }
59 _super.prototype.visitBinaryExpression.call(this, node);
60 };
61 ValidTypeofRuleWalker.prototype.validateTypeOf = function (node) {
62 if (Rule.VALID_TERMS.indexOf(node.text) === -1) {
63 var start = node.getStart();
64 var width = node.getWidth();
65 this.addFailureAt(start, width, Rule.FAILURE_STRING + this.getClosestTerm(node.text) + '?');
66 }
67 };
68 ValidTypeofRuleWalker.prototype.getClosestTerm = function (term) {
69 var _this = this;
70 var closestMatch = 99999999;
71 return Rule.VALID_TERMS.reduce(function (closestTerm, thisTerm) {
72 var distance = _this.levenshteinDistance(term, thisTerm);
73 if (distance < closestMatch) {
74 closestMatch = distance;
75 closestTerm = thisTerm;
76 }
77 return closestTerm;
78 }, '');
79 };
80 ValidTypeofRuleWalker.prototype.levenshteinDistance = function (a, b) {
81 if (a.length === 0) {
82 return b.length;
83 }
84 if (b.length === 0) {
85 return a.length;
86 }
87 var matrix = [];
88 for (var i = 0; i <= b.length; i++) {
89 matrix[i] = [i];
90 }
91 for (var i = 0; i <= a.length; i++) {
92 matrix[0][i] = i;
93 }
94 for (var i = 1; i <= b.length; i++) {
95 for (var j = 1; j <= a.length; j++) {
96 if (b.charAt(i - 1) === a.charAt(j - 1)) {
97 matrix[i][j] = matrix[i - 1][j - 1];
98 }
99 else {
100 var substitutionValue = matrix[i - 1][j - 1] + 1;
101 var insertionValue = matrix[i][j - 1] + 1;
102 var deletionDistance = matrix[i - 1][j] + 1;
103 var minDistance = Math.min(substitutionValue, insertionValue, deletionDistance);
104 matrix[i][j] = minDistance;
105 }
106 }
107 }
108 return matrix[b.length][a.length];
109 };
110 return ValidTypeofRuleWalker;
111}(ErrorTolerantWalker_1.ErrorTolerantWalker));
112//# sourceMappingURL=validTypeofRule.js.map
\No newline at end of file