UNPKG

1.19 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag use of an empty block statement
3 * @author Nicholas C. Zakas
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = function(context) {
12
13 return {
14
15 "BlockStatement": function(node) {
16 var ancestors = context.getAncestors(),
17 parent = ancestors[ancestors.length - 1],
18 parentType = parent.type,
19 isFinallyBlock = (parentType === "TryStatement") && (parent.finalizer === node);
20
21 if (/FunctionExpression|FunctionDeclaration|CatchClause/.test(parentType) ||
22 (isFinallyBlock && !parent.handlers.length)) {
23 return;
24 }
25
26 if (node.body.length === 0) {
27 context.report(node, "Empty block statement.");
28 }
29 },
30
31 "SwitchStatement": function(node) {
32
33 if (typeof node.cases === "undefined" || node.cases.length === 0) {
34 context.report(node, "Empty switch statement.");
35 }
36 }
37 };
38
39};