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");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_ALLOW_PUBLIC = "allow-public";
24 var OPTION_ALLOW_PROTECTED = "allow-protected";
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) {
31 return this.applyWithWalker(new PreferFunctionOverMethodWalker(sourceFile, this.ruleName, {
32 allowProtected: this.ruleArguments.indexOf(OPTION_ALLOW_PROTECTED) !== -1,
33 allowPublic: this.ruleArguments.indexOf(OPTION_ALLOW_PUBLIC) !== -1,
36 /* tslint:disable:object-literal-sort-keys */
38 ruleName: "prefer-function-over-method",
39 description: "Warns for class methods that do not use 'this'.",
40 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n \"", "\" excludes checking of public methods.\n \"", "\" excludes checking of protected methods."], ["\n \"", "\" excludes checking of public methods.\n \"", "\" excludes checking of protected methods."])), OPTION_ALLOW_PUBLIC, OPTION_ALLOW_PROTECTED),
43 enum: [OPTION_ALLOW_PUBLIC, OPTION_ALLOW_PROTECTED],
45 optionExamples: [true, [true, OPTION_ALLOW_PUBLIC, OPTION_ALLOW_PROTECTED]],
47 typescriptOnly: false,
49 /* tslint:enable:object-literal-sort-keys */
50 Rule.FAILURE_STRING = "Class method does not use 'this'. Use a function instead.";
52 }(Lint.Rules.AbstractRule));
54 var PreferFunctionOverMethodWalker = /** @class */ (function (_super) {
55 tslib_1.__extends(PreferFunctionOverMethodWalker, _super);
56 function PreferFunctionOverMethodWalker() {
57 return _super !== null && _super.apply(this, arguments) || this;
59 PreferFunctionOverMethodWalker.prototype.walk = function (sourceFile) {
61 var cb = function (node) {
62 if (tsutils_1.isMethodDeclaration(node) && !_this.isExempt(node)) {
63 // currentScope is always undefined here, so we don't need to save it and just set it to undefined afterwards
64 _this.currentScope = {
66 name: tsutils_1.getPropertyName(node.name),
68 ts.forEachChild(node, cb);
69 if (!_this.currentScope.isThisUsed) {
70 _this.addFailureAtNode(node.name, Rule.FAILURE_STRING);
72 _this.currentScope = undefined;
74 else if (tsutils_1.hasOwnThisReference(node)) {
75 var scope = _this.currentScope;
76 _this.currentScope = undefined;
77 ts.forEachChild(node, cb);
78 _this.currentScope = scope;
80 else if (_this.currentScope !== undefined &&
81 ((node.kind === ts.SyntaxKind.ThisKeyword &&
82 !isRecursiveCall(node, _this.currentScope.name)) ||
83 node.kind === ts.SyntaxKind.SuperKeyword)) {
84 _this.currentScope.isThisUsed = true;
87 return ts.forEachChild(node, cb);
90 return ts.forEachChild(sourceFile, cb);
92 PreferFunctionOverMethodWalker.prototype.isExempt = function (node) {
93 // TODO: handle the override keyword once it lands in the language
94 return (node.body === undefined || // exclude abstract methods and overload signatures
95 // exclude object methods
96 (node.parent.kind !== ts.SyntaxKind.ClassDeclaration &&
97 node.parent.kind !== ts.SyntaxKind.ClassExpression) ||
98 tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword) ||
99 (this.options.allowProtected &&
100 tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword)) ||
101 (this.options.allowPublic &&
102 (tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword) ||
103 !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.PrivateKeyword))));
105 return PreferFunctionOverMethodWalker;
106 }(Lint.AbstractWalker));
107 function isRecursiveCall(node, name) {
108 return (name !== undefined &&
109 node.parent.kind === ts.SyntaxKind.PropertyAccessExpression &&
110 node.parent.name.text === name);
112 var templateObject_1;