noVoidExpressionRule.js 4.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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. default:
  74. return false;
  75. }
  76. }
  77. }
  78. function isPossiblyVoidExpression(node) {
  79. switch (node.kind) {
  80. case ts.SyntaxKind.AwaitExpression:
  81. case ts.SyntaxKind.CallExpression:
  82. case ts.SyntaxKind.TaggedTemplateExpression:
  83. return true;
  84. default:
  85. return false;
  86. }
  87. }
  88. var templateObject_1, templateObject_2;