This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathrequire-early-return.js
110 lines (101 loc) · 2.68 KB
/
require-early-return.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Requires to return early in a function.
*
* Types: `Boolean`
*
* Values:
* - `true`: disallow to use of else if the corresponding `if` block contain a return.
*
* #### Example
*
* ```js
* "requireEarlyReturn": true
* ```
*
* ##### Valid
*
* ```js
* function test() {
* if (x) {
* return x;
* }
* return y;
* }
* ```
*
* ##### Invalid
*
* ```js
* function test() {
* if (x) {
* return x;
* } else {
* return y;
* }
* }
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option allow only the `true` value'
);
},
getOptionName: function() {
return 'requireEarlyReturn';
},
check: function(file, errors) {
function addError(entity) {
errors.add(
'Use of else after return',
entity
);
}
// Check if the IfStatement node contain a ReturnStatement.
// If the node has a block, check all the statements in backward order to see if there is one.
// This is to ensure that code like this will still return true:
//
// if (true) {
// return;
// eval();
// }
function hasNodeReturn(node) {
if (node.type === 'BlockStatement') {
for (var i = node.body.length - 1; i >= 0; i--) {
if (node.body[i].type === 'ReturnStatement') {
return true;
}
}
return false;
}
return node.type === 'ReturnStatement';
}
file.iterateNodesByType('IfStatement', function(node) {
if (!node.alternate) {
return;
}
// Check if all the parents have a return statement, if not continue to the following IfStatement node.
//
// Example:
//
// if (foo) {
// return;
// } else if (bar) { <-- error
// bar();
// } else if (baz) { <-- safe
// return baz();
// } else { <-- safe
// bas();
// }
for (var nodeIf = node; nodeIf && nodeIf.type === 'IfStatement'; nodeIf = nodeIf.parentElement) {
if (nodeIf.alternate && !hasNodeReturn(nodeIf.consequent)) {
return;
}
}
return addError(file.getPrevToken(file.getFirstNodeToken(node.alternate)));
});
}
};