2 * @fileoverview Rule to flag use of an empty block statement
3 * @author Nicholas C. Zakas
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils = require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
22 description: "disallow empty block statements",
23 category: "Possible Errors",
25 url: "https://eslint.org/docs/rules/no-empty"
37 additionalProperties: false
42 unexpected: "Empty {{type}} statement."
47 const options = context.options[0] || {},
48 allowEmptyCatch = options.allowEmptyCatch || false;
50 const sourceCode = context.getSourceCode();
53 BlockStatement(node) {
55 // if the body is not empty, we can just return immediately
56 if (node.body.length !== 0) {
60 // a function is generally allowed to be empty
61 if (astUtils.isFunction(node.parent)) {
65 if (allowEmptyCatch && node.parent.type === "CatchClause") {
69 // any other block is only allowed to be empty, if it contains a comment
70 if (sourceCode.getCommentsInside(node).length > 0) {
74 context.report({ node, messageId: "unexpected", data: { type: "block" } });
77 SwitchStatement(node) {
79 if (typeof node.cases === "undefined" || node.cases.length === 0) {
80 context.report({ node, messageId: "unexpected", data: { type: "switch" } });