noInvalidThisRule.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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. function walk(ctx) {
  63. var sourceFile = ctx.sourceFile, checkFuncInMethod = ctx.options;
  64. var inClass = false;
  65. var inFunctionInClass = false;
  66. ts.forEachChild(sourceFile, function cb(node) {
  67. switch (node.kind) {
  68. case ts.SyntaxKind.ClassDeclaration:
  69. case ts.SyntaxKind.ClassExpression:
  70. if (!inClass) {
  71. inClass = true;
  72. ts.forEachChild(node, cb);
  73. inClass = false;
  74. return;
  75. }
  76. break;
  77. case ts.SyntaxKind.FunctionDeclaration:
  78. case ts.SyntaxKind.FunctionExpression:
  79. if (node.parameters.some(tsutils_1.isThisParameter)) {
  80. return;
  81. }
  82. if (inClass) {
  83. inFunctionInClass = true;
  84. ts.forEachChild(node, cb);
  85. inFunctionInClass = false;
  86. return;
  87. }
  88. break;
  89. case ts.SyntaxKind.ThisKeyword:
  90. if (!inClass) {
  91. ctx.addFailureAtNode(node, Rule.FAILURE_STRING_OUTSIDE);
  92. }
  93. else if (checkFuncInMethod && inFunctionInClass) {
  94. ctx.addFailureAtNode(node, Rule.FAILURE_STRING_INSIDE);
  95. }
  96. return;
  97. }
  98. ts.forEachChild(node, cb);
  99. });
  100. }
  101. var templateObject_1;