.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noVoidExpressionRule.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_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_IGNORE_ARROW_FUNCTION_SHORTHAND = "ignore-arrow-function-shorthand";
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.applyWithProgram = function (sourceFile, program) {
30         var ignoreArrowFunctionShorthand = this.ruleArguments.indexOf(OPTION_IGNORE_ARROW_FUNCTION_SHORTHAND) !== -1;
31         return this.applyWithFunction(sourceFile, walk, { ignoreArrowFunctionShorthand: ignoreArrowFunctionShorthand }, program.getTypeChecker());
32     };
33     /* tslint:disable:object-literal-sort-keys */
34     Rule.metadata = {
35         ruleName: "no-void-expression",
36         description: "Requires expressions of type `void` to appear in statement position.",
37         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            If `", "` is provided, `() => returnsVoid()` will be allowed.\n            Otherwise, it must be written as `() => { returnsVoid(); }`."], ["\n            If \\`", "\\` is provided, \\`() => returnsVoid()\\` will be allowed.\n            Otherwise, it must be written as \\`() => { returnsVoid(); }\\`."])), OPTION_IGNORE_ARROW_FUNCTION_SHORTHAND),
38         options: {
39             type: "array",
40             items: {
41                 type: "string",
42                 enum: [OPTION_IGNORE_ARROW_FUNCTION_SHORTHAND],
43             },
44             minLength: 0,
45             maxLength: 1,
46         },
47         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            It's misleading returning the results of an expression whose type is `void`.\n            Attempting to do so is likely a symptom of expecting a different return type from a function.\n            For example, the following code will log `undefined` but looks like it logs a value:\n\n            ```\n            const performWork = (): void => {\n                workFirst();\n                workSecond();\n            };\n\n            console.log(performWork());\n            ```\n        "], ["\n            It's misleading returning the results of an expression whose type is \\`void\\`.\n            Attempting to do so is likely a symptom of expecting a different return type from a function.\n            For example, the following code will log \\`undefined\\` but looks like it logs a value:\n\n            \\`\\`\\`\n            const performWork = (): void => {\n                workFirst();\n                workSecond();\n            };\n\n            console.log(performWork());\n            \\`\\`\\`\n        "]))),
48         requiresTypeInfo: true,
49         type: "functionality",
50         typescriptOnly: false,
51     };
52     /* tslint:enable:object-literal-sort-keys */
53     Rule.FAILURE_STRING = "Expression has type `void`. Put it on its own line as a statement.";
54     return Rule;
55 }(Lint.Rules.TypedRule));
56 exports.Rule = Rule;
57 function walk(ctx, checker) {
58     var sourceFile = ctx.sourceFile, ignoreArrowFunctionShorthand = ctx.options.ignoreArrowFunctionShorthand;
59     return ts.forEachChild(sourceFile, function cb(node) {
60         if (isPossiblyVoidExpression(node) &&
61             !isParentAllowedVoid(node) &&
62             tsutils_1.isTypeFlagSet(checker.getTypeAtLocation(node), ts.TypeFlags.Void)) {
63             ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
64         }
65         return ts.forEachChild(node, cb);
66     });
67     function isParentAllowedVoid(node) {
68         switch (node.parent.kind) {
69             case ts.SyntaxKind.ExpressionStatement:
70                 return true;
71             case ts.SyntaxKind.ArrowFunction:
72                 return ignoreArrowFunctionShorthand;
73             // Something like "x && console.log(x)".
74             case ts.SyntaxKind.BinaryExpression:
75                 return isParentAllowedVoid(node.parent);
76             // Something like "!!cond ? console.log(true) : console.log(false)"
77             case ts.SyntaxKind.ConditionalExpression:
78                 return true;
79             default:
80                 return false;
81         }
82     }
83 }
84 function isPossiblyVoidExpression(node) {
85     switch (node.kind) {
86         case ts.SyntaxKind.AwaitExpression:
87         case ts.SyntaxKind.CallExpression:
88         case ts.SyntaxKind.TaggedTemplateExpression:
89             return true;
90         default:
91             return false;
92     }
93 }
94 var templateObject_1, templateObject_2;