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