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 utils_1 = require("../utils");
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 /* tslint:disable:object-literal-sort-keys */
34 ruleName: "no-return-await",
35 description: "Disallows unnecessary `return await`.",
36 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n An async function always wraps the return value in a Promise.\n Using `return await` just adds extra time before the overreaching promise is resolved without changing the semantics.\n "], ["\n An async function always wraps the return value in a Promise.\n Using \\`return await\\` just adds extra time before the overreaching promise is resolved without changing the semantics.\n "]))),
37 optionsDescription: "Not configurable.",
39 optionExamples: [true],
40 type: "functionality",
41 typescriptOnly: false,
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING = "Unnecessary 'await'.";
47 }(Lint.Rules.AbstractRule));
50 return ts.forEachChild(ctx.sourceFile, function cb(node) {
51 if (node.kind === ts.SyntaxKind.AwaitExpression && isUnnecessaryAwait(node)) {
52 var expression = node.expression;
53 var keywordStart = expression.pos - "await".length;
54 ctx.addFailure(keywordStart, expression.pos, Rule.FAILURE_STRING, Lint.Replacement.deleteFromTo(keywordStart, expression.getStart(ctx.sourceFile)));
56 return ts.forEachChild(node, cb);
59 function isUnnecessaryAwait(node) {
61 var parent = node.parent;
62 outer: switch (parent.kind) {
63 case ts.SyntaxKind.ArrowFunction:
65 case ts.SyntaxKind.ReturnStatement:
66 return !isInsideTryBlock(parent.parent);
67 case ts.SyntaxKind.ParenthesizedExpression:
69 case ts.SyntaxKind.ConditionalExpression:
70 if (parent.condition === node) {
74 case ts.SyntaxKind.BinaryExpression:
75 if (parent.right === node) {
76 switch (parent.operatorToken.kind) {
77 case ts.SyntaxKind.AmpersandAmpersandToken:
78 case ts.SyntaxKind.BarBarToken:
79 case ts.SyntaxKind.CommaToken:
90 function isInsideTryBlock(node) {
91 while (node.parent !== undefined) {
92 // tslint:disable:deprecation This is needed for https://github.com/palantir/tslint/pull/4274 and will be fixed once TSLint
93 // requires tsutils > 3.0.
94 if (utils_1.isFunctionScopeBoundary(node)) {
95 // tslint:enable:deprecation
98 if (tsutils_1.isTryStatement(node.parent)) {
100 // statements inside the try block always have an error handler, either catch or finally
101 node.parent.tryBlock === node ||
102 // statement inside the catch block only have an error handler if there is a finally block
103 (node.parent.finallyBlock !== undefined && node.parent.catchClause === node)) {
106 node = node.parent.parent;
114 var templateObject_1;