33df4c4eac70d847156ef50fbd58208de38d8829
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag use of an empty block statement
3  * @author Nicholas C. Zakas
4  * @copyright Nicholas C. Zakas. All rights reserved.
5  * @copyright 2015 Dieter Oberkofler. All rights reserved.
6  */
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function(context) {
14
15     return {
16
17         "BlockStatement": function(node) {
18             var parent = node.parent,
19                 parentType = parent.type,
20                 comments,
21                 i;
22
23             // if the body is not empty, we can just return immediately
24             if (node.body.length !== 0) {
25                 return;
26             }
27
28             // a function is generally allowed to be empty
29             if (parentType === "FunctionDeclaration" || parentType === "FunctionExpression" || parentType === "ArrowFunctionExpression") {
30                 return;
31             }
32
33             // any other block is only allowed to be empty, if it contains an empty comment
34             comments = context.getComments(node).trailing;
35             for (i = 0; i < comments.length; i++) {
36                 if (comments[i].value.trim() === "empty") {
37                     return;
38                 }
39             }
40
41             context.report(node, "Empty block statement.");
42         },
43
44         "SwitchStatement": function(node) {
45
46             if (typeof node.cases === "undefined" || node.cases.length === 0) {
47                 context.report(node, "Empty switch statement.");
48             }
49         }
50     };
51
52 };