noFloatingPromisesRule.js 4.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  29. return this.applyWithFunction(sourceFile, walk, ["Promise"].concat(this.ruleArguments), program.getTypeChecker());
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "no-floating-promises",
  34. description: "Promises returned by functions must be handled appropriately.",
  35. descriptionDetails: "Unhandled Promises can cause unexpected behavior, such as resolving at unexpected times.",
  36. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n A list of 'string' names of any additional classes that should also be handled as Promises.\n "], ["\n A list of \\'string\\' names of any additional classes that should also be handled as Promises.\n "]))),
  37. options: {
  38. type: "list",
  39. listType: {
  40. type: "array",
  41. items: { type: "string" },
  42. },
  43. },
  44. optionExamples: [true, [true, "JQueryPromise"]],
  45. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Creating a Promise and not storing or returning may lets other code run independent of of its results.\n This can cause unexpected and/or non-deterministic behavior depending on external timing factors.\n\n It's typically better to return Promises from functions that start them, then handle them in calling code.\n\n Use `no-unused-expression` in addition to this rule to reveal even more floating promises.\n "], ["\n Creating a Promise and not storing or returning may lets other code run independent of of its results.\n This can cause unexpected and/or non-deterministic behavior depending on external timing factors.\n\n It's typically better to return Promises from functions that start them, then handle them in calling code.\n\n Use \\`no-unused-expression\\` in addition to this rule to reveal even more floating promises.\n "]))),
  46. type: "functionality",
  47. typescriptOnly: true,
  48. requiresTypeInfo: true,
  49. };
  50. /* tslint:enable:object-literal-sort-keys */
  51. Rule.FAILURE_STRING = "Promises must be handled appropriately";
  52. return Rule;
  53. }(Lint.Rules.TypedRule));
  54. exports.Rule = Rule;
  55. function walk(ctx, tc) {
  56. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  57. if (tsutils_1.isExpressionStatement(node)) {
  58. var expression = node.expression;
  59. if (tsutils_1.isCallExpression(expression) &&
  60. !isPromiseCatchCall(expression) &&
  61. !isPromiseThenCallWithRejectionHandler(expression)) {
  62. var symbol = tc.getTypeAtLocation(expression).symbol;
  63. if (symbol !== undefined && ctx.options.indexOf(symbol.name) !== -1) {
  64. ctx.addFailureAtNode(expression, Rule.FAILURE_STRING);
  65. }
  66. }
  67. }
  68. return ts.forEachChild(node, cb);
  69. });
  70. }
  71. function isPromiseCatchCall(expression) {
  72. return tsutils_1.isPropertyAccessExpression(expression.expression) && expression.expression.name.text === "catch";
  73. }
  74. function isPromiseThenCallWithRejectionHandler(expression) {
  75. return tsutils_1.isPropertyAccessExpression(expression.expression) &&
  76. expression.expression.name.text === "then" &&
  77. expression.arguments.length >= 2;
  78. }
  79. var templateObject_1, templateObject_2;