4 * Copyright 2017 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
21 var utils = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var ALLOW_THIS_DESTRUCTURING = "allow-destructuring";
25 var ALLOWED_THIS_NAMES = "allowed-names";
26 var parseConfigOptions = function (configOptions) {
27 var allowedNames = [];
28 var allowDestructuring = false;
29 if (configOptions !== undefined) {
30 allowDestructuring = !!configOptions[ALLOW_THIS_DESTRUCTURING];
31 if (configOptions[ALLOWED_THIS_NAMES] !== undefined) {
32 allowedNames.push.apply(allowedNames, configOptions[ALLOWED_THIS_NAMES]);
35 return { allowedNames: allowedNames, allowDestructuring: allowDestructuring };
37 var Rule = /** @class */ (function (_super) {
38 tslib_1.__extends(Rule, _super);
40 return _super !== null && _super.apply(this, arguments) || this;
42 Rule.FAILURE_STRING_FACTORY_IDENTIFIERS = function (name) {
43 return "Assigning `this` reference to local variable not allowed: " + name + ".";
45 Rule.prototype.apply = function (sourceFile) {
46 var options = parseConfigOptions(this.ruleArguments[0]);
47 var noThisAssignmentWalker = new NoThisAssignmentWalker(sourceFile, this.ruleName, options);
48 return this.applyWithWalker(noThisAssignmentWalker);
51 description: "Disallows unnecessary references to `this`.",
57 _a[ALLOWED_THIS_NAMES] = ["^self$"],
58 _a[ALLOW_THIS_DESTRUCTURING] = true,
63 additionalProperties: false,
65 _b[ALLOW_THIS_DESTRUCTURING] = {
68 _b[ALLOWED_THIS_NAMES] = {
75 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Two options may be provided on an object:\n\n * `", "` allows using destructuring to access members of `this` (e.g. `{ foo, bar } = this;`).\n * `", "` may be specified as a list of regular expressions to match allowed variable names."], ["\n Two options may be provided on an object:\n\n * \\`", "\\` allows using destructuring to access members of \\`this\\` (e.g. \\`{ foo, bar } = this;\\`).\n * \\`", "\\` may be specified as a list of regular expressions to match allowed variable names."])), ALLOW_THIS_DESTRUCTURING, ALLOWED_THIS_NAMES),
76 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Assigning a variable to `this` instead of properly using arrow lambdas may be a symptom of pre-ES6 practices\n or not managing scope well.\n\n Instead of storing a reference to `this` and using it inside a `function () {`:\n\n ```\n const self = this;\n\n setTimeout(function () {\n self.doWork();\n });\n ```\n\n Use `() =>` arrow lambdas, as they preserve `this` scope for you:\n\n ```\n setTimeout(() => {\n this.doWork();\n });\n ```\n "], ["\n Assigning a variable to \\`this\\` instead of properly using arrow lambdas may be a symptom of pre-ES6 practices\n or not managing scope well.\n\n Instead of storing a reference to \\`this\\` and using it inside a \\`function () {\\`:\n\n \\`\\`\\`\n const self = this;\n\n setTimeout(function () {\n self.doWork();\n });\n \\`\\`\\`\n\n Use \\`() =>\\` arrow lambdas, as they preserve \\`this\\` scope for you:\n\n \\`\\`\\`\n setTimeout(() => {\n this.doWork();\n });\n \\`\\`\\`\n "]))),
77 ruleName: "no-this-assignment",
78 type: "functionality",
79 typescriptOnly: false,
81 Rule.FAILURE_STRING_BINDINGS = "Don't assign members of `this` to local variables.";
83 }(Lint.Rules.AbstractRule));
85 var NoThisAssignmentWalker = /** @class */ (function (_super) {
86 tslib_1.__extends(NoThisAssignmentWalker, _super);
87 function NoThisAssignmentWalker() {
88 var _this = _super !== null && _super.apply(this, arguments) || this;
89 _this.allowedThisNameTesters = _this.options.allowedNames.map(function (allowedThisName) { return new RegExp(allowedThisName); });
90 _this.visitNode = function (node) {
91 if (utils.isVariableDeclaration(node)) {
92 _this.visitVariableDeclaration(node);
94 ts.forEachChild(node, _this.visitNode);
98 NoThisAssignmentWalker.prototype.walk = function (sourceFile) {
99 ts.forEachChild(sourceFile, this.visitNode);
101 NoThisAssignmentWalker.prototype.visitVariableDeclaration = function (node) {
102 if (node.initializer === undefined || node.initializer.kind !== ts.SyntaxKind.ThisKeyword) {
105 switch (node.name.kind) {
106 case ts.SyntaxKind.Identifier:
107 if (this.variableNameIsBanned(node.name.text)) {
108 this.addFailureAtNode(node, Rule.FAILURE_STRING_FACTORY_IDENTIFIERS(node.name.text));
112 if (!this.options.allowDestructuring) {
113 this.addFailureAtNode(node, Rule.FAILURE_STRING_BINDINGS);
117 NoThisAssignmentWalker.prototype.variableNameIsBanned = function (name) {
118 for (var _i = 0, _a = this.allowedThisNameTesters; _i < _a.length; _i++) {
120 if (tester.test(name)) {
126 return NoThisAssignmentWalker;
127 }(Lint.AbstractWalker));
128 var templateObject_1, templateObject_2;