UNPKG

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