.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noAsyncWithoutAwaitRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 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 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var noAsyncWithoutAwait_examples_1 = require("./code-examples/noAsyncWithoutAwait.examples");
24 var Rule = /** @class */ (function (_super) {
25     tslib_1.__extends(Rule, _super);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     Rule.prototype.apply = function (sourceFile) {
30         return this.applyWithFunction(sourceFile, walk);
31     };
32     Rule.FAILURE_STRING = "Functions marked async must contain an await or return statement.";
33     Rule.metadata = {
34         codeExamples: noAsyncWithoutAwait_examples_1.codeExamples,
35         description: Rule.FAILURE_STRING,
36         hasFix: false,
37         optionExamples: [true],
38         options: null,
39         optionsDescription: "Not configurable.",
40         /* tslint:disable:max-line-length */
41         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n        Marking a function as `async` without using `await` or returning a value inside it can lead to an unintended promise return and a larger transpiled output.\n        Often the function can be synchronous and the `async` keyword is there by mistake.\n        Return statements are allowed as sometimes it is desirable to wrap the returned value in a Promise."], ["\n        Marking a function as \\`async\\` without using \\`await\\` or returning a value inside it can lead to an unintended promise return and a larger transpiled output.\n        Often the function can be synchronous and the \\`async\\` keyword is there by mistake.\n        Return statements are allowed as sometimes it is desirable to wrap the returned value in a Promise."]))),
42         /* tslint:enable:max-line-length */
43         ruleName: "no-async-without-await",
44         type: "functionality",
45         typescriptOnly: false,
46     };
47     return Rule;
48 }(Lint.Rules.AbstractRule));
49 exports.Rule = Rule;
50 function walk(context) {
51     var reportFailureIfAsyncFunction = function (node) {
52         var asyncModifier = getAsyncModifier(node);
53         if (asyncModifier !== undefined) {
54             context.addFailureAt(asyncModifier.getStart(), asyncModifier.getEnd() - asyncModifier.getStart(), Rule.FAILURE_STRING);
55         }
56     };
57     var addFailureIfAsyncFunctionHasNoAwait = function (node) {
58         if (node.body === undefined) {
59             if (node.type === undefined) {
60                 reportFailureIfAsyncFunction(node);
61             }
62             return;
63         }
64         if (!isShortArrowReturn(node) &&
65             !functionBlockHasAwait(node.body) &&
66             !functionBlockHasReturn(node.body)) {
67             reportFailureIfAsyncFunction(node);
68         }
69     };
70     return ts.forEachChild(context.sourceFile, function visitNode(node) {
71         if (tsutils.isArrowFunction(node) ||
72             tsutils.isFunctionDeclaration(node) ||
73             tsutils.isFunctionExpression(node) ||
74             tsutils.isMethodDeclaration(node)) {
75             addFailureIfAsyncFunctionHasNoAwait(node);
76         }
77         return ts.forEachChild(node, visitNode);
78     });
79 }
80 var getAsyncModifier = function (node) {
81     if (node.modifiers !== undefined) {
82         return node.modifiers.find(function (modifier) { return modifier.kind === ts.SyntaxKind.AsyncKeyword; });
83     }
84     return undefined;
85 };
86 var isReturn = function (node) { return node.kind === ts.SyntaxKind.ReturnKeyword; };
87 var functionBlockHasAwait = function (node) {
88     if (tsutils.isAwaitExpression(node)) {
89         return true;
90     }
91     if (node.kind === ts.SyntaxKind.ArrowFunction ||
92         node.kind === ts.SyntaxKind.FunctionDeclaration) {
93         return false;
94     }
95     return node.getChildren().some(functionBlockHasAwait);
96 };
97 var functionBlockHasReturn = function (node) {
98     if (isReturn(node)) {
99         return true;
100     }
101     if (node.kind === ts.SyntaxKind.ArrowFunction ||
102         node.kind === ts.SyntaxKind.FunctionDeclaration) {
103         return false;
104     }
105     return node.getChildren().some(functionBlockHasReturn);
106 };
107 var isShortArrowReturn = function (node) {
108     return node.kind === ts.SyntaxKind.ArrowFunction && node.body.kind !== ts.SyntaxKind.Block;
109 };
110 var templateObject_1;