38adba7f318ea0f900d02a32e486e58d3af81522
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag consistent return values
3  * @author Nicholas C. Zakas
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 module.exports = function(context) {
12
13     var functions = [];
14
15     //--------------------------------------------------------------------------
16     // Helpers
17     //--------------------------------------------------------------------------
18
19     /**
20      * Marks entrance into a function by pushing a new object onto the functions
21      * stack.
22      * @returns {void}
23      * @private
24      */
25     function enterFunction() {
26         functions.push({});
27     }
28
29     /**
30      * Marks exit of a function by popping off the functions stack.
31      * @returns {void}
32      * @private
33      */
34     function exitFunction() {
35         functions.pop();
36     }
37
38
39     //--------------------------------------------------------------------------
40     // Public
41     //--------------------------------------------------------------------------
42
43     return {
44
45         "Program": enterFunction,
46         "FunctionDeclaration": enterFunction,
47         "FunctionExpression": enterFunction,
48         "ArrowFunctionExpression": enterFunction,
49
50         "Program:exit": exitFunction,
51         "FunctionDeclaration:exit": exitFunction,
52         "FunctionExpression:exit": exitFunction,
53         "ArrowFunctionExpression:exit": exitFunction,
54
55         "ReturnStatement": function(node) {
56
57             var returnInfo = functions[functions.length - 1],
58                 returnTypeDefined = "type" in returnInfo;
59
60             if (returnTypeDefined) {
61
62                 if (returnInfo.type !== !!node.argument) {
63                     context.report(node, "Expected " + (returnInfo.type ? "a" : "no") + " return value.");
64                 }
65
66             } else {
67                 returnInfo.type = !!node.argument;
68             }
69
70         }
71     };
72
73 };