123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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 ALLOW_EMPTY_CATCH = "allow-empty-catch";
  24. var ALLOW_EMPTY_FUNCTIONS = "allow-empty-functions";
  25. var Rule = /** @class */ (function (_super) {
  26. tslib_1.__extends(Rule, _super);
  27. function Rule() {
  28. return _super !== null && _super.apply(this, arguments) || this;
  29. }
  30. Rule.prototype.apply = function (sourceFile) {
  31. return this.applyWithFunction(sourceFile, walk, {
  32. allowEmptyCatch: this.ruleArguments.indexOf(ALLOW_EMPTY_CATCH) !== -1,
  33. allowEmptyFunctions: this.ruleArguments.indexOf(ALLOW_EMPTY_FUNCTIONS) !== -1,
  34. });
  35. };
  36. /* tslint:disable:object-literal-sort-keys */
  37. Rule.metadata = {
  38. ruleName: "no-empty",
  39. description: "Disallows empty blocks.",
  40. descriptionDetails: "Blocks with a comment inside are not considered empty.",
  41. rationale: "Empty blocks are often indicators of missing code.",
  42. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n If `", "` is specified, then catch blocks are allowed to be empty.\n If `", "` is specified, then function definitions are allowed to be empty."], ["\n If \\`", "\\` is specified, then catch blocks are allowed to be empty.\n If \\`", "\\` is specified, then function definitions are allowed to be empty."])), ALLOW_EMPTY_CATCH, ALLOW_EMPTY_FUNCTIONS),
  43. options: {
  44. type: "array",
  45. items: {
  46. anyOf: [
  47. {
  48. type: "string",
  49. enum: [ALLOW_EMPTY_CATCH],
  50. },
  51. {
  52. type: "string",
  53. enum: [ALLOW_EMPTY_FUNCTIONS],
  54. },
  55. ],
  56. },
  57. },
  58. optionExamples: [
  59. true,
  60. [true, ALLOW_EMPTY_CATCH],
  61. [true, ALLOW_EMPTY_FUNCTIONS],
  62. [true, ALLOW_EMPTY_CATCH, ALLOW_EMPTY_FUNCTIONS],
  63. ],
  64. type: "functionality",
  65. typescriptOnly: false,
  66. };
  67. /* tslint:enable:object-literal-sort-keys */
  68. Rule.FAILURE_STRING = "block is empty";
  69. return Rule;
  70. }(Lint.Rules.AbstractRule));
  71. exports.Rule = Rule;
  72. function walk(ctx) {
  73. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  74. if (node.kind === ts.SyntaxKind.Block &&
  75. node.statements.length === 0 &&
  76. !isExcluded(node.parent, ctx.options)) {
  77. var start = node.getStart(ctx.sourceFile);
  78. // Block always starts with open brace. Adding 1 to its start gives us the end of the brace,
  79. // which can be used to conveniently check for comments between braces
  80. if (Lint.hasCommentAfterPosition(ctx.sourceFile.text, start + 1)) {
  81. return;
  82. }
  83. return ctx.addFailure(start, node.end, Rule.FAILURE_STRING);
  84. }
  85. return ts.forEachChild(node, cb);
  86. });
  87. }
  88. function isExcluded(node, options) {
  89. if (options.allowEmptyCatch && node.kind === ts.SyntaxKind.CatchClause) {
  90. return true;
  91. }
  92. if (options.allowEmptyFunctions &&
  93. (node.kind === ts.SyntaxKind.FunctionDeclaration ||
  94. node.kind === ts.SyntaxKind.FunctionExpression ||
  95. node.kind === ts.SyntaxKind.ArrowFunction)) {
  96. return true;
  97. }
  98. return tsutils_1.isConstructorDeclaration(node) &&
  99. (
  100. /* If constructor is private or protected, the block is allowed to be empty.
  101. The constructor is there on purpose to disallow instantiation from outside the class */
  102. /* The public modifier does not serve a purpose here. It can only be used to allow instantiation of a base class where
  103. the super constructor is protected. But then the block would not be empty, because of the call to super() */
  104. tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword) ||
  105. node.parameters.some(tsutils_1.isParameterProperty));
  106. }
  107. var templateObject_1;