UNPKG

3.12 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 = 'Assigning this reference to local variable: ';
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 if (Rule.isWarningShown === false) {
27 console.warn('Warning: no-var-self rule is deprecated. Replace your usage with the TSLint no-this-assignment rule.');
28 Rule.isWarningShown = true;
29 }
30 return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
31 };
32 Rule.prototype.parseOptions = function (options) {
33 var opt = {
34 bannedVariableNames: /.*/
35 };
36 if (options.ruleArguments && options.ruleArguments.length > 0) {
37 opt.bannedVariableNames = new RegExp(options.ruleArguments[0]);
38 }
39 return opt;
40 };
41 Rule.metadata = {
42 ruleName: 'no-var-self',
43 type: 'maintainability',
44 description: 'Do not use var self = this; instead, manage scope with arrow functions/lambdas.',
45 options: null,
46 optionsDescription: '',
47 typescriptOnly: true,
48 issueClass: 'Non-SDL',
49 issueType: 'Warning',
50 severity: 'Important',
51 level: 'Opportunity for Excellence',
52 group: 'Deprecated',
53 recommendation: 'false',
54 commonWeaknessEnumeration: '398, 710'
55 };
56 Rule.isWarningShown = false;
57 return Rule;
58}(Lint.Rules.AbstractRule));
59exports.Rule = Rule;
60function walk(ctx) {
61 var bannedVariableNames = ctx.options.bannedVariableNames;
62 function cb(node) {
63 if (tsutils.isVariableDeclaration(node)) {
64 if (node.initializer && node.initializer.kind === ts.SyntaxKind.ThisKeyword) {
65 if (tsutils.isIdentifier(node.name)) {
66 var identifier = node.name;
67 if (bannedVariableNames.test(identifier.text)) {
68 ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + node.getText());
69 }
70 }
71 }
72 }
73 return ts.forEachChild(node, cb);
74 }
75 return ts.forEachChild(ctx.sourceFile, cb);
76}
77//# sourceMappingURL=noVarSelfRule.js.map
\No newline at end of file