.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noThisAssignmentRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var _a, _b;
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]);
33         }
34     }
35     return { allowedNames: allowedNames, allowDestructuring: allowDestructuring };
36 };
37 var Rule = /** @class */ (function (_super) {
38     tslib_1.__extends(Rule, _super);
39     function Rule() {
40         return _super !== null && _super.apply(this, arguments) || this;
41     }
42     Rule.FAILURE_STRING_FACTORY_IDENTIFIERS = function (name) {
43         return "Assigning `this` reference to local variable not allowed: " + name + ".";
44     };
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);
49     };
50     Rule.metadata = {
51         description: "Disallows unnecessary references to `this`.",
52         optionExamples: [
53             true,
54             [
55                 true,
56                 (_a = {},
57                     _a[ALLOWED_THIS_NAMES] = ["^self$"],
58                     _a[ALLOW_THIS_DESTRUCTURING] = true,
59                     _a),
60             ],
61         ],
62         options: {
63             additionalProperties: false,
64             properties: (_b = {},
65                 _b[ALLOW_THIS_DESTRUCTURING] = {
66                     type: "boolean",
67                 },
68                 _b[ALLOWED_THIS_NAMES] = {
69                     listType: "string",
70                     type: "list",
71                 },
72                 _b),
73             type: "object",
74         },
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,
80     };
81     Rule.FAILURE_STRING_BINDINGS = "Don't assign members of `this` to local variables.";
82     return Rule;
83 }(Lint.Rules.AbstractRule));
84 exports.Rule = Rule;
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);
93             }
94             ts.forEachChild(node, _this.visitNode);
95         };
96         return _this;
97     }
98     NoThisAssignmentWalker.prototype.walk = function (sourceFile) {
99         ts.forEachChild(sourceFile, this.visitNode);
100     };
101     NoThisAssignmentWalker.prototype.visitVariableDeclaration = function (node) {
102         if (node.initializer === undefined || node.initializer.kind !== ts.SyntaxKind.ThisKeyword) {
103             return;
104         }
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));
109                 }
110                 break;
111             default:
112                 if (!this.options.allowDestructuring) {
113                     this.addFailureAtNode(node, Rule.FAILURE_STRING_BINDINGS);
114                 }
115         }
116     };
117     NoThisAssignmentWalker.prototype.variableNameIsBanned = function (name) {
118         for (var _i = 0, _a = this.allowedThisNameTesters; _i < _a.length; _i++) {
119             var tester = _a[_i];
120             if (tester.test(name)) {
121                 return false;
122             }
123         }
124         return true;
125     };
126     return NoThisAssignmentWalker;
127 }(Lint.AbstractWalker));
128 var templateObject_1, templateObject_2;