noEmptyRule.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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.apply = function (sourceFile) {
  30. return this.applyWithFunction(sourceFile, walk, {
  31. allowEmptyCatch: this.ruleArguments.indexOf(ALLOW_EMPTY_CATCH) !== -1,
  32. });
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "no-empty",
  37. description: "Disallows empty blocks.",
  38. descriptionDetails: "Blocks with a comment inside are not considered empty.",
  39. rationale: "Empty blocks are often indicators of missing code.",
  40. 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 catch blocks are allowed to be empty."])), ALLOW_EMPTY_CATCH),
  41. options: {
  42. type: "string",
  43. enum: [ALLOW_EMPTY_CATCH],
  44. },
  45. optionExamples: [true, [true, ALLOW_EMPTY_CATCH]],
  46. type: "functionality",
  47. typescriptOnly: false,
  48. };
  49. /* tslint:enable:object-literal-sort-keys */
  50. Rule.FAILURE_STRING = "block is empty";
  51. return Rule;
  52. }(Lint.Rules.AbstractRule));
  53. exports.Rule = Rule;
  54. function walk(ctx) {
  55. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  56. if (node.kind === ts.SyntaxKind.Block &&
  57. node.statements.length === 0 &&
  58. !isExcluded(node.parent, ctx.options)) {
  59. var start = node.getStart(ctx.sourceFile);
  60. // Block always starts with open brace. Adding 1 to its start gives us the end of the brace,
  61. // which can be used to conveniently check for comments between braces
  62. if (Lint.hasCommentAfterPosition(ctx.sourceFile.text, start + 1)) {
  63. return;
  64. }
  65. return ctx.addFailure(start, node.end, Rule.FAILURE_STRING);
  66. }
  67. return ts.forEachChild(node, cb);
  68. });
  69. }
  70. function isExcluded(node, options) {
  71. if (options.allowEmptyCatch && node.kind === ts.SyntaxKind.CatchClause) {
  72. return true;
  73. }
  74. return tsutils_1.isConstructorDeclaration(node) &&
  75. (
  76. /* If constructor is private or protected, the block is allowed to be empty.
  77. The constructor is there on purpose to disallow instantiation from outside the class */
  78. /* The public modifier does not serve a purpose here. It can only be used to allow instantiation of a base class where
  79. the super constructor is protected. But then the block would not be empty, because of the call to super() */
  80. tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword) ||
  81. node.parameters.some(tsutils_1.isParameterProperty));
  82. }
  83. var templateObject_1;