.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noReturnAwaitRule.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 utils_1 = require("../utils");
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     /* tslint:disable:object-literal-sort-keys */
33     Rule.metadata = {
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.",
38         options: null,
39         optionExamples: [true],
40         type: "functionality",
41         typescriptOnly: false,
42         hasFix: true,
43     };
44     /* tslint:enable:object-literal-sort-keys */
45     Rule.FAILURE_STRING = "Unnecessary 'await'.";
46     return Rule;
47 }(Lint.Rules.AbstractRule));
48 exports.Rule = Rule;
49 function walk(ctx) {
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)));
55         }
56         return ts.forEachChild(node, cb);
57     });
58 }
59 function isUnnecessaryAwait(node) {
60     while (true) {
61         var parent = node.parent;
62         outer: switch (parent.kind) {
63             case ts.SyntaxKind.ArrowFunction:
64                 return true;
65             case ts.SyntaxKind.ReturnStatement:
66                 return !isInsideTryBlock(parent.parent);
67             case ts.SyntaxKind.ParenthesizedExpression:
68                 break;
69             case ts.SyntaxKind.ConditionalExpression:
70                 if (parent.condition === node) {
71                     return false;
72                 }
73                 break;
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:
80                             break outer;
81                     }
82                 }
83                 return false;
84             default:
85                 return false;
86         }
87         node = parent;
88     }
89 }
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
96             return false;
97         }
98         if (tsutils_1.isTryStatement(node.parent)) {
99             if (
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)) {
104                 return true;
105             }
106             node = node.parent.parent;
107         }
108         else {
109             node = node.parent;
110         }
111     }
112     return false;
113 }
114 var templateObject_1;