.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / awaitPromiseRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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         var promiseTypes = new Set(["Promise"].concat(this.ruleArguments));
30         return this.applyWithFunction(sourceFile, walk, promiseTypes, program.getTypeChecker());
31     };
32     /* tslint:disable:object-literal-sort-keys */
33     Rule.metadata = {
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        "]))),
37         options: {
38             type: "list",
39             listType: {
40                 type: "array",
41                 items: { type: "string" },
42             },
43         },
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",
47         typescriptOnly: true,
48         requiresTypeInfo: true,
49     };
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.";
53     return Rule;
54 }(Lint.Rules.TypedRule));
55 exports.Rule = Rule;
56 function walk(ctx, tc) {
57     var promiseTypes = ctx.options;
58     return ts.forEachChild(ctx.sourceFile, cb);
59     function cb(node) {
60         if (tsutils_1.isAwaitExpression(node) &&
61             !containsType(tc.getTypeAtLocation(node.expression), isPromiseType)) {
62             ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
63         }
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);
68         }
69         return ts.forEachChild(node, cb);
70     }
71     function isPromiseType(name) {
72         return promiseTypes.has(name);
73     }
74 }
75 function containsType(type, predicate) {
76     if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Any)) {
77         return true;
78     }
79     if (tsutils_1.isTypeReference(type)) {
80         type = type.target;
81     }
82     if (type.symbol !== undefined && predicate(type.symbol.name)) {
83         return true;
84     }
85     if (tsutils_1.isUnionOrIntersectionType(type)) {
86         return type.types.some(function (t) { return containsType(t, predicate); });
87     }
88     var bases = type.getBaseTypes();
89     return bases !== undefined && bases.some(function (t) { return containsType(t, predicate); });
90 }
91 function isAsyncIterable(name) {
92     return (name === "AsyncIterable" || name === "AsyncIterableIterator" || name === "AsyncGenerator");
93 }
94 var templateObject_1, templateObject_2;