UNPKG

1.54 kBJavaScriptView Raw
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
11module.exports = function(context) {
12
13 var functions = [];
14
15 //--------------------------------------------------------------------------
16 // Helpers
17 //--------------------------------------------------------------------------
18
19 function enterFunction() {
20 functions.push({});
21 }
22
23 function exitFunction() {
24 functions.pop();
25 }
26
27
28 //--------------------------------------------------------------------------
29 // Public
30 //--------------------------------------------------------------------------
31
32 return {
33
34 "FunctionDeclaration": enterFunction,
35 "FunctionExpression": enterFunction,
36 "FunctionDeclaration:exit": exitFunction,
37 "FunctionExpression:exit": exitFunction,
38
39 "ReturnStatement": function(node) {
40
41 var returnInfo = functions[functions.length - 1],
42 returnTypeDefined = "type" in returnInfo;
43
44 if (returnTypeDefined) {
45
46 if (returnInfo.type !== !!node.argument) {
47 context.report(node, "Expected " + (returnInfo.type ? "a" : "no") + " return value.");
48 }
49
50 } else {
51 returnInfo.type = !!node.argument;
52 }
53
54 }
55 };
56
57};