4 * Copyright 2018 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.applyWithProgram = function (sourceFile, program) {
29 return this.applyWithFunction(sourceFile, walk, ["Promise"].concat(this.ruleArguments), program.getTypeChecker());
31 /* tslint:disable:object-literal-sort-keys */
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 "]))),
41 items: { type: "string" },
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 it may let other code run independently of its result.\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 it may let other code run independently of its result.\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",
48 requiresTypeInfo: true,
50 /* tslint:enable:object-literal-sort-keys */
51 Rule.FAILURE_STRING = "Promises must be handled appropriately";
53 }(Lint.Rules.TypedRule));
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);
68 return ts.forEachChild(node, cb);
71 function isPromiseCatchCall(expression) {
72 return (tsutils_1.isPropertyAccessExpression(expression.expression) &&
73 expression.expression.name.text === "catch");
75 function isPromiseThenCallWithRejectionHandler(expression) {
76 return (tsutils_1.isPropertyAccessExpression(expression.expression) &&
77 expression.expression.name.text === "then" &&
78 expression.arguments.length >= 2);
80 var templateObject_1, templateObject_2;