4 * Copyright 2013 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 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);
28 return _super !== null && _super.apply(this, arguments) || this;
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,
36 /* tslint:disable:object-literal-sort-keys */
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),
49 enum: [ALLOW_EMPTY_CATCH],
53 enum: [ALLOW_EMPTY_FUNCTIONS],
60 [true, ALLOW_EMPTY_CATCH],
61 [true, ALLOW_EMPTY_FUNCTIONS],
62 [true, ALLOW_EMPTY_CATCH, ALLOW_EMPTY_FUNCTIONS],
64 type: "functionality",
65 typescriptOnly: false,
67 /* tslint:enable:object-literal-sort-keys */
68 Rule.FAILURE_STRING = "block is empty";
70 }(Lint.Rules.AbstractRule));
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)) {
83 return ctx.addFailure(start, node.end, Rule.FAILURE_STRING);
85 return ts.forEachChild(node, cb);
88 function isExcluded(node, options) {
89 if (options.allowEmptyCatch && node.kind === ts.SyntaxKind.CatchClause) {
92 if (options.allowEmptyFunctions &&
93 (node.kind === ts.SyntaxKind.MethodDeclaration ||
94 node.kind === ts.SyntaxKind.FunctionDeclaration ||
95 node.kind === ts.SyntaxKind.FunctionExpression ||
96 node.kind === ts.SyntaxKind.ArrowFunction)) {
99 return (tsutils_1.isConstructorDeclaration(node) &&
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)));
107 var templateObject_1;