4 * Copyright 2017 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 var promiseTypes = new Set(["Promise"].concat(this.ruleArguments));
30 return this.applyWithFunction(sourceFile, walk, promiseTypes, program.getTypeChecker());
32 /* tslint:disable:object-literal-sort-keys */
34 ruleName: "await-promise",
35 description: "Warns for an awaited value that is not a Promise.",
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 treated as Promises.\n For example, if you are using a class called 'Future' that implements the Thenable interface,\n you might tell the rule to consider type references with the name 'Future' as valid Promise-like\n types. Note that this rule doesn't check for type assignability or compatibility; it just checks\n type reference names.\n "], ["\n A list of 'string' names of any additional classes that should also be treated as Promises.\n For example, if you are using a class called 'Future' that implements the Thenable interface,\n you might tell the rule to consider type references with the name 'Future' as valid Promise-like\n types. Note that this rule doesn't check for type assignability or compatibility; it just checks\n type reference names.\n "]))),
41 items: { type: "string" },
44 optionExamples: [true, [true, "Thenable"]],
45 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n While it is valid JavaScript to await a non-Promise-like value (it will resolve immediately),\n this pattern is often a programmer error and the resulting semantics can be unintuitive.\n\n Awaiting non-Promise-like values often is an indication of programmer error, such as\n forgetting to add parenthesis to call a function that returns a Promise.\n "], ["\n While it is valid JavaScript to await a non-Promise-like value (it will resolve immediately),\n this pattern is often a programmer error and the resulting semantics can be unintuitive.\n\n Awaiting non-Promise-like values often is an indication of programmer error, such as\n forgetting to add parenthesis to call a function that returns a Promise.\n "]))),
46 type: "functionality",
48 requiresTypeInfo: true,
50 /* tslint:enable:object-literal-sort-keys */
51 Rule.FAILURE_STRING = "Invalid 'await' of a non-Promise value.";
52 Rule.FAILURE_FOR_AWAIT_OF = "Invalid 'for-await-of' of a non-AsyncIterable value.";
54 }(Lint.Rules.TypedRule));
56 function walk(ctx, tc) {
57 var promiseTypes = ctx.options;
58 return ts.forEachChild(ctx.sourceFile, cb);
60 if (tsutils_1.isAwaitExpression(node) &&
61 !containsType(tc.getTypeAtLocation(node.expression), isPromiseType)) {
62 ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
64 else if (tsutils_1.isForOfStatement(node) &&
65 node.awaitModifier !== undefined &&
66 !containsType(tc.getTypeAtLocation(node.expression), isAsyncIterable)) {
67 ctx.addFailureAtNode(node.expression, Rule.FAILURE_FOR_AWAIT_OF);
69 return ts.forEachChild(node, cb);
71 function isPromiseType(name) {
72 return promiseTypes.has(name);
75 function containsType(type, predicate) {
76 if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Any)) {
79 if (tsutils_1.isTypeReference(type)) {
82 if (type.symbol !== undefined && predicate(type.symbol.name)) {
85 if (tsutils_1.isUnionOrIntersectionType(type)) {
86 return type.types.some(function (t) { return containsType(t, predicate); });
88 var bases = type.getBaseTypes();
89 return bases !== undefined && bases.some(function (t) { return containsType(t, predicate); });
91 function isAsyncIterable(name) {
92 return (name === "AsyncIterable" || name === "AsyncIterableIterator" || name === "AsyncGenerator");
94 var templateObject_1, templateObject_2;