4 * Copyright 2018 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");
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);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
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);
36 /* tslint:disable:object-literal-sort-keys */
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),
46 enum: [OPTION_FUNCTION_IN_METHOD],
51 optionExamples: [true, [true, OPTION_FUNCTION_IN_METHOD]],
52 type: "functionality",
53 typescriptOnly: false,
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";
60 }(Lint.Rules.AbstractRule));
62 var thisAllowedParents = new Set([2 /* ClassMethod */, 3 /* BoundRegularFunction */]);
64 var sourceFile = ctx.sourceFile, checkFuncInMethod = ctx.options;
65 var currentParent = 0 /* None */;
67 ts.forEachChild(sourceFile, function cb(node) {
68 var originalParent = currentParent;
69 var originalInClass = inClass;
71 case ts.SyntaxKind.ClassDeclaration:
72 case ts.SyntaxKind.ClassExpression:
74 currentParent = 1 /* Class */;
75 ts.forEachChild(node, cb);
76 currentParent = originalParent;
77 inClass = originalInClass;
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;
93 currentParent = node.parameters.some(tsutils_1.isThisParameter)
94 ? 3 /* BoundRegularFunction */
95 : 4 /* UnboundRegularFunction */;
96 ts.forEachChild(node, cb);
97 currentParent = originalParent;
100 case ts.SyntaxKind.ThisKeyword:
101 if (!thisAllowedParents.has(currentParent)) {
103 ctx.addFailureAtNode(node, Rule.FAILURE_STRING_OUTSIDE);
105 else if (checkFuncInMethod) {
106 ctx.addFailureAtNode(node, Rule.FAILURE_STRING_INSIDE);
111 ts.forEachChild(node, cb);
114 var templateObject_1;