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 = 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);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk);
32 Rule.FAILURE_STRING = "Functions marked async must contain an await or return statement.";
34 codeExamples: noAsyncWithoutAwait_examples_1.codeExamples,
35 description: Rule.FAILURE_STRING,
37 optionExamples: [true],
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,
48 }(Lint.Rules.AbstractRule));
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);
57 var addFailureIfAsyncFunctionHasNoAwait = function (node) {
58 if (node.body === undefined) {
59 if (node.type === undefined) {
60 reportFailureIfAsyncFunction(node);
64 if (!isShortArrowReturn(node) &&
65 !functionBlockHasAwait(node.body) &&
66 !functionBlockHasReturn(node.body)) {
67 reportFailureIfAsyncFunction(node);
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);
77 return ts.forEachChild(node, visitNode);
80 var getAsyncModifier = function (node) {
81 if (node.modifiers !== undefined) {
82 return node.modifiers.find(function (modifier) { return modifier.kind === ts.SyntaxKind.AsyncKeyword; });
86 var isReturn = function (node) { return node.kind === ts.SyntaxKind.ReturnKeyword; };
87 var functionBlockHasAwait = function (node) {
88 if (tsutils.isAwaitExpression(node)) {
91 if (node.kind === ts.SyntaxKind.ArrowFunction ||
92 node.kind === ts.SyntaxKind.FunctionDeclaration) {
95 return node.getChildren().some(functionBlockHasAwait);
97 var functionBlockHasReturn = function (node) {
101 if (node.kind === ts.SyntaxKind.ArrowFunction ||
102 node.kind === ts.SyntaxKind.FunctionDeclaration) {
105 return node.getChildren().some(functionBlockHasReturn);
107 var isShortArrowReturn = function (node) {
108 return node.kind === ts.SyntaxKind.ArrowFunction && node.body.kind !== ts.SyntaxKind.Block;
110 var templateObject_1;