noUnboundMethodRule.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_IGNORE_STATIC = "ignore-static";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  30. return this.applyWithFunction(sourceFile, walk, {
  31. ignoreStatic: this.ruleArguments.indexOf(OPTION_IGNORE_STATIC) !== -1,
  32. }, program.getTypeChecker());
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "no-unbound-method",
  37. description: "Warns when a method is used as outside of a method call.",
  38. optionsDescription: "You may optionally pass \"" + OPTION_IGNORE_STATIC + "\" to ignore static methods.",
  39. options: {
  40. type: "string",
  41. enum: [OPTION_IGNORE_STATIC],
  42. },
  43. optionExamples: [true, [true, OPTION_IGNORE_STATIC]],
  44. type: "functionality",
  45. typescriptOnly: true,
  46. requiresTypeInfo: true,
  47. };
  48. /* tslint:enable:object-literal-sort-keys */
  49. Rule.FAILURE_STRING = "Avoid referencing unbound methods which may cause unintentional scoping of 'this'.";
  50. return Rule;
  51. }(Lint.Rules.TypedRule));
  52. exports.Rule = Rule;
  53. function walk(ctx, tc) {
  54. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  55. if (tsutils_1.isPropertyAccessExpression(node) && !isSafeUse(node)) {
  56. var symbol = tc.getSymbolAtLocation(node);
  57. var declaration = symbol === undefined ? undefined : symbol.valueDeclaration;
  58. if (declaration !== undefined && isMethod(declaration, ctx.options.ignoreStatic)) {
  59. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  60. }
  61. }
  62. return ts.forEachChild(node, cb);
  63. });
  64. }
  65. function isMethod(node, ignoreStatic) {
  66. switch (node.kind) {
  67. case ts.SyntaxKind.MethodDeclaration:
  68. case ts.SyntaxKind.MethodSignature:
  69. return !(ignoreStatic && tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword));
  70. default:
  71. return false;
  72. }
  73. }
  74. function isSafeUse(node) {
  75. var parent = node.parent;
  76. switch (parent.kind) {
  77. case ts.SyntaxKind.CallExpression:
  78. return parent.expression === node;
  79. case ts.SyntaxKind.TaggedTemplateExpression:
  80. return parent.tag === node;
  81. // E.g. `obj.method.bind(obj)`.
  82. case ts.SyntaxKind.PropertyAccessExpression:
  83. return true;
  84. // Allow most binary operators, but don't allow e.g. `myArray.forEach(obj.method || otherObj.otherMethod)`.
  85. case ts.SyntaxKind.BinaryExpression:
  86. return parent.operatorToken.kind !== ts.SyntaxKind.BarBarToken;
  87. case ts.SyntaxKind.NonNullExpression:
  88. case ts.SyntaxKind.AsExpression:
  89. case ts.SyntaxKind.TypeAssertionExpression:
  90. case ts.SyntaxKind.ParenthesizedExpression:
  91. return isSafeUse(parent);
  92. // Allow use in conditions
  93. case ts.SyntaxKind.ConditionalExpression:
  94. return parent.condition === node;
  95. case ts.SyntaxKind.IfStatement:
  96. case ts.SyntaxKind.WhileStatement:
  97. case ts.SyntaxKind.DoStatement:
  98. case ts.SyntaxKind.ForStatement:
  99. case ts.SyntaxKind.PrefixUnaryExpression:
  100. return true;
  101. default:
  102. return false;
  103. }
  104. }