preferFunctionOverMethodRule.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 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);
  27. function Rule() {
  28. return _super !== null && _super.apply(this, arguments) || this;
  29. }
  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,
  34. }));
  35. };
  36. /* tslint:disable:object-literal-sort-keys */
  37. Rule.metadata = {
  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),
  41. options: {
  42. type: "string",
  43. enum: [OPTION_ALLOW_PUBLIC, OPTION_ALLOW_PROTECTED],
  44. },
  45. optionExamples: [
  46. true,
  47. [true, OPTION_ALLOW_PUBLIC, OPTION_ALLOW_PROTECTED],
  48. ],
  49. type: "style",
  50. typescriptOnly: false,
  51. };
  52. /* tslint:enable:object-literal-sort-keys */
  53. Rule.FAILURE_STRING = "Class method does not use 'this'. Use a function instead.";
  54. return Rule;
  55. }(Lint.Rules.AbstractRule));
  56. exports.Rule = Rule;
  57. var PreferFunctionOverMethodWalker = /** @class */ (function (_super) {
  58. tslib_1.__extends(PreferFunctionOverMethodWalker, _super);
  59. function PreferFunctionOverMethodWalker() {
  60. return _super !== null && _super.apply(this, arguments) || this;
  61. }
  62. PreferFunctionOverMethodWalker.prototype.walk = function (sourceFile) {
  63. var _this = this;
  64. var cb = function (node) {
  65. if (tsutils_1.isMethodDeclaration(node) && !_this.isExempt(node)) {
  66. // currentScope is always undefined here, so we don't need to save it and just set it to undefined afterwards
  67. _this.currentScope = {
  68. isThisUsed: false,
  69. name: tsutils_1.getPropertyName(node.name),
  70. };
  71. ts.forEachChild(node, cb);
  72. if (!_this.currentScope.isThisUsed) {
  73. _this.addFailureAtNode(node.name, Rule.FAILURE_STRING);
  74. }
  75. _this.currentScope = undefined;
  76. }
  77. else if (tsutils_1.hasOwnThisReference(node)) {
  78. var scope = _this.currentScope;
  79. _this.currentScope = undefined;
  80. ts.forEachChild(node, cb);
  81. _this.currentScope = scope;
  82. }
  83. else if (_this.currentScope !== undefined &&
  84. (node.kind === ts.SyntaxKind.ThisKeyword && !isRecursiveCall(node, _this.currentScope.name) ||
  85. node.kind === ts.SyntaxKind.SuperKeyword)) {
  86. _this.currentScope.isThisUsed = true;
  87. }
  88. else {
  89. return ts.forEachChild(node, cb);
  90. }
  91. };
  92. return ts.forEachChild(sourceFile, cb);
  93. };
  94. PreferFunctionOverMethodWalker.prototype.isExempt = function (node) {
  95. // TODO: handle the override keyword once it lands in the language
  96. return node.body === undefined || // exclude abstract methods and overload signatures
  97. // exclude object methods
  98. node.parent.kind !== ts.SyntaxKind.ClassDeclaration && node.parent.kind !== ts.SyntaxKind.ClassExpression ||
  99. tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword) ||
  100. this.options.allowProtected && tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword) ||
  101. this.options.allowPublic && (tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword) ||
  102. !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.PrivateKeyword));
  103. };
  104. return PreferFunctionOverMethodWalker;
  105. }(Lint.AbstractWalker));
  106. function isRecursiveCall(node, name) {
  107. return name !== undefined &&
  108. node.parent.kind === ts.SyntaxKind.PropertyAccessExpression &&
  109. node.parent.name.text === name;
  110. }
  111. var templateObject_1;