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_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);
27 return _super !== null && _super.apply(this, arguments) || this;
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());
33 /* tslint:disable:object-literal-sort-keys */
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),
42 enum: [OPTION_IGNORE_ARROW_FUNCTION_SHORTHAND],
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,
52 /* tslint:enable:object-literal-sort-keys */
53 Rule.FAILURE_STRING = "Expression has type `void`. Put it on its own line as a statement.";
55 }(Lint.Rules.TypedRule));
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);
65 return ts.forEachChild(node, cb);
67 function isParentAllowedVoid(node) {
68 switch (node.parent.kind) {
69 case ts.SyntaxKind.ExpressionStatement:
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:
84 function isPossiblyVoidExpression(node) {
86 case ts.SyntaxKind.AwaitExpression:
87 case ts.SyntaxKind.CallExpression:
88 case ts.SyntaxKind.TaggedTemplateExpression:
94 var templateObject_1, templateObject_2;