.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noInvalidThisRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 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 tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("..");
23 var OPTION_FUNCTION_IN_METHOD = "check-function-in-method";
24 var DEPRECATED_OPTION_FUNCTION_IN_METHOD = "no-this-in-function-in-method";
25 var Rule = /** @class */ (function (_super) {
26     tslib_1.__extends(Rule, _super);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     Rule.prototype.apply = function (sourceFile) {
31         var _this = this;
32         var hasOption = function (name) { return _this.ruleArguments.indexOf(name) !== -1; };
33         var checkFuncInMethod = hasOption(DEPRECATED_OPTION_FUNCTION_IN_METHOD) || hasOption(OPTION_FUNCTION_IN_METHOD);
34         return this.applyWithFunction(sourceFile, walk, checkFuncInMethod);
35     };
36     /* tslint:disable:object-literal-sort-keys */
37     Rule.metadata = {
38         ruleName: "no-invalid-this",
39         description: "Disallows using the `this` keyword outside of classes.",
40         rationale: "See [the rule's author's rationale here.](https://github.com/palantir/tslint/pull/1105#issue-147549402)",
41         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            One argument may be optionally provided:\n\n            * `", "` disallows using the `this` keyword in functions within class methods."], ["\n            One argument may be optionally provided:\n\n            * \\`", "\\` disallows using the \\`this\\` keyword in functions within class methods."])), OPTION_FUNCTION_IN_METHOD),
42         options: {
43             type: "array",
44             items: {
45                 type: "string",
46                 enum: [OPTION_FUNCTION_IN_METHOD],
47             },
48             minLength: 0,
49             maxLength: 1,
50         },
51         optionExamples: [true, [true, OPTION_FUNCTION_IN_METHOD]],
52         type: "functionality",
53         typescriptOnly: false,
54     };
55     /* tslint:enable:object-literal-sort-keys */
56     Rule.FAILURE_STRING_OUTSIDE = 'the "this" keyword is disallowed outside of a class body';
57     Rule.FAILURE_STRING_INSIDE = 'the "this" keyword is disallowed in function bodies inside class methods, ' +
58         "use arrow functions instead";
59     return Rule;
60 }(Lint.Rules.AbstractRule));
61 exports.Rule = Rule;
62 var thisAllowedParents = new Set([2 /* ClassMethod */, 3 /* BoundRegularFunction */]);
63 function walk(ctx) {
64     var sourceFile = ctx.sourceFile, checkFuncInMethod = ctx.options;
65     var currentParent = 0 /* None */;
66     var inClass = false;
67     ts.forEachChild(sourceFile, function cb(node) {
68         var originalParent = currentParent;
69         var originalInClass = inClass;
70         switch (node.kind) {
71             case ts.SyntaxKind.ClassDeclaration:
72             case ts.SyntaxKind.ClassExpression:
73                 inClass = true;
74                 currentParent = 1 /* Class */;
75                 ts.forEachChild(node, cb);
76                 currentParent = originalParent;
77                 inClass = originalInClass;
78                 return;
79             case ts.SyntaxKind.MethodDeclaration:
80             case ts.SyntaxKind.GetAccessor:
81             case ts.SyntaxKind.SetAccessor:
82             case ts.SyntaxKind.Constructor:
83             case ts.SyntaxKind.PropertyDeclaration:
84             case ts.SyntaxKind.FunctionDeclaration:
85             case ts.SyntaxKind.FunctionExpression:
86                 if (currentParent === 1 /* Class */) {
87                     currentParent = 2 /* ClassMethod */;
88                     ts.forEachChild(node, cb);
89                     currentParent = originalParent;
90                     return;
91                 }
92                 else {
93                     currentParent = node.parameters.some(tsutils_1.isThisParameter)
94                         ? 3 /* BoundRegularFunction */
95                         : 4 /* UnboundRegularFunction */;
96                     ts.forEachChild(node, cb);
97                     currentParent = originalParent;
98                     return;
99                 }
100             case ts.SyntaxKind.ThisKeyword:
101                 if (!thisAllowedParents.has(currentParent)) {
102                     if (!inClass) {
103                         ctx.addFailureAtNode(node, Rule.FAILURE_STRING_OUTSIDE);
104                     }
105                     else if (checkFuncInMethod) {
106                         ctx.addFailureAtNode(node, Rule.FAILURE_STRING_INSIDE);
107                     }
108                 }
109                 return;
110         }
111         ts.forEachChild(node, cb);
112     });
113 }
114 var templateObject_1;