UNPKG

3.09 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 FAILURE_STRING = 'Avoid typeof x === \'undefined\' comparisons. Prefer x == undefined or x === undefined: ';
17var Rule = (function (_super) {
18 __extends(Rule, _super);
19 function Rule() {
20 return _super !== null && _super.apply(this, arguments) || this;
21 }
22 Rule.prototype.apply = function (sourceFile) {
23 return this.applyWithWalker(new NoTypeofUndefinedRuleWalker(sourceFile, this.getOptions()));
24 };
25 Rule.metadata = {
26 ruleName: 'no-typeof-undefined',
27 type: 'maintainability',
28 description: 'Do not use the idiom typeof `x === \'undefined\'`. You can safely use the simpler x === undefined ' +
29 'or perhaps x == null if you want to check for either null or undefined.',
30 options: null,
31 optionsDescription: '',
32 typescriptOnly: true,
33 issueClass: 'Non-SDL',
34 issueType: 'Warning',
35 severity: 'Important',
36 level: 'Opportunity for Excellence',
37 group: 'Clarity',
38 commonWeaknessEnumeration: '710'
39 };
40 return Rule;
41}(Lint.Rules.AbstractRule));
42exports.Rule = Rule;
43var NoTypeofUndefinedRuleWalker = (function (_super) {
44 __extends(NoTypeofUndefinedRuleWalker, _super);
45 function NoTypeofUndefinedRuleWalker() {
46 return _super !== null && _super.apply(this, arguments) || this;
47 }
48 NoTypeofUndefinedRuleWalker.prototype.visitBinaryExpression = function (node) {
49 if ((this.isUndefinedString(node.left) && this.isTypeOfExpression(node.right))
50 || this.isUndefinedString(node.right) && this.isTypeOfExpression(node.left)) {
51 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + node.getText());
52 }
53 _super.prototype.visitBinaryExpression.call(this, node);
54 };
55 NoTypeofUndefinedRuleWalker.prototype.isTypeOfExpression = function (node) {
56 return node.kind === ts.SyntaxKind.TypeOfExpression;
57 };
58 NoTypeofUndefinedRuleWalker.prototype.isUndefinedString = function (node) {
59 if (node.kind === ts.SyntaxKind.StringLiteral) {
60 if (node.text === 'undefined') {
61 return true;
62 }
63 }
64 return false;
65 };
66 return NoTypeofUndefinedRuleWalker;
67}(ErrorTolerantWalker_1.ErrorTolerantWalker));
68//# sourceMappingURL=noTypeofUndefinedRule.js.map
\No newline at end of file