noVoidExpressionRule.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. requiresTypeInfo: true,
  48. type: "functionality",
  49. typescriptOnly: false,
  50. };
  51. /* tslint:enable:object-literal-sort-keys */
  52. Rule.FAILURE_STRING = "Expression has type `void`. Put it on its own line as a statement.";
  53. return Rule;
  54. }(Lint.Rules.TypedRule));
  55. exports.Rule = Rule;
  56. function walk(ctx, checker) {
  57. var sourceFile = ctx.sourceFile, ignoreArrowFunctionShorthand = ctx.options.ignoreArrowFunctionShorthand;
  58. return ts.forEachChild(sourceFile, function cb(node) {
  59. if (isPossiblyVoidExpression(node)
  60. && !isParentAllowedVoid(node)
  61. && tsutils_1.isTypeFlagSet(checker.getTypeAtLocation(node), ts.TypeFlags.Void)) {
  62. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  63. }
  64. return ts.forEachChild(node, cb);
  65. });
  66. function isParentAllowedVoid(node) {
  67. switch (node.parent.kind) {
  68. case ts.SyntaxKind.ExpressionStatement:
  69. return true;
  70. case ts.SyntaxKind.ArrowFunction:
  71. return ignoreArrowFunctionShorthand;
  72. default:
  73. return false;
  74. }
  75. }
  76. }
  77. function isPossiblyVoidExpression(node) {
  78. switch (node.kind) {
  79. case ts.SyntaxKind.AwaitExpression:
  80. case ts.SyntaxKind.CallExpression:
  81. case ts.SyntaxKind.TaggedTemplateExpression:
  82. return true;
  83. default:
  84. return false;
  85. }
  86. }
  87. var templateObject_1;