promiseFunctionAsyncRule.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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("../index");
  23. var OPTION_FUNCTION_DECLARATION = "check-function-declaration";
  24. var OPTION_FUNCTION_EXPRESSION = "check-function-expression";
  25. var OPTION_ARROW_FUNCTION = "check-arrow-function";
  26. var OPTION_METHOD_DECLARATION = "check-method-declaration";
  27. var KIND_FOR_OPTION = (_a = {},
  28. _a[OPTION_FUNCTION_DECLARATION] = ts.SyntaxKind.FunctionDeclaration,
  29. _a[OPTION_FUNCTION_EXPRESSION] = ts.SyntaxKind.FunctionExpression,
  30. _a[OPTION_ARROW_FUNCTION] = ts.SyntaxKind.ArrowFunction,
  31. _a[OPTION_METHOD_DECLARATION] = ts.SyntaxKind.MethodDeclaration,
  32. _a);
  33. function parseOptions(ruleArguments) {
  34. if (ruleArguments.length === 0) {
  35. ruleArguments = Object.keys(KIND_FOR_OPTION);
  36. }
  37. var enabledKinds = new Set();
  38. for (var _i = 0, ruleArguments_1 = ruleArguments; _i < ruleArguments_1.length; _i++) {
  39. var arg = ruleArguments_1[_i];
  40. enabledKinds.add(KIND_FOR_OPTION[arg]);
  41. }
  42. return enabledKinds;
  43. }
  44. var Rule = /** @class */ (function (_super) {
  45. tslib_1.__extends(Rule, _super);
  46. function Rule() {
  47. return _super !== null && _super.apply(this, arguments) || this;
  48. }
  49. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  50. return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments), program.getTypeChecker());
  51. };
  52. /* tslint:disable:object-literal-sort-keys */
  53. Rule.metadata = {
  54. ruleName: "promise-function-async",
  55. description: "Requires any function or method that returns a promise to be marked async.",
  56. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Ensures that each function is only capable of 1) returning a rejected promise, or 2)\n throwing an Error object. In contrast, non-`async` `Promise`-returning functions\n are technically capable of either. This practice removes a requirement for consuming\n code to handle both cases.\n\n If no optional arguments are provided then all function types are checked,\n otherwise the specific function types are checked:\n\n * `\"", "\"` check function declarations.\n * `\"", "\"` check function expressions.\n * `\"", "\"` check arrow functions.\n * `\"", "\"` check method declarations.\n "], ["\n Ensures that each function is only capable of 1) returning a rejected promise, or 2)\n throwing an Error object. In contrast, non-\\`async\\` \\`Promise\\`-returning functions\n are technically capable of either. This practice removes a requirement for consuming\n code to handle both cases.\n\n If no optional arguments are provided then all function types are checked,\n otherwise the specific function types are checked:\n\n * \\`\"", "\"\\` check function declarations.\n * \\`\"", "\"\\` check function expressions.\n * \\`\"", "\"\\` check arrow functions.\n * \\`\"", "\"\\` check method declarations.\n "])), OPTION_FUNCTION_DECLARATION, OPTION_FUNCTION_EXPRESSION, OPTION_ARROW_FUNCTION, OPTION_METHOD_DECLARATION),
  57. optionsDescription: "Not configurable.",
  58. options: {
  59. type: "array",
  60. items: {
  61. type: "string",
  62. enum: [
  63. OPTION_FUNCTION_DECLARATION,
  64. OPTION_FUNCTION_EXPRESSION,
  65. OPTION_ARROW_FUNCTION,
  66. OPTION_METHOD_DECLARATION,
  67. ],
  68. },
  69. minLength: 0,
  70. maxLength: 4,
  71. },
  72. optionExamples: [true,
  73. [true, OPTION_FUNCTION_DECLARATION, OPTION_METHOD_DECLARATION]],
  74. type: "typescript",
  75. typescriptOnly: false,
  76. requiresTypeInfo: true,
  77. };
  78. /* tslint:enable:object-literal-sort-keys */
  79. Rule.FAILURE_STRING = "functions that return promises must be async";
  80. return Rule;
  81. }(Lint.Rules.TypedRule));
  82. exports.Rule = Rule;
  83. function walk(ctx, tc) {
  84. var sourceFile = ctx.sourceFile, options = ctx.options;
  85. return ts.forEachChild(sourceFile, function cb(node) {
  86. if (options.has(node.kind)) {
  87. switch (node.kind) {
  88. case ts.SyntaxKind.MethodDeclaration:
  89. case ts.SyntaxKind.FunctionDeclaration:
  90. if (node.body === undefined) {
  91. break;
  92. }
  93. // falls through
  94. case ts.SyntaxKind.FunctionExpression:
  95. case ts.SyntaxKind.ArrowFunction:
  96. if (!tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword)
  97. && returnsPromise(node, tc)) {
  98. ctx.addFailure(node.getStart(sourceFile), node.body.pos, Rule.FAILURE_STRING);
  99. }
  100. }
  101. }
  102. return ts.forEachChild(node, cb);
  103. });
  104. }
  105. function returnsPromise(node, tc) {
  106. var type = tc.getReturnTypeOfSignature(tc.getTypeAtLocation(node).getCallSignatures()[0]);
  107. return type.symbol !== undefined && type.symbol.name === "Promise";
  108. }
  109. var templateObject_1;
  110. var _a;